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

@ -1,6 +1,7 @@
package ei.game.scene.units;
import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Sprite;
import ei.game.gamestate.InGameState;
@ -8,36 +9,93 @@ import ei.game.scene.GameEntity;
import ei.game.scene.weapons.Weapon;
public abstract class Unit extends GameEntity{
// The texture
private Sprite looks;
// The wepon of the unit
private Weapon weapon;
// Som temp pos for moving the unit
private Vector2i oldPos;
private Vector2f moveTo;
/**
* Creates a empty unit
*
* @param l The max life of the unit
*/
public Unit(int l) {
super(l);
looks = new Sprite("none");
InGameState.getHuman().addUnit(this);
setPos(0, 0);
}
/**
* Returns the sprite for the unit
*
* @return The sprite for the unit
*/
public Sprite getSprite(){
return looks;
}
/**
* Set the sprite for the unit
*
* @param image The sprite for the unit
*/
public void setSprite(Sprite 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) {
oldPos = new Vector2i(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) {
if(oldPos!=null) {
InGameState.getMap().removePos(oldPos.getX(), oldPos.getY());
}
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);
}
/**
* 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;
}
}
}
}