Did some stuff
This commit is contained in:
parent
c6ba0ea532
commit
af1a23b5af
6 changed files with 142 additions and 64 deletions
Binary file not shown.
|
Before Width: | Height: | Size: 217 KiB |
|
|
@ -6,6 +6,7 @@ import ei.game.input.InGameMouseInput;
|
|||
import ei.game.player.HumanPlayer;
|
||||
import ei.game.player.PlayerHandler;
|
||||
import ei.game.scene.Map;
|
||||
import ei.game.scene.units.Bomber;
|
||||
import ei.game.scene.units.Tank;
|
||||
import ei.game.scene.weapons.WeaponHandler;
|
||||
|
||||
|
|
@ -24,13 +25,14 @@ public class InGameState extends GameState{
|
|||
super.getInput().addInput(mouse);
|
||||
|
||||
HumanPlayer player = new HumanPlayer();
|
||||
Tank t1 = new Tank();
|
||||
Tank t1 = new Tank(player);
|
||||
t1.setLife(10);
|
||||
player.addUnit(t1);
|
||||
Tank t2 = new Tank(1,0);
|
||||
Tank t2 = new Tank(1,0, player);
|
||||
t2.setLife(30);
|
||||
player.addUnit(t2);
|
||||
player.addUnit(new Tank(2,0));
|
||||
player.addUnit(new Tank(2,0, player));
|
||||
player.addUnit(new Bomber(3, 0, player));
|
||||
|
||||
rootNode.add(player.getNode());
|
||||
PlayerHandler.getInstance().addPlayer(player);
|
||||
|
|
|
|||
|
|
@ -2,13 +2,16 @@ package ei.game.scene;
|
|||
|
||||
import ei.engine.math.Vector2i;
|
||||
import ei.engine.scene.Entity;
|
||||
import ei.game.player.Player;
|
||||
|
||||
public abstract class GameEntity{
|
||||
private int max_life;
|
||||
private int life;
|
||||
private Player player;
|
||||
|
||||
public GameEntity(int l){
|
||||
public GameEntity(int l, Player p){
|
||||
setLife(l);
|
||||
player = p;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -32,7 +35,18 @@ public abstract class GameEntity{
|
|||
}
|
||||
if(getSelection() != null)getSelection().setValue(l);
|
||||
}
|
||||
|
||||
/**
|
||||
* The unit looses life.
|
||||
* @param l
|
||||
*/
|
||||
public void damaged(int l) {
|
||||
setLife(life-l);
|
||||
}
|
||||
/**
|
||||
* The unit is destroyed.
|
||||
*
|
||||
*/
|
||||
public abstract void destroyed();
|
||||
/**
|
||||
* Returns the max life
|
||||
*
|
||||
|
|
@ -51,6 +65,12 @@ public abstract class GameEntity{
|
|||
max_life = l;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the player.
|
||||
*/
|
||||
public Player getPlayer(){
|
||||
return player;
|
||||
}
|
||||
/**
|
||||
* Sets a unit to be selected or not.
|
||||
* @param b true or false.
|
||||
|
|
@ -74,5 +94,7 @@ public abstract class GameEntity{
|
|||
public abstract Entity getNode();
|
||||
|
||||
public abstract void attack(Vector2i target);
|
||||
|
||||
public abstract Entity getSprite();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,19 +3,23 @@ package ei.game.scene.units;
|
|||
import ei.engine.math.Vector2f;
|
||||
import ei.engine.math.Vector2i;
|
||||
import ei.engine.scene.Sprite;
|
||||
import ei.game.player.Player;
|
||||
import ei.game.scene.SelectBox;
|
||||
import ei.game.scene.weapons.CannonBall;
|
||||
import ei.game.scene.weapons.Weapon;
|
||||
|
||||
public class Tank extends Unit{
|
||||
private SelectBox selectionBox;
|
||||
public Tank() {
|
||||
this(0, 0);
|
||||
private Sprite sprite;
|
||||
public Tank(Player p) {
|
||||
this(0, 0, p);
|
||||
}
|
||||
|
||||
public Tank(int x, int y){
|
||||
super(100, new Vector2i(x,y));
|
||||
Sprite sp = new Sprite("Tank", "data/units/tank.png");
|
||||
sp.setSize(new Vector2f(50,37));
|
||||
getNode().add(sp);
|
||||
public Tank(int x, int y, Player p){
|
||||
super(100, new Vector2i(x,y), p);
|
||||
this.sprite = new Sprite("Tank", "data/units/tank/tank0000.png");
|
||||
sprite.setSize(new Vector2f(40,40));
|
||||
getNode().add(sprite);
|
||||
|
||||
selectionBox = new SelectBox(40,40,getMaxLife());
|
||||
setLife(50);
|
||||
|
|
@ -24,6 +28,32 @@ public class Tank extends Unit{
|
|||
protected SelectBox getSelection() {
|
||||
return selectionBox;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the weapon connected to this type
|
||||
* of unit.
|
||||
*/
|
||||
public Weapon getWeapon(Vector2f startPos) {
|
||||
return new CannonBall(startPos);
|
||||
}
|
||||
/**
|
||||
* This unit type is now destroyed.
|
||||
*/
|
||||
public void destroyed(){
|
||||
|
||||
}
|
||||
/**
|
||||
* Manages the sprite connected to this unit.
|
||||
* @param s
|
||||
*/
|
||||
public void setSprite(Sprite s) {
|
||||
this.sprite = s;
|
||||
}
|
||||
/**
|
||||
* returns the sprite connected to this type
|
||||
* of unit.
|
||||
* @return
|
||||
*/
|
||||
public Sprite getSprite() {
|
||||
return this.sprite;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,8 +10,10 @@ import ei.engine.scene.Node;
|
|||
import ei.game.algo.AStar;
|
||||
import ei.game.algo.AStarNode;
|
||||
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;
|
||||
|
||||
|
|
@ -37,8 +39,8 @@ public abstract class Unit extends GameEntity{
|
|||
*
|
||||
* @param l The max life of the unit
|
||||
*/
|
||||
public Unit(int l, Vector2i pos) {
|
||||
super(l);
|
||||
public Unit(int l, Vector2i pos, Player p) {
|
||||
super(l, p);
|
||||
unitNode = new Node("unit");
|
||||
unitNode.setLocation(Map.getPixelByPos(pos.getX(), pos.getY()));
|
||||
setPos(pos.getX(), pos.getY());
|
||||
|
|
@ -85,6 +87,7 @@ public abstract class Unit extends GameEntity{
|
|||
System.out.println(oldPos);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Moving a unit to the given pos
|
||||
*
|
||||
|
|
@ -105,17 +108,21 @@ public abstract class Unit extends GameEntity{
|
|||
* Lets a unit attack another unit or object in the world;
|
||||
*/
|
||||
public void attack(Vector2i target) {
|
||||
Weapon wep = new Weapon(10, new Vector2f(unitNode.getLocation().getX(), unitNode.getLocation().getY()), 4.0f);
|
||||
Weapon wep = getWeapon(new Vector2f(unitNode.getLocation().getX(), unitNode.getLocation().getY()));
|
||||
wep.launch(target);
|
||||
WeaponHandler.getInstance().addWeapon(wep);
|
||||
}
|
||||
|
||||
public abstract Weapon getWeapon(Vector2f startPos);
|
||||
/**
|
||||
* Updating the unit
|
||||
*/
|
||||
public void update() {
|
||||
if(getLife()<=0) {
|
||||
unitNode.remove("Tank");
|
||||
getPlayer().removeUnit(this);
|
||||
}
|
||||
|
||||
if(moveTo != null) {
|
||||
else if(moveTo != null) {
|
||||
Vector2i moveVect = new Vector2i((int)moveTo.getX(),(int)moveTo.getY());
|
||||
|
||||
int moveXminus = moveVect.getX()-oldVect.getX();
|
||||
|
|
@ -126,19 +133,19 @@ public abstract class Unit extends GameEntity{
|
|||
|
||||
//The rotation animation is done here.
|
||||
if(moveVect.getX() < oldVect.getX() && divideY==1) {
|
||||
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 90));
|
||||
getSprite().setRotation(new Vector3f(0, 0, 90));
|
||||
}
|
||||
|
||||
if(moveVect.getX() > oldVect.getX() && divideY==1) {
|
||||
unitNode.get("Tank").setRotation(new Vector3f(0, 0, -90));
|
||||
getSprite().setRotation(new Vector3f(0, 0, -90));
|
||||
}
|
||||
|
||||
if(moveVect.getY() < oldVect.getY() && divideX==1) {
|
||||
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 180));
|
||||
getSprite().setRotation(new Vector3f(0, 0, 180));
|
||||
}
|
||||
|
||||
if(moveVect.getY() > oldVect.getY() && divideX==1) {
|
||||
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 0));
|
||||
getSprite().setRotation(new Vector3f(0, 0, 0));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ 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;
|
||||
/**
|
||||
* The weapon class.
|
||||
|
|
@ -11,11 +13,13 @@ import ei.game.scene.Map;
|
|||
*
|
||||
*/
|
||||
|
||||
public class Weapon {
|
||||
public abstract class Weapon {
|
||||
private int range;
|
||||
private int damage;
|
||||
private Vector2i target;
|
||||
private float velocity;
|
||||
private float minVelocity;
|
||||
private boolean hit;
|
||||
private Particles part;
|
||||
private Vector2f startPos;
|
||||
|
||||
|
|
@ -26,20 +30,22 @@ public class Weapon {
|
|||
* @param startPos
|
||||
* @param velocity
|
||||
*/
|
||||
public Weapon(int range, Vector2f startPos, float velocity) {
|
||||
this.range = range;
|
||||
public Weapon(Vector2f startPos) {
|
||||
this.startPos = startPos;
|
||||
this.velocity = velocity;
|
||||
this.minVelocity = 5;
|
||||
part = new Particles("weapon");
|
||||
this.hit = false;
|
||||
part = getWeapon();
|
||||
part.setLocation(startPos);
|
||||
part.MaxSpeedX=500;
|
||||
part.MaxSpeedY=400;
|
||||
part.MaxSpeedZ=600;
|
||||
part.rainbow = false;
|
||||
part.size=5;
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* Launches the weapon type.
|
||||
|
|
@ -50,46 +56,55 @@ public class Weapon {
|
|||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* updates the weapons movement and positions, also the behaviour of
|
||||
* the weapon.
|
||||
*
|
||||
*/
|
||||
public void update() {
|
||||
|
||||
Vector2f vect = Map.getPixelByPos(target.getX(), target.getY());
|
||||
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) {
|
||||
yVelocity = (yVelocity/xVelocity)*minVelocity;
|
||||
xVelocity = minVelocity;
|
||||
}
|
||||
if(yVelocity < minVelocity && yVelocity < xVelocity) {
|
||||
xVelocity = (xVelocity/yVelocity)*minVelocity;
|
||||
yVelocity = minVelocity;
|
||||
}
|
||||
if(vect.getX() > part.getLocation().getX()) {
|
||||
part.getLocation().add(xVelocity, 0f, 0f);
|
||||
}
|
||||
if(vect.getX() < part.getLocation().getX()) {
|
||||
part.getLocation().add(-xVelocity, 0f, 0f);
|
||||
}
|
||||
if(vect.getY() > part.getLocation().getY()) {
|
||||
part.getLocation().add(0f, yVelocity, 0f);
|
||||
}
|
||||
if(vect.getY() < part.getLocation().getY()) {
|
||||
part.getLocation().add(0f, -yVelocity, 0f);
|
||||
}
|
||||
if(Math.abs(part.getLocation().getX()-vect.getX()) < 10 && Math.abs(part.getLocation().getY()-vect.getY()) < 10) {
|
||||
part.regenerate = false;
|
||||
if(part.isDead()) {
|
||||
WeaponHandler.getInstance().removeWeapon(this);
|
||||
if(Math.sqrt(Math.pow((vect.getX()-startPos.getX()), 2) + Math.pow((vect.getY()-startPos.getY()), 2))<=range) {
|
||||
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) {
|
||||
yVelocity = (yVelocity/xVelocity)*minVelocity;
|
||||
xVelocity = minVelocity;
|
||||
}
|
||||
if(yVelocity < minVelocity && yVelocity < xVelocity) {
|
||||
xVelocity = (xVelocity/yVelocity)*minVelocity;
|
||||
yVelocity = minVelocity;
|
||||
}
|
||||
if(vect.getX() > part.getLocation().getX()) {
|
||||
part.getLocation().add(xVelocity, 0f, 0f);
|
||||
}
|
||||
if(vect.getX() < part.getLocation().getX()) {
|
||||
part.getLocation().add(-xVelocity, 0f, 0f);
|
||||
}
|
||||
if(vect.getY() > part.getLocation().getY()) {
|
||||
part.getLocation().add(0f, yVelocity, 0f);
|
||||
}
|
||||
if(vect.getY() < part.getLocation().getY()) {
|
||||
part.getLocation().add(0f, -yVelocity, 0f);
|
||||
}
|
||||
if(Math.abs(part.getLocation().getX()-vect.getX()) < 10 && Math.abs(part.getLocation().getY()-vect.getY()) < 10) {
|
||||
part.regenerate = false;
|
||||
if(!hit) {
|
||||
InGameState.getMap().getPos(target.getX(), target.getY()).damaged(damage);
|
||||
hit = true;
|
||||
}
|
||||
if(part.isDead()) {
|
||||
WeaponHandler.getInstance().removeWeapon(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else{
|
||||
WeaponHandler.getInstance().removeWeapon(this);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns the node with the weapon.
|
||||
|
|
@ -98,5 +113,7 @@ public class Weapon {
|
|||
public Entity getNode() {
|
||||
return part;
|
||||
}
|
||||
|
||||
protected abstract Particles getWeapon();
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue