Added ui to the engine and buttons and implemented them in the game fixed also a mouse position problem
This commit is contained in:
parent
515281351c
commit
728a68cc48
35 changed files with 436 additions and 23 deletions
|
|
@ -47,11 +47,13 @@ public class InGameState extends GameState{
|
|||
rootNode.add(map.getMapNode());
|
||||
rootNode.add(PlayerHandler.getInstance().getNode());
|
||||
rootNode.add(WeaponHandler.getInstance().getNode());
|
||||
music = new Sound("music", "data/sounds/ei.ogg");
|
||||
music.loop();
|
||||
|
||||
hud = new InGameHud(player);
|
||||
mouse.setHud(hud);
|
||||
rootNode.add(hud.getNode());
|
||||
|
||||
music = new Sound("music", "data/sounds/ei.ogg");
|
||||
music.loop();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
77
src/ei/game/hud/InGameBuildHud.java
Normal file
77
src/ei/game/hud/InGameBuildHud.java
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
package ei.game.hud;
|
||||
|
||||
import ei.engine.math.Vector2f;
|
||||
import ei.engine.scene.Node;
|
||||
import ei.engine.scene.Sprite;
|
||||
import ei.engine.ui.Button;
|
||||
import ei.engine.ui.UiComponent;
|
||||
import ei.engine.ui.UiHandler;
|
||||
import ei.engine.ui.UiListener;
|
||||
|
||||
/**
|
||||
* This class handels the build buttons for the units
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public class InGameBuildHud {
|
||||
private UiHandler ui;
|
||||
|
||||
public InGameBuildHud(int x ,int y){
|
||||
ui = new UiHandler("BuildMenu");
|
||||
|
||||
Vector2f size = new Vector2f(40,40);
|
||||
|
||||
Button apu = new Button("TankButton", size);
|
||||
Sprite s1 = new Sprite("ApuButton","data/ui/apu_button.png");
|
||||
s1.setSize(size);
|
||||
apu.setButtonSprite(s1);
|
||||
Sprite s2 = new Sprite("ApuButton_Selected","data/ui/apu_button_selected.png");
|
||||
s2.setSize(size);
|
||||
apu.setOnTopButtonSprite(s2);
|
||||
apu.getNode().setLocation(new Vector2f(x,y));
|
||||
apu.addListener(new UiListener(){
|
||||
public void ActionEvent(UiComponent source){
|
||||
System.out.println("apu");
|
||||
}
|
||||
});
|
||||
ui.addUi(apu);
|
||||
|
||||
Button tank = new Button("TankButton", size);
|
||||
Sprite s3 = new Sprite("TankButton","data/ui/tank_button.png");
|
||||
s3.setSize(size);
|
||||
tank.setButtonSprite(s3);
|
||||
Sprite s4 = new Sprite("TankButton_Selected","data/ui/tank_button_selected.png");
|
||||
s4.setSize(size);
|
||||
tank.setOnTopButtonSprite(s4);
|
||||
tank.getNode().setLocation(new Vector2f(x+(size.getX()*2),y));
|
||||
tank.addListener(new UiListener(){
|
||||
public void ActionEvent(UiComponent source){
|
||||
System.out.println("tank");
|
||||
}
|
||||
});
|
||||
ui.addUi(tank);
|
||||
|
||||
Button bomber = new Button("TankButton", size);
|
||||
Sprite s5 = new Sprite("BomberButton","data/ui/bomber_button.png");
|
||||
s5.setSize(size);
|
||||
bomber.setButtonSprite(s5);
|
||||
Sprite s6 = new Sprite("BomberButton_Selected","data/ui/bomber_button_selected.png");
|
||||
s6.setSize(size);
|
||||
bomber.setOnTopButtonSprite(s6);
|
||||
bomber.getNode().setLocation(new Vector2f(x+(size.getX()*4),y));
|
||||
bomber.addListener(new UiListener(){
|
||||
public void ActionEvent(UiComponent source){
|
||||
System.out.println("bomber");
|
||||
}
|
||||
});
|
||||
ui.addUi(bomber);
|
||||
}
|
||||
|
||||
public UiHandler getUi(){
|
||||
return ui;
|
||||
}
|
||||
|
||||
public Node getNode(){
|
||||
return ui.getNode();
|
||||
}
|
||||
}
|
||||
|
|
@ -9,11 +9,18 @@ import ei.engine.scene.Node;
|
|||
import ei.engine.scene.Sprite;
|
||||
import ei.game.player.Player;
|
||||
|
||||
/**
|
||||
* This clas handles the hud of the game
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public class InGameHud {
|
||||
private Node hudNode;
|
||||
private BitmapText money;
|
||||
private Player player;
|
||||
private ProgressBar buildBar;
|
||||
private InGameBuildHud buildHud;
|
||||
private Sprite buildBack;
|
||||
|
||||
public InGameHud(Player p){
|
||||
player = p;
|
||||
|
|
@ -30,7 +37,7 @@ public class InGameHud {
|
|||
money.setLocation(new Vector2f(LWJGLGameWindow.getWidth()-money.getBound().width,5));
|
||||
hudNode.add(money);
|
||||
|
||||
Sprite buildBack = new Sprite("BuildBackground","data/hud/buildmenu.png");
|
||||
buildBack = new Sprite("BuildBackground","data/hud/buildmenu.png");
|
||||
buildBack.setLocation(new Vector2f(
|
||||
LWJGLGameWindow.getWidth()/2,
|
||||
LWJGLGameWindow.getHeight()-buildBack.getSize().getY()/2));
|
||||
|
|
@ -43,6 +50,10 @@ public class InGameHud {
|
|||
LWJGLGameWindow.getWidth()/2+50,
|
||||
LWJGLGameWindow.getHeight()-55));
|
||||
hudNode.add(buildBar.getNode());
|
||||
|
||||
buildHud = new InGameBuildHud((int)(buildBack.getLocation().getX()-buildBack.getSize().getX()/4),
|
||||
(int)(buildBack.getLocation().getY()+10));
|
||||
hudNode.add(buildHud.getNode());
|
||||
}
|
||||
|
||||
public void update(){
|
||||
|
|
@ -59,4 +70,12 @@ public class InGameHud {
|
|||
public Node getNode(){
|
||||
return hudNode;
|
||||
}
|
||||
|
||||
public InGameBuildHud getBuildHud(){
|
||||
return buildHud;
|
||||
}
|
||||
|
||||
public Sprite getBuildBar(){
|
||||
return buildBack;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import ei.engine.math.Vector4f;
|
|||
import ei.engine.scene.Box;
|
||||
import ei.engine.scene.Sprite;
|
||||
import ei.engine.texture.Texture;
|
||||
import ei.game.hud.InGameHud;
|
||||
import ei.game.scene.GameEntity;
|
||||
import ei.game.scene.Map;
|
||||
|
||||
|
|
@ -22,6 +23,7 @@ public class InGameMouseInput extends MouseInput{
|
|||
private Vector2f leftKlickPos;
|
||||
private Box markingBox;
|
||||
|
||||
private InGameHud hud;
|
||||
private Map map;
|
||||
|
||||
public InGameMouseInput(Map map) {
|
||||
|
|
@ -40,6 +42,7 @@ public class InGameMouseInput extends MouseInput{
|
|||
|
||||
@Override
|
||||
public void mouseUpdate(int x, int y, int w) {
|
||||
if(hud != null)hud.getBuildHud().getUi().mousePos(x, y);
|
||||
removeDead();
|
||||
|
||||
// mov cam to the left
|
||||
|
|
@ -55,15 +58,21 @@ public class InGameMouseInput extends MouseInput{
|
|||
LWJGLGameWindow.getCamera().getLocation().add(0,-CAMERA_MOVE_SPEED,0);
|
||||
}
|
||||
// mov cam down
|
||||
if(y > LWJGLGameWindow.getHeight()-CAMERA_MOVE_BORDER){
|
||||
if(y > LWJGLGameWindow.getHeight()-10){
|
||||
LWJGLGameWindow.getCamera().getLocation().add(0,CAMERA_MOVE_SPEED,0);
|
||||
}
|
||||
|
||||
Vector2i pos = Map.getPosByPixel(
|
||||
LWJGLGameWindow.getCamera().getLocation().getX()+x,
|
||||
LWJGLGameWindow.getCamera().getLocation().getY()+y);
|
||||
LWJGLGameWindow.getCamera().getLocation().getX()+x+Map.POS_SIZE/2,
|
||||
LWJGLGameWindow.getCamera().getLocation().getY()+y+Map.POS_SIZE/2);
|
||||
|
||||
if(map.posExist(pos.getX(), pos.getY())){
|
||||
//checks if the mous is on the buildbar
|
||||
if(x >= (LWJGLGameWindow.getWidth()/2)-hud.getBuildBar().getSize().getX()/2 &&
|
||||
x <= (LWJGLGameWindow.getWidth()/2)+hud.getBuildBar().getSize().getX()/2 &&
|
||||
y >= LWJGLGameWindow.getHeight()-hud.getBuildBar().getSize().getY() &&
|
||||
y <= LWJGLGameWindow.getHeight()){}
|
||||
// checks wich position the mouse is on top of
|
||||
else if(map.posExist(pos.getX(), pos.getY())){
|
||||
// The marking box is sized and positioned
|
||||
if(leftKlickPos != null){
|
||||
float markingSizeX = 0;
|
||||
|
|
@ -83,7 +92,7 @@ public class InGameMouseInput extends MouseInput{
|
|||
markingSizeY = -Math.abs(leftKlickPos.getY()-(LWJGLGameWindow.getCamera().getLocation().getY()+y));
|
||||
}
|
||||
|
||||
markingBox.setSize(new Vector2f(markingSizeX+getSprite().getSize().getX()/2,markingSizeY+getSprite().getSize().getY()/2));
|
||||
markingBox.setSize(new Vector2f(markingSizeX,markingSizeY));
|
||||
markingBox.setLocation(new Vector2f(
|
||||
leftKlickPos.getX()-markingBox.getSize().getX()/2,
|
||||
leftKlickPos.getY()-markingBox.getSize().getY()/2));
|
||||
|
|
@ -109,11 +118,17 @@ public class InGameMouseInput extends MouseInput{
|
|||
@Override
|
||||
public void mouseDown(int event,int x, int y) {
|
||||
Vector2i pos = Map.getPosByPixel(
|
||||
LWJGLGameWindow.getCamera().getLocation().getX()+x,
|
||||
LWJGLGameWindow.getCamera().getLocation().getY()+y);
|
||||
LWJGLGameWindow.getCamera().getLocation().getX()+x+Map.POS_SIZE/2,
|
||||
LWJGLGameWindow.getCamera().getLocation().getY()+y+Map.POS_SIZE/2);
|
||||
removeDead();
|
||||
//map.printAllUnits();
|
||||
if(map.posExist(pos.getX(), pos.getY())){
|
||||
//checks if the mous is on the buildbar
|
||||
if(x >= (LWJGLGameWindow.getWidth()/2)-hud.getBuildBar().getSize().getX()/2 &&
|
||||
x <= (LWJGLGameWindow.getWidth()/2)+hud.getBuildBar().getSize().getX()/2 &&
|
||||
y >= LWJGLGameWindow.getHeight()-hud.getBuildBar().getSize().getY() &&
|
||||
y <= LWJGLGameWindow.getHeight()){}
|
||||
// checks wich position the mouse presed
|
||||
else if(map.posExist(pos.getX(), pos.getY())){
|
||||
//selecting unit.
|
||||
if(event==LEFT_MOUSE_BUTTON){
|
||||
if(leftKlickPos == null){
|
||||
|
|
@ -159,13 +174,14 @@ public class InGameMouseInput extends MouseInput{
|
|||
@Override
|
||||
public void mouseUp(int event,int x, int y) {
|
||||
Vector2i pos = Map.getPosByPixel(
|
||||
LWJGLGameWindow.getCamera().getLocation().getX()+x,
|
||||
LWJGLGameWindow.getCamera().getLocation().getY()+y);
|
||||
LWJGLGameWindow.getCamera().getLocation().getX()+x+Map.POS_SIZE/2,
|
||||
LWJGLGameWindow.getCamera().getLocation().getY()+y+Map.POS_SIZE/2);
|
||||
if(hud != null)hud.getBuildHud().getUi().mouseDown(x, y, event);
|
||||
removeDead();
|
||||
if(map.posExist(pos.getX(), pos.getY())){
|
||||
if(leftKlickPos != null){
|
||||
deselectAllUnits();
|
||||
selectUnits(Map.getPosByPixel(leftKlickPos.getX(), leftKlickPos.getY()),pos);
|
||||
selectUnits(Map.getPosByPixel((leftKlickPos.getX()+Map.POS_SIZE/2), (leftKlickPos.getY())+Map.POS_SIZE/2),pos);
|
||||
leftKlickPos = null;
|
||||
getNode().remove(markingBox);
|
||||
}
|
||||
|
|
@ -207,5 +223,9 @@ public class InGameMouseInput extends MouseInput{
|
|||
}
|
||||
selected.clear();
|
||||
}
|
||||
|
||||
public void setHud(InGameHud u){
|
||||
hud = u;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ public class Map {
|
|||
|
||||
public static final int OBJ_STONE = 1;
|
||||
|
||||
private static final int POS_SIZE = 50;
|
||||
public static final int POS_SIZE = 50;
|
||||
private int width;
|
||||
private int hight;
|
||||
private GameEntity[][] map;
|
||||
|
|
|
|||
|
|
@ -30,7 +30,6 @@ public abstract class Building extends GameEntity{
|
|||
buildQueue = new LinkedList<Unit>();
|
||||
unitNode = new Node("UnitNode");
|
||||
unitNode.setLocation(Map.getPixelByPos(pos.getX(), pos.getY(), this.size));
|
||||
System.out.println("location: "+unitNode.getLocation());
|
||||
setPos(pos.getX(), pos.getY(), this.size);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ public class CommandCenter extends Building{
|
|||
|
||||
public CommandCenter(int x, int y, Player p){
|
||||
super(1000, new Vector2i(x,y), p, 4);
|
||||
this.sprite = new Sprite("APU", "data/buildings/commandcenter.png");
|
||||
this.sprite = new Sprite("APU", "data/buildings/cc/cc.png");
|
||||
sprite.setSize(new Vector2f(200,200));
|
||||
getNode().add(sprite);
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import ei.engine.math.Vector2f;
|
|||
import ei.engine.math.Vector2i;
|
||||
import ei.engine.scene.Sprite;
|
||||
import ei.engine.sound.Sound;
|
||||
import ei.engine.texture.AnimatedTexture;
|
||||
import ei.game.player.Player;
|
||||
import ei.game.scene.SelectBox;
|
||||
import ei.game.scene.weapons.BomberWeapon;
|
||||
|
|
@ -24,7 +25,15 @@ public class Bomber extends Unit{
|
|||
|
||||
public Bomber(int x, int y, Player p){
|
||||
super(200, new Vector2i(x,y), p);
|
||||
this.sprite = new Sprite("Bomber", "data/units/bomber/bomber0000.png");
|
||||
AnimatedTexture tex = new AnimatedTexture("BomberTexture");
|
||||
tex.addAnimation("normal", new String[]{
|
||||
"data/units/bomber/bomber0000.png",
|
||||
"data/units/bomber/bomber0001.png",
|
||||
"data/units/bomber/bomber0002.png",
|
||||
"data/units/bomber/bomber0003.png"
|
||||
});
|
||||
tex.setDelay(5);
|
||||
sprite = new Sprite("Bomber",tex);
|
||||
sprite.setSize(new Vector2f(50,60));
|
||||
getNode().add(sprite);
|
||||
setBuildTime(40);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue