OnSheet
AS INTEGER 'Which sheet does our character appear on Xoff
AS INTEGER 'What's the Xoffset to where the first frame appears? Yoff
AS INTEGER 'What's the Yoffset to where the first frame appears? ScreenX
AS _FLOAT 'Where does the character appear on the visible screen? LastX
AS INTEGER 'Where was the character at last, on the map? MoveSpeed
AS INTEGER 'How fast can this character move? Facing
AS INTEGER 'which direction are we facing? Frame
AS INTEGER 'which frame is currently showing? Moving
AS _FLOAT 'is the character moving? MoveDirection
AS INTEGER 'Which direction are they moving
DIM SHARED MaxSheetLimit
AS INTEGER: MaxSheetLimit
= 100 'default to a maximum of 100 sprite sheets being loaded at a time DIM SHARED MaxCharacterLimit
AS INTEGER: MaxCharacterLimit
= 255 'default to a maximum of 255 characters being tracked at a time
CONST Up%
= 8, Down%
= 1, Left%
= 2, Right%
= 4 CONST North%
= 8, South%
= 1, West%
= 2, East%
= 4
MaxCharacterLimit = 10 'for my demo, all I need is a max of 10 characters. The fewer, the faster we process them
MainSheet = LoadSpriteSheet("Actor1.png")
Hero = SetActor(MainSheet, 0, 0)
TwinSis = SetActor(MainSheet, 144, 0)
MoveActor Hero, 5, 5
MoveActor TwinSis, 0, 3
FaceActor Hero, North
FaceActor TwinSis, East
SetActorSpeed Hero, 10
CharacterSelected = Hero
IF Actor
(CharacterSelected
).Moving
= 0 THEN 'If we're not moving, then we can change where we're looking CASE 18432: FaceActor CharacterSelected
, North
'up CASE 19200: FaceActor CharacterSelected
, West
'left CASE 19712: FaceActor CharacterSelected
, East
'right CASE 20480: FaceActor CharacterSelected
, South
'south IF CharacterSelected
= Hero
THEN CharacterSelected
= TwinSis
ELSE CharacterSelected
= Hero
Actor(CharacterSelected).MoveSpeed = Actor(CharacterSelected).MoveSpeed - 1
Actor(CharacterSelected).MoveSpeed = Actor(CharacterSelected).MoveSpeed + 1
IF Actor
(CharacterSelected
).ScreenY
> 0 AND Actor
(CharacterSelected
).Moving
= 0 THEN FaceActor CharacterSelected, North
Actor(CharacterSelected).MoveDirection = Up
Actor(CharacterSelected).Moving = 1
IF Actor
(CharacterSelected
).ScreenX
> 0 AND Actor
(CharacterSelected
).Moving
= 0 THEN FaceActor CharacterSelected, West
Actor(CharacterSelected).MoveDirection = West
Actor(CharacterSelected).Moving = 1
IF Actor
(CharacterSelected
).ScreenX
< _WIDTH - 48 AND Actor
(CharacterSelected
).Moving
= 0 THEN FaceActor CharacterSelected, East
Actor(CharacterSelected).MoveDirection = East
Actor(CharacterSelected).Moving = 1
IF Actor
(CharacterSelected
).ScreenY
< _HEIGHT - 48 AND Actor
(CharacterSelected
).Moving
= 0 THEN FaceActor CharacterSelected, South
Actor(CharacterSelected).MoveDirection = South
Actor(CharacterSelected).Moving = 1
DrawActors 0
LOCATE 1, 1:
PRINT "Hero Speed:"; Actor
(Hero
).MoveSpeed
LOCATE 2, 1:
PRINT "Twin Speed:"; Actor
(TwinSis
).MoveSpeed
FOR i
= 1 TO MaxCharacterLimit
IF Actor
(i
).OnSheet
= 0 THEN _CONTINUE 'if it's not a valid actor, skip them Actor(i).LastX = Actor(i).ScreenX
Actor(i).LastY = Actor(i).ScreenY
move = 48 * Actor(i).MoveSpeed / Limit
FrameChange = 0: Actor(i).Moving = 0 'we're too damn slow to move!
CASE Up: YChange
= -move: XChange
= 0 CASE West: XChange
= -move: YChange
= 0 CASE East: XChange
= move: YChange
= 0 CASE South: YChange
= move: XChange
= 0 FrameChange = FrameChange + move
Actor(i).Frame = 0
FrameChange = 0: Actor(i).Moving = 0 'once we've traveled 48 pixels, we're finished
Actor
(i
).ScreenX
= Actor
(i
).LastX
+ SGN(XChange
) * 48 Actor
(i
).ScreenY
= Actor
(i
).LastY
+ SGN(YChange
) * 48 Actor
(i
).Frame
= (ABS(Actor
(i
).LastX
- Actor
(i
).ScreenX
) + ABS(Actor
(i
).LastY
- Actor
(i
).ScreenY
)) \
16 'Which frame of our animation are we playing? Actor(i).ScreenY = Actor(i).ScreenY + YChange 'otherwise we move the proper amount across the screen
Actor(i).ScreenX = Actor(i).ScreenX + XChange
Actor(i).Frame = 0
Xoff = Actor(i).Xoff + 48 * Actor(i).Frame
Yoff = Actor(i).Yoff
CASE 8: Yoff
= Yoff
+ 144 _PUTIMAGE (Actor
(i
).ScreenX
, Actor
(i
).ScreenY
), Actor
(i
).OnSheet
, Where
, (Xoff
, Yoff
)-STEP(48, 48)
Actor(Who).ScreenX = WhereX * 48
Actor(Who).ScreenY = WhereY * 48
Actor(Who).LastX = WhereX * 48
Actor(Who).LastY = WhereY * 48
Actor(Who).MoveSpeed = HowFast
Actor(Who).Facing = WhichDirection
'We'll get an ERROR 5 -- "Illegal Function Call" -- if we try and access a sheet that hasn't been loaded,
'freed, or is otherwise an invalid handle.
FOR i
= 1 TO MaxCharacterLimit
IF Actor
(i
).OnSheet
= 0 THEN 'it's a free handle for the character/actor to use Actor(i).OnSheet = SpriteSheet(WhichSheet).InUse
Actor(i).Xoff = Xoff
Actor(i).Yoff = Yoff
Actor(i).ScreenX = -1000 'we start off the screen, by default
Actor(i).ScreenY = -1000
Actor(i).LastX = -1000 'we start off the screen, by default
Actor(i).LastY = -1000
Actor(i).MoveSpeed = 1
Actor(i).Frame = 1
SetActor = i
IF temp
= -1 THEN ERROR 57:
EXIT SUB 'Error 57 is "Device I/O Error". Apparently we found the file ' (or else we'd toss the error above), but it won't load properly.
' This sounds like a Device I/O Error to me, and keeps us from having
' issues trying to debug if the file exists, or the path is proper, by
' tossing the same error message.
'If we're to here, we now need to check for an open file handle, and then assign it for use.
FOR i
= 1 TO MaxSheetLimit
IF SpriteSheet
(i
).InUse
= 0 THEN SpriteSheet(i).InUse = temp 'Our image handle, for where the sprite sheet is loaded at in memory
LoadSpriteSheet = i
EXIT SUB 'If all works as expected, we exit our sub here 'If we made it to here, we have a problem -- there's no file handles available for usage.
'By default, I'm setting the MaxSheetLimit to 100, so as to make certain that we keep memory usage reasonable.
'Free up some unused sprite sheets, combine a few, or manually increase the limit yourself.
'Otherwise, you're going to see:
ERROR 67 'Error 67 is "Too Many Files"