Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
TrainSprite.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 TrainSprite extends Sprite
20 {
21  static int [] Cx = { 0, 16, 0, -16 };
22  static int [] Cy = { -16, 0, 16, 0 };
23  static int [] Dx = { 0, 4, 0, -4, 0 };
24  static int [] Dy = { -4, 0, 4, 0, 0 };
25  static int [] TrainPic2 = { 1, 2, 1, 2, 5 };
26  static final int TRA_GROOVE_X = 8;
27  static final int TRA_GROOVE_Y = 8;
28 
29  static final int FRAME_NORTHSOUTH = 1;
30  static final int FRAME_EASTWEST = 2;
31  static final int FRAME_NW_SE = 3;
32  static final int FRAME_SW_NE = 4;
33  static final int FRAME_UNDERWATER = 5;
34 
35  static final int DIR_NORTH = 0;
36  static final int DIR_EAST = 1;
37  static final int DIR_SOUTH = 2;
38  static final int DIR_WEST = 3;
39  static final int DIR_NONE = 4; //not moving
40 
41  public TrainSprite(Micropolis engine, int xpos, int ypos)
42  {
43  super(engine, SpriteKind.TRA);
44  this.x = xpos * 16 + TRA_GROOVE_X;
45  this.y = ypos * 16 + TRA_GROOVE_Y;
46  this.offx = -16;
47  this.offy = -16;
48  this.dir = DIR_NONE; //not moving
49  }
50 
51  @Override
52  public void moveImpl()
53  {
54  if (frame == 3 || frame == 4) {
55  frame = TrainPic2[this.dir];
56  }
57  x += Dx[this.dir];
58  y += Dy[this.dir];
59  if (city.acycle % 4 == 0) {
60  // should be at the center of a cell, if not, correct it
61  x = (x/16) * 16 + TRA_GROOVE_X;
62  y = (y/16) * 16 + TRA_GROOVE_Y;
63  int d1 = city.PRNG.nextInt(4);
64  for (int z = d1; z < d1 + 4; z++) {
65  int d2 = z % 4;
66  if (this.dir != DIR_NONE) { //impossible?
67  if (d2 == (this.dir + 2) % 4)
68  continue;
69  }
70 
71  int c = getChar(this.x + Cx[d2], this.y + Cy[d2]);
72  if (((c >= RAILBASE) && (c <= LASTRAIL)) || //track?
73  (c == RAILVPOWERH) ||
74  (c == RAILHPOWERV))
75  {
76  if ((this.dir != d2) && (this.dir != DIR_NONE)) {
77  if (this.dir + d2 == 3)
78  this.frame = FRAME_NW_SE;
79  else
80  this.frame = FRAME_SW_NE;
81  }
82  else {
83  this.frame = TrainPic2[d2];
84  }
85 
86  if ((c == RAILBASE) || (c == (RAILBASE+1))) {
87  //underwater
88  this.frame = FRAME_UNDERWATER;
89  }
90  this.dir = d2;
91  return;
92  }
93  }
94  if (this.dir == DIR_NONE) {
95  // train has nowhere to go, so retire
96  this.frame = 0;
97  return;
98  }
99  this.dir = DIR_NONE;
100  }
101  }
102 }