Removed static things, fixed astar bugs. only thing left is bomber attacking ronge

This commit is contained in:
Ziver Koc 2007-06-17 22:31:09 +00:00
parent 91c3e77465
commit 526ce82d40
33 changed files with 293 additions and 196 deletions

View file

@ -74,13 +74,20 @@ public strictfp class AStarPathFinder implements PathFinder {
* @see org.newdawn.util.map.PathFinder#findPath(int, int, int, int, int)
*/
public synchronized Path findPath(int sx,int sy,int dx,int dy,int maxsearch) {
this.sx = sx;
this.sy = sy;
this.maxsearch = maxsearch;
Step step = new Step(null,dx,dy);
open.add(step);
if(findNearest){
this.sx = dx;
this.sy = dy;
Step step = new Step(null,sx,sy);
open.add(step);
}
else{
this.sx = sx;
this.sy = sy;
Step step = new Step(null,dx,dy);
open.add(step);
}
return processNodes();
}
@ -105,8 +112,8 @@ public strictfp class AStarPathFinder implements PathFinder {
if (validNode(xp,yp)) {
Step s = new Step(step,xp,yp);
open.add(s);
if(nearest == null || s.h < nearest.h){
nearest = s;
if(findNearest && (nearest == null || step.h < nearest.h)){
nearest = step;
}
}
}
@ -133,13 +140,29 @@ public strictfp class AStarPathFinder implements PathFinder {
Path path = new Path();
while (step.parent != null) {
path.addPoint(step.x,step.y);
step = step.parent;
if(findNearest){
Path temp = new Path();
while (step.parent != null) {
temp.addPoint(step.x,step.y);
step = step.parent;
}
temp.addPoint(step.x,step.y);
for(int i=temp.getSize()-1; i>=0 ;i--){
path.addPoint(temp.getX(i), temp.getY(i));
System.out.println(temp.getX(i)+", "+temp.getY(i));
}
}
path.addPoint(step.x,step.y);
else{
System.out.println(step.x+", "+step.y);
while (step.parent != null) {
path.addPoint(step.x,step.y);
step = step.parent;
System.out.println(step.x+", "+step.y);
}
path.addPoint(step.x,step.y);
}
System.out.println();
ArrayList<Vector2i> lol = path.getArray();
for(int y=0; y<map.getMapHeight() ;y++){
System.out.println();

View file

@ -13,10 +13,12 @@ public class EndGameState extends GameState{
private Fade fade;
private int status;
private int timer;
private InGameState inGameState;
public EndGameState(String name, int status){
public EndGameState(String name, int status, InGameState inGameState){
super(name,LWJGLGameWindow.getCamera());
this.inGameState = inGameState;
this.status = status;
}
@ -40,7 +42,7 @@ public class EndGameState extends GameState{
fade.setEntity(d);
break;
}
InGameState.input(false);
inGameState.input(false);
}
@Override

View file

@ -17,14 +17,17 @@ import ei.game.scene.weapons.WeaponHandler;
public class InGameState extends GameState{
private PlayerHandler playerHandler;
private WeaponHandler weaponHandler;
private Node rootNode;
private InGameHud hud;
private static Map map;
private Map map;
private Sound music;
private static InGameMouseInput mouse;
private static InGameKeyboardInput keyboard;
private InGameMouseInput mouse;
private InGameKeyboardInput keyboard;
protected static boolean network = false;
protected boolean network = false;
public InGameState(String name){
super(name);
@ -32,40 +35,40 @@ public class InGameState extends GameState{
@Override
public void init() {
PlayerHandler.getInstance().clear();
WeaponHandler.getInstance().clear();
playerHandler = new PlayerHandler();
weaponHandler = new WeaponHandler();
SoundManager.getInstnace().clear();
rootNode = new Node("InGameNode");
map = new Map(20,20);
map = new Map(20,20, this);
map.init("data/map/default");
HumanPlayer player = new HumanPlayer();
HumanPlayer player = new HumanPlayer(this);
mouse = new InGameMouseInput(map,player);
keyboard = new InGameKeyboardInput();
super.getInput().addInput(mouse);
super.getInput().addInput(keyboard);
player.addUnit(new Tank(0, 0, player));
player.addUnit(new Tank(1,0, player));
player.addUnit(new Tank(2,0, player));
player.addUnit(new APU(4, 0, player));
player.addUnit(new APU(5, 0, player));
PlayerHandler.getInstance().addPlayer(player);
player.addUnit(new Tank(0, 0, player, this));
player.addUnit(new Tank(1,0, player, this));
player.addUnit(new Tank(2,0, player, this));
player.addUnit(new APU(4, 0, player, this));
player.addUnit(new APU(5, 0, player, this));
getPlayerHandler().addPlayer(player);
AiPlayer ai = new AiPlayer();
ai.addUnit(new Tank(29,33, ai));
ai.addUnit(new Tank(30,33, ai));
ai.addUnit(new APU(31, 33, ai));
ai.addUnit(new APU(32, 33, ai));
PlayerHandler.getInstance().addPlayer(ai);
AiPlayer ai = new AiPlayer(this);
ai.addUnit(new Tank(29,33, ai, this));
ai.addUnit(new Tank(30,33, ai, this));
ai.addUnit(new APU(31, 33, ai, this));
ai.addUnit(new APU(32, 33, ai, this));
getPlayerHandler().addPlayer(ai);
rootNode.add(map.getMapNode());
rootNode.add(PlayerHandler.getInstance().getNode());
rootNode.add(WeaponHandler.getInstance().getNode());
rootNode.add(getPlayerHandler().getNode());
rootNode.add(getWeaponHandler().getNode());
hud = new InGameHud(player);
hud = new InGameHud(player, this);
mouse.setHud(hud);
rootNode.add(hud.getNode());
@ -84,8 +87,8 @@ public class InGameState extends GameState{
* Updates the gamestate
*/
public void update() {
PlayerHandler.getInstance().update();
WeaponHandler.getInstance().update();
getPlayerHandler().update();
getWeaponHandler().update();
hud.update();
rootNode.update();
}
@ -95,16 +98,24 @@ public class InGameState extends GameState{
*
* @return The map of the game
*/
public static Map getMap() {
public Map getMap() {
return map;
}
public static boolean isNetwork(){
public boolean isNetwork(){
return network;
}
public static void input(boolean b){
public void input(boolean b){
mouse.setEnabled(b);
keyboard.setEnabled(b);
}
public PlayerHandler getPlayerHandler(){
return playerHandler;
}
public WeaponHandler getWeaponHandler(){
return weaponHandler;
}
}

View file

@ -8,6 +8,7 @@ import ei.engine.ui.Button;
import ei.engine.ui.UiComponent;
import ei.engine.ui.UiHandler;
import ei.engine.ui.UiListener;
import ei.game.gamestate.InGameState;
import ei.game.player.Player;
import ei.game.scene.units.APU;
import ei.game.scene.units.Bomber;
@ -22,13 +23,15 @@ public class InGameBuildHud implements UiListener{
private UiHandler ui;
private Player player;
private BitmapText queueSize;
private InGameState inGameState;
public Button removeLastQueue;
public Button apuBuildButton;
public Button tankBuildButton;
public Button bomberBuildButton;
public InGameBuildHud(int x ,int y, Player p){
public InGameBuildHud(int x ,int y, Player p, InGameState inGameState){
this.inGameState = inGameState;
player = p;
ui = new UiHandler("BuildMenu");
@ -95,15 +98,15 @@ public class InGameBuildHud implements UiListener{
public void ActionEvent(UiComponent source){
// builds a apu
if(source == apuBuildButton){
player.getCC().buildUnit(new APU(player));
player.getCC().buildUnit(new APU(player, getInGameState()));
}
// builds a tank
else if(source == tankBuildButton){
player.getCC().buildUnit(new Tank(player));
player.getCC().buildUnit(new Tank(player, getInGameState()));
}
// builds a bomber
else if(source == bomberBuildButton){
player.getCC().buildUnit(new Bomber(player));
player.getCC().buildUnit(new Bomber(player, getInGameState()));
}
// removes the last unit in the queue
@ -111,4 +114,8 @@ public class InGameBuildHud implements UiListener{
player.getCC().removeLast();
}
}
public InGameState getInGameState(){
return inGameState;
}
}

View file

@ -7,6 +7,7 @@ import ei.engine.math.Vector2f;
import ei.engine.math.Vector4f;
import ei.engine.scene.Node;
import ei.engine.scene.Sprite;
import ei.game.gamestate.InGameState;
import ei.game.player.Player;
/**
@ -22,7 +23,7 @@ public class InGameHud {
private InGameBuildHud buildHud;
private Sprite buildBack;
public InGameHud(Player p){
public InGameHud(Player p, InGameState inGameState){
player = p;
hudNode = new Node("Hud");
@ -52,7 +53,7 @@ public class InGameHud {
hudNode.add(buildBar.getNode());
buildHud = new InGameBuildHud((int)(buildBack.getLocation().getX()-buildBack.getSize().getX()/4),
(int)(buildBack.getLocation().getY()+10),player);
(int)(buildBack.getLocation().getY()+10),player, inGameState);
hudNode.add(buildHud.getNode());
}

View file

@ -10,8 +10,8 @@ public abstract class NetworkBuilding extends Building{
private static int nextId;
private int buildingId;
public NetworkBuilding(int l, Vector2i pos, Player p, int size) {
super(l, pos, p, size);
public NetworkBuilding(int l, Vector2i pos, Player p, int size, InGameState inGameState) {
super(l, pos, p, size, inGameState);
buildingId = nextId;
nextId++;
}
@ -21,7 +21,7 @@ public abstract class NetworkBuilding extends Building{
}
public void setPos(int x, int y, int size) {
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.setPos(x, y, size);
}
else{
@ -30,7 +30,7 @@ public abstract class NetworkBuilding extends Building{
}
public void buildUnit(Unit u){
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.buildUnit(u);
}
else{
@ -39,7 +39,7 @@ public abstract class NetworkBuilding extends Building{
}
public void removeLast(){
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.removeLast();
}
else{
@ -48,7 +48,7 @@ public abstract class NetworkBuilding extends Building{
}
public void remove(){
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.remove();
}
else{
@ -57,7 +57,7 @@ public abstract class NetworkBuilding extends Building{
}
public void update() {
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.update();
}
else{

View file

@ -5,8 +5,12 @@ import ei.game.player.Player;
public abstract class NetworkPlayer extends Player{
public NetworkPlayer(InGameState inGameState) {
super(inGameState);
}
public void setKredits(int k){
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.setKredits(k);
}
else{
@ -15,7 +19,7 @@ public abstract class NetworkPlayer extends Player{
}
public void addKredits(int add){
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.addKredits(add);
}
else{

View file

@ -9,14 +9,14 @@ public abstract class NetworkUnit extends Unit{
private static int nextId;
private int unitId = -1;
public NetworkUnit(int l, Player p) {
super(l, p);
public NetworkUnit(int l, Player p, InGameState inGameState) {
super(l, p, inGameState);
unitId = nextId;
nextId++;
}
public NetworkUnit(int l, Vector2i pos, Player p) {
super(l, pos, p);
public NetworkUnit(int l, Vector2i pos, Player p, InGameState inGameState) {
super(l, pos, p, inGameState);
unitId = nextId;
nextId++;
}
@ -26,7 +26,7 @@ public abstract class NetworkUnit extends Unit{
}
protected void move(int x, int y, boolean b) {
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.move(x, y, b);
}
else{
@ -35,7 +35,7 @@ public abstract class NetworkUnit extends Unit{
}
public void attack(Vector2i target, boolean play) {
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.attack(target, play);
}
else{
@ -44,7 +44,7 @@ public abstract class NetworkUnit extends Unit{
}
public void remove(){
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.remove();
}
else{
@ -53,7 +53,7 @@ public abstract class NetworkUnit extends Unit{
}
public void setPos(int x, int y) {
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.setPos(x, y);
}
else{
@ -62,7 +62,7 @@ public abstract class NetworkUnit extends Unit{
}
public void forcePos(int x, int y){
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.forcePos(x, y);
}
else{
@ -71,7 +71,7 @@ public abstract class NetworkUnit extends Unit{
}
public void update() {
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.update();
}
else{

View file

@ -7,12 +7,12 @@ import ei.game.scene.weapons.Weapon;
public abstract class NetworkWepon extends Weapon{
public NetworkWepon(Vector2f startPos) {
super(startPos);
public NetworkWepon(Vector2f startPos, InGameState inGameState) {
super(startPos, inGameState);
}
public void launch(Vector2i target) {
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.launch(target);
}
else{
@ -21,7 +21,7 @@ public abstract class NetworkWepon extends Weapon{
}
public void update() {
if(!InGameState.isNetwork()){
if(!getInGameState().isNetwork()){
super.update();
}
else{

View file

@ -2,6 +2,7 @@ package ei.game.player;
import java.util.ArrayList;
import ei.game.gamestate.InGameState;
import ei.game.network.entities.NetworkPlayer;
import ei.game.scene.GameEntity;
import ei.game.scene.buildings.Building;
@ -24,9 +25,9 @@ public class AiPlayer extends NetworkPlayer{
* Constructor for the AiPlayer class. Creates a temporary
*
*/
public AiPlayer(){
super();
cc = new CommandCenter(30,25,this);
public AiPlayer(InGameState inGameState){
super(inGameState);
cc = new CommandCenter(30,25, this, getInGameState());
addUnit(cc);
attackingUnits = new ArrayList<GameEntity>();
}
@ -43,13 +44,13 @@ public class AiPlayer extends NetworkPlayer{
if(getKredits() >= 800 && cc.getBuildQueueSize()==0) {
int unitToBuild = (int)(Math.random()*2);
if(unitToBuild==0) {
cc.buildUnit(new APU(this));
cc.buildUnit(new APU(this, getInGameState()));
}
else if(unitToBuild==1) {
cc.buildUnit(new Tank(this));
cc.buildUnit(new Tank(this, getInGameState()));
}
else if(unitToBuild==2) {
cc.buildUnit(new Bomber(this));
cc.buildUnit(new Bomber(this, getInGameState()));
}
}
if((unitCount()>=10 || attackingUnits.size()>=2) && timer%100==0) {

View file

@ -1,5 +1,6 @@
package ei.game.player;
import ei.game.gamestate.InGameState;
import ei.game.network.entities.NetworkPlayer;
import ei.game.scene.buildings.Building;
@ -11,6 +12,10 @@ import ei.game.scene.buildings.Building;
*/
public class GaiaPlayer extends NetworkPlayer{
public GaiaPlayer(InGameState inGameState) {
super(inGameState);
}
@Override
public Building getCC() {
return null;

View file

@ -2,6 +2,7 @@ package ei.game.player;
import ei.engine.state.GameStateManager;
import ei.game.gamestate.EndGameState;
import ei.game.gamestate.InGameState;
import ei.game.network.entities.NetworkPlayer;
import ei.game.scene.buildings.Building;
import ei.game.scene.buildings.CommandCenter;
@ -9,9 +10,9 @@ import ei.game.scene.buildings.CommandCenter;
public class HumanPlayer extends NetworkPlayer{
private CommandCenter cc;
public HumanPlayer(){
super();
cc = new CommandCenter(10,5,this);
public HumanPlayer(InGameState inGameState){
super(inGameState);
cc = new CommandCenter(10,5,this,getInGameState());
addUnit(cc);
}
@ -29,7 +30,7 @@ public class HumanPlayer extends NetworkPlayer{
@Override
public void endGame(int s) {
GameStateManager.getInstance().addState(new EndGameState("EndGameState",s));
GameStateManager.getInstance().addState(new EndGameState("EndGameState", s, getInGameState()));
GameStateManager.getInstance().setActive("EndGameState");
}

View file

@ -3,6 +3,7 @@ package ei.game.player;
import java.util.ArrayList;
import ei.engine.scene.Node;
import ei.game.gamestate.InGameState;
import ei.game.scene.GameEntity;
import ei.game.scene.buildings.Building;
@ -10,8 +11,10 @@ public abstract class Player {
private ArrayList<GameEntity> units;
private Node unitsNode;
private int kredits;
private InGameState inGameState;
public Player(){
public Player(InGameState inGameState){
this.inGameState = inGameState;
units = new ArrayList<GameEntity>();
unitsNode = new Node("HumanPlayerNode");
kredits = 1000;
@ -64,6 +67,10 @@ public abstract class Player {
}
}
public InGameState getInGameState(){
return inGameState;
}
public abstract Building getCC();
public abstract boolean defeated();

View file

@ -7,7 +7,7 @@ import ei.game.gamestate.EndGameState;
public class PlayerHandler {
// The instance of this class
private static PlayerHandler instance;
//private static PlayerHandler instance;
// The player list
private ArrayList<Player> players;
private Node playerNode;
@ -104,13 +104,13 @@ public class PlayerHandler {
* Returns the instance of this class
*
* @return The instance
*/
public static PlayerHandler getInstance(){
if(instance == null){
instance = new PlayerHandler();
}
return instance;
}
}*/
public void clear(){
players.clear();

View file

@ -2,16 +2,19 @@ package ei.game.scene;
import ei.engine.math.Vector2i;
import ei.engine.scene.Entity;
import ei.game.gamestate.InGameState;
import ei.game.player.Player;
public abstract class GameEntity{
private int max_life;
private int life;
private Player player;
private InGameState inGameState;
public GameEntity(int l, Player p){
public GameEntity(int l, Player p, InGameState inGameState){
setLife(l);
player = p;
this.inGameState = inGameState;
}
/**
@ -70,6 +73,11 @@ public abstract class GameEntity{
public Player getPlayer(){
return player;
}
public InGameState getInGameState(){
return inGameState;
}
/**
* Sets a unit to be selected or not.
* @param b true or false.

View file

@ -15,9 +15,9 @@ import ei.engine.util.MultiPrintStream;
import ei.game.algo.PathFinderCallback;
import ei.game.algo.TileMap;
import ei.game.algo.TileSet;
import ei.game.gamestate.InGameState;
import ei.game.player.GaiaPlayer;
import ei.game.player.Player;
import ei.game.player.PlayerHandler;
import ei.game.scene.map.Factory;
import ei.game.scene.map.Prison;
import ei.game.scene.map.Stone;
@ -38,6 +38,8 @@ public class Map implements TileMap, TileSet, PathFinderCallback{
public static final int OBJ_WATER = 4;
public static final int POS_SIZE = 50;
private InGameState inGameState;
private int width;
private int hight;
private GameEntity[][] map;
@ -55,7 +57,8 @@ public class Map implements TileMap, TileSet, PathFinderCallback{
};
private int parkingIndex;
public Map(int w, int h){
public Map(int w, int h, InGameState inGameState){
this.inGameState = inGameState;
this.width = w;
this.hight = h;
//init();
@ -79,10 +82,10 @@ public class Map implements TileMap, TileSet, PathFinderCallback{
}
}
Player gaia = new GaiaPlayer();
PlayerHandler.getInstance().addPlayer(gaia);
Player gaia = new GaiaPlayer(getInGameState());
inGameState.getPlayerHandler().addPlayer(gaia);
for(int i=0; i<5 ;i++){
gaia.addUnit(new Stone(new Vector2i((int)(Math.random()*width),(int)(Math.random()*hight)),gaia));
gaia.addUnit(new Stone(new Vector2i((int)(Math.random()*width),(int)(Math.random()*hight)), gaia, getInGameState()));
}
}
@ -141,22 +144,22 @@ public class Map implements TileMap, TileSet, PathFinderCallback{
}
// init the gaia player and stones etc...
Player gaia = new GaiaPlayer();
PlayerHandler.getInstance().setGaiaPlayer(gaia);
Player gaia = new GaiaPlayer(getInGameState());
inGameState.getPlayerHandler().setGaiaPlayer(gaia);
for(int i=0; i<width ;i++){
for(int j=0; j<hight ;j++){
switch(objData[i][j]){
case OBJ_STONE:
gaia.addUnit(new Stone(new Vector2i(i,j),gaia));
gaia.addUnit(new Stone(new Vector2i(i,j), gaia, getInGameState()));
break;
case OBJ_FACTORY:
gaia.addUnit(new Factory(new Vector2i(i,j),gaia));
gaia.addUnit(new Factory(new Vector2i(i,j), gaia, getInGameState()));
break;
case OBJ_PRISON:
gaia.addUnit(new Prison(new Vector2i(i,j),gaia));
gaia.addUnit(new Prison(new Vector2i(i,j), gaia, getInGameState()));
break;
case OBJ_WATER:
gaia.addUnit(new Water(new Vector2i(i,j),gaia));
gaia.addUnit(new Water(new Vector2i(i,j), gaia, getInGameState()));
break;
}
}
@ -399,6 +402,10 @@ public class Map implements TileMap, TileSet, PathFinderCallback{
}
}
}
public InGameState getInGameState(){
return inGameState;
}
//*************************************************************

View file

@ -20,6 +20,7 @@ import ei.game.scene.units.Unit;
*/
public abstract class Building extends GameEntity{
private LinkedList<Unit> buildQueue;
// -1 not counting over 0 starts to count
private int buildTime;
private Node unitNode;
private Vector2i oldPos;
@ -28,8 +29,8 @@ public abstract class Building extends GameEntity{
private int size;
public Building(int l, Vector2i pos, Player p, int size) {
super(l, p);
public Building(int l, Vector2i pos, Player p, int size, InGameState inGameState) {
super(l, p, inGameState);
this.size = size;
buildQueue = new LinkedList<Unit>();
unitNode = new Node("BuildingNode");
@ -41,6 +42,8 @@ public abstract class Building extends GameEntity{
moveFrom.sub(unitNode.getLocation().getX()-20,unitNode.getLocation().getY());
moveTo.sub(unitNode.getLocation().getX(),unitNode.getLocation().getY());
buildTime = -1;
initGraphics();
}
@ -108,10 +111,10 @@ public abstract class Building extends GameEntity{
*/
public void setPos(int x, int y, int size) {
if(oldPos!=null) {
InGameState.getMap().removeBuildPos(oldPos.getX(), oldPos.getY(), size);
getInGameState().getMap().removeBuildPos(oldPos.getX(), oldPos.getY(), size);
}
oldPos = new Vector2i(x, y);
InGameState.getMap().setBuildPos(this, x, y, size);
getInGameState().getMap().setBuildPos(this, x, y, size);
}
/**
@ -136,7 +139,12 @@ public abstract class Building extends GameEntity{
*/
public void removeLast(){
if(!buildQueue.isEmpty()){
buildQueue.removeLast();
Unit unit = buildQueue.removeLast();
if(buildQueue.isEmpty()){
buildTime = -1;
getPlayer().addKredits(unit.getPrice());
}
}
}
@ -162,7 +170,7 @@ public abstract class Building extends GameEntity{
public void remove(){
unitNode.remove(getSprite());
getPlayer().removeUnit(this);
InGameState.getMap().removeBuildPos(oldPos.getX(), oldPos.getY(), this.size);
getInGameState().getMap().removeBuildPos(oldPos.getX(), oldPos.getY(), this.size);
}
/**
@ -174,11 +182,15 @@ public abstract class Building extends GameEntity{
remove();
}
if(!buildQueue.isEmpty() && getPlayer().getKredits() >= buildQueue.peek().getPrice()){
if(buildTime == 0){
if(!buildQueue.isEmpty()){
if(buildTime < 0 && getPlayer().getKredits() >= buildQueue.peek().getPrice()){
getPlayer().addKredits(-buildQueue.peek().getPrice());
buildTime = 0;
}
buildTime++;
if(buildTime >= 0)
buildTime++;
if(buildTime >= buildQueue.peek().getBuildTime()){
if(!unitNode.contains(buildQueue.peek().getNode())){
unitNode.add(buildQueue.peek().getNode(),unitNode.size()/2);
@ -186,8 +198,8 @@ public abstract class Building extends GameEntity{
}
Vector2i temp = Map.getPosByPixel(moveTo.getX()+unitNode.getLocation().getX(), moveTo.getY()+unitNode.getLocation().getY());
if(!InGameState.getMap().isPosEmpty(temp.getX(),temp.getY())){
InGameState.getMap().getPos(temp.getX(),temp.getY()).move(false, oldPos.getX()+(size/2), oldPos.getY()-(size/2));
if(!getInGameState().getMap().isPosEmpty(temp.getX(),temp.getY())){
getInGameState().getMap().getPos(temp.getX(),temp.getY()).move(false, oldPos.getX()+(size/2), oldPos.getY()-(size/2));
}
else{
//The moving is done here.
@ -206,7 +218,7 @@ public abstract class Building extends GameEntity{
if(Math.abs(moveTo.getX() - buildQueue.peek().getNode().getLocation().getX()) < buildQueue.peek().getVelocity()+1
&& Math.abs(moveTo.getY() - buildQueue.peek().getNode().getLocation().getY())< buildQueue.peek().getVelocity()+1 ){
buildTime = 0;
buildTime = -1;
unitNode.remove(buildQueue.peek().getNode());
buildQueue.peek().forcePos(temp.getX(),temp.getY());
getPlayer().addUnit(buildQueue.poll());

View file

@ -4,10 +4,10 @@ import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Entity;
import ei.engine.scene.Sprite;
import ei.game.gamestate.InGameState;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.weapons.Explotion;
import ei.game.scene.weapons.WeaponHandler;
/**
*
@ -19,12 +19,12 @@ public class CommandCenter extends Building{
private Sprite building;
private Sprite ground;
public CommandCenter(Player p) {
this(0, 0, p);
public CommandCenter(Player p, InGameState inGameState) {
this(0, 0, p, inGameState);
}
public CommandCenter(int x, int y, Player p){
super(1000, new Vector2i(x,y), p, 4);
public CommandCenter(int x, int y, Player p, InGameState inGameState){
super(1000, new Vector2i(x,y), p, 4, inGameState);
}
public void initGraphics(){
@ -46,7 +46,7 @@ public class CommandCenter extends Building{
* This unit type is now destroyed.
*/
public void destroyed(){
WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
getInGameState().getWeaponHandler().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY()), getInGameState()));
}
/**

View file

@ -4,11 +4,11 @@ import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Entity;
import ei.engine.scene.Sprite;
import ei.game.gamestate.InGameState;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.buildings.Building;
import ei.game.scene.weapons.Explotion;
import ei.game.scene.weapons.WeaponHandler;
public class Factory extends Building{
private static final String[] img = {
@ -18,8 +18,8 @@ public class Factory extends Building{
private SelectBox selectionBox;
private Sprite factory;
public Factory(Vector2i pos, Player p) {
super(500, pos, p, 4);
public Factory(Vector2i pos, Player p, InGameState inGameState) {
super(500, pos, p, 4, inGameState);
}
public void initGraphics(){
@ -52,7 +52,7 @@ public class Factory extends Building{
@Override
public void destroyed() {
WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
getInGameState().getWeaponHandler().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY()), getInGameState()));
}
@Override

View file

@ -3,6 +3,7 @@ package ei.game.scene.map;
import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.sound.Sound;
import ei.game.gamestate.InGameState;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.units.Unit;
@ -10,8 +11,8 @@ import ei.game.scene.weapons.Weapon;
public abstract class MapEntity extends Unit{
public MapEntity(int l, Vector2i pos, Player p) {
super(l, pos, p);
public MapEntity(int l, Vector2i pos, Player p, InGameState inGameState) {
super(l, pos, p, inGameState);
}
public void update() {

View file

@ -4,11 +4,11 @@ import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Entity;
import ei.engine.scene.Sprite;
import ei.game.gamestate.InGameState;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.buildings.Building;
import ei.game.scene.weapons.Explotion;
import ei.game.scene.weapons.WeaponHandler;
public class Prison extends Building{
private static final String[] img = {
@ -17,8 +17,8 @@ public class Prison extends Building{
private SelectBox selectionBox;
private Sprite prison;
public Prison(Vector2i pos, Player p) {
super(2000, pos, p, 6);
public Prison(Vector2i pos, Player p, InGameState inGameState) {
super(2000, pos, p, 6, inGameState);
}
public void initGraphics(){
@ -51,7 +51,7 @@ public class Prison extends Building{
@Override
public void destroyed() {
WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
getInGameState().getWeaponHandler().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY()), getInGameState()));
}
@Override

View file

@ -4,6 +4,7 @@ import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Entity;
import ei.engine.scene.Sprite;
import ei.game.gamestate.InGameState;
import ei.game.player.Player;
public class Stone extends MapEntity{
@ -20,8 +21,8 @@ public class Stone extends MapEntity{
private Sprite stone;
public Stone(Vector2i pos, Player p) {
super(500, pos, p);
public Stone(Vector2i pos, Player p, InGameState inGameState) {
super(500, pos, p, inGameState);
}
@Override

View file

@ -4,6 +4,7 @@ import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Entity;
import ei.engine.scene.Sprite;
import ei.game.gamestate.InGameState;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.buildings.Building;
@ -14,8 +15,8 @@ public class Water extends Building{
};
private Sprite water;
public Water(Vector2i pos, Player p) {
super(Integer.MAX_VALUE, pos, p, 6);
public Water(Vector2i pos, Player p, InGameState inGameState) {
super(Integer.MAX_VALUE, pos, p, 6, inGameState);
}
public void initGraphics(){

View file

@ -4,13 +4,13 @@ import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Sprite;
import ei.engine.sound.Sound;
import ei.game.gamestate.InGameState;
import ei.game.network.entities.NetworkUnit;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.weapons.Explotion;
import ei.game.scene.weapons.MachineGun;
import ei.game.scene.weapons.Weapon;
import ei.game.scene.weapons.WeaponHandler;
/**
*
@ -26,12 +26,12 @@ public class APU extends NetworkUnit{
private Sound attackSound;
private Sound selectSound;
public APU(Player p) {
super(70, p);
public APU(Player p, InGameState inGameState) {
super(70, p, inGameState);
}
public APU(int x, int y, Player p){
super(70, new Vector2i(x,y), p);
public APU(int x, int y, Player p, InGameState inGameState){
super(70, new Vector2i(x,y), p, inGameState);
}
public void init(){
@ -68,13 +68,13 @@ public class APU extends NetworkUnit{
* of unit.
*/
public Weapon getWeapon(Vector2f startPos) {
return new MachineGun(startPos);
return new MachineGun(startPos, getInGameState());
}
/**
* This unit type is now destroyed.
*/
public void destroyed(){
WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
getInGameState().getWeaponHandler().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY()), getInGameState()));
}
public Sound getGunSound() {
return gunSound;

View file

@ -5,13 +5,13 @@ import ei.engine.math.Vector2i;
import ei.engine.scene.Sprite;
import ei.engine.sound.Sound;
import ei.engine.texture.AnimatedTexture;
import ei.game.gamestate.InGameState;
import ei.game.network.entities.NetworkUnit;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.weapons.BomberWeapon;
import ei.game.scene.weapons.Explotion;
import ei.game.scene.weapons.Weapon;
import ei.game.scene.weapons.WeaponHandler;
/**
*
@ -27,21 +27,18 @@ public class Bomber extends NetworkUnit{
private Sound attackSound;
private Sound selectSound;
public Bomber(Player p) {
super(200, p);
public Bomber(Player p, InGameState inGameState) {
super(200, p, inGameState);
}
public Bomber(int x, int y, Player p){
super(200, new Vector2i(x,y), p);
public Bomber(int x, int y, Player p, InGameState inGameState){
super(200, new Vector2i(x,y), p, inGameState);
}
public void init(){
getNode().add(sprite);
setBuildTime(400);
setPrice(800);
autoAttack(false);
}
public void initGraphics(){
@ -55,6 +52,7 @@ public class Bomber extends NetworkUnit{
tex.setDelay(5);
sprite = new Sprite("Bomber",tex);
sprite.setSize(new Vector2f(50,60));
getNode().add(sprite);
gunSound = new Sound("gunSound", "data/sounds/bomberweapon.wav");
moveSound = new Sound[2];
@ -80,13 +78,13 @@ public class Bomber extends NetworkUnit{
* of unit.
*/
public Weapon getWeapon(Vector2f startPos) {
return new BomberWeapon(startPos);
return new BomberWeapon(startPos, getInGameState());
}
/**
* This unit type is now destroyed.
*/
public void destroyed(){
WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
getInGameState().getWeaponHandler().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY()), getInGameState()));
}
public Sound getGunSound() {
return gunSound;

View file

@ -4,13 +4,13 @@ import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Sprite;
import ei.engine.sound.Sound;
import ei.game.gamestate.InGameState;
import ei.game.network.entities.NetworkUnit;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.weapons.CannonBall;
import ei.game.scene.weapons.Explotion;
import ei.game.scene.weapons.Weapon;
import ei.game.scene.weapons.WeaponHandler;
/**
*
@ -26,12 +26,12 @@ public class Tank extends NetworkUnit{
private Sound attackSound;
private Sound selectSound;
public Tank(Player p) {
super(100, p);
public Tank(Player p, InGameState inGameState) {
super(100, p, inGameState);
}
public Tank(int x, int y, Player p){
super(100, new Vector2i(x,y), p);
public Tank(int x, int y, Player p, InGameState inGameState){
super(100, new Vector2i(x,y), p, inGameState);
}
public void init(){
@ -68,7 +68,7 @@ public class Tank extends NetworkUnit{
* of unit.
*/
public Weapon getWeapon(Vector2f startPos) {
return new CannonBall(startPos);
return new CannonBall(startPos, getInGameState());
}
public Sound getGunSound() {
return gunSound;
@ -86,7 +86,7 @@ public class Tank extends NetworkUnit{
* This unit type is now destroyed.
*/
public void destroyed(){
WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
getInGameState().getWeaponHandler().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY()), getInGameState()));
}
/**
* returns the velocity of the unit type.

View file

@ -16,7 +16,6 @@ import ei.game.scene.GameEntity;
import ei.game.scene.Map;
import ei.game.scene.SelectBox;
import ei.game.scene.weapons.Weapon;
import ei.game.scene.weapons.WeaponHandler;
/**
* The Unit class, handles the units in the game.
@ -50,14 +49,14 @@ public abstract class Unit extends GameEntity{
*
* @param l The max life of the unit
*/
public Unit(int l, Vector2i pos, Player p) {
super(l, p);
public Unit(int l, Vector2i pos, Player p, InGameState inGameState) {
super(l, p, inGameState);
unitNode = new Node("UnitNode");
unitNode.setLocation(Map.getPixelByPos(pos.getX(), pos.getY()));
setPos(pos.getX(), pos.getY());
autoAttack = true;
if(pathfinder == null){
pathfinder = new AStarPathFinder(InGameState.getMap(),InGameState.getMap(),InGameState.getMap());
pathfinder = new AStarPathFinder(getInGameState().getMap(),getInGameState().getMap(),getInGameState().getMap());
}
init();
initGraphics();
@ -68,8 +67,8 @@ public abstract class Unit extends GameEntity{
*
* @param l The max life of the unit
*/
public Unit(int l, Player p) {
super(l, p);
public Unit(int l, Player p, InGameState inGameState) {
super(l, p, inGameState);
unitNode = new Node("UnitNode");
autoAttack = true;
init();
@ -133,18 +132,18 @@ public abstract class Unit extends GameEntity{
*/
public void setPos(int x, int y) {
if(oldPos!=null) {
InGameState.getMap().removePos(oldPos.getX(), oldPos.getY());
getInGameState().getMap().removePos(oldPos.getX(), oldPos.getY());
}
oldPos = new Vector2i(x, y);
InGameState.getMap().setPos(this, x, y);
getInGameState().getMap().setPos(this, x, y);
}
public void forcePos(int x, int y){
if(oldPos!=null) {
InGameState.getMap().removePos(oldPos.getX(), oldPos.getY());
getInGameState().getMap().removePos(oldPos.getX(), oldPos.getY());
}
oldPos = new Vector2i(x, y);
InGameState.getMap().setPos(this, x, y);
getInGameState().getMap().setPos(this, x, y);
unitNode.setLocation(Map.getPixelByPos(x, y));
}
@ -198,8 +197,8 @@ public abstract class Unit extends GameEntity{
attack = null;
}
System.out.println(oldPos.getX()+" "+oldPos.getY()+"-"+x+" "+y);
Path p = pathfinder.findPath(oldPos.getX(),oldPos.getY(),x,y,600);
System.out.println(oldPos.getX()+","+oldPos.getY()+" -> "+x+","+y);
Path p = pathfinder.findPath(oldPos.getX(),oldPos.getY(), x, y, 200);
pathfinder.reset();
if(p != null) path = p.getList();
else path = null;
@ -220,7 +219,7 @@ public abstract class Unit extends GameEntity{
getAttackSound().play();
}
this.target = target;
attack = InGameState.getMap().getPos(target.getX(),target.getY());
attack = getInGameState().getMap().getPos(target.getX(),target.getY());
}
public void autoAttack(boolean auto) {
@ -244,7 +243,7 @@ public abstract class Unit extends GameEntity{
public void remove(){
unitNode.remove(getSprite());
getPlayer().removeUnit(this);
InGameState.getMap().removePos(oldPos.getX(), oldPos.getY());
getInGameState().getMap().removePos(oldPos.getX(), oldPos.getY());
}
/**
@ -332,14 +331,13 @@ public abstract class Unit extends GameEntity{
if(temp.getX() == oldPos.getX() && temp.getY() == oldPos.getY()){
temp = path.poll();
}
if(InGameState.getMap().isPosEmpty(temp.getX(), temp.getY())){
if(temp != null && getInGameState().getMap().isPosEmpty(temp.getX(), temp.getY())){
oldVect = new Vector2i((int)moveTo.getX(), (int)moveTo.getY());
moveTo = Map.getPixelByPos(temp.getX(), temp.getY());
setPos(temp.getX(), temp.getY());
}
else if(!path.isEmpty()){
//path = null;
System.out.println("lol: "+temp);
move(false, path.getLast().getX(), path.getLast().getY());
}
}
@ -351,8 +349,8 @@ public abstract class Unit extends GameEntity{
else if(attack != null){
Weapon wepon = getWeapon(new Vector2f(unitNode.getLocation().getX(), unitNode.getLocation().getY()));
Vector2i enamyPos;
if(InGameState.getMap().getPos(target.getX(), target.getY())!=attack) {
enamyPos = InGameState.getMap().getPosIndex(attack);
if(getInGameState().getMap().getPos(target.getX(), target.getY())!=attack) {
enamyPos = getInGameState().getMap().getPosIndex(attack);
}
else{
enamyPos = target.getCopy();
@ -367,7 +365,7 @@ public abstract class Unit extends GameEntity{
if(weponTimer >= wepon.getReload()){
wepon.launch(enamyPos);
getGunSound().play();
WeaponHandler.getInstance().addWeapon(wepon);
getInGameState().getWeaponHandler().addWeapon(wepon);
weponTimer = 0;
}
@ -383,9 +381,9 @@ public abstract class Unit extends GameEntity{
for(int j=oldPos.getY()-(wep.getRange()/50); j<=oldPos.getY()+(wep.getRange()/50); j++) {
//System.out.println("i :"+i+" j: "+j);
if(InGameState.getMap().posExist(i, j)&& !InGameState.getMap().isPosEmpty(i, j)
&& getPlayer()!=InGameState.getMap().getPos(i, j).getPlayer() && wep.onRange(Map.getPixelByPos(i, j))
&& InGameState.getMap().getPos(i, j).isTerrain()==false) {
if(getInGameState().getMap().posExist(i, j)&& !getInGameState().getMap().isPosEmpty(i, j)
&& getPlayer()!=getInGameState().getMap().getPos(i, j).getPlayer() && wep.onRange(Map.getPixelByPos(i, j))
&& getInGameState().getMap().getPos(i, j).isTerrain()==false) {
//System.out.println("ziver is shit");
attack(new Vector2i(i, j), false);
i = oldPos.getX()+(wep.getRange()/50)+1;

View file

@ -17,8 +17,8 @@ public class BomberWeapon extends NetworkWepon{
private boolean hit;
private Vector2i position;
public BomberWeapon(Vector2f startPos) {
super(startPos);
public BomberWeapon(Vector2f startPos, InGameState inGameState) {
super(startPos, inGameState);
setVelocity(4);
setRange(100);
setDamage(40);
@ -52,16 +52,16 @@ public class BomberWeapon extends NetworkWepon{
for(int i=position.getX()-1; i<=position.getX()+1; i++ ) {
for(int j=position.getY()-1; j<=position.getY()+1; j++) {
System.out.println("i :"+i+" j: "+j);
if(InGameState.getMap().posExist(i, j)&& !InGameState.getMap().isPosEmpty(i, j)) {
if(getInGameState().getMap().posExist(i, j)&& !getInGameState().getMap().isPosEmpty(i, j)) {
System.out.println("Damaged");
InGameState.getMap().getPos(i, j).damaged(getDamage());
getInGameState().getMap().getPos(i, j).damaged(getDamage());
}
}
}
hit = true;
}
if(part.isDead()) {
WeaponHandler.getInstance().removeWeapon(this);
getInGameState().getWeaponHandler().removeWeapon(this);
}
}

View file

@ -2,6 +2,7 @@ package ei.game.scene.weapons;
import ei.engine.effects.Particles;
import ei.engine.math.Vector2f;
import ei.game.gamestate.InGameState;
import ei.game.network.entities.NetworkWepon;
/**
@ -10,8 +11,8 @@ import ei.game.network.entities.NetworkWepon;
*/
public class CannonBall extends NetworkWepon{
public CannonBall(Vector2f startPos) {
super(startPos);
public CannonBall(Vector2f startPos, InGameState inGameState) {
super(startPos, inGameState);
setVelocity(4);
setRange(500);
setDamage(15);

View file

@ -2,6 +2,7 @@ package ei.game.scene.weapons;
import ei.engine.effects.Particles;
import ei.engine.math.Vector2f;
import ei.game.gamestate.InGameState;
import ei.game.network.entities.NetworkWepon;
/**
@ -12,8 +13,8 @@ import ei.game.network.entities.NetworkWepon;
public class Explotion extends NetworkWepon{
private Particles part;
public Explotion(Vector2f startPos) {
super(startPos);
public Explotion(Vector2f startPos, InGameState inGameState) {
super(startPos, inGameState);
setVelocity(4);
setRange(Integer.MIN_VALUE);
setDamage(Integer.MIN_VALUE);
@ -41,7 +42,7 @@ public class Explotion extends NetworkWepon{
public void update() {
if(part.isDead()) {
WeaponHandler.getInstance().removeWeapon(this);
getInGameState().getWeaponHandler().removeWeapon(this);
}
}

View file

@ -2,6 +2,7 @@ package ei.game.scene.weapons;
import ei.engine.effects.Particles;
import ei.engine.math.Vector2f;
import ei.game.gamestate.InGameState;
import ei.game.network.entities.NetworkWepon;
/**
@ -11,8 +12,8 @@ import ei.game.network.entities.NetworkWepon;
*/
public class MachineGun extends NetworkWepon{
public MachineGun(Vector2f startPos) {
super(startPos);
public MachineGun(Vector2f startPos, InGameState inGameState) {
super(startPos, inGameState);
setVelocity(4);
setRange(500);
setDamage(2);

View file

@ -23,6 +23,7 @@ public abstract class Weapon {
private Vector2f startPos;
private int reload;
private InGameState inGameState;
/**
* Constructor for the class Weapon. Initializes the weapon.
@ -30,10 +31,11 @@ public abstract class Weapon {
* @param startPos
* @param velocity
*/
public Weapon(Vector2f startPos) {
public Weapon(Vector2f startPos, InGameState inGameState) {
this.startPos = startPos;
this.minVelocity = 10;
this.hit = false;
this.inGameState = inGameState;
part = getWeapon();
part.setLocation(startPos);
part.reset();
@ -118,17 +120,17 @@ public abstract class Weapon {
}
if(Math.abs(part.getLocation().getX()-vect.getX()) < 15 && Math.abs(part.getLocation().getY()-vect.getY()) < 15) {
part.regenerate = false;
if(!hit && !InGameState.getMap().isPosEmpty(target.getX(), target.getY())) {
InGameState.getMap().getPos(target.getX(), target.getY()).damaged(damage);
if(!hit && !getInGameState().getMap().isPosEmpty(target.getX(), target.getY())) {
getInGameState().getMap().getPos(target.getX(), target.getY()).damaged(damage);
hit = true;
}
if(part.isDead()) {
WeaponHandler.getInstance().removeWeapon(this);
inGameState.getWeaponHandler().removeWeapon(this);
}
}
}
else{
WeaponHandler.getInstance().removeWeapon(this);
inGameState.getWeaponHandler().removeWeapon(this);
}
}
/**
@ -139,6 +141,10 @@ public abstract class Weapon {
return part;
}
public InGameState getInGameState(){
return inGameState;
}
protected abstract Particles getWeapon();
}

View file

@ -11,7 +11,7 @@ import ei.engine.scene.Node;
*
*/
public class WeaponHandler {
private static WeaponHandler instance;
//private static WeaponHandler instance;
private Node weaponNode;
private ArrayList<Weapon> weapons;
@ -71,13 +71,13 @@ public class WeaponHandler {
* Returns the instance of this class
*
* @return The instance
*/
public static WeaponHandler getInstance(){
if(instance == null){
instance = new WeaponHandler();
}
return instance;
}
} */
public void clear(){
weapons.clear();