Added a player handler so the code look a bit better

This commit is contained in:
Ziver Koc 2007-04-05 12:45:22 +00:00
parent 34decd72e4
commit c2a579788a
6 changed files with 167 additions and 15 deletions

View file

@ -6,11 +6,11 @@ import ei.engine.scene.Node;
import ei.game.scene.GameEntity;
import ei.game.scene.units.Unit;
public class Human {
public class HumanPlayer extends Player{
private ArrayList<GameEntity> units;
private Node unitsNode;
public Human(){
public HumanPlayer(){
units = new ArrayList<GameEntity>();
unitsNode = new Node("UnitsNode");
}
@ -28,4 +28,10 @@ public class Human {
public Node getNode(){
return unitsNode;
}
public void update() {
for(int i=0; i<units.size() ;i++){
units.get(i).update();
}
}
}

View file

@ -0,0 +1,15 @@
package ei.game.player;
import ei.engine.scene.Node;
import ei.game.scene.units.Unit;
public abstract class Player {
public abstract void addUnit(Unit u);
public abstract void removeUnit(Unit u);
public abstract Node getNode();
public abstract void update();
}

View file

@ -0,0 +1,63 @@
package ei.game.player;
import java.util.ArrayList;
public class PlayerHandler {
// The instance of this class
private static PlayerHandler instance;
// The player list
private ArrayList<Player> players;
/**
* Creates a PlayerHandler
*
*/
public PlayerHandler(){
players = new ArrayList<Player>();
}
/**
* Add a player to the handler
*
* @param p The player to add to the handler
* @return true if added else false
*/
public boolean addPlayer(Player p){
if(!players.contains(p)){
players.add(p);
return true;
}
return false;
}
/**
* Removes a player from the handler
* @param p The player to remove
* @return true if succesful else false
*/
public boolean removePlayer(Player p){
if(players.contains(p)){
players.remove(p);
return true;
}
return false;
}
public void update(){
for(int i=0; i<players.size() ;i++){
players.get(i).update();
}
}
/**
* Returns the instance of this class
*
* @return The instance
*/
public static PlayerHandler getInstance(){
if(instance == null){
instance = new PlayerHandler();
}
return instance;
}
}