View Javadoc

1   package erland.game.tileadventure;
2   /*
3    * Copyright (C) 2004 Erland Isaksson (erland_i@hotmail.com)
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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          // Not supported
81      }
82  
83      public void read(ParameterValueStorageExInterface in) {
84          // Not supported
85      }
86  
87      public Object clone() {
88          AnimatedObject o = (AnimatedObject) super.clone();
89          o.animation = (AnimationInterface) animation.clone();
90          return o;
91      }
92  }