예제 #1
0
 @Override
 public void onPinchZoom(
     PinchZoomDetector pPinchZoomDetector, TouchEvent pTouchEvent, float pZoomFactor) {
   // zoom, but make sure the camera does not zoom outside the TMX bounds by using maxZoom
   this.mCamera.setZoomFactor(
       Math.min(Math.max(maxZoom, this.mPinchZoomStartedCameraZoomFactor * pZoomFactor), 2));
   this.mCamera.setBounds(0, 0, tmxLayer.getWidth(), tmxLayer.getHeight());
 }
예제 #2
0
  @Override
  protected Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());
    // TODO Auto-generated method stub
    scene = new Scene();
    try {
      final TMXLoader tmxLoader =
          new TMXLoader(
              this.getAssets(),
              this.mEngine.getTextureManager(),
              TextureOptions.BILINEAR_PREMULTIPLYALPHA,
              this.getVertexBufferObjectManager(),
              new ITMXTilePropertiesListener() {
                @Override
                public void onTMXTileWithPropertiesCreated(
                    final TMXTiledMap pTMXTiledMap,
                    final TMXLayer pTMXLayer,
                    final TMXTile pTMXTile,
                    final TMXProperties<TMXTileProperty> pTMXTileProperties) {}
              });
      this.mTMXTiledMap = tmxLoader.loadFromAsset("tmx/map_pro.tmx");

    } catch (final TMXLoadException e) {
      Debug.e(e);
    }
    final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);

    scene.attachChild(tmxLayer);

    /* Make the camera not exceed the bounds of the TMXEntity. */
    this.mBoundChaseCamera.setBounds(0, 0, tmxLayer.getHeight(), tmxLayer.getWidth());
    this.mBoundChaseCamera.setBoundsEnabled(true);

    final float centerX = (MyCamera.CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
    final float centerY = (MyCamera.CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
    face =
        new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());

    scene.attachChild(face);

    // Add playerSprite to screen
    initplayerSprite(scene, this.mPlayerTextureRegion);

    // Scene UpdateHandler

    scene.setTouchAreaBindingOnActionDownEnabled(true);
    scene.setOnAreaTouchListener(pOnAreaTouchListener);
    scene.registerUpdateHandler(pUpdateHandler);
    scene.registerTouchArea(tmxLayer);
    scene.setOnSceneTouchListener(pOnSceneTouchListener);
    return scene;
  }
