package ei.game.input; import java.util.ArrayList; import ei.engine.LWJGLGameWindow; import ei.engine.input.MouseInput; import ei.engine.math.Vector2f; import ei.engine.math.Vector2i; 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.player.Player; import ei.game.scene.GameEntity; import ei.game.scene.Map; public class InGameMouseInput extends MouseInput{ private static final int CAMERA_MOVE_BORDER = 40; private static final float CAMERA_MOVE_SPEED = 6.0f; private ArrayList selected; private GameEntity oldMouseOver; private Vector2f leftKlickPos; private Box markingBox; private InGameHud hud; private Map map; private Player player; public InGameMouseInput(Map map, Player p) { super("InGameMouseInput","data/cursor/cursor.png"); this.map = map; this.player = p; this.selected = new ArrayList(); //inits the mouse texture Sprite s = getSprite(); s.setSize(new Vector2f(38,50)); Texture tex = new Texture(); tex.setColor(new Vector4f(0.5f, 1.0f, 0.5f,1)); markingBox = new Box("MarkingBob", tex); } @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 if(x < CAMERA_MOVE_BORDER){ LWJGLGameWindow.getCamera().getLocation().add(-CAMERA_MOVE_SPEED,0,0); } // mov cam to the right if(x > LWJGLGameWindow.getWidth()-CAMERA_MOVE_BORDER){ LWJGLGameWindow.getCamera().getLocation().add(CAMERA_MOVE_SPEED,0,0); } // mov cam upp if(y < CAMERA_MOVE_BORDER){ LWJGLGameWindow.getCamera().getLocation().add(0,-CAMERA_MOVE_SPEED,0); } // mov cam down if(y > LWJGLGameWindow.getHeight()-10){ LWJGLGameWindow.getCamera().getLocation().add(0,CAMERA_MOVE_SPEED,0); } Vector2i pos = Map.getPosByPixel( LWJGLGameWindow.getCamera().getLocation().getX()+x+Map.POS_SIZE/2, LWJGLGameWindow.getCamera().getLocation().getY()+y+Map.POS_SIZE/2); //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; float markingSizeY = 0; if(leftKlickPos.getX() > (LWJGLGameWindow.getCamera().getLocation().getX()+x)){ markingSizeX = Math.abs(leftKlickPos.getX()-(LWJGLGameWindow.getCamera().getLocation().getX()+x)); } else{ markingSizeX = -Math.abs(leftKlickPos.getX()-(LWJGLGameWindow.getCamera().getLocation().getX()+x)); } if(leftKlickPos.getY() > (LWJGLGameWindow.getCamera().getLocation().getY()+y)){ markingSizeY = Math.abs(leftKlickPos.getY()-(LWJGLGameWindow.getCamera().getLocation().getY()+y)); } else{ markingSizeY = -Math.abs(leftKlickPos.getY()-(LWJGLGameWindow.getCamera().getLocation().getY()+y)); } markingBox.setSize(new Vector2f(markingSizeX,markingSizeY)); markingBox.setLocation(new Vector2f( leftKlickPos.getX()-markingBox.getSize().getX()/2, leftKlickPos.getY()-markingBox.getSize().getY()/2)); } // Make unit mouseover select if(!map.isPosEmpty(pos.getX(), pos.getY())){ if(oldMouseOver != map.getPos(pos.getX(), pos.getY())){ if(oldMouseOver != null)oldMouseOver.setMouseOver(player,false); oldMouseOver = map.getPos(pos.getX(), pos.getY()); oldMouseOver.setMouseOver(player,true); } } else{ if(oldMouseOver != null){ oldMouseOver.setMouseOver(player,false); oldMouseOver = null; } } } } @Override public void mouseDown(int event,int x, int y) { Vector2i pos = Map.getPosByPixel( LWJGLGameWindow.getCamera().getLocation().getX()+x+Map.POS_SIZE/2, LWJGLGameWindow.getCamera().getLocation().getY()+y+Map.POS_SIZE/2); removeDead(); //map.printAllUnits(); //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){ leftKlickPos = new Vector2f( LWJGLGameWindow.getCamera().getLocation().getX()+x, LWJGLGameWindow.getCamera().getLocation().getY()+y); markingBox.setSize(new Vector2f(0,0)); getNode().add(markingBox); } } //unit action. else if(event==RIGHT_MOUSE_BUTTON) { if(!map.isPosEmpty(pos.getX(), pos.getY())){ int rand = (int)(Math.random()*selected.size()); for(int i=0; i