Fixed with nodes and selection
This commit is contained in:
parent
c1e3fcdf9e
commit
1cb10a3166
5 changed files with 75 additions and 29 deletions
|
|
@ -55,13 +55,19 @@ public class InGameMouseInput extends MouseInput{
|
|||
Vector2i pos = map.getPosByPixel(
|
||||
LWJGLGameWindow.getCamera().getLocation().getX()+x,
|
||||
LWJGLGameWindow.getCamera().getLocation().getY()+y);
|
||||
//selecting unit.
|
||||
if(!map.isPosEmpty(pos.getX(), pos.getY())){
|
||||
//map.printAllUnits();
|
||||
for(int i=0; i<selected.size(); i++) {
|
||||
selected.get(i).setSelected(false);
|
||||
}
|
||||
selected.clear();
|
||||
MultiPrintStream.out.println("Selecting: "+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());
|
||||
for(int i=0; i<selected.size() ;i++){
|
||||
selected.get(i).move(pos.getX(),pos.getY());
|
||||
|
|
|
|||
|
|
@ -17,12 +17,12 @@ public class HumanPlayer extends Player{
|
|||
|
||||
public void addUnit(Unit u){
|
||||
units.add(u);
|
||||
unitsNode.add(u.getSprite());
|
||||
unitsNode.add(u.getNode());
|
||||
}
|
||||
|
||||
public void removeUnit(Unit u){
|
||||
units.remove(u);
|
||||
unitsNode.remove(u.getSprite());
|
||||
unitsNode.remove(u.getNode());
|
||||
}
|
||||
|
||||
public Node getNode(){
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
package ei.game.scene;
|
||||
|
||||
import ei.engine.scene.Entity;
|
||||
|
||||
public abstract class GameEntity{
|
||||
private int life;
|
||||
private boolean selected;
|
||||
|
||||
public GameEntity(int l){
|
||||
life = l;
|
||||
this.selected = false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -15,6 +19,21 @@ public abstract class GameEntity{
|
|||
public int getLife(){
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,18 +1,34 @@
|
|||
package ei.game.scene.units;
|
||||
|
||||
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.texture.Texture;
|
||||
|
||||
public class Tank extends Unit{
|
||||
private Box box;
|
||||
public Tank() {
|
||||
this(0, 0);
|
||||
}
|
||||
|
||||
public Tank(int x, int y){
|
||||
super(10);
|
||||
setSprite(new Sprite("Tank", "data/units/tank.png"));
|
||||
getSprite().setSize(new Vector2f(50,37));
|
||||
Sprite sp = new Sprite("Tank", "data/units/tank.png");
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,14 +3,20 @@ package ei.game.scene.units;
|
|||
|
||||
import ei.engine.math.Vector2f;
|
||||
import ei.engine.math.Vector2i;
|
||||
import ei.engine.scene.Node;
|
||||
import ei.engine.scene.Sprite;
|
||||
import ei.game.gamestate.InGameState;
|
||||
import ei.game.scene.GameEntity;
|
||||
import ei.game.scene.weapons.Weapon;
|
||||
|
||||
/**
|
||||
* The Unit class, handles the units in the game.
|
||||
* @author Jesper Lundin
|
||||
*
|
||||
*/
|
||||
public abstract class Unit extends GameEntity{
|
||||
// The texture
|
||||
private Sprite looks;
|
||||
private Node unitNode;
|
||||
// The wepon of the unit
|
||||
private Weapon weapon;
|
||||
// Som temp pos for moving the unit
|
||||
|
|
@ -24,28 +30,27 @@ public abstract class Unit extends GameEntity{
|
|||
*/
|
||||
public Unit(int l) {
|
||||
super(l);
|
||||
looks = new Sprite("none");
|
||||
unitNode = new Node("unit");
|
||||
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
|
||||
*
|
||||
* @return The sprite for the unit
|
||||
*/
|
||||
public Sprite getSprite(){
|
||||
return looks;
|
||||
public Node getNode(){
|
||||
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
|
||||
*
|
||||
|
|
@ -78,21 +83,21 @@ public abstract class Unit extends GameEntity{
|
|||
*/
|
||||
public void update() {
|
||||
if(moveTo!=null) {
|
||||
if(moveTo.getX() > looks.getLocation().getX()) {
|
||||
looks.getLocation().add(1.5f, 0f, 0f);
|
||||
if(moveTo.getX() > unitNode.getLocation().getX()) {
|
||||
unitNode.getLocation().add(1.5f, 0f, 0f);
|
||||
}
|
||||
if(moveTo.getX() < looks.getLocation().getX()) {
|
||||
looks.getLocation().add(-1.5f, 0f, 0f);
|
||||
if(moveTo.getX() < unitNode.getLocation().getX()) {
|
||||
unitNode.getLocation().add(-1.5f, 0f, 0f);
|
||||
}
|
||||
if(moveTo.getY() > looks.getLocation().getY()) {
|
||||
looks.getLocation().add(0f, 1.5f, 0f);
|
||||
if(moveTo.getY() > unitNode.getLocation().getY()) {
|
||||
unitNode.getLocation().add(0f, 1.5f, 0f);
|
||||
}
|
||||
if(moveTo.getY() < looks.getLocation().getY()) {
|
||||
looks.getLocation().add(0f, -1.5f, 0f);
|
||||
if(moveTo.getY() < unitNode.getLocation().getY()) {
|
||||
unitNode.getLocation().add(0f, -1.5f, 0f);
|
||||
}
|
||||
|
||||
if(moveTo.getX() == looks.getLocation().getX()
|
||||
&& moveTo.getY() == looks.getLocation().getY()) {
|
||||
if(moveTo.getX() == unitNode.getLocation().getX()
|
||||
&& moveTo.getY() == unitNode.getLocation().getY()) {
|
||||
moveTo = null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue