An animation package will solve some problems usualy associated with loading and managing resources besides providing the usual animation services:
* a way to store resources in memory in a centralized way. That is one single copy for each resource, several links to it.
* a way to store a animation functions and use them.
* a way to compute animation values.
* a data file format to define resource alias names to store animation tables themselves (hardcoded animation data is hugly).
With this we can code a game loop that will compute the animations for the each entity and redraw the scene. The most flexible way for us to do this is to use an animation just like a math function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| animSystem.setStartTime(sometimevalue); int WALKING_ANIM = animSystem.getAnimationId("walking"); int IMAGE_PROP = animSystem.getPropertyId("sound"); int SOUND_PROP = animSystem.getPropertyId("image"); int RTX_PROP = animSystem.getPropertyId("rtx"); ...
animSystem.updateCurrentTime(currenttime); int imgid = animSystem.GetAnimValue(WALKING_ANIM, IMAGE_PROP); int sndid = animSystem.GetAnimValue(WALKING_ANIM, SOUND_PROP); float trx = animSystem.GetAnimValue(WALKING_ANIM, RTX_PROP);
MyImageClass image = (MyImageClass)animSystem.getResource(imgid); MySoundClass sound = (MySoundClass)animSystem.getResource(sndid); |
We do the drawing ourselves in each frame and get the position values from each entity AI. We disturb our position x value with trx, like this (x += trx) for the effect.
When we use an AI framework, we give the AI the input of whats happening in the world from the perspective and sensorial capabilities of some AI, then query the AI for state changes (reactions) of that entity and get whatever animations we should use from the entity. If we should move or keep the same position, if we should fire, etc.
But the fundamental problem remains: how to handle animations inside the game loop.
There are some fundamental issues to be solved here. Here's some hints on how to solve them:
* the game loop needs to keep track of current frame time and the last frame time
* the game loop will track several entities at once, query their states, and decide if they should move, fire or whatever. Each entity should have complete information that should be needed to do an animation step (entities have their animation data inside them).
* alternatively we can have an HashMap<Entity,EntityInfo>. We can associate extra information to each entity that will be used only in the context of a game cycle.
* in a multiple scenario entities will collide with each other, with the environment and with static objects that are marked as colidable. To solve this problem the game should have a collision simulation algorithm (maybe a CollisionHandler class).
* To avoid the collision simulation algorithm to be called so many times each entity AI can have some sort of path finding behavior, with a vison system and perhaps a messaging system to dialog with nearby entities and negotiate how to avoid collision.
* its important to have game logic loops separated from rendering loops.
For more info see this site with great info:
fivedots.coe.psu.ac.th/~ad/jg/
The chapter that interests for this subject:
fivedots.coe.psu.ac.th/~a...index.html