public void checkDrag() {
    Land land = (Land) getWorld();
    if (Greenfoot.mouseDragged(this)) {
      MouseInfo mouse = Greenfoot.getMouseInfo();
      if (cost <= land.getMoneyAmount()) {
        if (hasTurret == false) {
          land.removeObjects(land.getObjects(Radius.class));

          machineGun = new MachineGun_Turret();
          getWorld().addObject(machineGun, mouse.getX(), mouse.getY());
          hasTurret = true;
          land.setMoneyCounter(-cost);
        }
      }
      if (hasTurret == true) machineGun.setLocation(mouse.getX(), mouse.getY());
    }
    if ((Greenfoot.mouseDragEnded(null)) && (machineGun != null)) {
      grid();
      if (machineGun.checkLocation()) {
        machineGun.placed = true;
        hasTurret = false;
        machineGun = null;
      } else {
        land.removeObject(machineGun);
        machineGun = null;
        land.setMoneyCounter(cost);
        hasTurret = false;
      }
    }
  }
Esempio n. 2
0
  public void act() {
    int mouseX, mouseY;

    if (Greenfoot.mouseDragged(this)) {
      MouseInfo mouse = Greenfoot.getMouseInfo();
      mouseX = mouse.getX();
      mouseY = mouse.getY();
      setLocation(mouseX, mouseY);
    }
  }
Esempio n. 3
0
 public void act() {
   if (Greenfoot.mouseDragged(this)) {
     if (!scaled) {
       board.setFront(this);
       scaled = true;
       scale();
     }
     MouseInfo info = Greenfoot.getMouseInfo();
     toSpace = board.getFeld(info.getY(), info.getX());
     if (toSpace != null) {
       setLocation(info.getX(), info.getY());
     }
   } else if (Greenfoot.mouseDragEnded(this)) {
     this.getImage().scale(origWidth, origHeight);
     scaled = false;
     if (isA && toSpace != null && toSpace.getReihe() == 1 && toSpace.getSpalte() == 'd') {
       board.setPlayerAChoice(staerke);
     } else if (!isA && toSpace != null && toSpace.getReihe() == 8 && toSpace.getSpalte() == 'd') {
       board.setPlayerBChoice(staerke);
     } else {
       setLocation(space.getWorldColumn(), space.getWorldRow());
     }
   }
 }
Esempio n. 4
0
  /**
   * Update the lastState property. Should ideally be called on each World.act(), or at least before
   * getState() in a frame where mouse info is wanted.
   */
  public void act() {
    MouseInfo mouseinfo = Greenfoot.getMouseInfo();

    /* To-be properties of MouseState */
    boolean held = lastState.held;
    boolean down = false;
    boolean up = false;
    boolean moved;
    Vector position;

    if (mouseinfo != null) {
      /* Get the new mouse position */
      position = new Vector(mouseinfo.getX(), mouseinfo.getY());
      /* We have moved if there is a difference between this mouse position and
       * the previous mouse position. */
      moved = lastState.position == null || position.subtract(lastState.position).length() > 0.5;

    } else {
      /* mouseinfo == null */

      /* Use the previous mouse position */
      position = lastState.position;
      /* We haven't moved the mouse pointer. */
      moved = false;
    }

    if (lastState.held) {
      /* Figure out if the mouse has been released. This has to be done
       * differently on Mac OS X, as MouseInfo contains different info in case
       * of mouse release on that platform. */
      if (System.getProperty("os.name").equals("Mac OS X")) {
        if (Greenfoot.mouseClicked(null)
            || mouseinfo != null
                && (mouseinfo.getActor() != concerning || mouseinfo.getButton() != 1)) {
          up = true;
          held = false;
        }
      } else {
        /* TODO: Check if this behaves correctly in Windows */
        if (Greenfoot.mouseClicked(concerning)
            || mouseinfo != null
                && (mouseinfo.getActor() != concerning || mouseinfo.getButton() != 0)) {
          up = true;
          held = false;
        }
      }

    } else {
      /* !lastState.held */

      /* Figure out if mouse has been pressed. This is thankfully
       * cross-platform consistent. */
      if (Greenfoot.mousePressed(concerning)) {
        down = true;
        held = true;
      }
    }

    /* Store the state in the lastState property. */
    lastState = new MouseState(held, down, up, moved, position);
  }