Android Open Source - ssniper-andengine Result Scene






From Project

Back to project page ssniper-andengine.

License

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.

Java Source Code

package com.cladophora.ssniper.scene;
/*w w w  .  j  a  v  a  2s .  com*/
import com.cladophora.ssniper.BaseActivity;
import com.cladophora.ssniper.HUDManager;
import com.cladophora.ssniper.R;
import com.cladophora.ssniper.entity.EnemyLayer;
import com.cladophora.ssniper.entity.Rifle;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.MoveYModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.CameraScene;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.color.Color;

public class ResultScene extends CameraScene implements IOnSceneTouchListener {

  private static boolean done = false;
  private static BaseActivity activity;
    public static boolean missionSuccess;
    public static Text result;
    public static Rectangle bg;
    public static String missionResultMessage;
    private static String msg;
    public static int timeCashBonus;
    public static int accuracyCashBonus;
    public static HUD resultHUD;
    public static MoveYModifier slideUpFromBottom;
    public static float accuracy;
    public static int headShotsInCurrentLevel;
    public static int cashEarnedInCurrentLevel;
    public static String accuracyString;
    public static float headshotPercent;
    public static float missionTimeElapsed;
    public static String reason;

    public ResultScene(Camera mCamera) {
    super(mCamera);
    activity = BaseActivity.getSharedInstance();
        done = false;
        GameScene.hideLayer();
        GameScene.pause();
        setBackgroundEnabled(false);
        setOnSceneTouchListener(this);
        calculateResults();
        displayResultsOnHUD();
        animateResultMessage();
  }

    private static void calculateResults() {
        missionSuccess = EnemyLayer.isEmpty();
        calculateAccuracy();
        calculateHeadshotMetrics();
        calculateMissionTime();
        if (SurvivorScene.inSurvivorMode) {
            calculateBonusCashSM();
            finalizeEarnedCash();
            generateResultMessageSM();
        } else {
            calculateBonusCash();
            generateLevelResultMessage();
        }
    }

    private void displayResultsOnHUD() {
        createResultHUDEntities();
        positionHUDChildren();
        attachHUDChildren();
        mCamera.setHUD(resultHUD);
    }

    private static void generateLevelResultMessage() {

        if (GameScene.levelID == GameScene.FINAL_LEVEL) {
            generateFinalLevelResults();
            return;
        }

        String levelNumber = "#" + String.valueOf(GameScene.levelID + 1) + " / " + String.valueOf(GameScene.FINAL_LEVEL);

        if (missionSuccess) {
            missionResultMessage = " - " + activity.getString(R.string.missionsuccess);
            GameScene.levelID++;
            finalizeEarnedCash();
        } else {
            getReasonForMissionEnd();
            missionResultMessage = " - " + activity.getString(R.string.missionfail) + reason;
            resetCash();
        }

        msg = levelNumber + missionResultMessage + "\n"
                + activity.getString(R.string.accuracy)    + ": "  + accuracyString + "\n"
                + activity.getString(R.string.headshots)   + ": "  + String.valueOf(headShotsInCurrentLevel) + "\n"
                + activity.getString(R.string.moneyearned) + ": $" + String.valueOf(cashEarnedInCurrentLevel) + "\n"
                + activity.getString(R.string.timebonus)   + ": $" + String.valueOf(timeCashBonus);
                //+ activity.getString(R.string.accbonus)    + ": $" + String.valueOf(accuracyCashBonus);
    }

    private static void getReasonForMissionEnd() {
        if (GameScene.playerHP <= 0) {
            reason = "\n" + activity.getString(R.string.reason_health);
        } else if (Rifle.roundsLeft <= 0) {
            reason = "\n" + activity.getString(R.string.reason_ammo);
        } else if (GameScene.timeRemaining <= 0) {
            reason = "\n" + activity.getString(R.string.reason_time);
        } else {
            reason = "";
        }
    }

    private static void resetCash() {
        GameScene.cash = GameScene.initialCash;
        cashEarnedInCurrentLevel = 0;
        timeCashBonus = 0;
    }

