Added ai player and fixed so player kan create units

This commit is contained in:
Ziver Koc 2007-05-06 20:35:09 +00:00
parent e7403858f8
commit c83605d5c4
22 changed files with 373 additions and 123 deletions

View file

@ -1,20 +1,40 @@
00000000000000000000 0000000000000000000000000000000000000000
00000000000000000000 0000000000000000000000000000000000000000
00011111111111110000 0001111111111111000000011111111111110000
00000000000000100000 0000000000000010000000000000000000100000
00000000000001000000 0000000000000100000000000000000001000000
00000000000010000000 0000000000001000000000000000000010000000
00000000001000000000 0000000000100000000000000000001000000000
00000000010000000000 0000000001000000000000000000010000000000
00000000100000000000 0000000010000000000000000000100000000000
00000001000000000000 0000000100000000000000000001000000000000
00000010000000000000 0000001000000000000000000010000000000000
00000100000000000000 0000010000000000000000000100000000000000
00001000000000000000 0000100000000000000000001000000000000000
00010000000000000000 0001000000000000000000010000000000000000
00111111111111111000 0011111111111111100000111111111111111000
00000000000000000000 0000000000000000000000000000000000000000
02222222222222222220 0222222222222222222002222222222222222220
00000000000000000000 0000000000000000000000000000000000000000
00000000000000000000 0000000000000000000000000000000000000000
00000000000000000000 0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0001111111111111000000011111111111110000
0000000000000010000000000000000000100000
0000000000000100000000000000000001000000
0000000000001000000000000000000010000000
0000000000100000000000000000001000000000
0000000001000000000000000000010000000000
0000000010000000000000000000100000000000
0000000100000000000000000001000000000000
0000001000000000000000000010000000000000
0000010000000000000000000100000000000000
0000100000000000000000001000000000000000
0001000000000000000000010000000000000000
0011111111111111100000111111111111111000
0000000000000000000000000000000000000000
0222222222222222222002222222222222222220
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000

View file

@ -1,20 +1,40 @@
00000000000000000000 0000000000000000000000000000000000000000
00000000000000000000 0000000000000000000000000000000000000000
00000000000000000000 0000000000000000000000000000000000000000
00000001111000000000 0000000111100000000000000001111000000000
00000000001000000000 0000000000100000000000000000001000000000
00000000001000000000 0000000000100000000000000000001000000000
00000000001000000000 0000000000100000000000000000001000000000
00000000001000000000 0000000000100000000000000000001000000000
00000000001000000000 0000000000100000000000000000001000000000
00000000000000000000 0000000000000000000000000000000000000000
00000000000000000000 0000000000000000000000000000000000000000
00000010000000000000 0000001000000000000000000010000000000000
00000010000000000000 0000001000000000000000000010000000000000
00000001100000000000 0000000110000000000000000001100000000000
00000000000000000000 0000000000000000000000000000000000000000
00000000000000000000 0000000000000000000000000000000000000000
00000000000000000000 0000000000000000000000000000000000000000
00000000000000000000 0000000000000000000000000000000000000000
00000000000000000000 0000000000000000000000000000000000000000
00000000000000000000 0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000111100000000000000001111000000000
0000000000100000000000000000001000000000
0000000000100000000000000000001000000000
0000000000100000000000000000001000000000
0000000000100000000000000000001000000000
0000000000100000000000000000001000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000001000000000000000000010000000000000
0000001000000000000000000010000000000000
0000000110000000000000000001100000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000

View file

