Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
SpaceshipSprite.java
Go to the documentation of this file.
1 // This file is part of MicropolisJ.
2 // Copyright (C) 2013 Jason Long
3 // Portions Copyright (C) 1989-2007 Electronic Arts Inc.
4 //
5 // MicropolisJ is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GPLv3, with additional terms.
7 // See the README file, included in this distribution, for details.
8 
9 package micropolisj.engine;
10 
11 import micropolisj.gui.MainWindow;
12 import micropolisj.util.MP3;
13 
19 public class SpaceshipSprite extends Sprite {
20  private int destX;
21  private int destY;
22  private double speedFactor;
23  private boolean soundPlaying;
24  private MP3 sound;
25  private int prevDist;
26 
27  // CONSTRUCTORS
28  public SpaceshipSprite(Micropolis engine, int xpos, int ypos) {
29  this(engine, xpos, ypos, 1.0);
30  }
31 
32  public SpaceshipSprite(Micropolis engine, int xpos, int ypos, double speedFactor) {
33  super(engine, SpriteKind.SPACESHIP);
34 
35  this.speedFactor = speedFactor;
36  this.soundPlaying = false;
37  this.prevDist = Integer.MAX_VALUE;
38  x = xpos;
39  y = ypos;
40 
41  // size of the sprite image in pixels
42  this.width = 96;
43  this.height = 96;
44 
45  this.offx = -48;
46  this.offy = -48;
47 
48  MainWindow window = engine.mainWindow;
49 
50  // the 2 is indeed a magic number: it makes the spaceship vanish ~1sec
51  // after flying off the map
52  int diff = Math.max(window.getWidth() - xpos, window.getHeight() - ypos) * 2;
53 
54  destX = x + diff;
55  destY = y + diff;
56 
57  frame = 1;
58  }
59 
60  public void moveImpl() {
61  // play sound
62  if(!soundPlaying && speedFactor > 1.05) {
63  sound = new MP3(MainWindow.class.getResource("/sounds/" + Sound.SPACESHIP_LAUNCH.getWavName() + ".mp3"));
64  sound.play();
65  soundPlaying = true;
66  }
67 
68  int dist = getDis(x, y, destX, destY);
69 
70  if(dist > prevDist) {
71  System.out.println("exploding spaceship");
72  remove();
73  }
74 
75  prevDist = dist;
76 
77  this.x += 10 * speedFactor;
78  this.y += 10 * speedFactor;
79  speedFactor *= 1.03;
80  }
81 }