Added so that the unit now follows the enamy unit and i added a reload timmer

This commit is contained in:
Ziver Koc 2007-04-18 21:19:46 +00:00
parent 6d06a75144
commit b97df125dd
8 changed files with 83 additions and 24 deletions

View file

@ -54,7 +54,7 @@ public class AStar{
else MultiPrintStream.out.print(0);
}
}
MultiPrintStream.out.println();
/*
for(int x = 0, nodeId = 0; width > x; x++) {
for(int y = 0; hight > y; y++, nodeId++) {

View file

@ -90,10 +90,12 @@ public class InGameMouseInput extends MouseInput{
}
// Make unit mouseover select
if(!map.isPosEmpty(pos.getX(), pos.getY())){
if(oldMouseOver != null)oldMouseOver.setMouseOver(false);
oldMouseOver = map.getPos(pos.getX(), pos.getY());
oldMouseOver.setMouseOver(true);
if(!map.isPosEmpty(pos.getX(), pos.getY())){
if(oldMouseOver != map.getPos(pos.getX(), pos.getY())){
if(oldMouseOver != null)oldMouseOver.setMouseOver(false);
oldMouseOver = map.getPos(pos.getX(), pos.getY());
oldMouseOver.setMouseOver(true);
}
}
else{
if(oldMouseOver != null)oldMouseOver.setMouseOver(false);

View file

@ -6,7 +6,6 @@ import ei.engine.scene.Sprite;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.weapons.BomberWeapon;
import ei.game.scene.weapons.CannonBall;
import ei.game.scene.weapons.Weapon;
public class Bomber extends Unit{

View file

@ -13,7 +13,6 @@ import ei.game.gamestate.InGameState;
import ei.game.player.Player;
import ei.game.scene.GameEntity;
import ei.game.scene.Map;
import ei.game.scene.weapons.CannonBall;
import ei.game.scene.weapons.Weapon;
import ei.game.scene.weapons.WeaponHandler;
@ -26,12 +25,15 @@ import ei.game.scene.weapons.WeaponHandler;
public abstract class Unit extends GameEntity{
// The texture
private Node unitNode;
// The wepon of the unit
private Weapon weapon;
// Som temp pos for moving the unit
private Vector2i oldPos;
private Vector2i oldVect;
// Order variables
private Vector2f moveTo;
private GameEntity attack;
// The wepon reload timer
private int weponTimer;
// The path to travel
private LinkedList<AStarNode> path;
/**
@ -72,6 +74,7 @@ public abstract class Unit extends GameEntity{
public Node getNode(){
return unitNode;
}
/**
* Changes the pos of the unit in the map
*
@ -94,7 +97,21 @@ public abstract class Unit extends GameEntity{
* @param x The x pos to move to
* @param y The y pos to move to
*/
public void move(int x, int y) {
public void move(int x, int y){
move(x,y,true);
}
/**
* Moving a unit to the given pos
*
* @param x The x pos to move to
* @param y The y pos to move to
* @param b True to clear old orders else false
*/
private void move(int x, int y, boolean b) {
if(b){
attack = null;
}
path = (LinkedList<AStarNode>) new AStar().startSearch(oldPos,new Vector2i(x,y));
if(path != null && !path.isEmpty() && moveTo == null){
@ -104,27 +121,28 @@ public abstract class Unit extends GameEntity{
setPos(temp.getX(), temp.getY());
}
}
/**
* Lets a unit attack another unit or object in the world;
*/
public void attack(Vector2i target) {
Weapon wep = getWeapon(new Vector2f(unitNode.getLocation().getX(), unitNode.getLocation().getY()));
wep.launch(target);
WeaponHandler.getInstance().addWeapon(wep);
attack = InGameState.getMap().getPos(target.getX(),target.getY());
}
public abstract Weapon getWeapon(Vector2f startPos);
public abstract float getVelocity();
/**
* Updating the unit
*/
public void update() {
if(getLife()<=0) {
unitNode.remove("Tank");
getPlayer().removeUnit(this);
InGameState.getMap().removePos(oldPos.getX(), oldPos.getY());
}
else if(moveTo != null) {
Vector2i moveVect = new Vector2i((int)moveTo.getX(),(int)moveTo.getY());
@ -189,8 +207,12 @@ public abstract class Unit extends GameEntity{
if(moveTo.getY() < unitNode.getLocation().getY()) {
unitNode.getLocation().add(0f, -getVelocity(), 0f);
}
if(Math.abs(moveTo.getX() - unitNode.getLocation().getX()) < getVelocity()+1
if(attack != null && getWeapon(new Vector2f(unitNode.getLocation().getX(), unitNode.getLocation().getY()))
.onRange(new Vector2f(attack.getNode().getLocation().getX(), attack.getNode().getLocation().getY()))){
moveTo = null;
}
else if(Math.abs(moveTo.getX() - unitNode.getLocation().getX()) < getVelocity()+1
&& Math.abs(moveTo.getY() - unitNode.getLocation().getY())< getVelocity()+1 ){
if(path != null && !path.isEmpty()){
AStarNode temp = path.poll();
@ -208,5 +230,22 @@ public abstract class Unit extends GameEntity{
}
}
}
else if(attack != null){
Weapon wepon = getWeapon(new Vector2f(unitNode.getLocation().getX(), unitNode.getLocation().getY()));
Vector2i enamyPos = Map.getPosByPixel(attack.getNode().getLocation().getX(), attack.getNode().getLocation().getY());
if(wepon.onRange(new Vector2f(attack.getNode().getLocation().getX(), attack.getNode().getLocation().getY()))){
if(weponTimer >= wepon.getReload()){
wepon.launch(enamyPos);
WeaponHandler.getInstance().addWeapon(wepon);
weponTimer = 0;
}
else{
weponTimer++;
}
}
else{
move(enamyPos.getX(),enamyPos.getY(), false);
}
}
}
}

View file

@ -16,6 +16,7 @@ public class BomberWeapon extends Weapon{
setVelocity(4);
setRange(100);
setDamage(10);
setReload(150);
hit = false;
position = Map.getPosByPixel(startPos.getX(), startPos.getY());
}

View file

@ -10,6 +10,7 @@ public class CannonBall extends Weapon{
setVelocity(4);
setRange(500);
setDamage(5);
setReload(100);
}
public Particles getWeapon() {
Particles part = new Particles("cannon");

View file

@ -4,7 +4,6 @@ import ei.engine.effects.Particles;
import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Entity;
import ei.engine.scene.Node;
import ei.game.gamestate.InGameState;
import ei.game.scene.Map;
/**
@ -22,6 +21,7 @@ public abstract class Weapon {
private boolean hit;
private Particles part;
private Vector2f startPos;
private int reload;
/**
@ -38,26 +38,46 @@ public abstract class Weapon {
part.setLocation(startPos);
part.reset();
}
public void setRange(int range) {
this.range = range;
}
public void setVelocity(float velocity) {
this.velocity = velocity;
}
public void setDamage(int damage) {
this.damage = damage;
}
public int getDamage() {
return this.damage;
}
public void setReload(int r){
reload = r;
}
public int getReload(){
return reload;
}
/**
* Launches the weapon type.
* @param target
*/
public void launch(Vector2i target) {
this.target = target;
this.target = target;
}
/**
* Returns true if the wepon is on range else false
* @param vect The position to attack
* @return True if the wepon is on range else false
*/
public boolean onRange(Vector2f vect){
return Math.sqrt(Math.pow((vect.getX()-startPos.getX()), 2) + Math.pow((vect.getY()-startPos.getY()), 2))<=range;
}
/**
@ -68,10 +88,9 @@ public abstract class Weapon {
public void update() {
Vector2f vect = Map.getPixelByPos(target.getX(), target.getY());
if(Math.sqrt(Math.pow((vect.getX()-startPos.getX()), 2) + Math.pow((vect.getY()-startPos.getY()), 2))<=range) {
if(onRange(vect)) {
float yVelocity = (Math.abs((vect.getY()-startPos.getY()) / velocity))/10;
float xVelocity = (Math.abs((vect.getX()-startPos.getX()) / velocity))/10;
System.out.println("xVel: "+xVelocity);
//System.out.println("xVel: "+Math.abs(part.getLocation().getX()-vect.getX()));
if(xVelocity < minVelocity && xVelocity > yVelocity) {

View file

@ -4,8 +4,6 @@ import java.util.ArrayList;
import ei.engine.scene.Entity;
import ei.engine.scene.Node;
import ei.game.player.Player;
import ei.game.player.PlayerHandler;
/**
* The WeaponHandler class handles weapons.