evil-inside/src/ei/game/scene/units/Unit.java

185 lines
5.2 KiB
Java
Raw Normal View History

package ei.game.scene.units;
2007-04-04 18:16:37 +00:00
2007-04-16 15:52:24 +00:00
import java.util.LinkedList;
import ei.engine.math.Vector2f;
2007-04-04 14:44:25 +00:00
import ei.engine.math.Vector2i;
2007-04-16 13:25:25 +00:00
import ei.engine.math.Vector3f;
2007-04-16 11:34:57 +00:00
import ei.engine.scene.Node;
2007-04-16 15:52:24 +00:00
import ei.game.algo.AStar;
import ei.game.algo.AStarNode;
2007-04-04 16:28:57 +00:00
import ei.game.gamestate.InGameState;
import ei.game.scene.GameEntity;
2007-04-16 13:25:25 +00:00
import ei.game.scene.Map;
2007-04-04 14:44:25 +00:00
import ei.game.scene.weapons.Weapon;
2007-04-16 11:34:57 +00:00
/**
* The Unit class, handles the units in the game.
* @author Jesper Lundin
2007-04-16 21:47:07 +00:00
* @author Ziver koc
2007-04-16 11:34:57 +00:00
*
*/
public abstract class Unit extends GameEntity{
// The texture
2007-04-16 11:34:57 +00:00
private Node unitNode;
// The wepon of the unit
2007-04-04 14:44:25 +00:00
private Weapon weapon;
// Som temp pos for moving the unit
2007-04-04 16:28:57 +00:00
private Vector2i oldPos;
private Vector2f moveTo;
2007-04-16 15:52:24 +00:00
private LinkedList<AStarNode> path;
/**
* Creates a empty unit
*
* @param l The max life of the unit
*/
2007-04-16 16:13:44 +00:00
public Unit(int l, Vector2i pos) {
super(l);
2007-04-16 11:34:57 +00:00
unitNode = new Node("unit");
2007-04-16 16:13:44 +00:00
unitNode.setLocation(Map.getPixelByPos(pos.getX(), pos.getY()));
setPos(pos.getX(), pos.getY());
}
2007-04-16 16:13:44 +00:00
2007-04-16 11:34:57 +00:00
public void setSelected(boolean b) {
super.setSelected(b);
if(b) {
2007-04-17 12:51:26 +00:00
unitNode.add(getSelection().getNode());
2007-04-16 11:34:57 +00:00
}
else{
2007-04-17 12:51:26 +00:00
unitNode.remove(getSelection().getNode());
2007-04-16 11:34:57 +00:00
}
}
2007-04-04 14:44:25 +00:00
/**
* Returns the sprite for the unit
*
* @return The sprite for the unit
*/
2007-04-16 11:34:57 +00:00
public Node getNode(){
return unitNode;
2007-04-04 14:44:25 +00:00
}
/**
2007-04-16 21:47:07 +00:00
* Changes the pos of the unit in the map
*
* @param x The x pos to move to
* @param y The y pos to move to
*/
2007-04-04 16:28:57 +00:00
public void setPos(int x, int y) {
2007-04-16 21:47:07 +00:00
if(oldPos!=null) {
InGameState.getMap().removePos(oldPos.getX(), oldPos.getY());
}
2007-04-04 16:28:57 +00:00
oldPos = new Vector2i(x, y);
2007-04-17 13:09:50 +00:00
InGameState.getMap().setPos(this, x, y);
System.out.println(oldPos);
2007-04-04 16:28:57 +00:00
}
/**
* Moving a unit to the given pos
*
* @param x The x pos to move to
* @param y The y pos to move to
*/
2007-04-04 16:28:57 +00:00
public void move(int x, int y) {
2007-04-16 21:47:07 +00:00
path = (LinkedList<AStarNode>) new AStar().startSearch(oldPos,new Vector2i(x,y));
2007-04-17 14:21:36 +00:00
if(path != null && !path.isEmpty() && moveTo == null){
2007-04-16 21:47:07 +00:00
AStarNode temp = path.poll();
moveTo = Map.getPixelByPos(temp.getX(), temp.getY());
setPos(temp.getX(), temp.getY());
2007-04-04 16:28:57 +00:00
}
}
/**
* Updating the unit
*/
public void update() {
2007-04-17 12:51:26 +00:00
getSelection().setValue(getLife());
2007-04-16 21:47:07 +00:00
if(moveTo != null) {
2007-04-17 13:09:50 +00:00
Vector2i moveVect = new Vector2i((int)moveTo.getX(),(int)moveTo.getY());
Vector2i currentVect = new Vector2i((int)unitNode.getLocation().getX(), (int)unitNode.getLocation().getY());
int moveXminus = moveVect.getX()-currentVect.getX();
int moveYminus = moveVect.getY()-currentVect.getY();
System.out.println(moveVect.getY());
System.out.println(currentVect);
float divideY = (moveVect.getY()+1)/(currentVect.getY()+1);
float divideX = (moveVect.getX()+1)/(currentVect.getX()+1);
2007-04-16 15:59:30 +00:00
//The rotation animation is done here.
2007-04-17 13:09:50 +00:00
if(moveVect.getX() < currentVect.getX() && divideY==1) {
2007-04-16 13:25:25 +00:00
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 90));
2007-04-17 13:09:50 +00:00
}
if(moveVect.getY() > currentVect.getY() && divideX==1) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 0));
}
if(moveVect.getX() > currentVect.getX() && divideY==1) {
2007-04-16 13:25:25 +00:00
unitNode.get("Tank").setRotation(new Vector3f(0, 0, -90));
2007-04-17 13:09:50 +00:00
}
if(moveVect.getY() < currentVect.getY() && divideX==1) {
2007-04-16 13:25:25 +00:00
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 180));
2007-04-17 13:09:50 +00:00
}
//Diagonally.
if(moveVect.getX() > currentVect.getX() && moveVect.getY() > currentVect.getY()
&& moveXminus>=2 && moveYminus>=2) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, -45));
}
if(moveVect.getX() < currentVect.getX() && moveVect.getY() < currentVect.getY()
&& moveXminus<=-2 && moveYminus<=-2) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 135));
}
if(moveVect.getX() > currentVect.getX() && moveVect.getY() < currentVect.getY()
&& moveXminus>=2 && moveYminus<=-2) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, -135));
}
if(moveVect.getX() < currentVect.getX() && moveVect.getY() > currentVect.getY()
&& moveXminus<=-2 && moveYminus>=2) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 45));
}
2007-04-16 15:59:30 +00:00
2007-04-16 13:25:25 +00:00
//System.out.println(unitNode.get("Tank").getRotation());
2007-04-17 13:09:50 +00:00
2007-04-16 15:59:30 +00:00
//The moving is done here.
2007-04-16 11:34:57 +00:00
if(moveTo.getX() > unitNode.getLocation().getX()) {
unitNode.getLocation().add(1.5f, 0f, 0f);
}
2007-04-16 11:34:57 +00:00
if(moveTo.getX() < unitNode.getLocation().getX()) {
unitNode.getLocation().add(-1.5f, 0f, 0f);
}
2007-04-16 11:34:57 +00:00
if(moveTo.getY() > unitNode.getLocation().getY()) {
unitNode.getLocation().add(0f, 1.5f, 0f);
}
2007-04-16 11:34:57 +00:00
if(moveTo.getY() < unitNode.getLocation().getY()) {
unitNode.getLocation().add(0f, -1.5f, 0f);
}
2007-04-17 13:09:50 +00:00
2007-04-16 21:47:07 +00:00
if(Math.abs(moveTo.getX() - unitNode.getLocation().getX()) < 2
&& Math.abs(moveTo.getY() - unitNode.getLocation().getY())< 2 ){
if(path != null && !path.isEmpty()){
AStarNode temp = path.poll();
if(InGameState.getMap().isPosEmpty(temp.getX(), temp.getY())){
moveTo = Map.getPixelByPos(temp.getX(), temp.getY());
setPos(temp.getX(), temp.getY());
}
else if(!path.isEmpty()){
move(path.getLast().getX(), path.getLast().getY());
}
2007-04-16 15:52:24 +00:00
}
else{
moveTo = null;
}
}
}
}
}