Example #1
0
 public static void initializeMap() {
   ResourceManager resourceManager = ResourceManager.getInstance();
   HashMap<BaseTextureRegion, Class<?>> aMap = new HashMap<BaseTextureRegion, Class<?>>();
   aMap.put(resourceManager.getTurretTowerRegion(), TurretTower.class);
   aMap.put(resourceManager.getDartTowerRegion(), DartTower.class);
   aMap.put(resourceManager.getFlameTowerRegion(), FlameTower.class);
   aMap.put(resourceManager.getIceTowerRegion(), IceTower.class);
   aMap.put(resourceManager.getSpikeTowerRegion(), SpikeTower.class);
   towerMap = aMap;
 }
Example #2
0
  /** 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;
  }
Example #3
0
  // ***********************************************************
  // CONSTRUCTOR
  // ***********************************************************
  public GameScene(MapType type) {
    instance = this;

    gameMap = new GameMap(type);

    // Zoom-Camera configuration
    this.setOnAreaTouchTraversalFrontToBack();
    this.mScrollDetector = new SurfaceScrollDetector(this);

    activity = TowerDefenseActivity.getSharedInstance();
    resourceManager = ResourceManager.getInstance();

    if (MultiTouch.isSupported(activity)) {
      this.mPinchZoomDetector = new PinchZoomDetector(this);
    } else {
      this.mPinchZoomDetector = null;
    }

    this.setOnSceneTouchListener(this);
    this.setOnSceneTouchListenerBindingOnActionDownEnabled(true);

    fingerOnSceneCount = 0;
    zooming = false;

    String map = "";

    if (type == MapType.DESERT) map = "tmx/new_desert_path.tmx";
    else if (type == MapType.GRASS) map = "tmx/grass_path.tmx";
    else if (type == MapType.TUNDRA) map = "tmx/tundra_path.tmx";

    try {
      final TMXLoader tmxLoader =
          new TMXLoader(
              activity.getAssets(),
              activity.getEngine().getTextureManager(),
              TextureOptions.BILINEAR_PREMULTIPLYALPHA,
              activity.getVertexBufferObjectManager(),
              new ITMXTilePropertiesListener() {
                @Override
                public void onTMXTileWithPropertiesCreated(
                    final TMXTiledMap pTMXTiledMap,
                    final TMXLayer pTMXLayer,
                    final TMXTile pTMXTile,
                    final TMXProperties<TMXTileProperty> pTMXTileProperties) {}
              });
      this.mTMXTiledMap = tmxLoader.loadFromAsset(map);
    } catch (final TMXLoadException e) {
      Debug.e(e);
    }

    gameMap.setMap(mTMXTiledMap);

    tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
    tmxLayer.setIgnoreUpdate(true);
    this.attachChild(tmxLayer);

    mCamera = activity.getCamera();
    this.mCamera.setBounds(
        0, (mCamera.getHeight() - tmxLayer.getHeight()), tmxLayer.getWidth(), tmxLayer.getHeight());
    this.mCamera.setBoundsEnabled(true);

    float camera_width = activity.getCamera().getWidth();
    float camera_height = activity.getCamera().getHeight();
    if (camera_width / tmxLayer.getHeight() >= camera_height / tmxLayer.getWidth())
      maxZoom = camera_width / (tmxLayer.getHeight() * 2);
    else maxZoom = camera_height / (tmxLayer.getWidth() * 2);

    // 2-dimensional array of tiles
    TMXTile[][] tiles = tmxLayer.getTMXTiles();

    startTile = null;
    endTile = null;
    outer:
    for (int i = 0; i < tiles.length; i++) {
      for (int j = 0; j < tiles[0].length; j++) {

        int[] start = gameMap.getStartTile();
        int[] end = gameMap.getEndTile();

        if (i == start[0] && j == start[1]) {
          startTile = tiles[i][j];
        } else if (i == end[0] && j == end[1]) {
          endTile = tiles[i][j];
          break outer;
        }
      }
    }

    money = 60;
    lives = 20;

    // Initializes the HUD
    panel = new BottomPanel(mCamera, gameMap);
    this.attachChild(panel);

    TowerTile.initializeMap();

    TowerTile turretTile = new TowerTile(resourceManager.getTurretTowerRegion());
    panel.placeTowerAccess(turretTile, 1);

    TowerTile iceTile = new TowerTile(resourceManager.getIceTowerRegion());
    panel.placeTowerAccess(iceTile, 2);

    TowerTile dartTile = new TowerTile(resourceManager.getDartTowerRegion());
    panel.placeTowerAccess(dartTile, 3);

    TowerTile spikeTile = new TowerTile(resourceManager.getSpikeTowerRegion());
    panel.placeTowerAccess(spikeTile, 4);

    TowerTile flameTile = new TowerTile(resourceManager.getFlameTowerRegion());
    panel.placeTowerAccess(flameTile, 5);

    startButton =
        new AnimatedSprite(
            0.0f,
            0.0f,
            resourceManager.getStartButtonRegion(),
            activity.getVertexBufferObjectManager()) {
          @Override
          public boolean onAreaTouched(
              TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {

            if (readyToPressAgain) {
              startCurrentWave();

              readyToPressAgain = false;
              this.registerUpdateHandler(
                  new TimerHandler(
                      1.0f,
                      new ITimerCallback() {

                        @Override
                        public void onTimePassed(TimerHandler pTimerHandler) {
                          readyToPressAgain = true;
                          unregisterUpdateHandler(pTimerHandler);
                        }
                      }));
            }

            panel.detachTowerTextDescription();

            return true;
          }
        };

    startButton.setScale(0.473372781f);
    panel.placeStartButton(startButton);

    // Getting texture regions for submenu items
    SubMenuManager.getDeleteRegion(resourceManager.getDeleteOptionRegion());
    SubMenuManager.getUpgradeRegion(resourceManager.getUpgradeOptionRegion());
    SubMenuManager.getReticalRegion(resourceManager.getTowerSightRegion());

    // Initializing tower array
    towers = new ArrayList<ITower>();

    downCoords = new Vector2();

    blockedTileList = new ArrayList<TMXTile>();

    aStarHelper = new AStarPathHelper(mTMXTiledMap, endTile);
    waveGenerator = new WaveHelper();
    waveCount = 0;
    deadEnemies = 0;

    waveFinished = true;
    initializedNewWave = false;

    // Sets up paths/move modifiers of enemies in the first wave
    initializeNextWave();

    FlameTower.initialize(resourceManager.getFlameParticleRegion());

    Log.i(
        "Info",
        "Dead Enemies: "
            + deadEnemies
            + " Finished Enemies: "
            + aStarHelper.getNumberOfEnemiesFinished()
            + " Current Wave Length: "
            + currentWave.getEnemies().size());

    speedFactor = 0.5f;
    readyToPressAgain = true;

    // collisionDetect = new TimerHandler((float)0.0125/speedFactor, true, new ITimerCallback() {

    collisionDetect =
        new TimerHandler(
            0.025f,
            true,
            new ITimerCallback() {

              @Override
              public void onTimePassed(TimerHandler pTimerHandler) {
                collisionDetect();
              }
            });

    enemyQueues =
        new TimerHandler(
            0.3f,
            true,
            new ITimerCallback() {
              @Override
              public void onTimePassed(TimerHandler pTimerHandler) {
                addEnemiesToTowerQueues();
              }
            });

    disableBackButton = false;
  }