Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
MonsterSprite.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 static micropolisj.engine.TileConstants.*;
12 
16 public class MonsterSprite extends Sprite
17 {
18  int count;
19  int soundCount;
20  int destX;
21  int destY;
22  int origX;
23  int origY;
24  int step;
25  boolean flag; //true if the monster wants to return home
26 
27  //GODZILLA FRAMES
28  // 1...3 : northeast
29  // 4...6 : southeast
30  // 7...9 : southwest
31  // 10..12 : northwest
32  // 13 : north
33  // 14 : east
34  // 15 : south
35  // 16 : west
36 
37  // movement deltas
38  static int [] Gx = { 2, 2, -2, -2, 0 };
39  static int [] Gy = { -2, 2, 2, -2, 0 };
40 
41  static int [] ND1 = { 0, 1, 2, 3 };
42  static int [] ND2 = { 1, 2, 3, 0 };
43  static int [] nn1 = { 2, 5, 8, 11 };
44  static int [] nn2 = { 11, 2, 5, 8 };
45 
46  public MonsterSprite(Micropolis engine, int xpos, int ypos)
47  {
48  super(engine, SpriteKind.GOD);
49  this.x = xpos * 16 + 8;
50  this.y = ypos * 16 + 8;
51  this.width = 48;
52  this.height = 48;
53  this.offx = -24;
54  this.offy = -24;
55 
56  this.origX = x;
57  this.origY = y;
58 
59  this.frame = xpos > city.getWidth() / 2 ?
60  (ypos > city.getHeight() / 2 ? 10 : 7) :
61  (ypos > city.getHeight() / 2 ? 1 : 4);
62 
63  this.count = 1000;
65  this.destX = p.x * 16 + 8;
66  this.destY = p.y * 16 + 8;
67  this.flag = false;
68  this.step = 1;
69  }
70 
71  @Override
72  public void moveImpl()
73  {
74  if (this.frame == 0) {
75  return;
76  }
77 
78  if (soundCount > 0) {
79  soundCount--;
80  }
81 
82  int d = (this.frame - 1) / 3; // basic direction
83  int z = (this.frame - 1) % 3; // step index (only valid for d<4)
84 
85  if (d < 4) { //turn n s e w
86  assert step == -1 || step == 1;
87  if (z == 2) step = -1;
88  if (z == 0) step = 1;
89  z += step;
90 
91  if (getDis(x, y, destX, destY) < 60) {
92 
93  // reached destination
94 
95  if (!flag) {
96  // destination was the pollution center;
97  // now head for home
98  flag = true;
99  destX = origX;
100  destY = origY;
101  }
102  else {
103  // destination was origX, origY;
104  // hide the sprite
105  this.frame = 0;
106  return;
107  }
108  }
109 
110  int c = getDir(x, y, destX, destY);
111  c = (c - 1) / 2; //convert to one of four basic headings
112  assert c >= 0 && c < 4;
113 
114  if ((c != d) && city.PRNG.nextInt(11) == 0) {
115  // randomly determine direction to turn
116  if (city.PRNG.nextInt(2) == 0) {
117  z = ND1[d];
118  }
119  else {
120  z = ND2[d];
121  }
122  d = 4; //transition heading
123 
124  if (soundCount == 0) {
125  city.makeSound(x/16, y/16, Sound.MONSTER);
126  soundCount = 50 + city.PRNG.nextInt(101);
127  }
128  }
129  }
130  else {
131  assert this.frame >= 13 && this.frame <= 16;
132 
133  int z2 = (this.frame - 13) % 4;
134 
135  if (city.PRNG.nextInt(4) == 0) {
136  int newFrame;
137  if (city.PRNG.nextInt(2) == 0) {
138  newFrame = nn1[z2];
139  } else {
140  newFrame = nn2[z2];
141  }
142  d = (newFrame-1) / 3;
143  z = (newFrame-1) % 3;
144 
145  assert d < 4;
146  }
147  else {
148  d = 4;
149  }
150  }
151 
152  this.frame = ((d * 3) + z) + 1;
153 
154  assert this.frame >= 1 && this.frame <= 16;
155 
156  this.x += Gx[d];
157  this.y += Gy[d];
158 
159  if (this.count > 0) {
160  this.count--;
161  }
162 
163  int c = getChar(x, y);
164  if (c == -1 ||
165  (c == RIVER && this.count != 0 && false)
166  ) {
167  this.frame = 0; //kill zilla
168  }
169 
170  for (Sprite s : city.allSprites())
171  {
172  if (checkSpriteCollision(s) &&
173  (s.kind == SpriteKind.AIR ||
174  s.kind == SpriteKind.COP ||
175  s.kind == SpriteKind.SHI ||
176  s.kind == SpriteKind.TRA)
177  ) {
178  s.explodeSprite();
179  }
180  }
181 
182  destroyTile(x / 16, y / 16);
183  }
184 }