Back to project page ssniper-andengine.
The source code is released under:
Apache License
If you think the Android project ssniper-andengine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.cladophora.ssniper.scene; //from w w w . jav a 2 s . co m import com.cladophora.ssniper.*; import com.cladophora.ssniper.entity.*; import org.andengine.engine.camera.Camera; import org.andengine.entity.modifier.FadeInModifier; import org.andengine.entity.modifier.FadeOutModifier; import org.andengine.entity.modifier.LoopEntityModifier; import org.andengine.entity.modifier.SequenceEntityModifier; import org.andengine.entity.scene.IOnSceneTouchListener; import org.andengine.entity.scene.Scene; import org.andengine.entity.sprite.Sprite; import org.andengine.input.touch.TouchEvent; import org.andengine.util.color.Color; import org.json.JSONObject; import java.util.Iterator; import java.util.Timer; import java.util.TimerTask; public class GameScene extends Scene implements IOnSceneTouchListener { public static final float P_ALPHA = 0.7f; public static float enemyScale = 1.0f; public static float plantScale = 1.0f; public static float itemScale = 1.0f; public BaseActivity activity = BaseActivity.getSharedInstance(); public static final LevelLoader levelLoader = new LevelLoader(); public static Camera mCamera; public static Scene mCurrentScene; public static Reticle reticle; public static GameScene instance; public static Rifle rifle; private static Timer timer; public static int FINAL_LEVEL = -1; public static boolean inTutorial = false; public static boolean inGame = false; public static boolean isEnding = false; public static int backgroundType; // Game Fields public static int difficultyLevel = 1; public static boolean userControl = true; public static int roundsOnTarget; public static int roundsFired; public static int totalHeadshots; public static int startingCash = 0; public static int initialCash; public static int initialAmmo; public static int initialHeadshots; public static float initialTime; public static int totalRoundsOnTarget; public static int totalRoundsFired; public static int totalCashEarned; public static int cash = 0; public static float tStart; public static float missionTime; public static float bonusTime; public static float slowTime; public static float headShotBonus = 5; public static float killBonus = 1; public static int levelID = 0; public static final long EFFECT_DURATION = 1500; public static final float CLOCK_RESOLUTION = 0.05f; public static final int MISS_DAMAGE = 1; public static float SLOW_TIME_BANK_MAX = 4; public static float SLOW_TIME_RECHARGE_RATE = 0.50f; public static float SLOW_TIME_USAGE_RATE = 1.0f; public static float slowTimeSpeedModifier = 0.35f; public static final int SLOW_TIME_BAR_WIDTH = 2; public static final float SLOW_TIME_BAR_MAXHEIGHT = Reticle.RETICLE_RADIUS / 2; public static final float TARGET_INDICATOR_SIZE = 12; public static final long TARGET_INDICATOR_DURATION = 1200; public static float slowTimeBank = 3; public static EnemyLayer enemyLayer; public static ItemLayer itemLayer; public static PlantLayer plantLayer; public static Sprite glass; public static int playerHP; public static float timeRemaining; public static boolean slowTimeActive; public static final LoopEntityModifier blinkModifier = new LoopEntityModifier(new SequenceEntityModifier(new FadeOutModifier(0.25f), new FadeInModifier(0.25f))); public GameScene() { instance = this; timer = new Timer(); mCamera = BaseActivity.mCamera; itemLayer = ItemLayer.getSharedInstance(); enemyLayer = EnemyLayer.getSharedInstance(); plantLayer = PlantLayer.getSharedInstance(); rifle = Rifle.getSharedInstance(); reticle = Reticle.getSharedInstance(); setBG(0); PlantLayer.addPlantsRandomly(); attachLayers(); Rifle.init(); Reticle.init(); BaseActivity.setCurrentScene(this); setOnSceneTouchListener(this); HUDManager.initializeHUD(); initializeEffects(); loadLevel(levelID); if (levelID == 0 && difficultyLevel == 1) { loadChildScene(new TutorialScene(mCamera)); } } @Override protected void onManagedUpdate(float pSecondsElapsed) { if (!slowTimeActive) { // gradually recharge slowTimeBank slowTimeBank += (SLOW_TIME_RECHARGE_RATE * pSecondsElapsed); if (slowTimeBank > SLOW_TIME_BANK_MAX) { slowTimeBank = SLOW_TIME_BANK_MAX; } super.onManagedUpdate(pSecondsElapsed); } else { slowTimeBank -= (SLOW_TIME_USAGE_RATE * pSecondsElapsed); if (slowTimeBank <= 0) { deactivateSlowTime(); } slowTime += pSecondsElapsed * (1 - slowTimeSpeedModifier); super.onManagedUpdate(pSecondsElapsed * slowTimeSpeedModifier); } } public static void loadChildScene(final Scene scene) { mCurrentScene = scene; instance.setChildScene(mCurrentScene); } public static void loadPauseScene(final Scene scene) { mCurrentScene = scene; instance.setChildScene(mCurrentScene,false,true,true); } public static void attachLayers() { enemyLayer.detachSelf(); itemLayer.detachSelf(); plantLayer.detachSelf(); instance.attachChild(enemyLayer); instance.attachChild(itemLayer); instance.attachChild(plantLayer); enemyLayer.setZIndex(5); itemLayer.setZIndex(4); plantLayer.setZIndex(6); } public static void deactivateSlowTime() { slowTimeActive = false; Reticle.setColor(Color.RED); HUDManager.clock.setColor(Color.BLACK); } public static GameScene getSharedInstance() { if (instance == null) { instance = new GameScene(); } return instance; } public static void pause() { deactivateSlowTime(); getSharedInstance().setIgnoreUpdate(true); } public static void resume() { getSharedInstance().setIgnoreUpdate(false); } public JSONObject currentLevel() { return levelLoader.getLevel(activity, levelID); } public static void loadLevel(final int levelID) { mCurrentScene = getSharedInstance(); userControl = true; inGame = true; GameScene.roundsOnTarget = 0; GameScene.roundsFired = 0; getSharedInstance().clearChildScene(); GameScene.deactivateSlowTime(); EnemyLayer.hide(); tStart = BaseActivity.getSharedInstance().getEngine().getSecondsElapsedTotal(); HUDManager.registerClockUpdateHandler(); // since we have a new update handler we need to re-register mCamera.setHUD(HUDManager.mHUD); HUDManager.mHUD.setVisible(true); if (SurvivorScene.inSurvivorMode || ResultScene.missionSuccess) { setRandomBackground(); } PlantLayer.show(); EnemyLayer.purge(); ItemLayer.purge(); ItemLayer.hide(); if (SurvivorScene.inSurvivorMode) { loadPauseScene(new SurvivorIntroScene(mCamera)); } else { final JSONObject currentLevel = levelLoader.getLevel(BaseActivity.getSharedInstance(), levelID); setInitialGameState(currentLevel); if (levelID > 0) { inTutorial = false; loadPauseScene(new IntroScene(mCamera)); } else { loadPauseScene(new TutorialScene(mCamera)); } } inGame = true; ResultScene.missionSuccess = false; } public static void setRandomBackground() { if (SurvivorScene.inSurvivorMode) { setBG(BaseActivity.r.nextInt(3)); } else if (levelID < 3) { setBG(levelID - 1); // first 3 levels have predefined backgrounds } PlantLayer.addPlantsRandomly(); } public static void setBG(final int bgID) { backgroundType = bgID; switch (bgID) { case 0: GameScene.getSharedInstance().setBackground(SpriteManager.mSnowBGTR); break; case 1: GameScene.getSharedInstance().setBackground(SpriteManager.mSandBGTR); break; case 2: GameScene.getSharedInstance().setBackground(SpriteManager.mGrassBGTR); break; default: GameScene.getSharedInstance().setBackground(SpriteManager.mSnowBGTR); break; } } private static void setInitialGameState(JSONObject currentLevel) { killBonus = levelLoader.getKillBonus(currentLevel); headShotBonus = levelLoader.getHeadShotBonus(currentLevel); initialAmmo = levelLoader.getAmmo(currentLevel); initialTime = levelLoader.getInitialTime(currentLevel); missionTime = initialTime; initialHeadshots = totalHeadshots; bonusTime = 0; slowTime = 0; initialCash = cash; if (levelID < 3){ slowTimeBank = SLOW_TIME_BANK_MAX; } else { slowTimeBank = SLOW_TIME_BANK_MAX / (levelID / 3); } if (playerHP < 3) { setHP(3); } if (difficultyLevel > 1) { killBonus = 0; headShotBonus = totalHeadshots / (difficultyLevel + 1); initialAmmo -= difficultyLevel; initialTime -= difficultyLevel * 2; if (initialTime < 10) { initialTime = 10; } if (initialAmmo <= 5) { initialAmmo = 5; } // don't allow extra health to carry over to next level setHP(3); } if (difficultyLevel > 2) { setHP(2); } if (difficultyLevel > 5) { setHP(1); } if (Rifle.roundsLeft < initialAmmo) { rifle.setRoundsLeft(initialAmmo); } rifle.resetValues(); EnemyLayer.loadLevel(currentLevel); HUDManager.updateHUDAmmoCount(); } private void initializeEffects() { glass = new Sprite(0, 0, SpriteManager.mGlassTR, activity.getVertexBufferObjectManager()); final float x = (BaseActivity.CAMERA_WIDTH / 2) - (glass.getWidth() * 0.3f); final float y = (BaseActivity.CAMERA_HEIGHT / 2) - (glass.getHeight() / 2); glass.setPosition(x, y); glass.detachSelf(); glass.setVisible(false); attachChild(glass); glass.setZIndex(BaseActivity.CAMERA_HEIGHT + 1); } public static void activateSlowTime () { if (slowTimeActive) { return; } else if (slowTimeBank < SLOW_TIME_BANK_MAX * 0.6f) { return; } slowTimeActive = true; Reticle.setColor(Color.BLUE); HUDManager.clock.setColor(Color.BLUE); SoundManager.mSlowSound.play(); } @Override public void clearChildScene () { mCurrentScene = null; mCurrentScene = this; super.clearChildScene(); } public static void sortEnemies() { Iterator<Enemy> enemyIterator = EnemyLayer.getIterator(); while (enemyIterator.hasNext()) { Enemy enemy = enemyIterator.next(); enemy.sprite.setZIndex((int) (enemy.sprite.getY() + enemy.sprite.getHeightScaled())); } GameScene.getSharedInstance().sortChildren(); } public static void setHP(int hp) { playerHP = hp; if(HUDManager.healthCount != null) { HUDManager.healthCount.setText("x" + String.format("%d", playerHP)); } } @Override public boolean onSceneTouchEvent(Scene pScene, TouchEvent event) { if (!GameScene.userControl) {return false;} Reticle.getSharedInstance().aim(event.getX(), event.getY()); GameEvents.fire(); HUDManager.updateHUDAmmoCount(); return true; } public void detach() { clearUpdateHandlers(); detachChildren(); EnemyPool.instance = null; } public void takeDamage(final int damage) { glass.setVisible(true); new Timer().schedule(new HideDamageEffectTask(), EFFECT_DURATION); setHP(playerHP - damage); if (playerHP <= 0) { endMission(); } } class HideDamageEffectTask extends TimerTask { public void run() { glass.setVisible(false); } } public void scheduleMissionEnd () { if (GameScene.isEnding) { //avoid scheduling endmission twice return; } GameScene.isEnding = true; addBonusTime(2.0f); timer.schedule(new EndMissionTask(), 2000); } public void showHeadshotIndicator() { HUDManager.targetIcon.setVisible(true); HUDManager.targetBody.setVisible(false); HUDManager.targetHead.setVisible(true); resetTimer(); timer.schedule(new HideTargetTask(), TARGET_INDICATOR_DURATION); } public void showBodyshotIndicator() { HUDManager.targetIcon.setVisible(true); HUDManager.targetHead.setVisible(false); HUDManager.targetBody.setVisible(true); resetTimer(); timer.schedule(new HideTargetTask(), TARGET_INDICATOR_DURATION); } private static void resetTimer() { if (timer != null) { timer.cancel(); timer.purge(); } timer = new Timer(); } class HideTargetTask extends TimerTask { public void run() { HUDManager.hideTarget(); } } public void addBonusTime(float timeBonus) { bonusTime = bonusTime + timeBonus; } public void endMission() { if (!inGame) { return; } inGame = false; clearUpdateHandlers(); slowTimeBank = 0; HUDManager.mHUD.setVisible(false); Reticle.hide(); hideLayer(); loadChildScene(new ResultScene(mCamera)); } public static void exitMission() { getSharedInstance().clearUpdateHandlers(); hideLayer(); } public static void hideLayer() { PlantLayer.hide(); EnemyLayer.hide(); ItemLayer.hide(); } class EndMissionTask extends TimerTask { public void run() { isEnding = false; endMission(); } } }