Fixed with nodes and selection

This commit is contained in:
Jesper Lundin 2007-04-16 11:34:57 +00:00
parent c1e3fcdf9e
commit 1cb10a3166
5 changed files with 75 additions and 29 deletions

View file

@ -55,13 +55,19 @@ public class InGameMouseInput extends MouseInput{
Vector2i pos = map.getPosByPixel( Vector2i pos = map.getPosByPixel(
LWJGLGameWindow.getCamera().getLocation().getX()+x, LWJGLGameWindow.getCamera().getLocation().getX()+x,
LWJGLGameWindow.getCamera().getLocation().getY()+y); LWJGLGameWindow.getCamera().getLocation().getY()+y);
//selecting unit.
if(!map.isPosEmpty(pos.getX(), pos.getY())){ if(!map.isPosEmpty(pos.getX(), pos.getY())){
//map.printAllUnits(); //map.printAllUnits();
for(int i=0; i<selected.size(); i++) {
selected.get(i).setSelected(false);
}
selected.clear(); selected.clear();
MultiPrintStream.out.println("Selecting: "+pos.getX()+", "+pos.getY()); MultiPrintStream.out.println("Selecting: "+pos.getX()+", "+pos.getY());
selected.add(map.getPos(pos.getX(), pos.getY())); selected.add(map.getPos(pos.getX(), pos.getY()));
map.getPos(pos.getX(), pos.getY()).setSelected(true);
} }
else{ else{ //unit action.
MultiPrintStream.out.println("Moving: "+pos.getX()+", "+pos.getY()); MultiPrintStream.out.println("Moving: "+pos.getX()+", "+pos.getY());
for(int i=0; i<selected.size() ;i++){ for(int i=0; i<selected.size() ;i++){
selected.get(i).move(pos.getX(),pos.getY()); selected.get(i).move(pos.getX(),pos.getY());

View file

@ -17,12 +17,12 @@ public class HumanPlayer extends Player{
public void addUnit(Unit u){ public void addUnit(Unit u){
units.add(u); units.add(u);
unitsNode.add(u.getSprite()); unitsNode.add(u.getNode());
} }
public void removeUnit(Unit u){ public void removeUnit(Unit u){
units.remove(u); units.remove(u);
unitsNode.remove(u.getSprite()); unitsNode.remove(u.getNode());
} }
public Node getNode(){ public Node getNode(){

View file

@ -1,10 +1,14 @@
package ei.game.scene; package ei.game.scene;
import ei.engine.scene.Entity;
public abstract class GameEntity{ public abstract class GameEntity{
private int life; private int life;
private boolean selected;
public GameEntity(int l){ public GameEntity(int l){
life = l; life = l;
this.selected = false;
} }
/** /**
@ -15,6 +19,21 @@ public abstract class GameEntity{
public int getLife(){ public int getLife(){
return life; return life;
} }
/**
* If the unit is selected.
* @return true if selected.
*/
public boolean isSelected() {
return selected;
}
/**
* Sets a unit to be selected or not.
* @param b true or false.
*/
public void setSelected(boolean b) {
this.selected = b;
}
protected abstract Entity getSelection();
/** /**
* Set the life * Set the life

View file

@ -1,18 +1,34 @@
package ei.game.scene.units; package ei.game.scene.units;
import ei.engine.math.Vector2f; import ei.engine.math.Vector2f;
import ei.engine.math.Vector4f;
import ei.engine.scene.Box;
import ei.engine.scene.Entity;
import ei.engine.scene.Sprite; import ei.engine.scene.Sprite;
import ei.engine.texture.Texture;
public class Tank extends Unit{ public class Tank extends Unit{
private Box box;
public Tank() { public Tank() {
this(0, 0); this(0, 0);
} }
public Tank(int x, int y){ public Tank(int x, int y){
super(10); super(10);
setSprite(new Sprite("Tank", "data/units/tank.png")); Sprite sp = new Sprite("Tank", "data/units/tank.png");
getSprite().setSize(new Vector2f(50,37)); sp.setSize(new Vector2f(50,37));
getNode().add(sp);
Texture tex = new Texture();
tex.setColor(new Vector4f(0.5f, 1.0f, 0.5f,1));
box = new Box("selectionBox",tex);
box.setSize(new Vector2f(40,40));
}
protected Entity getSelection() {
return box;
} }

View file

@ -3,14 +3,20 @@ package ei.game.scene.units;
import ei.engine.math.Vector2f; import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i; import ei.engine.math.Vector2i;
import ei.engine.scene.Node;
import ei.engine.scene.Sprite; import ei.engine.scene.Sprite;
import ei.game.gamestate.InGameState; import ei.game.gamestate.InGameState;
import ei.game.scene.GameEntity; import ei.game.scene.GameEntity;
import ei.game.scene.weapons.Weapon; import ei.game.scene.weapons.Weapon;
/**
* The Unit class, handles the units in the game.
* @author Jesper Lundin
*
*/
public abstract class Unit extends GameEntity{ public abstract class Unit extends GameEntity{
// The texture // The texture
private Sprite looks; private Node unitNode;
// The wepon of the unit // The wepon of the unit
private Weapon weapon; private Weapon weapon;
// Som temp pos for moving the unit // Som temp pos for moving the unit
@ -24,28 +30,27 @@ public abstract class Unit extends GameEntity{
*/ */
public Unit(int l) { public Unit(int l) {
super(l); super(l);
looks = new Sprite("none"); unitNode = new Node("unit");
setPos(0, 0); setPos(0, 0);
} }
public void setSelected(boolean b) {
super.setSelected(b);
if(b) {
unitNode.add(getSelection());
}
else{
unitNode.remove(getSelection());
}
}
/** /**
* Returns the sprite for the unit * Returns the sprite for the unit
* *
* @return The sprite for the unit * @return The sprite for the unit
*/ */
public Sprite getSprite(){ public Node getNode(){
return looks; return unitNode;
} }
/**
* 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 * Sets the pos of the unit whitout removing it from the old
* *
@ -78,21 +83,21 @@ public abstract class Unit extends GameEntity{
*/ */
public void update() { public void update() {
if(moveTo!=null) { if(moveTo!=null) {
if(moveTo.getX() > looks.getLocation().getX()) { if(moveTo.getX() > unitNode.getLocation().getX()) {
looks.getLocation().add(1.5f, 0f, 0f); unitNode.getLocation().add(1.5f, 0f, 0f);
} }
if(moveTo.getX() < looks.getLocation().getX()) { if(moveTo.getX() < unitNode.getLocation().getX()) {
looks.getLocation().add(-1.5f, 0f, 0f); unitNode.getLocation().add(-1.5f, 0f, 0f);
} }
if(moveTo.getY() > looks.getLocation().getY()) { if(moveTo.getY() > unitNode.getLocation().getY()) {
looks.getLocation().add(0f, 1.5f, 0f); unitNode.getLocation().add(0f, 1.5f, 0f);
} }
if(moveTo.getY() < looks.getLocation().getY()) { if(moveTo.getY() < unitNode.getLocation().getY()) {
looks.getLocation().add(0f, -1.5f, 0f); unitNode.getLocation().add(0f, -1.5f, 0f);
} }
if(moveTo.getX() == looks.getLocation().getX() if(moveTo.getX() == unitNode.getLocation().getX()
&& moveTo.getY() == looks.getLocation().getY()) { && moveTo.getY() == unitNode.getLocation().getY()) {
moveTo = null; moveTo = null;
} }
} }