1 package erland.game.tileadventure;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import erland.util.SubImageHandler;
22 import erland.util.ParameterValueStorageExInterface;
23 import erland.util.StringUtil;
24
25 import java.awt.*;
26
27 public class AnimatedObject extends GameObject implements GameObjectUpdateInterface {
28 private Image image;
29 private SubImageHandler subImages;
30 private String imageName;
31 private AnimationInterface animation;
32 private int currentSubImage=-1;
33
34 /***
35 * Set the animation
36 * @param image The main image
37 * @param animation The animation object containing the subimages to animate between
38 */
39 public void setAnimation(Image image, AnimationInterface animation) {
40 this.animation = animation;
41 this.image = image;
42 this.subImages = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment()).createSubImageHandler(this.image);
43 }
44
45 /***
46 * Set the animation
47 * @param image The name of the main image
48 * @param animation The animation object containing the subimages to animate between
49 */
50 public void setAnimation(String image, AnimationInterface animation)
51 {
52 if(getEnvironment().getImageHandler()!=null) {
53 setAnimation(getEnvironment().getImageHandler().getImage(image),animation);
54 }
55 this.imageName = image;
56 }
57
58 public boolean update() {
59 if(animation!=null) {
60 int image = animation.getNextImage();
61 if(image!=currentSubImage) {
62 currentSubImage = image;
63 return true;
64 }
65 }
66 return false;
67 }
68
69 public void draw(Graphics g) {
70 if(subImages!=null) {
71 if(getContainer().getVisible(getPosX(),getPosY(),getPosZ())) {
72 if(currentSubImage>=0) {
73 subImages.drawImage(g,currentSubImage,getDrawingPosX(0f,0f,0f),getDrawingPosY(0f,0f,0f));
74 }
75 }
76 }
77 }
78
79 public void write(ParameterValueStorageExInterface out) {
80
81 }
82
83 public void read(ParameterValueStorageExInterface in) {
84
85 }
86
87 public Object clone() {
88 AnimatedObject o = (AnimatedObject) super.clone();
89 o.animation = (AnimationInterface) animation.clone();
90 return o;
91 }
92 }