EntityManager.java
/*
* Copyright © 2011 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example.battletank.game.entities;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.game.LayerManager;
public abstract class EntityManager {
protected Entity[] entities;
private int nextIndex;
protected EntityManager(int capacity) {
entities = new Entity[capacity];
nextIndex = 0;
}
protected Entity nextEntity() {
Entity e = entities[nextIndex];
nextIndex = (nextIndex + 1)%entities.length;
return e;
}
public void appendTo(LayerManager lm) {
for(int i = 0; i < entities.length; i++) {
lm.append(entities[i].getSprite());
}
}
public void refresh() {
for(int i = 0; i < entities.length; i++) {
entities[i].refresh();
}
}
public void update() {
for(int i = 0; i < entities.length; i++) {
entities[i].update();
}
}
public void writeTo(DataOutputStream dout) throws IOException {
dout.writeInt(entities.length);
for(int i = 0; i < entities.length; i++) {
entities[i].writeTo(dout);
}
}
public void readFrom(DataInputStream din) throws IOException {
int length = din.readInt();
for(int i = 0; i < length; i++) {
if(i < entities.length) entities[i].readFrom(din);
}
}
}