예제 #3
0
  /**
   * Removes a tower from the field
   *
   * @param Tower t
   */
  public void removeTower(final ITower t, boolean removeTile) {
    TMXTile tile =
        tmxLayer.getTMXTileAt(
            t.getEntity().getX() + GameMap.getTileSize() / 2,
            t.getEntity().getY() + GameMap.getTileSize() / 2);

    if (blockedTileList.contains(tile) && removeTile) {
      blockedTileList.remove(tile);
    }
    activity.runOnUpdateThread(
        new Runnable() {

          @Override
          public void run() {
            t.getEntity().detachSelf();
            SubMenuManager.getReticle(t).detachSelf();
            t.getEntity().clearEntityModifiers();
            t.getEntity().clearUpdateHandlers();
          }
        });
    if (towers.contains(t)) {
      towers.remove(t);
    }

    // if the user manually deleted the tower, we need to adjust for that for the paths of the
    // enemies
    if (aStarHelper.isNavigating())
      for (Enemy e : currentWave.getEnemies()) updateEnemyPaths(true, e);
    else updateEnemyPaths(false, null);
  }
  private void processIsometricTMXObject() {
    /*
     * Implemented by - Paul Robinson
     * Also in TMXLayer.java
     * Referenced work - athanazio - "Working with Isometric Maps"
     * http://www.athanazio.com/2008/02/21/working-with-isometric-maps/
     * http://www.athanazio.com/wp-content/uploads/2008/02/isomapjava.txt
     */
    final int tileHeight = this.mTMXTiledMap.getTileHeight();
    final int tileWidth = this.mTMXTiledMap.getTileWidth();
    final int maxColumnPixel = tileHeight * this.getTileColumns();
    final int maxRowPixel = tileHeight * this.getTileRows();
    ArrayList<TMXObject> objects = this.mTMXObjectGroup.getTMXObjects();
    for (TMXObject tmxObject : objects) {
      final int columnPixel = tmxObject.getX();
      final int rowPixel = tmxObject.getY();
      // Check if its pixel can be divided by tile height
      int columnRemainder = columnPixel % tileHeight;
      int rowRemainder = rowPixel % tileHeight;
      if (columnPixel <= 0
          || rowPixel <= 0
          || columnPixel > maxColumnPixel
          || rowPixel > maxRowPixel) {
        // We've got a tile outside the map, we won't draw it so skip it.
        continue;
      }

      if (columnRemainder != 0 || rowRemainder != 0) {
        /*
         * Since there is a remainder that means we're not on a tile
         * This means someone had held ctrl when placing the object tile.
         * Drawing this tile is currently not supported.
         */
        continue;
      }

      /*
       * Work out what row and column this object should exist on.
       * -1 is magic! This is because our row and column start at 0(Thanks java?!?)
       * So taking away 1 accounts for starting at 0;
       */
      final int column = (columnPixel / tileHeight) - 1;
      final int row = (rowPixel / tileHeight) - 1;
      final int width = this.mTMXTiledMap.getTileColumns();
      final ITextureRegion tmxTileTextureRegion;
      final int pGlobalTileID = tmxObject.getGID();
      // If -1 it means its not a tile object, just a normal object
      if (pGlobalTileID == 0) {
        tmxTileTextureRegion = null;
      } else if (pGlobalTileID == -1) {
        Log.w(TAG, "Global Tile ID is -1 this means it is not a tile object that can be draw");
        continue;
      } else if (tmxObject.getPolygonPoints() != null) {
        Log.w(TAG, "This object has polygon points, cannot draw these");
        continue;
      } else if (tmxObject.getPolylinePoints() != null) {
        Log.w(TAG, "This object has polyline points, cannot draw these");
        continue;
      } else {
        tmxTileTextureRegion = this.mTMXTiledMap.getTextureRegionFromGlobalTileID(pGlobalTileID);
      }

      if (tmxTileTextureRegion != null) {
        // Unless this is a transparent tile, setup the texture
        if (this.mTexture == null) {
          this.mTexture = tmxTileTextureRegion.getTexture();
          super.initBlendFunction(this.mTexture);
        } else {
          if (this.mTexture != tmxTileTextureRegion.getTexture()) {
            throw new AndEngineRuntimeException(
                "All TMXTiles in a TMXObjetTileLayer ("
                    + this.getName()
                    + ") need to be in the same TMXTileSet.");
          }
        }
      }
      /*
       * Since objects layers aren't structured like normal layers,
       * we have to work out the z index based on the tile row and column
       */
      final int pZindex = (row * width) + column;
      TMXTile tmxTile =
          new TMXTile(
              this.mTMXTiledMap.getOrientation(),
              pGlobalTileID,
              pZindex,
              column,
              row,
              tileWidth,
              tileHeight,
              tmxTileTextureRegion);
      tmxTile.setTMXObject(tmxObject);

      // Get the offset for the tileset and the tileset size
      /*
       * element[0] is the X offset.
       * element[1] is the Y offset.
       * element[2] is the tile width.
       * element[3] is the tile height.
       */
      int[] offset_tilesize = {0, 0, tileWidth, tileHeight};
      offset_tilesize = this.mTMXTiledMap.checkTileSetOffsetAndSize(pGlobalTileID);

      float xRealIsoPos = (column * this.mIsoHalfTileWidth);
      xRealIsoPos = xRealIsoPos - (row * this.mIsoHalfTileWidth);
      float yRealIsoPos = (column * this.mIsoHalfTileHeight);
      yRealIsoPos = yRealIsoPos + (row * this.mIsoHalfTileHeight);

      /*
       * We need to apply the offset different here, in TMXLayer we wanted
       * to drop the offset down, here we need to bring it up
       * the X offset shouldn't really matter
       */
      float xOffsetPos = xRealIsoPos - Math.abs(offset_tilesize[0]);
      float yOffsetPos = yRealIsoPos - offset_tilesize[1];

      tmxTile.setTileXIso(xOffsetPos);
      tmxTile.setTileYIso(yOffsetPos);
      /*
       * Suppose we could skip this
       * If you want to touch this object then click on the tile space it
       * occupies
      int xCentre = xRealIsoPos + this.mIsoHalfTileWidth;
      int yCentre = yRealIsoPos + this.mIsoHalfTileHeight;
      tmxTile.setTileXIsoCentre(xCentre);
      tmxTile.setTileYIsoCentre(yCentre);
       */
      this.mTMXTiles[row][column] = tmxTile;
      this.count++;
      if (pGlobalTileID != 0) {
        this.setIndex(this.getSpriteBatchIndex(column, row));
        // Before we were drawing to the map tile size, not the tileset size
        this.drawWithoutChecks(
            tmxTileTextureRegion,
            tmxTile.getTileX(),
            tmxTile.getTileY(),
            offset_tilesize[2],
            offset_tilesize[3],
            Color.WHITE_ABGR_PACKED_FLOAT);
        this
            .submit(); // TODO Doesn't need to be called here, but should rather be called in a
                       // "init" step, when parsing the XML is complete.
        // Notify the ITMXTilePropertiesListener if it exists.
        /*
        Not supporting this, would require a slight change or passing null for TMXLayer argument.
        if(pTMXTilePropertyListener != null) {
        	final TMXProperties<TMXTileProperty> tmxTileProperties = this.mTMXTiledMap.getTMXTileProperties(pGlobalTileID);
        	if(tmxTileProperties != null) {
        		pTMXTilePropertyListener.onTMXTileWithPropertiesCreated(this.mTMXTiledMap, this, tmxTile, tmxTileProperties);
        		//Log.i(TAG, "tmxTileProperties created, size " + tmxTileProperties.size());
        	}
        }
         */
      }
    }
  }
예제 #5
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;
  }