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 version27;
10    
11    public class Monster extends Actor {
12      protected int vx;
13      protected static final double FIRING_FREQUENCY = 0.01;
14      
15      public Monster(Stage stage) {
16        super(stage);
17        setSpriteNames( new String[] {"bicho0.gif","bicho1.gif"});
18        setFrameSpeed(35);
19      }
20      
21      public void act() {
22        super.act();
23        x+=vx;
24        if (x < 0 || x > Stage.WIDTH)
25          vx = -vx;
26        if (Math.random()<FIRING_FREQUENCY)
27          fire();
28      }
29    
30      public int getVx() { return vx; }
31      public void setVx(int i) {vx = i; }
32      
33      public void collision(Actor a) {
34        if (a instanceof Bullet || a instanceof Bomb) {
35          remove();
36          stage.getSoundCache().playSound("explosion.wav");
37          spawn();
38          stage.getPlayer().addScore(20);
39        }
40      }
41      
42      public void spawn() {
43        Monster m = new Monster(stage);
44        m.setX( (int)(Math.random()*Stage.WIDTH) );
45        m.setY( (int)(Math.random()*Stage.PLAY_HEIGHT/2) );
46        m.setVx( (int)(Math.random()*20-10) );
47        stage.addActor(m);
48      }
49      
50      public void fire() {
51        Laser m = new Laser(stage);
52        m.setX(x+getWidth()/2);
53        m.setY(y + getHeight());
54        stage.addActor(m);
55        stage.getSoundCache().playSound("photon.wav");
56    
57      }
58    }
59