So, I’m working on an animation class that loads all the objects into an ArrayList right now, and really the best place I can think to store an ArrayList of said object, is within the object class itself. I’m curious if you guys think this is a good practice, or if I should put the ArrayList somewhere else?
It’s a simple little class, all it’s for is handling the animated tiles on my TiledMap, basically it loads a MapAnimations object into the MAP_ANIMATIONS ArrayList, but the catch is, that ArrayList MAP_ANIMATIONS is actually inside the MapAnimations object/class itself. It’s also should be noted it’s a static ArrayList that is dumped/reloaded when you change maps.
It works like a champ, and seems the most straightforward and simple way to do it, but at the same time feels awkward. What are your opinions on this? Should I go ahead and pull the ArrayList out of the object and put it in my MapController class?
To load a new animation, it seeks out any tiles with animation properties in the MapController class when the map is loaded:
for (int x = 0; x < map.getWidth(); x++) {
for (int y = 0; y < map.getHeight(); y++) {
for (int l = 0; l < tileID.length; l++) {
tileID[l] = map.getTileId(x, y, l);
//BUILD ANIMATIONS
if (!(map.getTileProperty(tileID[l], "animation", "false").equals("false"))){
MapAnimations.MAP_ANIMATION_ARRAY.add (new MapAnimations(x,y,map.getTileProperty(tileID[l], "animation", "false")));
}
}
}
}
Then, the MapAnimation class itself (Don’t mind the code, it’s really rough at the moment, I wrote it all up in about 5 minutes just to test the concept :o )
package core;
import java.util.ArrayList;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
public class MapAnimations {
private Animation animation;
int duration = 195;
private int spriteId;
private int spriteX,spriteY;
//STATIC VALUES
private static boolean firstTime = true;
private static ArrayList<SpriteSheet> MAP_ANIMATION_SHEET_ARRAY = new ArrayList<SpriteSheet>();
static ArrayList<MapAnimations> MAP_ANIMATION_ARRAY = new ArrayList<MapAnimations>();
public MapAnimations(int x, int y, String file) throws SlickException{
firstTimeLoad();
spriteX = x*32;
spriteY = y*32;
for (int x1 = 0; x1 < MAP_ANIMATION_SHEET_ARRAY.size(); x1++) {
if (MAP_ANIMATION_SHEET_ARRAY.get(x1).getResourceReference().equals("res/animations/"+file+".png")){
spriteId = x1;
break;
}
if (x1 == MAP_ANIMATION_SHEET_ARRAY.size()-1){
MAP_ANIMATION_SHEET_ARRAY.add(new SpriteSheet("res/animations/"+file+".png", 32, 32, 0));
spriteId = x1;
}
}
Image[] animArray = {MAP_ANIMATION_SHEET_ARRAY.get(spriteId).getSprite(0,0),MAP_ANIMATION_SHEET_ARRAY.get(spriteId).getSprite(1,0),MAP_ANIMATION_SHEET_ARRAY.get(spriteId).getSprite(2,0),MAP_ANIMATION_SHEET_ARRAY.get(spriteId).getSprite(3,0)};
animation = new Animation(animArray, duration, true);
}
public void render(float mapX, float mapY) throws SlickException{
animation.draw(spriteX+mapX,spriteY+mapY);
}
public void firstTimeLoad() throws SlickException{
if (firstTime){
System.out.println("Loading first time");
MAP_ANIMATION_SHEET_ARRAY.add(new SpriteSheet("res/animations/barLampLeft.png", 32, 32, 0));
firstTime = false;
}
}
}