Added mouse over
This commit is contained in:
parent
22965dbdd1
commit
c6ba0ea532
6 changed files with 129 additions and 87 deletions
|
|
@ -16,27 +16,28 @@ 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<GameEntity> selected;
|
||||
private GameEntity oldMouseOver;
|
||||
private Vector2f leftKlickPos;
|
||||
private Box markingBox;
|
||||
|
||||
|
||||
private Map map;
|
||||
|
||||
|
||||
public InGameMouseInput(Map map) {
|
||||
super("InGameMouseInput","data/cursor/cursor.png");
|
||||
this.map = map;
|
||||
this.selected = new ArrayList<GameEntity>();
|
||||
|
||||
|
||||
//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) {
|
||||
// mov cam to the left
|
||||
|
|
@ -55,61 +56,75 @@ public class InGameMouseInput extends MouseInput{
|
|||
if(y > LWJGLGameWindow.getHeight()-CAMERA_MOVE_BORDER){
|
||||
LWJGLGameWindow.getCamera().getLocation().add(0,CAMERA_MOVE_SPEED,0);
|
||||
}
|
||||
|
||||
// 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));
|
||||
|
||||
Vector2i pos = Map.getPosByPixel(
|
||||
LWJGLGameWindow.getCamera().getLocation().getX()+x,
|
||||
LWJGLGameWindow.getCamera().getLocation().getY()+y);
|
||||
|
||||
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+getSprite().getSize().getX()/2,markingSizeY+getSprite().getSize().getY()/2));
|
||||
markingBox.setLocation(new Vector2f(
|
||||
leftKlickPos.getX()-markingBox.getSize().getX()/2,
|
||||
leftKlickPos.getY()-markingBox.getSize().getY()/2));
|
||||
}
|
||||
else{
|
||||
markingSizeX = -Math.abs(leftKlickPos.getX()-(LWJGLGameWindow.getCamera().getLocation().getX()+x));
|
||||
|
||||
// Make unit mouseover select
|
||||
if(!map.isPosEmpty(pos.getX(), pos.getY())){
|
||||
if(oldMouseOver != null)oldMouseOver.setMouseOver(false);
|
||||
oldMouseOver = map.getPos(pos.getX(), pos.getY());
|
||||
oldMouseOver.setMouseOver(true);
|
||||
}
|
||||
|
||||
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+getSprite().getSize().getX()/2,markingSizeY+getSprite().getSize().getY()/2));
|
||||
markingBox.setLocation(new Vector2f(
|
||||
leftKlickPos.getX()-markingBox.getSize().getX()/2,
|
||||
leftKlickPos.getY()-markingBox.getSize().getY()/2));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void mouseDown(int event,int x, int y) {
|
||||
Vector2i pos = Map.getPosByPixel(
|
||||
LWJGLGameWindow.getCamera().getLocation().getX()+x,
|
||||
LWJGLGameWindow.getCamera().getLocation().getY()+y);
|
||||
|
||||
//selecting unit.
|
||||
if(event==LEFT_MOUSE_BUTTON){
|
||||
//map.printAllUnits();
|
||||
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())){
|
||||
for(int i=0; i<selected.size(); i++) {
|
||||
selected.get(i).attack(new Vector2i(pos.getX(), pos.getY()));
|
||||
if(map.posExist(pos.getX(), pos.getY())){
|
||||
//selecting unit.
|
||||
if(event==LEFT_MOUSE_BUTTON){
|
||||
//map.printAllUnits();
|
||||
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);
|
||||
}
|
||||
}
|
||||
else{
|
||||
//MultiPrintStream.out.println("Moving: "+pos.getX()+", "+pos.getY());
|
||||
for(int i=0; i<selected.size() ;i++){
|
||||
selected.get(i).move(pos.getX(),pos.getY());
|
||||
//unit action.
|
||||
else if(event==RIGHT_MOUSE_BUTTON) {
|
||||
if(!map.isPosEmpty(pos.getX(), pos.getY())){
|
||||
for(int i=0; i<selected.size(); i++) {
|
||||
selected.get(i).attack(new Vector2i(pos.getX(), pos.getY()));
|
||||
}
|
||||
}
|
||||
else{
|
||||
//MultiPrintStream.out.println("Moving: "+pos.getX()+", "+pos.getY());
|
||||
for(int i=0; i<selected.size() ;i++){
|
||||
selected.get(i).move(pos.getX(),pos.getY());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -120,15 +135,17 @@ public class InGameMouseInput extends MouseInput{
|
|||
Vector2i pos = Map.getPosByPixel(
|
||||
LWJGLGameWindow.getCamera().getLocation().getX()+x,
|
||||
LWJGLGameWindow.getCamera().getLocation().getY()+y);
|
||||
|
||||
if(leftKlickPos != null){
|
||||
deselectAllUnits();
|
||||
selectUnits(Map.getPosByPixel(leftKlickPos.getX(), leftKlickPos.getY()),pos);
|
||||
leftKlickPos = null;
|
||||
getNode().remove(markingBox);
|
||||
|
||||
if(map.posExist(pos.getX(), pos.getY())){
|
||||
if(leftKlickPos != null){
|
||||
deselectAllUnits();
|
||||
selectUnits(Map.getPosByPixel(leftKlickPos.getX(), leftKlickPos.getY()),pos);
|
||||
leftKlickPos = null;
|
||||
getNode().remove(markingBox);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Selecting all the units between the two positions
|
||||
*
|
||||
|
|
@ -136,16 +153,16 @@ public class InGameMouseInput extends MouseInput{
|
|||
* @param stopPos The stop position
|
||||
*/
|
||||
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 j=Math.min(startPos.getY(), stopPos.getY()); j<=Math.max(startPos.getY(), stopPos.getY()) ;j++){
|
||||
if(!map.isPosEmpty(i, j)){
|
||||
selected.add(map.getPos(i, j));
|
||||
map.getPos(i, j).setSelected(true);
|
||||
}
|
||||
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++){
|
||||
if(!map.isPosEmpty(i, j)){
|
||||
selected.add(map.getPos(i, j));
|
||||
map.getPos(i, j).setSelected(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deselects all the units
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue