-
Notifications
You must be signed in to change notification settings - Fork 0
Chunk targets
Miguel Gonzalez edited this page Jan 21, 2014
·
12 revisions
Each chunk has some content, like entities (e.g. player, opponents, obstacles). Therefore a chunk needs to know, what he has to save. For that purpose, chunx provides an interface, called ChunkTarget. By implementing the interface, you're able to add your entities to a chunk system.
Here is an example of a simple spaceship class which implements the interface:
public class Spaceship implements ChunkTarget {
private float x, y;
public Spaceship(float x, float y) {
this.x = x;
this.y = y;
}
@Override
public float getX() {
return x;
}
@Override
public float getY() {
return y;
}
@Override
public void setX(float x) {
this.x = x;
}
@Override
public void setY(float y) {
this.y = y;
}
}You learn how to use the target in the next tutorial about content providers.
It is quite often the case that your game contains moveable patys like bullets or, spaceships. Moveable elements are handled much differently as regular targets. Therefore you need to implement another interface, called MoveableChunkTarget.
A basic implementation would look like this:
public class Spaceship implements MoveableChunkTarget {
private float x, y;
// This one is needed to detect movement
private MovementDetector movementDetector;
public Spaceship(float x, float y) {
this.x = x;
this.y = y;
// Don't initialize it here, this is the constructor! (dangerous)
//movementDetector = new SimpleMovementDetector(this);
}
@Override
public float getX() {
return x;
}
@Override
public float getY() {
return y;
}
@Override
public void setX(float x) {
this.x = x;
}
@Override
public void setY(float y) {
this.y = y;
}
@Override
public void update() {
// Update the tracker, so chunx can detect movement
movementDetector.update();
}
@Override
public MovementDetector getMovementDetector() {
if (movementDetector == null) {
movementDetector = new SimpleMovementDetector(this);
}
return movementDetector;
}
@Override
public void setMovementDetector(MovementDetector detector) {
this.movementTracker = detector;
}
}