Hi guys,
I needed an animation system for new game but the loop object command is not powerfull enough so i decided to create a function that could automatically controll all the animations of every object in the game. That way I only need 1 command per object!!!
Okay so here we go. Here is the code first of all:
sync on : sync rate 0
set display mode 1280,1024,0
`this whole section sets up the custom type and other variables
global newanimid as integer = 0
type objectanim_type
objectid as integer
currentframe# as float
currentloop as integer
storeloop as integer
blend as integer
blendstage# as float
storestartframe as integer
endtype
maxobjectanim as integer = 500
dim objectanim(maxobjectanim) as objectanim_type
global animSpeed# as float = 25.0
global animTimer as integer = timer()
global timeFrame# as float
spider_obj = 1
spider_anim = NewAnimIndex() `this creates a new ID for the anim system
objectanim(spider_anim).objectid = spider_obj `stores the object number in the anim system
Load Object "spider.x",spider_obj
do
set cursor 0,0
print int(objectanim(spider_anim).currentframe#)
`these timers must be at the start of your loop
time = timer()-animTimer
timeFrame# = time*(animSpeed#/1000.0)
if upkey()
AnimController(spider_anim,1,19,0,25)
endif
if downkey()
AnimController(spider_anim,24,30,0,25)
endif
if spacekey()
AnimReset(spider_anim)
endif
`place this code at the end of your loop
animTimer = timer()
sync
loop
function AnimController(animID,startframe,endframe,loops,blend#)
if objectanim(animID).storestartframe <> startframe
objectanim(animID).blend = 0
objectanim(animID).blendstage# = 0
objectanim(animID).currentframe# = 0
objectanim(animID).currentloop = 0
endif
objectanim(animID).storestartframe = startframe
if objectanim(animID).blendstage# < 100
objectanim(animID).blend = 1
endif
if objectanim(animID).blend = 0
if objectanim(animID).currentframe# = 0
objectanim(animID).currentframe# = startframe
endif
if loops = 0
objectanim(animID).currentloop = 1
endif
if objectanim(animID).currentloop > -1
objectanim(animID).currentframe# = objectanim(animID).currentframe# + timeFrame#
endif
if objectanim(animID).currentframe# > endframe
objectanim(animID).currentframe# = startframe
inc objectanim(animID).currentloop
endif
if objectanim(animID).currentloop = loops
objectanim(animID).currentloop = -1
objectanim(animID).storeloop = 0
endif
if objectanim(animID).currentloop > -1 or loops = 0
set object frame objectanim(animID).objectid, objectanim(animID).currentframe#
endif
endif
if objectanim(animID).blend = 1
if blend# = 0
objectanim(animID).blendstage# = 100
else
objectanim(animID).blendstage# = objectanim(animID).blendstage# + (timeFrame#*(blend#))
endif
set object interpolation objectanim(animID).objectid, objectanim(animID).blendstage#
if objectanim(animID).blendstage# >= 100
objectanim(animID).blend = 0
objectanim(animID).blendstage# = 100
objectanim(animID).currentframe# = 0
endif
set object frame objectanim(animID).objectid, startframe
endif
endfunction
function AnimPause(animId)
if objectanim(animID).currentloop > -1
objectanim(animID).storeloop = objectanim(animID).currentloop
endif
objectanim(animID).currentloop = -1
endfunction
function AnimContinue(animId)
objectanim(animID).currentloop = objectanim(animID).storeloop
endfunction
function AnimReset(animId)
objectanim(animID).currentloop = 0
objectanim(animID).currentframe# = 0
endfunction
function NewAnimIndex()
newanimid=newanimid+1
endfunction newanimid
You'll need to copy all the functions and include the setup code at the top.
Instructions:
Once you create an object in your game you'll need to include it in this animation system by first getting a new animID. eg. mynewobjectanim = NewAnimIndex().
Now setup the system for your object using: objectanim(mynewobjectanim).objectid = objectnumber
Thats it! You are ready to use the commands:
The main function is AnimController. You can call this whenever you like in your loop and it will automatically take care of itself.
Usage:
AnimController(animID,startframe,endframe,loops,blend#)
animID : this is the number we assigned earlier (eg. mynewobjectanim)
startframe : the first frame of the animation you want to play
endframe : the last frame of the animation
loops : the number of times you want the animation to loop. The object will automatically stop animating when it has reached the set number of loops. Use 0 to loop infinitely.
blend# : The speed of the interpolation between different animations. Use 0 for no interpolation.
AnimPause(animId)
Pauses a currently playing animation. Use
AnimContinue() to starting playing again from where it left off.
AnimReset(animId)
Use this to reset the animation of an object that has stopped because it has finished playing through the loops. (Doesnt apply to objects set to loop infinitely).
I have included an exe file that show this in action. Use the Up key to play one animation and the Down key to play another. It will play at the same speed on any machine running any framerate. Notice how it automatically interpolates between animations.
Thats it. Hope this is usefull. Let me know what you think