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

210 lines
5.8 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;
2007-04-18 16:08:10 +00:00
import ei.game.player.Player;
import ei.game.scene.GameEntity;
2007-04-16 13:25:25 +00:00
import ei.game.scene.Map;
2007-04-18 16:08:10 +00:00
import ei.game.scene.weapons.CannonBall;
2007-04-04 14:44:25 +00:00
import ei.game.scene.weapons.Weapon;
2007-04-18 10:04:08 +00:00
import ei.game.scene.weapons.WeaponHandler;
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;
2007-04-17 14:45:29 +00:00
private Vector2i oldVect;
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-18 16:08:10 +00:00
public Unit(int l, Vector2i pos, Player p) {
super(l, p);
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) {
if(b) {
2007-04-18 16:06:44 +00:00
unitNode.add(getSelection().getSelectNode());
2007-04-16 11:34:57 +00:00
}
else{
2007-04-18 16:06:44 +00:00
unitNode.remove(getSelection().getSelectNode());
}
}
public void setMouseOver(boolean b) {
if(b) {
unitNode.add(getSelection().getMouseOverNode());
}
else{
unitNode.remove(getSelection().getMouseOverNode());
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
}
2007-04-18 16:08:10 +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();
2007-04-17 14:45:29 +00:00
oldVect = new Vector2i((int)unitNode.getLocation().getX(), (int)unitNode.getLocation().getY());
2007-04-16 21:47:07 +00:00
moveTo = Map.getPixelByPos(temp.getX(), temp.getY());
setPos(temp.getX(), temp.getY());
2007-04-04 16:28:57 +00:00
}
}
2007-04-18 10:04:08 +00:00
/**
* Lets a unit attack another unit or object in the world;
*/
public void attack(Vector2i target) {
2007-04-18 16:08:10 +00:00
Weapon wep = getWeapon(new Vector2f(unitNode.getLocation().getX(), unitNode.getLocation().getY()));
2007-04-18 10:04:08 +00:00
wep.launch(target);
WeaponHandler.getInstance().addWeapon(wep);
}
2007-04-18 16:08:10 +00:00
public abstract Weapon getWeapon(Vector2f startPos);
/**
* Updating the unit
*/
public void update() {
2007-04-18 16:08:10 +00:00
if(getLife()<=0) {
unitNode.remove("Tank");
getPlayer().removeUnit(this);
}
2007-04-17 12:51:26 +00:00
2007-04-18 16:08:10 +00:00
else if(moveTo != null) {
2007-04-17 13:09:50 +00:00
Vector2i moveVect = new Vector2i((int)moveTo.getX(),(int)moveTo.getY());
2007-04-17 14:45:29 +00:00
int moveXminus = moveVect.getX()-oldVect.getX();
int moveYminus = moveVect.getY()-oldVect.getY();
2007-04-17 13:09:50 +00:00
2007-04-18 10:04:08 +00:00
float divideY = (moveVect.getY()+2)/(oldVect.getY()+2);
float divideX = (moveVect.getX()+2)/(oldVect.getX()+2);
2007-04-17 13:09:50 +00:00
2007-04-16 15:59:30 +00:00
//The rotation animation is done here.
2007-04-17 14:45:29 +00:00
if(moveVect.getX() < oldVect.getX() && divideY==1) {
2007-04-18 16:08:10 +00:00
getSprite().setRotation(new Vector3f(0, 0, 90));
2007-04-17 13:09:50 +00:00
}
2007-04-17 14:45:29 +00:00
if(moveVect.getX() > oldVect.getX() && divideY==1) {
2007-04-18 16:08:10 +00:00
getSprite().setRotation(new Vector3f(0, 0, -90));
2007-04-17 13:09:50 +00:00
}
2007-04-17 14:45:29 +00:00
if(moveVect.getY() < oldVect.getY() && divideX==1) {
2007-04-18 16:08:10 +00:00
getSprite().setRotation(new Vector3f(0, 0, 180));
2007-04-17 14:45:29 +00:00
}
if(moveVect.getY() > oldVect.getY() && divideX==1) {
2007-04-18 16:08:10 +00:00
getSprite().setRotation(new Vector3f(0, 0, 0));
2007-04-17 14:45:29 +00:00
}
2007-04-17 13:09:50 +00:00
//Diagonally.
2007-04-17 14:45:29 +00:00
if(moveVect.getX() > oldVect.getX() && moveVect.getY() > oldVect.getY()
2007-04-17 13:09:50 +00:00
&& moveXminus>=2 && moveYminus>=2) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, -45));
}
2007-04-17 14:45:29 +00:00
if(moveVect.getX() < oldVect.getX() && moveVect.getY() < oldVect.getY()
2007-04-17 13:09:50 +00:00
&& moveXminus<=-2 && moveYminus<=-2) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 135));
}
2007-04-17 14:45:29 +00:00
if(moveVect.getX() > oldVect.getX() && moveVect.getY() < oldVect.getY()
2007-04-17 13:09:50 +00:00
&& moveXminus>=2 && moveYminus<=-2) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, -135));
}
2007-04-17 14:45:29 +00:00
if(moveVect.getX() < oldVect.getX() && moveVect.getY() > oldVect.getY()
2007-04-17 13:09:50 +00:00
&& 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())){
2007-04-17 14:45:29 +00:00
oldVect = new Vector2i((int)moveTo.getX(), (int)moveTo.getY());
2007-04-16 21:47:07 +00:00
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;
}
}
}
}
}