Android Open Source - ssniper-andengine Random Timer Handler






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.andengine;
//from  w ww  .  j  a  va  2  s  . co m
import org.andengine.engine.handler.IUpdateHandler;

import java.util.Random;

public class RandomTimerHandler implements IUpdateHandler {
    // ===========================================================
    // Constants
    // ===========================================================

    // ===========================================================
    // Fields
    // ===========================================================

    private float mTimerSeconds;
    private float mTimerSecondsElapsed;
    private boolean mTimerCallbackTriggered;
    protected final IRandomTimerCallBack mRandomTimerCallback;
    private boolean mAutoReset;
    private float mTimerSecondsMin;
    private float mTimerSecondsMax;
    private final Random r = new Random();

    // ===========================================================
    // Constructors
    // ===========================================================

    public RandomTimerHandler(final float pTimerSecondsMin, final float pTimerSecondsMax, final boolean pAutoReset, final IRandomTimerCallBack pRandomTimerCallback) {
        if(pTimerSecondsMin <= 0){
            throw new IllegalStateException("pTimerSecondsMin must be > 0!");
        }
        if(pTimerSecondsMax <= 0){
            throw new IllegalStateException("pTimerSecondsMax must be > 0!");
        }
        mTimerSecondsMin = pTimerSecondsMin;
        mTimerSecondsMax = pTimerSecondsMax;
        mTimerSeconds = pTimerSecondsMin + ((pTimerSecondsMax - pTimerSecondsMin) * r.nextFloat());
        //Log.v("com.cladophora.ssniper.andengine.RandomTimerHandler", "generated new mTimerSeconds " + String.valueOf(this.hashCode()) + " " + String.valueOf(this.mTimerSeconds));
        mAutoReset = pAutoReset;
        mRandomTimerCallback = pRandomTimerCallback;
    }

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    public boolean isAutoReset() {
        return this.mAutoReset;
    }

    public void setAutoReset(final boolean pAutoReset) {
        this.mAutoReset = pAutoReset;
    }

    public void setTimerSeconds(final float pTimerSeconds) {
        if(pTimerSeconds <= 0){
            throw new IllegalStateException("pTimerSeconds must be > 0!");
        }

        this.mTimerSeconds = pTimerSeconds;
    }

    public float getTimerSeconds() {
        return this.mTimerSeconds;
    }

    public float getTimerSecondsElapsed() {
        return this.mTimerSecondsElapsed;
    }

    public boolean isTimerCallbackTriggered() {
        return this.mTimerCallbackTriggered;
    }

    public void setTimerCallbackTriggered(boolean pTimerCallbackTriggered) {
        this.mTimerCallbackTriggered = pTimerCallbackTriggered;
    }

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    @Override
    public void onUpdate(final float pSecondsElapsed) {
        if(this.mAutoReset) {
            this.mTimerSecondsElapsed += pSecondsElapsed;
            while(this.mTimerSecondsElapsed >= this.mTimerSeconds) {
                this.mTimerSecondsElapsed = 0;
                this.mRandomTimerCallback.onTimePassed(this);
                //Log.v("com.cladophora.ssniper.RandomTimerHandler.onUpdate()","TimerSeconds reached");
            }
        } else {
            if(!this.mTimerCallbackTriggered) {
                this.mTimerSecondsElapsed += pSecondsElapsed;
                if(this.mTimerSecondsElapsed >= this.mTimerSeconds) {
                    this.mTimerCallbackTriggered = true;
                    this.mRandomTimerCallback.onTimePassed(this);
                }
            }
        }
    }

    @Override
    public void reset() {
        this.mTimerCallbackTriggered = false;
        this.mTimerSecondsElapsed = 0;
        // generate a random Timer value
        generateRandomTime();
    }

    // ===========================================================
    // Methods
    // ===========================================================
    public float generateRandomTime() {
        // Sets the RandomTimer seconds to a random number of seconds and returns the value that is set
        mTimerSeconds = mTimerSecondsMin + ((mTimerSecondsMax - mTimerSecondsMin) * r.nextFloat());
        return mTimerSeconds;
    }

    public void addTime(final float seconds) {
        mTimerSeconds = mTimerSeconds + seconds;
    }
    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}




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