This is a class to hold an animation and be able to refer to different animations by titles, but it seems like calling draw(Graphics2D g)
and update
every frame, would, with this implementation, cause a big slowdown with all the list.get(Object obj)
calls. Is this a big deal, and if so how should I change the implementation? Other comments on the code are very welcome.
package core.graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AnimationSet {
private int currentAnimation;
private Map<String, Integer> namesToAnimation;
private List<Animation> animations;
public AnimationSet() {
currentAnimation = 0;
namesToAnimation = new HashMap<String, Integer>();
animations = new ArrayList<Animation>();
}
public void addAnimation(String title, Animation animation){
animations.add(animation);
namesToAnimation.put(title, animations.size()-1);
}
public void goToAnimation(String title){
currentAnimation = namesToAnimation.get(title);
}
public void removeAnimation(String title){
animations.remove(namesToAnimation.get(title));
namesToAnimation.remove(title);
}
public void draw(Graphics2D g, int x, int y){
g.drawImage(animations.get(currentAnimation).getImage(), x, y, null);
}
public void update(){
animations.get(currentAnimation).update();;
}
}