EOS 2  1.1.0
Einfache Objektbasierte Sprache
Sprite.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.game;
2 
3 import java.util.Objects;
4 
5 import de.lathanda.eos.base.MutableColor;
6 import de.lathanda.eos.base.Picture;
7 import de.lathanda.eos.game.geom.Shape;
8 
46 public abstract class Sprite implements Comparable<Sprite> {
47 
51  private static int nextID = 1;
55  private final Integer id = nextID++;
56 
60  protected Shape shape;
70  /*package*/ final int plane;
74  protected long round = 0;
78  private long start = -1;
82  private long sleepUntil = -1;
83 
87  public Sprite() {
88  plane = 0;
89  }
90  public Sprite(int plane) {
91  this.plane = plane;
92  }
93 
99  public final Integer getID() {
100  return id;
101  }
102 
106  @Override
107  public final int hashCode() {
108  /*
109  * this value should be more predictable
110  */
111  return id;
112  }
113 
114  @Override
115  public boolean equals(Object obj) {
116  if (obj == null) {
117  return false;
118  }
119  if (getClass() != obj.getClass()) {
120  return false;
121  }
122  final Sprite other = (Sprite) obj;
123  if (!Objects.equals(this.id, other.id)) {
124  return false;
125  }
126  return true;
127  }
128 
133  @Override
134  public final int compareTo(Sprite w) {
135  if (w == null) {
136  return Integer.MIN_VALUE;
137  }
138  int v = w.plane - plane;
139  return (v == 0) ? w.id - id : v;
140  }
141 
147  public void init(Game game) {
148  }
149 
159  public void cleanup(Game game) {
160  }
161 
174  public void render(Picture p) {
175  if (shape != null) {
178  p.drawShape(shape);
179  }
180  };
181 
187  public final void update(long round, Game game) {
188  if (start == -1) {
189  start = round - 1;
190  }
191  this.round = round - start;
192  if (this.round > sleepUntil) {
193  update(game);
194  }
195  }
196 
211  public void update(Game game) {
212 
213  };
214 
223  public final void sleep(int rounds) {
224  sleepUntil = round + rounds;
225  }
226 
230  public final void sleep() {
231  sleepUntil = Long.MAX_VALUE;
232  }
233 
238  public final void wakeup() {
239  sleepUntil = -1;
240  }
283  public boolean processCollision(Sprite b, Game game) {
284  return false;
285  }
286 
287  public int getPlane() {
288  return plane;
289  }
290  public Shape getShape() {
291  return shape;
292  }
293 
294 }
static final MutableColor GREEN
static final MutableColor GRAY
void drawShape(Shape shape)
Definition: Picture.java:451
void setFillColor(MutableColor c)
Definition: Picture.java:443
void setLineColor(MutableColor c)
Definition: Picture.java:428
final void sleep(int rounds)
Definition: Sprite.java:223
boolean processCollision(Sprite b, Game game)
Definition: Sprite.java:283
final int compareTo(Sprite w)
Definition: Sprite.java:134
void init(Game game)
Definition: Sprite.java:147
final Integer getID()
Definition: Sprite.java:99
Shape shape
Bounding Box des Sprites.
Definition: Sprite.java:60
boolean equals(Object obj)
Definition: Sprite.java:115
void update(Game game)
Definition: Sprite.java:211
final void update(long round, Game game)
Definition: Sprite.java:187
void cleanup(Game game)
Definition: Sprite.java:159
void render(Picture p)
Definition: Sprite.java:174
long round
Anzahl der vergangenen Runden seit Objekterzeugung.
Definition: Sprite.java:74
Impressum