// *********************************************************** // 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; }
public void createExplosion(float x, float y, float radius, float damage, boolean harmPlayer) { final float X = x, Y = y; final HashMap<Character, Float> CHARACTERS_IN_RANGE = new HashMap<Character, Float>(); for (final Zombie z : Zombie.getAliveZombies()) this.onExplosionCheckCharacterCollision(x, y, radius, z, CHARACTERS_IN_RANGE); if (harmPlayer) this.onExplosionCheckCharacterCollision(x, y, radius, this.pPlayer, CHARACTERS_IN_RANGE); final Iterator<Character> CHARACTER_ITERATOR = CHARACTERS_IN_RANGE.keySet().iterator(); final Iterator<Float> DAMAGE_ITERATOR = CHARACTERS_IN_RANGE.values().iterator(); while (CHARACTER_ITERATOR.hasNext()) { final Character CHAR = CHARACTER_ITERATOR.next(); final Float DAMAGE = DAMAGE_ITERATOR.next(); CHAR.setHealthAmount((int) (CHAR.getHealthAmount() - damage * DAMAGE)); if (CHAR.getHealthAmount() <= 0) AchievementsManager.incrementZombiesKilledByExplosion(); } final ITiledTextureRegion EXPLOSION_TEXTURE = HudRegions.region_explosion; final AnimatedSprite EXPLOSION = new AnimatedSprite( x, y, EXPLOSION_TEXTURE, EnvironmentVars.MAIN_CONTEXT.getVertexBufferObjectManager()); if (this.explosionCount == 0) EXPLOSION_TEXTURE.getTexture().load(); EXPLOSION.setScaleCenter(0, 0); EXPLOSION.setScale(2f); EXPLOSION.setPosition( EXPLOSION.getX() - EXPLOSION.getWidthScaled() / 2f, EXPLOSION.getY() - EXPLOSION.getHeightScaled() / 2f); EXPLOSION.setZIndex(this.getLevel().getMapHeight()); this.attachChild(EXPLOSION); this.explosionCount++; EXPLOSION.animate( 30L, false, new IAnimationListener() { private boolean usedEffects; @Override public void onAnimationFinished(AnimatedSprite pAnimatedSprite) { EnvironmentVars.MAIN_CONTEXT.runOnUpdateThread( new Runnable() { @Override public void run() { SessionScene.this.explosionCount--; if (SessionScene.this.explosionCount == 0) EXPLOSION_TEXTURE.getTexture().unload(); EXPLOSION.detachSelf(); EXPLOSION.dispose(); } }); } @Override public void onAnimationFrameChanged( AnimatedSprite pAnimatedSprite, int pOldFrameIndex, int pNewFrameIndex) { if (pNewFrameIndex >= 3) if (!this.usedEffects) { SessionScene.this.createExplosionMark(X, Y); SessionScene.this.onExplosionCreateSmoke(X, Y); this.usedEffects = true; } } @Override public void onAnimationLoopFinished( AnimatedSprite pAnimatedSprite, int pRemainingLoopCount, int pInitialLoopCount) {} @Override public void onAnimationStarted(AnimatedSprite pAnimatedSprite, int pInitialLoopCount) {} }); this.getCameraManager().shakeRandomDirection(); SessionScene.EXPLOSION_SOUND.play(); }