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

@ -3,7 +3,8 @@ package ei.game.gamestate;
import ei.engine.scene.Node; import ei.engine.scene.Node;
import ei.engine.state.GameState; import ei.engine.state.GameState;
import ei.game.input.InGameMouseInput; import ei.game.input.InGameMouseInput;
import ei.game.player.Human; import ei.game.player.HumanPlayer;
import ei.game.player.PlayerHandler;
import ei.game.scene.Map; import ei.game.scene.Map;
import ei.game.scene.units.Tank; import ei.game.scene.units.Tank;
@ -11,7 +12,6 @@ import ei.game.scene.units.Tank;
public class InGameState extends GameState{ public class InGameState extends GameState{
private Node rootNode; private Node rootNode;
private static Map map; private static Map map;
private static Human player;
public InGameState(String name){ public InGameState(String name){
super(name); super(name);
@ -22,24 +22,32 @@ public class InGameState extends GameState{
InGameMouseInput mouse = new InGameMouseInput(map); InGameMouseInput mouse = new InGameMouseInput(map);
super.getInput().addInput(mouse); super.getInput().addInput(mouse);
HumanPlayer player = new HumanPlayer();
player = new Human(); player.addUnit(new Tank());
rootNode.add(player.getNode()); rootNode.add(player.getNode());
Tank tank = new Tank(); PlayerHandler.getInstance().addPlayer(player);
player.addUnit(tank);
//tank.move(10, 3);
}
public static Human getHuman(){
return player;
} }
/**
* Renders the gamestate
*/
public void render() { public void render() {
rootNode.render(); rootNode.render();
} }
/**
* Updates the gamestate
*/
public void update() { public void update() {
PlayerHandler.getInstance().update();
rootNode.update(); rootNode.update();
} }
/**
*Returns the map of the game
*
* @return The map of the game
*/
public static Map getMap() { public static Map getMap() {
return map; return map;
} }

View file

@ -6,11 +6,11 @@ import ei.engine.scene.Node;
import ei.game.scene.GameEntity; import ei.game.scene.GameEntity;
import ei.game.scene.units.Unit; import ei.game.scene.units.Unit;
public class Human { public class HumanPlayer extends Player{
private ArrayList<GameEntity> units; private ArrayList<GameEntity> units;
private Node unitsNode; private Node unitsNode;
public Human(){ public HumanPlayer(){
units = new ArrayList<GameEntity>(); units = new ArrayList<GameEntity>();
unitsNode = new Node("UnitsNode"); unitsNode = new Node("UnitsNode");
} }
@ -28,4 +28,10 @@ public class Human {
public Node getNode(){ public Node getNode(){
return unitsNode; 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;
}
}

View file

@ -26,4 +26,6 @@ public abstract class GameEntity{
public void move(int x, int y) { public void move(int x, int y) {
} }
public abstract void update();
} }

View file

@ -1,6 +1,7 @@
package ei.game.scene.units; package ei.game.scene.units;
import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i; import ei.engine.math.Vector2i;
import ei.engine.scene.Sprite; import ei.engine.scene.Sprite;
import ei.game.gamestate.InGameState; import ei.game.gamestate.InGameState;
@ -8,36 +9,93 @@ import ei.game.scene.GameEntity;
import ei.game.scene.weapons.Weapon; import ei.game.scene.weapons.Weapon;
public abstract class Unit extends GameEntity{ public abstract class Unit extends GameEntity{
// The texture
private Sprite looks; private Sprite looks;
// The wepon of the unit
private Weapon weapon; private Weapon weapon;
// Som temp pos for moving the unit
private Vector2i oldPos; private Vector2i oldPos;
private Vector2f moveTo;
/**
* Creates a empty unit
*
* @param l The max life of the unit
*/
public Unit(int l) { public Unit(int l) {
super(l); super(l);
looks = new Sprite("none"); looks = new Sprite("none");
InGameState.getHuman().addUnit(this);
setPos(0, 0); setPos(0, 0);
} }
/**
* Returns the sprite for the unit
*
* @return The sprite for the unit
*/
public Sprite getSprite(){ public Sprite getSprite(){
return looks; return looks;
} }
/**
* Set the sprite for the unit
*
* @param image The sprite for the unit
*/
public void setSprite(Sprite image) { public void setSprite(Sprite image) {
looks = image; looks = image;
} }
/**
* Sets the pos of the unit whitout removing it from the old
*
* @param x The x pos to move to
* @param y The y pos to move to
*/
public void setPos(int x, int y) { public void setPos(int x, int y) {
oldPos = new Vector2i(x, y); oldPos = new Vector2i(x, y);
InGameState.getMap().setPos(this, x, y); InGameState.getMap().setPos(this, x, y);
} }
/**
* Moving a unit to the given pos
*
* @param x The x pos to move to
* @param y The y pos to move to
*/
public void move(int x, int y) { public void move(int x, int y) {
if(oldPos!=null) { if(oldPos!=null) {
InGameState.getMap().removePos(oldPos.getX(), oldPos.getY()); InGameState.getMap().removePos(oldPos.getX(), oldPos.getY());
} }
setPos(x, y); setPos(x, y);
looks.move(InGameState.getMap().getPixelByPos((int)x, (int)y)); moveTo = InGameState.getMap().getPixelByPos((int)x, (int)y);
oldPos = new Vector2i(x, y); oldPos = new Vector2i(x, y);
} }
/**
* Updating the unit
*/
public void update() {
if(moveTo!=null) {
if(moveTo.getX() > looks.getLocation().getX()) {
looks.getLocation().add(1.5f, 0f, 0f);
}
if(moveTo.getX() < looks.getLocation().getX()) {
looks.getLocation().add(-1.5f, 0f, 0f);
}
if(moveTo.getY() > looks.getLocation().getY()) {
looks.getLocation().add(0f, 1.5f, 0f);
}
if(moveTo.getY() < looks.getLocation().getY()) {
looks.getLocation().add(0f, -1.5f, 0f);
}
if(moveTo.getX() == looks.getLocation().getX()
&& moveTo.getY() == looks.getLocation().getY()) {
moveTo = null;
}
}
}
} }