This has the basics of how it works, however the drawing priority is a separate matter. I posted one up a long time ago
here.
Before I got into 3D all I did was strictly 2D stuff so I have a few useful snippets floating around.
REM *************************
REM | Radius Collision |
REM | By Ashingda |
REM *************************
set display mode 640,480,16,1
sync on
sync rate 0
backdrop off
REM Constants
#constant Sprite_Max 5
#constant Sprite_Object 100
REM Globals
global Player_ID
global Radius_Flag
global KeyReturn
global KeySpace
REM Sprite Number that will act as Player
Player_ID = 1
REM Draw the Radius Below Sprite
Radius_Flag = 1
REM Array
dim SpriteAlive(Sprite_Max)
dim SpriteAction(Sprite_Max)
dim SpriteAct(Sprite_Max)
dim SpriteSize(Sprite_Max)
dim SpriteX(Sprite_Max)
dim SpriteY(Sprite_Max)
REM Initial Creation of Sprite
Make_Sprite()
REM ***********************************
REM Main Loop
REM ***********************************
do
cls
Handle_Keys()
Handle_Object()
sync
loop
REM ***********************************
REM Handle_Keys
REM ***********************************
Function Handle_Keys()
REM Help Text
text 0,440,"ReturnKey to reset sprites."
text 0,460,"SpaceKey to turn on/off radius."
REM ReturnKey to Reset
if returnkey()
if KeyReturn = 0 then Make_Sprite()
KeyReturn = 1
else
KeyReturn = 0
endif
REM SpaceKey to Toggle Radius
if spacekey()
if KeySpace = 0
if Radius_Flag = 0 then Radius_Flag = 1 else Radius_Flag = 0
endif
KeySpace = 1
else
KeySpace = 0
endif
EndFunction
REM ***********************************
REM Handle_Object
REM ***********************************
Function Handle_Object()
REM Handle all the Sprites
for ID = 1 to Sprite_Max
REM Control the Sprites
Object_Control(ID)
REM Draw the Radius
if Radius_Flag = 1 then circle SpriteX(ID),SpriteY(ID),SpriteSize(ID)/10
REM Draw the Sprite
paste sprite Sprite_Object+ID,SpriteX(ID),SpriteY(ID)
REM Draw the Player Name
if ID = Player_ID
ink rgb(255,255,255),0
center text SpriteX(ID),SpriteY(ID)-SpriteSize(ID)/4,"Player"
endif
next ID
EndFunction
REM *****************
REM Object_Control
REM *****************
Function Object_Control(ID)
REM IF this Sprite is the Player
if ID = Player_ID
REM WASD keys to Suggest a Move
if keystate(17) then MoveY = -1
if keystate(30) then MoveX = -1
if keystate(32) then MoveX = 1
if keystate(31) then MoveY = 1
endif
REM If this Sprite is NOT Player
if ID <> Player_ID
REM Counter
dec SpriteAct(ID)
REM Counter is up, give NPC an action
if SpriteAct(ID) =< 0
REM Random Action
SpriteAction(ID) = rnd(4)
REM Set the counter
SpriteAct(ID) = 10+rnd(10)
endif
REM Actions to Move
if SpriteAction(ID) = 0 then MoveX = 0 : MoveY = 0
if SpriteAction(ID) = 1 then MoveY = -1
if SpriteAction(ID) = 2 then MoveX = -1
if SpriteAction(ID) = 3 then MoveX = 1
if SpriteAction(ID) = 4 then MoveY = 1
endif
REM The Suggested Move needs to pass through Collision before actually Moving the Sprite.
if Collision_Radius(ID,MoveX,0) = 0 then inc SpriteX(ID),MoveX
if Collision_Radius(ID,0,MoveY) = 0 then inc SpriteY(ID),MoveY
EndFunction
REM --------------------------------------------------------
REM ********************************************************
REM Collision_Radius
REM ********************************************************
REM --------------------------------------------------------
Function Collision_Radius(ID,MoveX,MoveY)
REM ReturnValue of Zero means Clear
RetVal = 0
REM Player Values
x = SpriteX(ID)+MoveX
y = SpriteY(ID)+MoveY
size = SpriteSize(ID)
REM Check All sprites
for cID = 1 to Sprite_Max
REM Do not perform check on current ID
if cID <> ID
cx = SpriteX(cID)
cy = SpriteY(cID)
REM Distance Formula
dist# = (cx-x)*(cx-x) + (cy-y)*(cy-y)
REM Decide which Sprite Radius is larger and use that!
if size > SpriteSize(cID) then Radius = size else Radius = SpriteSize(cID)
REM Collision Check, ReturnValue of ONE means NO-GO!
if dist# < Radius
RetVal = 1
endif
endif
next cID
EndFunction RetVal
REM ***********************************
REM Make_Sprite
REM ***********************************
Function Make_Sprite()
REM Making all the sprites
for ID = 1 to Sprite_Max
x = 80*ID
y = 60*ID
Setup_Sprite(ID,x,y)
next ID
EndFunction
REM *****************
REM Setup_Sprite
REM *****************
Function Setup_Sprite(ID,x,y)
REM Drawing the Character
ink rgb(rnd(255),rnd(255),rnd(255))
text 4,9,"T"
text 4,0,"o"
text 4,15,"H"
text 0,6,"--"
REM Grabing the Sprite Image
get image Sprite_Object+ID,0,0,16,28,1
REM Setting the Sprite
sID = Sprite_Object+ID
SpriteAlive(ID) = 1
SpriteSize(ID) = 100+rnd(200)
SpriteX(ID) = x
SpriteY(ID) = y
sprite sID,SpriteX(ID),SpriteY(ID),sID
offset sprite sID,8,24
scale sprite sID,SpriteSize(ID)
hide sprite sID
EndFunction