    private static void finalizeEarnedCash() {
        //GameScene.cash += (timeCashBonus + accuracyCashBonus);
        GameScene.cash += timeCashBonus;
        GameScene.totalCashEarned += GameScene.cash;
        HUDManager.updateCashCount();
    }

    private static void generateResultMessageSM() {
        getReasonForMissionEnd();
        msg = BaseActivity.getSharedInstance().getString(R.string.survivor0) + reason + "\n"
                + activity.getString(R.string.accuracy)    + ": "  + accuracyString + "\n"
                + activity.getString(R.string.headshots)   + ": "  + String.valueOf(headShotsInCurrentLevel) + "\n"
                + activity.getString(R.string.moneyearned) + ": $" + String.valueOf(cashEarnedInCurrentLevel) + "\n"
                + activity.getString(R.string.timebonus)   + ": $" + String.valueOf(timeCashBonus);
                //+ activity.getString(R.string.accbonus)    + ": $" + String.valueOf(accuracyCashBonus);

    }

    private static void generateFinalLevelResults() {
        float totalAccuracy;
        if (GameScene.roundsFired == 0) {
            totalAccuracy = 0;
        } else {
            totalAccuracy = (GameScene.totalRoundsOnTarget * 100) / GameScene.totalRoundsFired;
        }
        final String totalAccuracyString = String.format("%.2f", totalAccuracy) + "%";
        msg = activity.getString(R.string.gamecomplete0) + "\n"
                + activity.getString(R.string.gamecomplete1) + "\n"
                + activity.getString(R.string.accuracy)      + ": "  + totalAccuracyString + "\n"
                + activity.getString(R.string.headshots)     + ": "  + String.valueOf(GameScene.totalHeadshots) + "\n"
                + activity.getString(R.string.moneyearned)   + ": $" + String.valueOf(GameScene.totalCashEarned);

        // Start over at higher difficulty level, keep remaining cash
        GameScene.difficultyLevel += 1;
        GameScene.levelID = 0;
        GameScene.startingCash = GameScene.cash;
        GameScene.totalHeadshots = 0;
        GameScene.totalRoundsFired = 0;
        GameScene.totalRoundsOnTarget = 0;
        GameScene.totalCashEarned = 0;
    }

    private static void calculateBonusCashSM() {
        // Retain cash earned in Survivor Mode
        cashEarnedInCurrentLevel = GameScene.cash - (GameScene.initialCash - GameScene.startingCash);
        timeCashBonus = (int) GameScene.bonusTime * 99;
        accuracyCashBonus = (headShotsInCurrentLevel * 99);
    }

    private static void calculateBonusCash() {
        cashEarnedInCurrentLevel = GameScene.cash - (GameScene.initialCash - GameScene.startingCash);
        float bonusMultiplier = (1 + (GameScene.levelID / 5));
        timeCashBonus = (int) (GameScene.bonusTime * (bonusMultiplier * 1500));
        accuracyCashBonus = (int) (headshotPercent * accuracy * (bonusMultiplier * 250));
    }

    public static void calculateMissionTime() {
        missionTimeElapsed = GameScene.initialTime + GameScene.bonusTime;
    }

    public static void calculateHeadshotMetrics() {
        headShotsInCurrentLevel = GameScene.totalHeadshots - GameScene.initialHeadshots;
        if (GameScene.roundsFired == 0) {
            headshotPercent = 0;
        } else {
            headshotPercent = headShotsInCurrentLevel / GameScene.roundsFired;
        }
    }

    public static void calculateAccuracy() {
        if (GameScene.roundsFired == 0) {
            accuracy = 0;
        } else {
            accuracy = (GameScene.roundsOnTarget * 100) / GameScene.roundsFired;
        }
        GameScene.totalRoundsOnTarget += GameScene.roundsOnTarget;
        GameScene.totalRoundsFired += GameScene.roundsFired;
        accuracyString = String.format("%.2f", accuracy) + "%";
    }

