@Santman
Something like that would be easy to do with an Array
But I am stuck, how to do it the best way for non array based Collisions without checking collisions agains every other Sprite
// 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)
xoff = 160-64
yoff = 28
yoffp = 28+32
xsize = 64
ysize = 64
xmax = 24+2
ymax = 14+2
MapSpr as integer []
For x=0 to xmax
For y=0 to ymax // offset by 1/2, more like 28 or 36
If (y=0) or (y=ymax) or (x=0) or (x=xmax)
sprID = CreateCollisionBox()
Else
If mod(x,2)=0 and mod(y,2)=0
sprID = CreateCollisionBox()
ElseIf mod((x*(xmax+1)+y),2)=0
sprID = CreateSprite(imgDark)
Else
sprID = CreateSprite(imgLight)
EndIf
EndIf
SetSpritePosition(sprID,xoff+xsize*x,yoff+ysize*y)
SetSpriteSize(sprID,xsize,ysize)
MapSpr.Insert(sprID)
Next
Next
x = 7
y = 8
sprPlayerCollision = CreateSprite(imgPlayer)
SetSpriteSize(sprPlayerCollision,64,64)
SetSpriteShapeCircle(sprPlayerCollision,0,0,32)
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
do
Print( Str(ScreenFPS(),2) )
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
/*
KEY_LEFT 37
KEY_UP 38
KEY_RIGHT 39
KEY_DOWN 40
*/
Sync()
loop
Function MovePlayer(sprID,x#,y#)
// xx# =
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
So for a Platformer, RPG or anything, maybe like Bomberman or so, it would be interesting, how to be able to optimise the collisions.
My idea is, to check next to each side with GetSpriteHit(X, Y) and if there is any Sprite in the same hit-group or category. (don't understand exactly what the difference is)
But e.g. for a Circle-Shape, I don't know, how to check it correctly. So maybe I am overthinking this, or I would have to have more collider-helper-"Sprites"