# How to code the animations of attacks in RPG game

I have an attack class that has the basics like damage and cost and works in the game. Originally I slapped it together to the the ai targeting algorithm so you choose your attack and you see the enemy take damage.

The problem is now I want to animate the attacks I find they don't conform to a single pattern like the other graphics. If you thing of a final fantasy game there is attacks the shoot fire balls so a single image would move across the screen. attacks that are like laser beams so an image would grow as it moves across the screen. attacks the would have things come from the top of the screen is a variety of manners.

I thought of making the class i have now a abstract class with an animation method and deriving several types of attack class from it but im worried about all the extra complexity that would add. I could override the animation method but that would make the attack class huge and waste memory.

Does anyone have any advice on what I can do ?

I asked myself a similar question a while back and here is what I came up with (using a component-based system):

Create an AnimationFrame class which only has a duration. When this updates, it updates the time passed. (If you have a baseObject class, make sure this derives from it)

Create an ImageAnimation class derived from AnimationFrame, which has a SpriteSheet variable to hold your image, and possibly a matrix to resize/crop the spritesheet. When this updates, it draws the image (cropped/resized). It should also super.update() to update the time passed.

Create a SpriteSheetManager class that can hold ImageAnimation objects. When SpriteSheetManager updates, it updates the current ImageAnimation object - ie the current frame. When the current ImageAnimation is done (duration has passed), the manager should move on to the next ImageAnimation. You would have to manually add in ImageAnimations and specify the image/duration/matrix (or somehow pull it from a data file).

Add the SpriteSheetManager to your Player objects, so they update when Players update.

Now if you had a baseObject class and your game objects are stored as an array of baseObjects, then you can add SpriteSheetManager to the game objects so it updates then. This would be useful for large spells that take up the whole screen.

You can also enhance the ImageAnimation class (or create a derived class) to handle scrolling images.

Hopefully this could give you some ideas...

Like mentioned above, it really depends on the way you wrote your code. But this seems like a general method that woudn't be too hard to implement:

(Pseudo code)

    public class User {

public void ExecuteAttack(Enemy target, string selectedAttack){

//calculates damage
int dmg = (baseDamge * somedamageformula);
Animation animation = AttackDictionary.getAnimation(name)

Animator.animateAttack(animation, damage); //executes the animation, might want to show damage number in animation
}

}

public class battle{

array enemies;
array party;
//etc

public void ExecuteAttack(PartyMember user, Enemy target){

user.attack(target, selectedAttack);

}

private void animationComplete(e Event){

nextTurn();
}

}

public class AttackDictionary{

enum attacks{

//long list of all atacks, could be an array an enem or you could collect it from xml or whatevr
}

public static void getAnimation(String attackname){

//you could prevent a big switch statement by using xml or a different method that
//allows you to couple info in 1 item (associative array for example)
switch attacks.attackname{

case attacks.slash{

return new slashanimation.png; //return the animation (spritesheet, model etc)
}
}

}
}


I'm not saying that this is the best method, this just what I would have done :)

This really depends on how your code is organized, I don't think anyone can give a really good answer to what you should and shouldn't do without knowing your code and style in depth. In any case, it doesn't sound like a problem that is prone to consuming problematic amounts of memory. But it could cause problems for you the programmer to manage because it is a lot of code. So don't worry too much about memory, worry about making something that you can work with.

I suspect this is one of those things that isn't elegant, just involves a lot of code. You can try to come up with reasonable building blocks to create attacks. You can try to generalize things massively. But you may spend more time trying to invent generalization than you would just coding it a dubious way, and there's no way you're accomplishing this without a significant chunk of code.

I'd just start coding new visual effects, either via a branch in your animation method, or via inheritance. Generalize where it makes sense (i.e. it's silly to write "animate fireball hitting enemy" and "animate energy bolt hitting enemy" classes, just write "animate image hitting enemy" and specify the image based on the spell.) As you write more visual effects, look for ways to combine them sensibly into a larger structure. Maybe one will become apparently. Maybe it won't! Maybe it's just annoyingly complex code.