Android Open Source - ssniper-andengine Game Util






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;
/*w ww  . java  2 s. c o  m*/
import com.cladophora.ssniper.entity.*;
import com.cladophora.ssniper.scene.GameScene;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by jmar on 1/28/14.
 */
public class GameUtil {

    public static GameUtil instance;
    public static TimerHandler autoAimTimerHandler;
    public static Timer autoAimTimer;

    private GameUtil() {
        resetTimer();
    }

    public static GameUtil getSharedInstance() {
        if (instance == null) {
            instance = new GameUtil();
        }
        return instance;
    }

    public void autoAimAndFireUntilMagEmpty(final long delay) {
        resetTimer();
        autoAimTimer.schedule(new AutoAimTask(),delay);
    }

    public static Enemy targetEnemy() {
        if (EnemyLayer.getIterator().hasNext()) {
            return EnemyLayer.getIterator().next();
        } else {
            return null;
        }
    }

    private static TimerHandler getAutoAimTimerHandler() {
        // Executes the callback to target an enemy's head once every ~2 frames
        return new TimerHandler(0.03f, true, getEnemyAutoAimTimerCallback());
    }

    private static ITimerCallback getEnemyAutoAimTimerCallback() {
        return new ITimerCallback() {
            Enemy enemy = targetEnemy();
            @Override
            public void onTimePassed(final TimerHandler pTimerHandler) {
                if (enemy != null) {
                    Reticle.center.setPosition(enemy.getHeadCenterX(), enemy.getHeadCenterY());
                    SPen.pointTo(Reticle.center.getX(), Reticle.center.getY());
                } else {
                    cancelAutoAim();
                }
            }
        };
    }

    class AutoAimTask extends TimerTask {
        public void run() {
            // This task locks on to an enemy then schedules the firing task a fraction later
            if (!GameScene.getSharedInstance().isIgnoreUpdate()) {
                GameUtil.autoAimTimerHandler = getAutoAimTimerHandler();
                GameScene.getSharedInstance().registerUpdateHandler(autoAimTimerHandler);
                autoAimTimer.schedule(new AutoFireTask(), 300);
                GameScene.activateSlowTime();
            }
        }
    }

    class AutoFireTask extends TimerTask {
        public void run() {
            // This task fires the weapon then turns off auto aim so that a new enemy can be locked on
            if (!GameScene.getSharedInstance().isIgnoreUpdate()) {
                GameEvents.fire();
                GameScene.getSharedInstance().unregisterUpdateHandler(autoAimTimerHandler);
                if (!Rifle.magEmpty()) {
                    autoAimTimer.schedule(new AutoAimTask(), 300);
                } else {
                    cancelAutoAim();
                }
            }
        }
    }

    public static void cancelAutoAim () {
        resetTimer();
        GameScene.getSharedInstance().unregisterUpdateHandler(autoAimTimerHandler);
        GameScene.userControl = true;
    }

    private static void resetTimer() {
        if (autoAimTimer != null) {
            autoAimTimer.cancel();
            autoAimTimer.purge();
        }
        autoAimTimer = new Timer();
    }

}




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