Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
RocketSprite.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 
16 public class RocketSprite extends Sprite {
17  private int destX;
18  private int destY;
19  private double speedFactor;
20  private boolean soundPlaying;
21 
22  public int ownerID;
23 
24  // NOTE: used for movement
25  static int[] CDx = {
26  0, 0, 6, 8, 6, 0, -6, -8, -6
27  };
28  static int[] CDy = {
29  0, -8, -6, 0, 6, 8, 6, 0, -6
30  };
31 
32  // CONSTRUCTORS
33  public RocketSprite(Micropolis engine, int xpos, int ypos, int xDest, int yDest, int ownerID) {
34  this(engine, xpos, ypos, xDest, yDest, 2, ownerID);
35  }
36 
37  public RocketSprite(Micropolis engine, int xpos, int ypos, int xDest, int yDest, double speedFactor, int ownerID) {
38  super(engine, SpriteKind.ROC);
39 
40  this.ownerID = ownerID;
41 
42  setStart(xpos, ypos);
43  setDestination(xDest, yDest);
44 
45  this.speedFactor = speedFactor;
46  this.soundPlaying = false;
47 
48  // size of the sprite image in pixels
49  this.width = 64;
50  this.height = 64;
51 
52  this.offx = -32;
53  this.offy = -32;
54 
55  frame = getDir(x, y, destY, destX);
56  }
57 
58  private void setStart(int x, int y) {
59  this.x = tilePosToPixel(x);
60  this.y = tilePosToPixel(y);
61  }
62 
63  private void setDestination(int x, int y) {
64  this.destX = tilePosToPixel(x);
65  this.destY = tilePosToPixel(y);
66  }
67 
68  // custom: override to make it move more smooth
69  public static int getDir(int orgX, int orgY, int desX, int desY) {
70  final int Gdtab[] = {
71  // 1 == north, 3 == east, 5 == south, 7 == west
72  0, 3, 2, 1, 3, 4, 5, 7, 6, 5, 7, 8, 1
73  };
74  int dispX = desX - orgX;
75  int dispY = desY - orgY;
76 
77  int z;
78  // going left
79  if(dispX < 0) {
80  // going up
81  if(dispY < 0) {
82  z = 11;
83  }
84  else {
85  z = 8;
86  }
87  }
88  // going right
89  else {
90  // going up
91  if(dispY < 0) {
92  z = 2;
93  }
94  else {
95  z = 5;
96  }
97  }
98 
99  dispX = Math.abs(dispX);
100  dispY = Math.abs(dispY);
101 
102  if(dispX <= 6 || dispY <= 6) {
103  if(dispX < dispY)
104  z++;
105  else if(dispY < dispX)
106  z--;
107  }
108 
109  if(z >= 1 && z <= 12) {
110  return Gdtab[z];
111  }
112 
113  return 0;
114  }
115 
116  private double stepsTilBoom() {
117  int absY = Math.abs(destY - y);
118  int absX = Math.abs(destX - x);
119  int diffMin = Math.min(absX, absY);
120  int diffMax = Math.max(absX, absY);
121  return (diffMin / 6 + (diffMax - diffMin) / 8) / speedFactor;
122  }
123 
124  @Override
125  protected void explodeSprite() {
126  this.frame = 0;
127  city.makeGiantExplosionAt(x, y, city.getPlayerInfo(ownerID).researchData.getRocketRadius());
128  int xpos = x / 16;
129  int ypos = y / 16;
130  city.crashLocation = new CityLocation(xpos, ypos);
131  city.sendMessageAt(MicropolisMessage.ROCKETCRASH_REPORT, xpos, ypos);
132  city.makeSound(xpos, ypos, Sound.EXPLOSION_HIGH);
133  }
134 
135  public void moveImpl() {
136  double secondsTilBoom = stepsTilBoom() / city.simSpeed.getAnimationsPerSecond();
137  double soundDuration = 3.25;
138 
139  // play sound
140  if(secondsTilBoom <= soundDuration && !soundPlaying) {
141  city.makeSound(x, y, Sound.DUBSPLOSION);
142  soundPlaying = true;
143  }
144 
145  if(getDis(x, y, destX, destY) <= 6 * speedFactor) {
146  System.out.println("explode");
147  this.explodeSprite();
148  remove();
149  }
150 
151  int d = getDir(x, y, destX, destY);
152  frame = turnTo(frame, d);
153 
154  this.x += CDx[frame] * speedFactor;
155  this.y += CDy[frame] * speedFactor;
156  }
157 }