Used an array for collision.
// Project: ElectroCat
// Created: 19-12-10
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "ElectroCat" )
SetWindowSize( 1280, 720, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1920, 1080 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 600, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )
global imgRed
global imgGreen
global imgPlayer
imgDark = CreateImageColor( 20, 20, 20,255) // dark_grey
imgLight = CreateImageColor( 30, 30, 30,255)
imgRed = CreateImageColor(100, 0, 0,255)
imgGreen = CreateImageColor( 0, 255, 0,255)
imgPlayer = CreateImageColor(0, 100, 100,255)
global xoff
global yoff
xoff = 160-64
yoff = 28
yoffp = 28+32
global xsize
global ysize
xsize = 64
ysize = 64
global xmax
global ymax
xmax = 24+2+1
ymax = 14+2+1
global nLevelHeight
nLevelHeight = ymax
global nLevelWidth
nLevelWidth = xmax
Type GameObject
SpriteID as integer
Solid as integer // 0 / 1, what about Dynamic Objects?
value as integer
EndType
Type GamePlayer
PosX# as float
PosY# as float
newPosX# as float
newPosY# as float
VelX# as float
VelY# as float
SpriteID as integer
EndType
global MapSpr as GameObject []
For y=0 to ymax-1 // offset by 1/2, more like 28 or 36
For x=0 to xmax-1
tempObject as GameObject
If (y=0) or (y=ymax-1) or (x=0) or (x=xmax-1)
sprID = CreateCollisionBox()
tempObject.Solid = 1
Else
If mod(x,2)=0 and mod(y,2)=0
sprID = CreateCollisionBox()
tempObject.Solid = 1
ElseIf mod((x*(xmax+1)+y),2)=0
sprID = CreateSprite(imgDark)
tempObject.Solid = 0
Else
sprID = CreateSprite(imgLight)
tempObject.Solid = 0
EndIf
EndIf
SetSpritePosition(sprID,xoff+xsize*x,yoff+ysize*y)
SetSpriteSize(sprID,xsize,ysize)
tempObject.SpriteID = sprID
MapSpr.Insert(tempObject)
Next
Next
x = 7
y = 8
sprPlayerCollision = CreateSprite(imgPlayer)
SetSpriteSize(sprPlayerCollision,64,64)
SetSpriteShapeCircle(sprPlayerCollision,0,0,30)
SetSpriteCategoryBits( sprPlayerCollision, 2 )
//SetSpriteOffset(sprPlayerCollision,0,0)
SetSpritePosition(sprPlayerCollision,xoff+xsize*x,yoff+ysize*y)
/*
sprPlayer = CreateSprite(imgPlayer)
SetSpriteSize(sprPlayer,64,96)
SetSpritePosition(sprPlayer,xoff+xsize*x,yoffp+ysize*y)
*/
SetPhysicsDebugOn()
global speed# // --> pro Player maybe different per item
speed# = 300
global gVelX# = 6.0
global gVelY# = 6.0
global vecPlayers as GamePlayer[]
tempPlayer as GamePlayer
tempPlayer.SpriteID = sprPlayerCollision
tempPlayer.NewPosX# = 7
tempPlayer.NewPosY# = 8
tempPlayer.PosX# = 7
tempPlayer.PosY# = 8
tempPlayer.VelX# = 0
tempPlayer.VelY# = 0
vecPlayers.insert(tempPlayer)
fOffsetX = 0
fOffsetY = 0
pOffsetX = xoff
pOffsetY = yoff
x as integer
y as integer
do
Print( Str(ScreenFPS(),2) )
for p = 0 to 0
vecPlayers[p].VelX# = 0
vecPlayers[p].VelY# = 0
x = 0 // cast integer
y = 0 // cast integer
If (GetRawKeyState(37)) // Left
vecPlayers[p].VelX# = -gVelX#
EndIf
If (GetRawKeyState(38)) // Up
vecPlayers[p].VelY# = -gVelY#
EndIf
If (GetRawKeyState(39)) // Right
vecPlayers[p].VelX# = gVelX#
EndIf
If (GetRawKeyState(40)) // Down
vecPlayers[p].VelY# = gVelY#
EndIf
vecPlayers[p].newPosX# = vecPlayers[p].PosX# + vecPlayers[p].VelX# * GetFrameTime()
vecPlayers[p].newPosY# = vecPlayers[p].PosY# + vecPlayers[p].VelY# * GetFrameTime()
// Collisions
if (vecPlayers[p].VelX# <= 0)
Print("Left")
Print(Str(GetTile( vecPlayers[p].newPosX#+0.0,vecPlayers[p].PosY# +0.0) <> 0 or GetTile(vecPlayers[p].newPosX#+0.0,vecPlayers[p].PosY# +0.9) ))
if GetTile( vecPlayers[p].newPosX#+0.0,vecPlayers[p].PosY# +0.0) <> 0 or GetTile(vecPlayers[p].newPosX#+0.0,vecPlayers[p].PosY# +0.9) <> 0
vecPlayers[p].newPosX# = trunc(vecPlayers[p].newPosX#)+1
vecPlayers[p].VelX# = 0
endif
else
if GetTile(vecPlayers[p].newPosX#+1.0,vecPlayers[p].PosY# + 0.0) <>0 or GetTile(vecPlayers[p].newPosX#+1.0,vecPlayers[p].PosY# +0.9) <>0
vecPlayers[p].newPosX# = trunc(vecPlayers[p].newPosX#) +0
vecPlayers[p].VelX# = 0
endif
endif
if (vecPlayers[p].VelY# <= 0)
if GetTile( vecPlayers[p].newPosX#+0.0,vecPlayers[p].NewPosY# +0.0) <> 0 or GetTile(vecPlayers[p].newPosX#+0.9,vecPlayers[p].NewPosY# +0) <> 0
vecPlayers[p].newPosY# = trunc(vecPlayers[p].newPosY#)+1
vecPlayers[p].VelY# = 0
endif
else
if GetTile(vecPlayers[p].newPosX#+0.0,vecPlayers[p].NewPosY# + 1.0) <>0 or GetTile(vecPlayers[p].newPosX#+0.9,vecPlayers[p].NewPosY# +1) <>0
vecPlayers[p].newPosY# = trunc(vecPlayers[p].newPosY#) +0
vecPlayers[p].VelY# = 0
endif
endif
vecPlayers[p].PosX# = vecPlayers[p].newPosX#
vecPlayers[p].PosY# = vecPlayers[p].newPosY#
SetSpritePosition(vecPlayers[p].SpriteID,(vecPlayers[p].PosX#-0) * xsize+pOffsetX,(vecPlayers[p].PosY#-0) * ysize+pOffsetY)
next
/*
If (GetRawKeyState(37))
MovePlayer(sprPlayerCollision,-1,0)
EndIf
If (GetRawKeyState(38))
MovePlayer(sprPlayerCollision,0,-1)
EndIf
If (GetRawKeyState(39))
MovePlayer(sprPlayerCollision, 1,0)
EndIf
If (GetRawKeyState(40))
MovePlayer(sprPlayerCollision, 0,1)
EndIf
MovePlayer(sprPlayerCollision,0,0)
*/
/*
KEY_LEFT 37
KEY_UP 38
KEY_RIGHT 39
KEY_DOWN 40
*/
Sync()
loop
Function GetTile(x as float, y as float)
ix = trunc(x) // very important
iy = trunc(y) // very important
if (ix>=0 and ix <nLevelWidth and iy>=0 and iy < nLevelHeight)
value = MapSpr[iy*nLevelWidth+ix].Solid // maybe have to change to Value
else
value = -1
endif
EndFunction value
Function MovePlayer(sprID,x#,y#)
Print(GetSpriteX(sprID))
Print(GetSpriteY(sprID))
// xoff+xsize*x
//pos = mod(y#/ysize,xmax)+ x#/xsize
xs = (GetSpriteX(sprID) - xoff) / xsize
ys = (GetSpriteY(sprID) - yoff) / ysize
minXp = xs-1
maxXp = xs+1
minYp = ys-1
maxYp = ys+1
for Xp=minXp to maxXp
for Yp=minYp to maxYp
pos = xmax * Yp + Xp
if pos>=0 and pos<= MapSpr.length
tempObject as GameObject
tempObject = MapSpr[pos]
If tempObject.Solid = 1
If GetSpriteExists(tempObject.SpriteID)
if GetSpriteCollision( sprID, tempObject.SpriteID)
// SetSpriteImage(sprID,imgRed)
// Print("HIT!!!: "+Str(tempObject.SpriteID))
else
// SetSpriteImage(sprID,imgPlayer)
//SetSpriteImage(tempSpr,imgGreen)
endIf
endif
endif
endif
next
next
//SetSpritePosition(sprID,GetSpriteX(sprID)+x#* speed#*GetFrameTime(),GetSpriteY(sprID)+y#* speed#*GetFrameTime())
// ->
/*
If GetSpriteHitCategory(GetSpriteX(sprID)+x#*GetFrameTime()+GetSpriteWidth(sprID),GetSpriteY(sprID)+y#*GetFrameTime()+GetSpriteHeight(sprID),2)
SetSpriteImage(sprID,imgGreen)
Else
SetSpriteImage(sprID,imgPlayer)
EndIf
*/
EndFunction
Function CreateCollisionBox()
sprID = CreateSprite(imgRed)
SetSpriteOffset(sprID,0,0)
SetSpriteShapeBox(sprID,0,0,64,64,0)
SetSpriteCategoryBits( sprID, 2 )
EndFunction sprID
Arrow keys to move. Tried to solve the collision with an array. (like here in C++ Code-It-Yourself! Simple Tile Based Platform Game #1 from javidx9)