@ -132,7 +132,14 @@ public class LWJGLGameWindow {
// Depth Buffer Setup // Depth Buffer Setup
GL11.glClearDepth(1.0f); GL11.glClearDepth(1.0f);
//Enable Blending
GL11.glEnable(GL11.GL_BLEND);
//GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,width,height);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
//Select The Projection Matrix //Select The Projection Matrix
GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glMatrixMode(GL11.GL_PROJECTION);
//Reset The Projection Matrix //Reset The Projection Matrix
@ -142,15 +149,9 @@ public class LWJGLGameWindow {
// Select The Modelview Matrix (controls model orientation) // Select The Modelview Matrix (controls model orientation)
GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glMatrixMode(GL11.GL_MODELVIEW);
//Enable Blending
GL11.glEnable(GL11.GL_BLEND);
//GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work) // Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
Display.setVSyncEnabled(true); //Display.setVSyncEnabled(true);
// The last steps // The last steps
@ -217,9 +218,9 @@ public class LWJGLGameWindow {
private void mainRender() { private void mainRender() {
// clear the screen // clear the screen
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_MODELVIEW); //GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity(); GL11.glLoadIdentity();
cam.setWorldTranslationGL(); cam.setWorldTranslationGL();
GL11.glPushMatrix(); GL11.glPushMatrix();
//let subsystem paint //let subsystem paint

View file

@ -18,8 +18,9 @@ public class Node extends Sprite {
/** /**
* Add a Entity * Add a Entity
* @param s the Sprite to add *
* @return false if the Entity alredy exist else true * @param s The Sprite to add
* @return False if the Entity alredy exist else true
*/ */
public boolean add(Entity s){ public boolean add(Entity s){
if(!entities.contains(s)){ if(!entities.contains(s)){
@ -30,8 +31,25 @@ public class Node extends Sprite {
return false; return false;
} }
/**
* Add a Entity to a specific pos
*
* @param s The Sprite to add
* @param pos the pos to put the entity to
* @return False if the Entity alredy exist else true
*/
public boolean add(Entity s,int pos){
if(!entities.contains(s)){
MultiPrintStream.out.println("Adding Entity To Node("+getName()+"): "+s.getName());
entities.add(pos,s);
return true;
}
return false;
}
/** /**
* get a Entity * get a Entity
*
* @param name the name of Entity to get * @param name the name of Entity to get
* @return null if the Entity wasnt found * @return null if the Entity wasnt found
*/ */
@ -62,7 +80,8 @@ public class Node extends Sprite {
} }
/** /**
* remove a Entity * Remove a Entity
*
* @param name the name of Entity to remove * @param name the name of Entity to remove
* @return false if the Entity alredy exist else true * @return false if the Entity alredy exist else true
*/ */
@ -121,6 +140,7 @@ public class Node extends Sprite {
/** /**
* Searches for the given name of a Entity * Searches for the given name of a Entity
*
* @param name The name of the Entity * @param name The name of the Entity
* @return The index of the Entity * @return The index of the Entity
*/ */
@ -140,4 +160,16 @@ public class Node extends Sprite {
} }
return rect; return rect;
} }
public int size(){
return entities.size();
}
public String toString(){
String msg = "Node[";
for(int i=0; i<entities.size() ;i++){
msg += entities.get(i).getName()+",";
}
return msg+"]";
}
} }

View file

@ -69,7 +69,7 @@ public class MultiPrintStream extends PrintStream {
* prints whit a new line to all the PrintStreams * prints whit a new line to all the PrintStreams
*/ */
public void println(String s){ public void println(String s){
s = getTime() + s; if(!s.equals(""))s = getTime() + s;
for(int i=0; i<streams.length ;i++) for(int i=0; i<streams.length ;i++)
streams[i].println(s); streams[i].println(s);
} }

View file

@ -4,11 +4,12 @@ import ei.engine.LWJGLGameWindow;
import ei.engine.LWJGLPropertiesDialog; import ei.engine.LWJGLPropertiesDialog;
import ei.engine.state.GameStateManager; import ei.engine.state.GameStateManager;
import ei.game.gamestate.InGameState; import ei.game.gamestate.InGameState;
import ei.game.gamestate.LoadingState;
import ei.game.gamestate.MenuState; import ei.game.gamestate.MenuState;
import ei.game.gamestate.SplashState; import ei.game.gamestate.SplashState;
public class EI extends LWJGLGameWindow{ public class EI extends LWJGLGameWindow{
public static final boolean debug = false; public static final boolean debug = true;
public static void main(String[] args){ public static void main(String[] args){
new EI(); new EI();
@ -21,7 +22,8 @@ public class EI extends LWJGLGameWindow{
protected void init(){ protected void init(){
if(debug){ if(debug){
GameStateManager.getInstance().addState(new InGameState("InGameState")); GameStateManager.getInstance().addState(new InGameState("InGameState"));
GameStateManager.getInstance().setActive("InGameState"); GameStateManager.getInstance().addState(new LoadingState("LoadingState","InGameState"));
GameStateManager.getInstance().setActive("LoadingState");
} }
else{ else{
GameStateManager.getInstance().addState(new SplashState("SplashState","MenuState")); GameStateManager.getInstance().addState(new SplashState("SplashState","MenuState"));

View file

@ -14,7 +14,7 @@ public class AStar{
private int width; private int width;
private int hight; private int hight;
private AStarNode[][] map; private static AStarNode[][] map;
protected boolean randomizeMap = true; protected boolean randomizeMap = true;
protected Random RAND = new Random(0); protected Random RAND = new Random(0);
@ -23,7 +23,12 @@ public class AStar{
width = InGameState.getMap().getSize().getX(); width = InGameState.getMap().getSize().getX();
hight = InGameState.getMap().getSize().getY(); hight = InGameState.getMap().getSize().getY();
initializePathfinder(); initializePathfinder();
initializeMap(); if(map != null){
reset();
}
else{
initializeMap();
}
} }
public List<AStarNode> startSearch(Vector2i start, Vector2i goal){ public List<AStarNode> startSearch(Vector2i start, Vector2i goal){
@ -38,13 +43,29 @@ public class AStar{
pathfinder = new AStarPathfinder(); pathfinder = new AStarPathfinder();
} }
protected void reset(){
for(int y = 0, nodeId = 0; hight > y; y++) {
MultiPrintStream.out.println("");
for(int x = 0; width > x; x++, nodeId++) {
if(!InGameState.getMap().isPosEmpty(x, y)) {
map[x][y].setBlocked(true);
MultiPrintStream.out.print(1);
}
else{
MultiPrintStream.out.print(""+0);
map[x][y].setBlocked(false);
}
map[x][y].setVisited(false);
}
}
}
protected void initializeMap() { protected void initializeMap() {
System.out.println("Initializing map"); System.out.println("Initializing map");
// Create the map. // Create the map.
map = new AStarNode2D[width][hight]; map = new AStarNode2D[width][hight];
for(int y = 0, nodeId = 0; hight > y; y++) { for(int y = 0, nodeId = 0; hight > y; y++) {
MultiPrintStream.out.println(); MultiPrintStream.out.println("");
for(int x = 0; width > x; x++, nodeId++) { for(int x = 0; width > x; x++, nodeId++) {
map[x][y] = new AStarNode2D(x, y, nodeId); map[x][y] = new AStarNode2D(x, y, nodeId);
@ -52,10 +73,10 @@ public class AStar{
map[x][y].setBlocked(true); map[x][y].setBlocked(true);
MultiPrintStream.out.print(1); MultiPrintStream.out.print(1);
} }
else MultiPrintStream.out.print(0); else MultiPrintStream.out.print(""+0);
} }
} }
MultiPrintStream.out.println(); MultiPrintStream.out.println("");
/* /*
for(int x = 0, nodeId = 0; width > x; x++) { for(int x = 0, nodeId = 0; width > x; x++) {
for(int y = 0; hight > y; y++, nodeId++) { for(int y = 0; hight > y; y++, nodeId++) {

View file

@ -7,6 +7,7 @@ import ei.engine.state.GameState;
import ei.game.hud.InGameHud; import ei.game.hud.InGameHud;
import ei.game.input.InGameKeyboardInput; import ei.game.input.InGameKeyboardInput;
import ei.game.input.InGameMouseInput; import ei.game.input.InGameMouseInput;
import ei.game.player.AiPlayer;
import ei.game.player.HumanPlayer; import ei.game.player.HumanPlayer;
import ei.game.player.PlayerHandler; import ei.game.player.PlayerHandler;
import ei.game.scene.Map; import ei.game.scene.Map;
@ -33,28 +34,36 @@ public class InGameState extends GameState{
SoundManager.getInstnace().clear(); SoundManager.getInstnace().clear();
rootNode = new Node("InGameNode"); rootNode = new Node("InGameNode");
map = new Map(20,20); map = new Map(20,20);
map.init("data/map/default"); map.init("data/map/default");
InGameMouseInput mouse = new InGameMouseInput(map);
super.getInput().addInput(mouse);
InGameKeyboardInput keyboard = new InGameKeyboardInput();
super.getInput().addInput(keyboard);
HumanPlayer player = new HumanPlayer(); HumanPlayer player = new HumanPlayer();
Tank t1 = new Tank(player); InGameMouseInput mouse = new InGameMouseInput(map,player);
player.addUnit(t1); InGameKeyboardInput keyboard = new InGameKeyboardInput();
Tank t2 = new Tank(1,0, player); super.getInput().addInput(mouse);
player.addUnit(t2); 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 Tank(2,0, player));
player.addUnit(new Bomber(3, 0, player)); player.addUnit(new Bomber(3, 0, player));
player.addUnit(new Bomber(6, 0, player)); player.addUnit(new Bomber(6, 0, player));
player.addUnit(new Bomber(7, 0, player)); player.addUnit(new Bomber(7, 0, player));
player.addUnit(new APU(4, 0, player)); player.addUnit(new APU(4, 0, player));
player.addUnit(new APU(5, 0, player)); player.addUnit(new APU(5, 0, player));
PlayerHandler.getInstance().addPlayer(player); PlayerHandler.getInstance().addPlayer(player);
AiPlayer ai = new AiPlayer();
ai.addUnit(new Tank(1,39, ai));
ai.addUnit(new Tank(2,39, ai));
ai.addUnit(new Bomber(3, 39, ai));
ai.addUnit(new Bomber(6, 39, ai));
ai.addUnit(new Bomber(7, 39, ai));
ai.addUnit(new APU(4, 39, ai));
ai.addUnit(new APU(5, 39, ai));
PlayerHandler.getInstance().addPlayer(ai);
rootNode.add(map.getMapNode()); rootNode.add(map.getMapNode());
rootNode.add(PlayerHandler.getInstance().getNode()); rootNode.add(PlayerHandler.getInstance().getNode());
rootNode.add(WeaponHandler.getInstance().getNode()); rootNode.add(WeaponHandler.getInstance().getNode());

View file

@ -95,15 +95,15 @@ public class InGameBuildHud implements UiListener{
public void ActionEvent(UiComponent source){ public void ActionEvent(UiComponent source){
// builds a apu // builds a apu
if(source == apuBuildButton){ if(source == apuBuildButton){
player.getCC().buildUnit(new APU(0,0,player)); player.getCC().buildUnit(new APU(player));
} }
// builds a tank // builds a tank
else if(source == tankBuildButton){ else if(source == tankBuildButton){
player.getCC().buildUnit(new Tank(0,0,player)); player.getCC().buildUnit(new Tank(player));
} }
// builds a bomber // builds a bomber
else if(source == bomberBuildButton){ else if(source == bomberBuildButton){
player.getCC().buildUnit(new Bomber(0,0,player)); player.getCC().buildUnit(new Bomber(player));
} }
// removes the last unit in the queue // removes the last unit in the queue

View file

@ -11,6 +11,7 @@ import ei.engine.scene.Box;
import ei.engine.scene.Sprite; import ei.engine.scene.Sprite;
import ei.engine.texture.Texture; import ei.engine.texture.Texture;
import ei.game.hud.InGameHud; import ei.game.hud.InGameHud;
import ei.game.player.Player;
import ei.game.scene.GameEntity; import ei.game.scene.GameEntity;
import ei.game.scene.Map; import ei.game.scene.Map;
@ -25,10 +26,12 @@ public class InGameMouseInput extends MouseInput{
private InGameHud hud; private InGameHud hud;
private Map map; private Map map;
private Player player;
public InGameMouseInput(Map map) { public InGameMouseInput(Map map, Player p) {
super("InGameMouseInput","data/cursor/cursor.png"); super("InGameMouseInput","data/cursor/cursor.png");
this.map = map; this.map = map;
this.player = p;
this.selected = new ArrayList<GameEntity>(); this.selected = new ArrayList<GameEntity>();
//inits the mouse texture //inits the mouse texture
@ -205,12 +208,15 @@ public class InGameMouseInput extends MouseInput{
public void selectUnits(Vector2i startPos, Vector2i stopPos){ public void selectUnits(Vector2i startPos, Vector2i stopPos){
for(int i=Math.min(startPos.getX(), stopPos.getX()); i<=Math.max(startPos.getX(), stopPos.getX()) ;i++){ for(int i=Math.min(startPos.getX(), stopPos.getX()); i<=Math.max(startPos.getX(), stopPos.getX()) ;i++){
for(int j=Math.min(startPos.getY(), stopPos.getY()); j<=Math.max(startPos.getY(), stopPos.getY()) ;j++){ for(int j=Math.min(startPos.getY(), stopPos.getY()); j<=Math.max(startPos.getY(), stopPos.getY()) ;j++){
if(!map.isPosEmpty(i, j)){ if(!map.isPosEmpty(i, j) && map.getPos(i, j).getPlayer() == player){
selected.add(map.getPos(i, j)); selected.add(map.getPos(i, j));
map.getPos(i, j).setSelected(true); map.getPos(i, j).setSelected(true,false);
} }
} }
} }
if(!selected.isEmpty()){
selected.get((int)(Math.random()*selected.size())).setSelected(true,true);
}
} }
/** /**
@ -219,7 +225,7 @@ public class InGameMouseInput extends MouseInput{
*/ */
public void deselectAllUnits(){ public void deselectAllUnits(){
for(int i=0; i<selected.size(); i++) { for(int i=0; i<selected.size(); i++) {
selected.get(i).setSelected(false); selected.get(i).setSelected(false,false);
} }
selected.clear(); selected.clear();
} }

View file

@ -4,7 +4,6 @@ import org.lwjgl.input.Keyboard;
import ei.engine.LWJGLGameWindow; import ei.engine.LWJGLGameWindow;
import ei.engine.input.KeyboardInput; import ei.engine.input.KeyboardInput;
import ei.engine.state.GameStateManager;
public class MenuKeyboardInput extends KeyboardInput{ public class MenuKeyboardInput extends KeyboardInput{

View file

@ -0,0 +1,20 @@
package ei.game.player;
import ei.game.scene.buildings.Building;
import ei.game.scene.buildings.CommandCenter;
public class AiPlayer extends Player{
private CommandCenter cc;
public AiPlayer(){
super();
cc = new CommandCenter(30,30,this);
addUnit(cc);
}
@Override
public Building getCC() {
return cc;
}
}

View file

@ -75,7 +75,7 @@ public abstract class GameEntity{
* Sets a unit to be selected or not. * Sets a unit to be selected or not.
* @param b true or false. * @param b true or false.
*/ */
public abstract void setSelected(boolean b); public abstract void setSelected(boolean b, boolean playSound);
/** /**
* Set if the mouse is over the unit or not * Set if the mouse is over the unit or not

View file

@ -8,6 +8,8 @@ import java.util.LinkedList;
import ei.engine.math.Vector2f; import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i; import ei.engine.math.Vector2i;
import ei.engine.math.Vector3f; import ei.engine.math.Vector3f;
import ei.engine.scene.Box;
import ei.engine.scene.Entity;
import ei.engine.scene.Node; import ei.engine.scene.Node;
import ei.engine.scene.Sprite; import ei.engine.scene.Sprite;
import ei.engine.util.MultiPrintStream; import ei.engine.util.MultiPrintStream;
@ -112,8 +114,8 @@ public class Map {
} }
} }
private Sprite getPosSprite(int x, int y, String file){ private Entity getPosSprite(int x, int y, String file){
Sprite s = new Sprite("MapPos("+x+","+y+")", file); Box s = new Box("MapPos("+x+","+y+")", file);
s.setLocation(new Vector3f(x*POS_SIZE,y*POS_SIZE,0)); s.setLocation(new Vector3f(x*POS_SIZE,y*POS_SIZE,0));
s.setSize(new Vector2f(POS_SIZE,POS_SIZE)); s.setSize(new Vector2f(POS_SIZE,POS_SIZE));
return s; return s;

View file

@ -8,6 +8,8 @@ import ei.engine.scene.Node;
import ei.engine.texture.Texture; import ei.engine.texture.Texture;
public class SelectBox { public class SelectBox {
public static final int COLOR_GREEN = 0;
public static final int COLOR_RED = 1;
private static final float height = 3; private static final float height = 3;
private Node selectNode; private Node selectNode;
private Node mouseOverNode; private Node mouseOverNode;
@ -20,12 +22,17 @@ public class SelectBox {
* @param y The size of the selection box * @param y The size of the selection box
* @param max The max health of the object * @param max The max health of the object
*/ */
public SelectBox(float x, float y, int max){ public SelectBox(int color, float x, float y, int max){
selectNode = new Node("SelectionNode"); selectNode = new Node("SelectionNode");
mouseOverNode = new Node("MouseOverNode"); mouseOverNode = new Node("MouseOverNode");
Texture tex = new Texture(); Texture tex = new Texture();
tex.setColor(new Vector4f(0.5f, 1.0f, 0.5f,1)); if(color == COLOR_RED){
tex.setColor(new Vector4f(1.0f, 0.5f, 0.5f,1));
}
else{
tex.setColor(new Vector4f(0.5f, 1.0f, 0.5f,1));
}
bar = new ProgressBar("Health",x,height,max); bar = new ProgressBar("Health",x,height,max);
bar.getNode().setLocation(new Vector2f(0,-x/2+height/2)); bar.getNode().setLocation(new Vector2f(0,-x/2+height/2));
@ -37,8 +44,7 @@ public class SelectBox {
Box select = new Box("select",tex); Box select = new Box("select",tex);
select.setSize(new Vector2f(x,y)); select.setSize(new Vector2f(x,y));
selectNode.add(select); selectNode.add(select);
} }
/** /**

View file

@ -2,7 +2,9 @@ package ei.game.scene.buildings;
import java.util.LinkedList; import java.util.LinkedList;
import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i; import ei.engine.math.Vector2i;
import ei.engine.scene.Entity;
import ei.engine.scene.Node; import ei.engine.scene.Node;
import ei.game.gamestate.InGameState; import ei.game.gamestate.InGameState;
import ei.game.player.Player; import ei.game.player.Player;
@ -20,6 +22,8 @@ public abstract class Building extends GameEntity{
private int buildTime; private int buildTime;
private Node unitNode; private Node unitNode;
private Vector2i oldPos; private Vector2i oldPos;
private Vector2f moveFrom;
private Vector2f moveTo;
private int size; private int size;
@ -30,9 +34,11 @@ public abstract class Building extends GameEntity{
unitNode = new Node("UnitNode"); unitNode = new Node("UnitNode");
unitNode.setLocation(Map.getPixelByPos(pos.getX(), pos.getY(), this.size)); unitNode.setLocation(Map.getPixelByPos(pos.getX(), pos.getY(), this.size));
setPos(pos.getX(), pos.getY(), this.size); setPos(pos.getX(), pos.getY(), this.size);
moveFrom = Map.getPixelByPos(oldPos.getX()+size-2, oldPos.getX()+size-1);
moveTo = Map.getPixelByPos( oldPos.getX()+size-2, oldPos.getX()+size);
} }
public void setSelected(boolean b) { public void setSelected(boolean b, boolean playSound) {
if(b) { if(b) {
unitNode.add(getSelection().getSelectNode()); unitNode.add(getSelection().getSelectNode());
} }
@ -110,8 +116,14 @@ public abstract class Building extends GameEntity{
* *
*/ */
public int getBuildProgress(){ public int getBuildProgress(){
// TODO return the procentage of completion of the unit if(buildQueue.isEmpty()){
return 50; return 0;
}
int progress = (int)(((float)buildTime/buildQueue.peekFirst().getBuildTime())*100);
if(progress > 100){
progress = 100;
}
return progress;
} }
/** /**
@ -128,10 +140,50 @@ public abstract class Building extends GameEntity{
* Updating the unit * Updating the unit
*/ */
public void update() { public void update() {
if(getLife()<=0) { if(getLife() <= 0) {
destroyed(); destroyed();
removeBuilding(); removeBuilding();
} }
if(!buildQueue.isEmpty() && getPlayer().getKredits() >= buildQueue.peekFirst().getPrice()){
if(buildTime == 0){
getPlayer().addKredits(-buildQueue.peekFirst().getPrice());
}
buildTime++;
if(buildTime >= buildQueue.peekFirst().getBuildTime()){
if(!unitNode.contains(buildQueue.peekFirst().getNode())){
System.out.println(buildQueue.peekFirst().getNode());
unitNode.add(buildQueue.peekFirst().getNode(),unitNode.size()/2);
buildQueue.peekFirst().getNode().setLocation(moveFrom);
}
Vector2i temp = Map.getPosByPixel(moveTo.getX(), moveTo.getY());
if(InGameState.getMap().isPosEmpty(temp.getX(),temp.getY())){
//The moving is done here.
if(moveTo.getX() > buildQueue.peekFirst().getNode().getLocation().getX()) {
buildQueue.peekFirst().getNode().getLocation().add(buildQueue.peekFirst().getVelocity(), 0f, 0f);
}
if(moveTo.getX() < buildQueue.peekFirst().getNode().getLocation().getX()) {
buildQueue.peekFirst().getNode().getLocation().add(-buildQueue.peekFirst().getVelocity(), 0f, 0f);
}
if(moveTo.getY() > buildQueue.peekFirst().getNode().getLocation().getY()) {
buildQueue.peekFirst().getNode().getLocation().add(0f, buildQueue.peekFirst().getVelocity(), 0f);
}
if(moveTo.getY() < buildQueue.peekFirst().getNode().getLocation().getY()) {
buildQueue.peekFirst().getNode().getLocation().add(0f, -buildQueue.peekFirst().getVelocity(), 0f);
}
System.out.println(buildQueue.peekFirst().getNode().getLocation());
if(Math.abs(moveTo.getX() - buildQueue.peekFirst().getNode().getLocation().getX()) < buildQueue.peekFirst().getVelocity()+1
&& Math.abs(moveTo.getY() - buildQueue.peekFirst().getNode().getLocation().getY())< buildQueue.peekFirst().getVelocity()+1 ){
buildTime = 0;
unitNode.remove(buildQueue.peekFirst().getNode());
buildQueue.peekFirst().forcePos(temp.getX(),temp.getY());
getPlayer().addUnit(buildQueue.poll());
}
}
}
}
} }
public abstract Entity getGroundSprite();
} }

View file

@ -2,6 +2,7 @@ package ei.game.scene.buildings;
import ei.engine.math.Vector2f; import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i; import ei.engine.math.Vector2i;
import ei.engine.scene.Entity;
import ei.engine.scene.Sprite; import ei.engine.scene.Sprite;
import ei.game.player.Player; import ei.game.player.Player;
import ei.game.scene.SelectBox; import ei.game.scene.SelectBox;
@ -15,7 +16,8 @@ import ei.game.scene.weapons.WeaponHandler;
*/ */
public class CommandCenter extends Building{ public class CommandCenter extends Building{
private SelectBox selectionBox; private SelectBox selectionBox;
private Sprite sprite; private Sprite building;
private Sprite ground;
public CommandCenter(Player p) { public CommandCenter(Player p) {
this(0, 0, p); this(0, 0, p);
@ -23,46 +25,60 @@ public class CommandCenter extends Building{
public CommandCenter(int x, int y, Player p){ public CommandCenter(int x, int y, Player p){
super(1000, new Vector2i(x,y), p, 4); super(1000, new Vector2i(x,y), p, 4);
this.sprite = new Sprite("APU", "data/buildings/cc/cc.png"); ground = new Sprite("CC_Ground", "data/buildings/cc/cc_ground.png");
sprite.setSize(new Vector2f(200,200)); ground.setSize(new Vector2f(200,200));
getNode().add(sprite); getNode().add(ground);
building = new Sprite("CC", "data/buildings/cc/cc_building.png");
building.setSize(new Vector2f(200,200));
getNode().add(building);
selectionBox = new SelectBox(200,200,getMaxLife()); selectionBox = new SelectBox(0,200,200,getMaxLife());
} }
protected SelectBox getSelection() { protected SelectBox getSelection() {
return selectionBox; return selectionBox;
} }
/** /**
* This unit type is now destroyed. * This unit type is now destroyed.
*/ */
public void destroyed(){ public void destroyed(){
WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY()))); WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
} }
/** /**
* Doesnt matter anyway. Since buildings cant attack. * Doesnt matter anyway. Since buildings cant attack.
*/ */
public void attack(Vector2i target, boolean play) { public void attack(Vector2i target, boolean play) {
} }
/** /**
* Manages the sprite connected to this unit. * Manages the sprite connected to this unit.
*
* @param s * @param s
*/ */
public void setSprite(Sprite s) { public void setSprite(Sprite s) {
this.sprite = s; building = s;
} }
/** /**
* returns the sprite connected to this type * returns the sprite connected to this type
* of unit. * of unit.
*
* @return * @return
*/ */
public Sprite getSprite() { public Entity getSprite() {
return this.sprite; return building;
} }
@Override @Override
public int getMaintenanceCost() { public int getMaintenanceCost() {
return 0; return 0;
} }
@Override
public Entity getGroundSprite() {
return ground;
}
} }

View file

@ -22,9 +22,13 @@ public class Stone extends MapEntity{
public Stone(Vector2i pos, Player p) { public Stone(Vector2i pos, Player p) {
super(10000, pos, p); super(10000, pos, p);
}
@Override
public void init() {
stone = new Sprite("Stone",img[(int)(Math.random()*img.length)]); stone = new Sprite("Stone",img[(int)(Math.random()*img.length)]);
stone.setSize(new Vector2f(40,40)); stone.setSize(new Vector2f(40,40));
getNode().add(stone); getNode().add(stone);
} }
@Override @Override
@ -35,7 +39,6 @@ public class Stone extends MapEntity{
@Override @Override
public int getMaintenanceCost() { public int getMaintenanceCost() {
return 0; return 0;
} }
} }

View file

@ -21,23 +21,27 @@ public class APU extends Unit{
private Sprite sprite; private Sprite sprite;
private Sound gunSound; private Sound gunSound;
private Sound moveSound[] = new Sound[2]; private Sound[] moveSound;
private Sound attackSound; private Sound attackSound;
private Sound selectSound; private Sound selectSound;
public APU(Player p) { public APU(Player p) {
this(0, 0, p); super(70, p);
} }
public APU(int x, int y, Player p){ public APU(int x, int y, Player p){
super(70, new Vector2i(x,y), p); super(70, new Vector2i(x,y), p);
this.sprite = new Sprite("APU", "data/units/apu/apu0000.png"); }
public void init(){
sprite = new Sprite("APU", "data/units/apu/apu0000.png");
sprite.setSize(new Vector2f(40,40)); sprite.setSize(new Vector2f(40,40));
getNode().add(sprite); getNode().add(sprite);
setBuildTime(20); setBuildTime(200);
setPrice(200); setPrice(200);
gunSound = new Sound("gunSound", "data/sounds/machinegun.wav"); gunSound = new Sound("gunSound", "data/sounds/machinegun.wav");
moveSound = new Sound[2];
moveSound[0] = new Sound("moveSound", "data/sounds/APUmove1.wav"); moveSound[0] = new Sound("moveSound", "data/sounds/APUmove1.wav");
moveSound[1] = new Sound("moveSound", "data/sounds/APUmove2.wav"); moveSound[1] = new Sound("moveSound", "data/sounds/APUmove2.wav");
selectSound = new Sound("selectSound", "data/sounds/APUselect.wav"); selectSound = new Sound("selectSound", "data/sounds/APUselect.wav");
@ -49,7 +53,7 @@ public class APU extends Unit{
selectSound.setLocation(getNode().getLocation()); selectSound.setLocation(getNode().getLocation());
attackSound.setLocation(getNode().getLocation()); attackSound.setLocation(getNode().getLocation());
selectionBox = new SelectBox(40,40,getMaxLife()); selectionBox = new SelectBox(0,40,40,getMaxLife());
} }
protected SelectBox getSelection() { protected SelectBox getSelection() {

View file

@ -22,16 +22,19 @@ public class Bomber extends Unit{
private Sprite sprite; private Sprite sprite;
private Sound gunSound; private Sound gunSound;
private Sound[] moveSound = new Sound[2]; private Sound[] moveSound;
private Sound attackSound; private Sound attackSound;
private Sound selectSound; private Sound selectSound;
public Bomber(Player p) { public Bomber(Player p) {
this(0, 0, p); super(200, p);
} }
public Bomber(int x, int y, Player p){ public Bomber(int x, int y, Player p){
super(200, new Vector2i(x,y), p); super(200, new Vector2i(x,y), p);
}
public void init(){
AnimatedTexture tex = new AnimatedTexture("BomberTexture"); AnimatedTexture tex = new AnimatedTexture("BomberTexture");
tex.addAnimation("normal", new String[]{ tex.addAnimation("normal", new String[]{
"data/units/bomber/bomber0000.png", "data/units/bomber/bomber0000.png",
@ -43,10 +46,11 @@ public class Bomber extends Unit{
sprite = new Sprite("Bomber",tex); sprite = new Sprite("Bomber",tex);
sprite.setSize(new Vector2f(50,60)); sprite.setSize(new Vector2f(50,60));
getNode().add(sprite); getNode().add(sprite);
setBuildTime(40); setBuildTime(400);
setPrice(800); setPrice(800);
gunSound = new Sound("gunSound", "data/sounds/bomberweapon.wav"); gunSound = new Sound("gunSound", "data/sounds/bomberweapon.wav");
moveSound = new Sound[2];
moveSound[0] = new Sound("moveSound", "data/sounds/BOMBERmove1.wav"); moveSound[0] = new Sound("moveSound", "data/sounds/BOMBERmove1.wav");
moveSound[1] = new Sound("moveSound", "data/sounds/BOMBERmove2.wav"); moveSound[1] = new Sound("moveSound", "data/sounds/BOMBERmove2.wav");
selectSound = new Sound("selectSound", "data/sounds/BOMBERselect.wav"); selectSound = new Sound("selectSound", "data/sounds/BOMBERselect.wav");
@ -58,7 +62,7 @@ public class Bomber extends Unit{
selectSound.setLocation(getNode().getLocation()); selectSound.setLocation(getNode().getLocation());
attackSound.setLocation(getNode().getLocation()); attackSound.setLocation(getNode().getLocation());
selectionBox = new SelectBox(40,40,getMaxLife()); selectionBox = new SelectBox(0,40,40,getMaxLife());
} }
protected SelectBox getSelection() { protected SelectBox getSelection() {

View file

@ -21,35 +21,39 @@ public class Tank extends Unit{
private Sprite sprite; private Sprite sprite;
private Sound gunSound; private Sound gunSound;
private Sound[] moveSound = new Sound[2]; private Sound[] moveSound;
private Sound attackSound; private Sound attackSound;
private Sound selectSound; private Sound selectSound;
public Tank(Player p) { public Tank(Player p) {
this(0, 0, p); super(100, p);
} }
public Tank(int x, int y, Player p){ public Tank(int x, int y, Player p){
super(100, new Vector2i(x,y), p); super(100, new Vector2i(x,y), p);
this.sprite = new Sprite("Tank", "data/units/tank/tank0000.png"); }
public void init(){
sprite = new Sprite("Tank", "data/units/tank/tank0000.png");
sprite.setSize(new Vector2f(40,40)); sprite.setSize(new Vector2f(40,40));
getNode().add(sprite); getNode().add(sprite);
setBuildTime(30); setBuildTime(300);
setPrice(400); setPrice(400);
gunSound = new Sound("gunSound", "data/sounds/tankweapon.wav"); gunSound = new Sound("gunSound", "data/sounds/tankweapon.wav");
moveSound = new Sound[2];
moveSound[0] = new Sound("moveSound", "data/sounds/TANKmove1.wav"); moveSound[0] = new Sound("moveSound", "data/sounds/TANKmove1.wav");
moveSound[1] = new Sound("moveSound", "data/sounds/TANKmove2.wav"); moveSound[1] = new Sound("moveSound", "data/sounds/TANKmove2.wav");
selectSound = new Sound("selectSound", "data/sounds/TANKselect.wav"); selectSound = new Sound("selectSound", "data/sounds/TANKselect.wav");
attackSound = new Sound("attackSound", "data/sounds/TANKattack.wav"); attackSound = new Sound("attackSound", "data/sounds/TANKattack.wav");
gunSound.setLocation(getNode().getLocation()); gunSound.setLocation(getNode().getLocation());
moveSound[0].setLocation(getNode().getLocation()); moveSound[0].setLocation(getNode().getLocation());
moveSound[1].setLocation(getNode().getLocation()); moveSound[1].setLocation(getNode().getLocation());
selectSound.setLocation(getNode().getLocation()); selectSound.setLocation(getNode().getLocation());
attackSound.setLocation(getNode().getLocation()); attackSound.setLocation(getNode().getLocation());
selectionBox = new SelectBox(40,40,getMaxLife()); selectionBox = new SelectBox(0,40,40,getMaxLife());
} }
protected SelectBox getSelection() { protected SelectBox getSelection() {

View file

@ -49,11 +49,23 @@ public abstract class Unit extends GameEntity{
unitNode = new Node("UnitNode"); unitNode = new Node("UnitNode");
unitNode.setLocation(Map.getPixelByPos(pos.getX(), pos.getY())); unitNode.setLocation(Map.getPixelByPos(pos.getX(), pos.getY()));
setPos(pos.getX(), pos.getY()); setPos(pos.getX(), pos.getY());
init();
} }
public void setSelected(boolean b) { /**
* Creates a empty unit
*
* @param l The max life of the unit
*/
public Unit(int l, Player p) {
super(l, p);
unitNode = new Node("UnitNode");
init();
}
public void setSelected(boolean b,boolean playSound) {
if(b) { if(b) {
getSelectSound().play(); if(playSound)getSelectSound().play();
unitNode.add(getSelection().getSelectNode()); unitNode.add(getSelection().getSelectNode());
} }
else{ else{
@ -93,6 +105,14 @@ public abstract class Unit extends GameEntity{
InGameState.getMap().setPos(this, x, y); InGameState.getMap().setPos(this, x, y);
} }
public void forcePos(int x, int y){
if(oldPos!=null) {
InGameState.getMap().removePos(oldPos.getX(), oldPos.getY());
}
oldPos = new Vector2i(x, y);
InGameState.getMap().setPos(this, x, y);
unitNode.setLocation(Map.getPixelByPos(x, y));
}
/** /**
* Moving a unit to the given pos * Moving a unit to the given pos
@ -164,6 +184,8 @@ public abstract class Unit extends GameEntity{
public abstract Sound getMoveSound(); public abstract Sound getMoveSound();
public abstract void init();
public void removeUnit(){ public void removeUnit(){
unitNode.remove(getSprite()); unitNode.remove(getSprite());
@ -204,8 +226,7 @@ public abstract class Unit extends GameEntity{
if(moveVect.getY() > oldVect.getY() && divideX==1) { if(moveVect.getY() > oldVect.getY() && divideX==1) {
getSprite().setRotation(new Vector3f(0, 0, 0)); getSprite().setRotation(new Vector3f(0, 0, 0));
} }
//Diagonally. //Diagonally.
if(moveVect.getX() > oldVect.getX() && moveVect.getY() > oldVect.getY() if(moveVect.getX() > oldVect.getX() && moveVect.getY() > oldVect.getY()
@ -298,4 +319,12 @@ public abstract class Unit extends GameEntity{
} }
} }
} }
public int getBuildTime(){
return buildTime;
}
public int getPrice(){
return price;
}
} }