コード例 #1
0
ファイル: GameScene.java プロジェクト: houledm/TowerDefense
  /**
   * When placing a tower on the field, this checks if the path needs to be updated for each enemy
   *
   * @param current
   * @param inMiddleOfWave
   */
  private void updateAffectedEnemies(TMXTile current, boolean inMiddleOfWave) {
    List<Enemy> enemies = currentWave.getEnemies();
    Path p;
    float pX;
    float pY;
    outer:
    for (Enemy enemy : enemies) {
      if (enemy == null) continue;
      p = enemy.getPath();
      for (int i = 0; i < p.getCoordinatesX().length; i++) {
        pX = p.getCoordinatesX()[i];
        pY = p.getCoordinatesY()[i];

        TMXTile tile = this.tmxLayer.getTMXTileAt(pX - enemy.getOffsetX(), pY - enemy.getOffsetY());
        if (current.equals(tile)) {
          updateEnemyPaths(inMiddleOfWave, enemy);
          if (!inMiddleOfWave) break outer;
          else break;
        }
      }
    }

    if (inMiddleOfWave) {
      Enemy dummy = new Enemy();
      dummy.setUserData("dummy");
      Path path = aStarHelper.getPath(dummy);

      if (path == null) {
        removeCurrentTower(true);
      }
    }
  }
