Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
ShipSprite.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 
19 public class ShipSprite extends Sprite
20 {
21  static int [] BDx = { 0, 0, 1, 1, 1, 0, -1, -1, -1 };
22  static int [] BDy = { 0, -1, -1, 0, 1, 1, 1, 0, -1 };
23  static int [] BPx = { 0, 0, 2, 2, 2, 0, -2, -2, -2 };
24  static int [] BPy = { 0, -2, -2, 0, 2, 2, 2, 0, -2 };
25  static int [] BtClrTab = { RIVER, CHANNEL, POWERBASE, POWERBASE+1,
26  RAILBASE, RAILBASE+1, BRWH, BRWV };
27 
28  int newDir;
29  int count;
30  int soundCount;
31 
32  public static final int NORTH_EDGE = 5;
33  public static final int EAST_EDGE = 7;
34  public static final int SOUTH_EDGE = 1;
35  public static final int WEST_EDGE = 3;
36 
37  public ShipSprite(Micropolis engine, int xpos, int ypos, int edge)
38  {
39  super(engine, SpriteKind.SHI);
40  this.x = xpos * 16 + 8;
41  this.y = ypos * 16 + 8;
42  this.width = 48;
43  this.height = 48;
44  this.offx = -24;
45  this.offy = -24;
46  this.frame = edge;
47  this.newDir = edge;
48  this.dir = 10;
49  this.count = 1;
50  }
51 
52  @Override
53  public void moveImpl()
54  {
55  int t = RIVER;
56 
57  this.soundCount--;
58  if (this.soundCount <= 0) {
59  if (city.PRNG.nextInt(4) == 0) {
60  city.makeSound(x/16,y/16,Sound.HONKHONK_LOW);
61  }
62  this.soundCount = 200;
63  }
64 
65  this.count--;
66  if (this.count <= 0) {
67  this.count = 9;
68  if (this.newDir != this.frame) {
69  this.frame = turnTo(this.frame, this.newDir);
70  return;
71  }
72  int tem = city.PRNG.nextInt(8);
73  int pem;
74  for (pem = tem; pem < (tem + 8); pem++) {
75  int z = (pem % 8) + 1;
76  if (z == this.dir)
77  continue;
78 
79  int xpos = this.x / 16 + BDx[z];
80  int ypos = this.y / 16 + BDy[z];
81 
82  if (city.testBounds(xpos, ypos)) {
83  t = city.getTile(xpos, ypos);
84  if ((t == CHANNEL) || (t == BRWH) || (t == BRWV) ||
85  tryOther(t, this.dir, z))
86  {
87  this.newDir = z;
88  this.frame = turnTo(this.frame, this.newDir);
89  this.dir = z + 4;
90  if (this.dir > 8) { this.dir -= 8; }
91  break;
92  }
93  }
94  }
95 
96  if (pem == (tem + 8)) {
97  this.dir = 10;
98  this.newDir = city.PRNG.nextInt(8)+1;
99  }
100  }
101  else {
102  int z = this.frame;
103  if (z == this.newDir) {
104  this.x += BPx[z];
105  this.y += BPy[z];
106  }
107  }
108 
109  if (!spriteInBounds()) {
110  this.frame = 0;
111  return;
112  }
113 
114  boolean found = false;
115  for (int z : BtClrTab) {
116  if (t == z) {
117  found = true;
118  }
119  }
120  if (!found) {
121  explodeSprite();
122  destroyTile(x/16, y/16);
123  }
124  }
125 
126  boolean tryOther(int tile, int oldDir, int newDir)
127  {
128  int z = oldDir + 4;
129  if (z > 8) z -= 8;
130  if (newDir != z) return false;
131 
132  return (tile == POWERBASE || tile == POWERBASE+1 ||
133  tile == RAILBASE || tile == RAILBASE+1);
134  }
135 
136  boolean spriteInBounds()
137  {
138  int xpos = x / 16;
139  int ypos = y / 16;
140  return city.testBounds(xpos, ypos);
141  }
142 }