Ok, my idea is to try to make a very simple, very basic platformer.
So far I had success putting a character on the screen and making it move and jump.
Now, I want to set a scene, and want the character to interact with it, jumping on platforms.
I'm trying to detect when the character is on the platform, but it seems I'm not understanding correctly how TILECOLL
works...
The idea I had was moving the character and detecting the collision with the background (when the character has a specified tile under it) to move the ground value to the value of the platform while the collision persisted. But, to be frank, I'm not sure how it works, or when and why I get certain values.
This is the code so far:
SCREEN 11:PALETTE 2 '320x256 (20x16 tiles)
'vars
ground=228
positionX=PSIZE(0)/2-7
positionY=ground
velocityX=0
velocityY=0
gravity=0.5
friction=0.85
speed=2
landed=0
rightSide=PSIZE(0)-14
'Set scene and load character
CALL render
CALL main
PROC main
f=FRAME()
DO
'check movement
p=PAD(0)
'register key up
IF p AND UP AND landed=1 THEN
CALL startJump
ENDIF
'register no key up
IF p=0 AND landed=0 THEN
CALL endJump
ENDIF
'register key right
IF p AND RIGHT AND positionX<rightSide-4 AND landed=1 THEN
CALL moveRight
ENDIF
'register key left
IF p AND LEFT AND positionX>4 AND landed=1 THEN
CALL moveLeft
ENDIF
'update
CALL update
'update sprite position
MOVE SPRITE 0 TO positionX,positionY
VSYNC
LOOP
RETURN
PROC startJump
'jump
IF landed THEN
velocityY=-10
landed=0
ENDIF
RETURN
PROC endJump
'short jump
IF velocityY<-4 THEN
velocityY=-4
ENDIF
RETURN
PROC moveRight
SPRITE 0 FLAGS 0
IF velocityX<speed THEN
velocityX=velocityX+1
ENDIF
RETURN
PROC moveLeft
SPRITE 0 FLAGS 2
IF velocityX>-speed THEN
velocityX=velocityX-1
ENDIF
RETURN
PROC update
velocityY=velocityY+gravity
positionY=positionY+velocityY
'add friction
IF landed=1 AND velocityX<>0 THEN
velocityX=velocityX*friction
ENDIF
positionX=positionX+velocityX
'hit ground
IF positionY>ground THEN
positionY=ground
velocityY=0
landed=1
ENDIF
'right wall
IF positionX>=rightSide THEN
velocityX=-velocityX
SPRITE 0 FLAGS 2
ENDIF
'left wall
IF positionX<=0 THEN
velocityX=-velocityX
SPRITE 0 FLAGS 0
ENDIF
'platform
IF positionX>160 AND positionX<226 AND positionY<=160 AND velocityY>0 THEN
ground=148
ELSE
ground=228
ENDIF
'bg collision detection attempt
gnd=TILECOLL(0,0,2)
LOCATE 2,2:PRINT gnd
ltile=TILECOLL(0,0,1)
LOCATE 8,2:PRINT ltile
RETURN
PROC render
'background
LOAD IMAGE "land-tiles.png" AS BG 0
BG 0 TILES 20,16 SIZE 16,16 ON
PLOT 0 MAP ASC("X") TO 0
PLOT 0 MAP ASC("L") TO 1
PLOT 0 MAP ASC("C") TO 2
PLOT 0 MAP ASC("R") TO 3
PLOT 0,10,10,"LCCR"
PLOT 0,0,15,"CCCCCCCCCCCCCCCCCCCC"
'character
LOAD IMAGE "char.png" AS SPRITE 0
SPRITE 0 SIZE 14,12 ON
RETURN
I'm simulating the platform with an IF statement at the moment, but that's not what I wanted to do, It's just there to try other things, and debug the movement of the character (that has a few quirks right now).
Here you have the tiles and character, in case you want to try it:
If you can help me understand a bit how TILECOLL
works, why I get the values I get, and if it's the right strategy to do what I want, I'd really appreciate it. Any other advice is also appreciated. Thanks.