    private static void createResultHUDEntities() {
        resultHUD = new HUD();
        final VertexBufferObjectManager vbom = BaseActivity.getSharedInstance().getVertexBufferObjectManager();
        result = new Text(0, 0, BaseActivity.mNotoSansFont, msg, vbom);
        result.setColor(Color.BLACK);
    }

    private static void positionHUDChildren() {
        bg = new Rectangle(BaseActivity.SCOPE_SIZE,(BaseActivity.CAMERA_HEIGHT / 2) + (result.getHeight() / 2 + 20), result.getWidth() + 40,result.getHeight() + 40,BaseActivity.getSharedInstance().getVertexBufferObjectManager());
        bg.setColor(Color.WHITE);
        bg.setAlpha(GameScene.P_ALPHA);
        bg.detachSelf();
        bg.setVisible(true);

        result.setPosition(20,20);
    }

    private static void attachHUDChildren () {
        resultHUD.attachChild(bg);
        result.detachSelf();
        bg.attachChild(result);
    }

    private static void animateResultMessage() {
        final int slideUpDuration = 2;
        slideUpFromBottom = new MoveYModifier(slideUpDuration, result.getY(), (BaseActivity.CAMERA_HEIGHT / 2) - (result.getHeight() / 2) - 20) {
            @Override
            protected void onModifierFinished(IEntity pItem) {
                done = true;
            }
        };
        bg.registerEntityModifier(slideUpFromBottom);
    }

    @Override
  public boolean onSceneTouchEvent(Scene arg0, TouchEvent arg1) {
    if (!done) { return true; }
        mCamera.setHUD(HUDManager.mHUD);
        GameScene.resume();
        GameScene.loadLevel(GameScene.levelID);
        return false;
  }

}




Java Source Code List

com.cladophora.ssniper.BaseActivity.java
com.cladophora.ssniper.DeviceUtil.java
com.cladophora.ssniper.EnemyPool.java
com.cladophora.ssniper.GameEffects.java
com.cladophora.ssniper.GameEvents.java
com.cladophora.ssniper.GameLoopUpdateHandler.java
com.cladophora.ssniper.GameUtil.java
com.cladophora.ssniper.HUDManager.java
com.cladophora.ssniper.HoverListener.java
com.cladophora.ssniper.ItemPool.java
com.cladophora.ssniper.LevelLoader.java
com.cladophora.ssniper.PlantPool.java
com.cladophora.ssniper.SPenHoverView.java
com.cladophora.ssniper.ScopeEngine.java
com.cladophora.ssniper.SoundManager.java
com.cladophora.ssniper.SpriteManager.java
com.cladophora.ssniper.andengine.IRandomTimerCallBack.java
com.cladophora.ssniper.andengine.RandomTimerHandler.java
com.cladophora.ssniper.entity.EnemyLayer.java
com.cladophora.ssniper.entity.Enemy.java
com.cladophora.ssniper.entity.ItemLayer.java
com.cladophora.ssniper.entity.Item.java
com.cladophora.ssniper.entity.PlantLayer.java
com.cladophora.ssniper.entity.Plant.java
com.cladophora.ssniper.entity.Reticle.java
com.cladophora.ssniper.entity.Rifle.java
com.cladophora.ssniper.entity.SPen.java
com.cladophora.ssniper.scene.GameScene.java
com.cladophora.ssniper.scene.InstructionScene.java
com.cladophora.ssniper.scene.IntroScene.java
com.cladophora.ssniper.scene.MainMenuScene.java
com.cladophora.ssniper.scene.PauseScene.java
com.cladophora.ssniper.scene.ResultScene.java
com.cladophora.ssniper.scene.ShopScene.java
com.cladophora.ssniper.scene.SplashScene.java
com.cladophora.ssniper.scene.SurvivorIntroScene.java
com.cladophora.ssniper.scene.SurvivorScene.java
com.cladophora.ssniper.scene.TutorialScene.java
com.cladophora.ssniper.scene.WeaponInfoScene.java