I am using Java, particularly the Slick2D library. Seeing as this is my first game, I have no idea how I should manage the Animations and the textures in the game in a way to be both memory efficient and processing efficient.
I was wondering if I should give each and every entity I have its own animations and its own textures. Currently, the way I'm doing this is by making a limited set of images, which are then shared by the animations. These images are only created once in memory.
The animations, however, are done by creating a separate and new animation for every new entity I have.
I have also created a class specifically to handle the resources.
I was wondering if this is the correct way of doing things, or is it not memory and/or performance efficient, or should the entities with similar animations share the same single animation in memory? I find this a bit inconvenient since if I want to add a frame to a particular entity's animation, then all the entities with those animations will be affected.
Or is it perhaps the other way around, and I should even make new textures for every single entity, from which I create new animations. The main reason I can think of for doing this is the effects I might want to do later on on a frame in an animation without affecting the other animations that might use this texture.
My second question is, I'm letting the resource class decide which animation it should send to an entity, depending on the state it's in. For example, it will see if the player is walking left, and give it the according animations, if he's walking right, then the right animation.
I wonder if it's more correct to let the Player class decide which animation it will request from the resource class.
This is my resource class:
package blackhorn;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
public final class CAnimations {
// Player walking left
Image playerWL1;
Image playerWL2;
Image playerWL3;
Image[] playerWL;
// Player walking right
Image playerWR1;
Image playerWR2;
Image playerWR3;
Image[] playerWR;
// Player standing
Image[] playerS;
// Enemy standing
Image[] enemyS;
//Ground
Image[] groundS;
//Bullet
Image[] bulletS;
public void init() throws SlickException {
playerWL1 = new Image("data/playerWL1.png");
playerWL2 = new Image("data/playerWL2.png");
playerWL3 = new Image("data/playerWL3.png");
playerWR1 = new Image("data/playerWR1.png");
playerWR2 = new Image("data/playerWR2.png");
playerWR3 = new Image("data/playerWR3.png");
playerS = new Image[] {new Image("data/playerS1.png")};
enemyS = new Image[] {new Image("data/enemyS1.png")};
groundS = new Image[] {new Image("data/groundS.png")};
bulletS = new Image[] {new Image("data/bulletS.png")};
playerWL = new Image[] { playerWL2, playerWL1, playerWL3, playerWL1 };
playerWR = new Image[] { playerWR2, playerWR1, playerWR3, playerWR1 };
}
public void getCurrentAnimation(Player player) {
if(player.getSideSpeed()<0)
player.setCurrentAnimation(new Animation(playerWL, 500));
else if(player.getSideSpeed()>0)
player.setCurrentAnimation(new Animation(playerWR, 500));
else if(player.getSideSpeed()==0)
player.setCurrentAnimation(new Animation(playerS, 500));
}
public void getCurrentAnimation(Enemy enemy) {
enemy.setCurrentAnimation(new Animation(enemyS, 500));
}
public void getCurrentAnimation(Ground ground) {
ground.setCurrentAnimation(new Animation(groundS, 500));
}
public void getCurrentAnimation(Bullet bullet) {
bullet.setCurrentAnimation(new Animation(bulletS, 500));
}
}
And this is my player class.
public class Player extends Character {
private boolean hasFired = false;
public Player(float x, float y) {
super(x, y, 1, 1, 1, 1, 0, CConstants.ROTATION_RIGHT, CConstants.PLAYER_JUMP_SPEED, CConstants.PLAYER_WEIGHT);
}
public void init(GameContainer container) throws SlickException {
MainGameState.animationList.getCurrentAnimation(this);
super.init(container);
}
public void update(GameContainer container, int delta) throws SlickException {
super.update(container, delta);
}
public void render(GameContainer gc, Graphics g) throws SlickException {
MainGameState.animationList.getCurrentAnimation(this); //animationList is the CAnimation class instance
super.render(gc, g);
}