1     /**
2      * Curso Básico de desarrollo de Juegos en Java - Invaders
3      * 
4      * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducción
5      * 
6      * http://www.planetalia.com
7      * 
8      */
9     package version17;
10    
11    import java.awt.Graphics2D;
12    import java.awt.image.BufferedImage;
13    
14    public class Actor {
15      protected int x,y;
16      protected int width, height;
17      protected String[] spriteNames;
18      protected int currentFrame;
19      protected int frameSpeed;
20      protected int t;
21      protected Stage stage;
22      protected SpriteCache spriteCache;
23      
24      public Actor(Stage stage) {
25        this.stage = stage;
26        spriteCache = stage.getSpriteCache();
27        currentFrame = 0;
28        frameSpeed = 1;
29        t=0;
30      }
31      
32      public void paint(Graphics2D g){
33        g.drawImage( spriteCache.getSprite(spriteNames[currentFrame]), x,y, stage );
34      }
35      
36      public int getX()  { return x; }
37      public void setX(int i) { x = i; }
38    
39      public int getY() { return y; }
40      public void setY(int i) { y = i; }
41      
42      public int getFrameSpeed() {return frameSpeed;  }
43      public void setFrameSpeed(int i) {frameSpeed = i; }
44      
45      
46      public void setSpriteNames(String[] names) { 
47        spriteNames = names;
48        height = 0;
49        width = 0;
50        for (int i = 0; i < names.length; i++ ) {
51          BufferedImage image = spriteCache.getSprite(spriteNames[i]);
52          height = Math.max(height,image.getHeight());
53          width = Math.max(width,image.getWidth());
54        }
55      }     
56      
57      public int getHeight() { return height; }
58      public int getWidth() { return width; }
59      public void setHeight(int i) {height = i; }
60      public void setWidth(int i) { width = i;  }
61    
62      public void act() {
63        t++;
64        if (t % frameSpeed == 0){
65          t=0;
66          currentFrame = (currentFrame + 1) % spriteNames.length;
67        }
68      }
69    }
70