コード例 #2
0
ファイル: GameScene.java プロジェクト: houledm/TowerDefense
  /** TODO So ugly :( Maybe make it its own class? */
  @Override
  public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
    final Float x = pSceneTouchEvent.getX();
    final Float y = pSceneTouchEvent.getY();

    currentTile = this.tmxLayer.getTMXTileAt(x, y);

    // if the user pinches or dragtouches the screen then...
    if (this.mPinchZoomDetector != null) {

      this.mPinchZoomDetector.onTouchEvent(pSceneTouchEvent);
      if (this.mPinchZoomDetector.isZooming()) {
        this.mScrollDetector.setEnabled(false);
      } else if (!towerMove) {
        if (pSceneTouchEvent.isActionDown()) {
          this.mScrollDetector.setEnabled(true);
        }
        this.mScrollDetector.onTouchEvent(pSceneTouchEvent);
      }

    } else {
      this.mScrollDetector.onTouchEvent(pSceneTouchEvent);
    }

    if (pSceneTouchEvent.isActionDown()) {
      downCoords.set(x, y);

      SubMenuManager.setReticalPosition(-500.0f, -500.0f);

      Log.i("Detaching now", "NOW");
      SubMenuManager.remove();
      this.unregisterTouchArea(SubMenuManager.getDeleteSprite());

      panel.detachTowerUpgradeDeleteText();
      panel.detachTowerTextDescription();
      panel.attachTowerTextDescription(pointOnTile(TouchEvent.ACTION_DOWN));
    }

    Class<?> tClass;
    if (pSceneTouchEvent.isActionMove()) {

      tClass = pointOnTile(TouchEvent.ACTION_MOVE);
      if (tClass != null && !towerMove) {
        dragTower = null;
        towerMove = true;
        if (tClass.equals(TurretTower.class) && canAfford(TurretTower.COST)) {
          dragTower = new TurretTower(x, y, resourceManager.getTurretTowerRegion());
        } else if (tClass.equals(DartTower.class) && canAfford(DartTower.COST)) {
          dragTower = new DartTower(x, y, resourceManager.getDartTowerRegion());
        } else if (tClass.equals(FlameTower.class) && canAfford(FlameTower.COST)) {
          dragTower = new FlameTower(x, y, resourceManager.getFlameTowerRegion());
        } else if (tClass.equals(IceTower.class) && canAfford(IceTower.COST)) {
          dragTower = new IceTower(x, y, resourceManager.getIceTowerRegion());
        } else if (tClass.equals(SpikeTower.class) && canAfford(SpikeTower.COST)) {
          dragTower = new SpikeTower(x, y, resourceManager.getSpikeTowerRegion());
        } else towerMove = false;

        if (dragTower != null) {
          dragTower.getEntity().setZIndex(2);
          dragTower.getEntity().setScale(0.5f);

          activity.runOnUpdateThread(
              new Runnable() {
                @Override
                public void run() {
                  attachChild(dragTower.getEntity());

                  try {
                    attachChild(SubMenuManager.getReticle(dragTower));
                  } catch (Exception e) {
                  }
                  sortChildren();
                }
              });
          tClass = null;
        }

      }

      // Moving an active drag tower
      else if (towerMove) {
        if (pointOnMap(x, y)) {

          dragTower.setPosition(
              currentTile.getTileX() - GameMap.getTileSize() / 2,
              currentTile.getTileY() - GameMap.getTileSize() / 2);

          if (highlightTile == null) {
            highlightTile =
                new Rectangle(
                    currentTile.getTileX(),
                    currentTile.getTileY(),
                    GameMap.getTileSize(),
                    GameMap.getTileSize(),
                    activity.getVertexBufferObjectManager());
            highlightTile.setTag(777);
            highlightTile.setZIndex(1);
            this.attachChild(highlightTile);
            this.sortChildren();
          } else {
            highlightTile.setPosition(currentTile.getTileX(), currentTile.getTileY());
          }

          if (!inLegitimatePosition(currentTile)) {
            highlightTile.setColor(Color.RED);
          } else {
            highlightTile.setColor(Color.BLUE);
          }

          // if you drag the dragtower off the map, and then back on, we need to be able to tag it
          // so we can display the highlight tile again
          if (this.getChildByTag(777) == null) {
            this.attachChild(highlightTile);
            this.sortChildren();
          }
        }
        // if point NOT on map
        else {
          if (highlightTile != null) {
            detachHighlightTile();
          }
          dragTower.setPosition(
              pSceneTouchEvent.getX() - dragTower.getEntity().getWidth() / 2,
              pSceneTouchEvent.getY() - dragTower.getEntity().getHeight() / 2);
        }
      }
    } else if (pSceneTouchEvent.isActionUp()) {

      if (zooming) {
        fingerOnSceneCount--;

        if (fingerOnSceneCount == 0) {
          zooming = false;
          setUserDataforTowerTiles("");

          for (TowerTile tile : panel.getTiles()) {
            tile.returnOnMoved();
          }
        }
      }

      if (towerMove) {
        towerMove = false;

        if (currentTile != null
            && highlightTile != null
            && highlightTile.getColor().equals(Color.BLUE)) {

          // Add the tile to the blocked list
          blockedTileList.add(currentTile);

          towers.add(dragTower);
          SubMenuManager.getReticle(dragTower).detachSelf();

          // need to get it out of the scene so that the next dragtower doesn't have to start with
          // it from where the
          // previous tower was placed
          SubMenuManager.setReticalPosition(-500.0f, -500.0f);

          // Nothing is free in this world
          this.payAmount(dragTower.getCost());

          // If we are in the middle of a wave, the AStarPath class must update
          // the path since there is now a new tower on the field
          updateAffectedEnemies(currentTile, aStarHelper.isNavigating());
        } else {
          removeCurrentTower(false);
        }

        if (highlightTile != null) {
          detachHighlightTile();
        }
      } else if (Math.abs(downCoords.x - x) < 15.0f && Math.abs(downCoords.y - y) < 15.0f) {

        final ITower tower = pointOnExistingTower(x, y);
        if (tower != null) {
          this.attachChild(SubMenuManager.display(tower));
          Log.i("Reticle", "Visible? " + SubMenuManager.getReticle(tower).isVisible() + "");
          panel.attachTowerUpgradeDeleteText(tower);

          if (camera.getZoomFactor() - 1.0f < 0.00005f && camera.getYMin() < 0.00005f) {

            final float displacement = tower.getRadius() - tower.getEntity().getHeightScaled() / 2;
            if (tower.getY() == -20.0f) {
              camera.set(
                  camera.getXMin(),
                  camera.getYMin() - displacement,
                  camera.getXMax(),
                  camera.getYMax() - displacement);
            } else if (tower.getY() == 340.0f) {
              camera.set(
                  camera.getXMin(),
                  camera.getYMin() + displacement,
                  camera.getXMax(),
                  camera.getYMax() + displacement);
            }
          }
        }
      }
    }
    return true;
  }
コード例 #3
0
ファイル: GameScene.java プロジェクト: houledm/TowerDefense
 /**
  * This just makes sure the drag tower isn't set on the start tile, end tile, or any tile already
  * occupied by another tower. There is still the fact that a player can place a tower that will
  * block the enemies in.
  *
  * <p>I am taking a reactive approach to this scenario since it is just too costly to recalculate
  * the enemy paths for each tile the dragtower is placed.
  *
  * @param currentTile
  * @return
  */
 private boolean inLegitimatePosition(TMXTile currentTile) {
   return !(currentTile.equals(endTile)
       || currentTile.equals(startTile)
       || blockedTileList.contains(currentTile));
 }