Android Open Source - ssniper-andengine Reticle






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.entity;
/*  w  w  w .j ava  2s .  co m*/
import com.cladophora.ssniper.BaseActivity;
import com.cladophora.ssniper.HUDManager;
import com.cladophora.ssniper.SpriteManager;
import com.cladophora.ssniper.scene.GameScene;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.color.Color;

/**
 * Created by jmar on 12/29/13.
 */
public class Reticle {
    public static Rectangle center;
    public static Reticle instance;
    public static Rectangle top;
    public static Rectangle right;
    public static Rectangle bottom;
    public static Rectangle left;
    public static Sprite reloadIndicator;

    public final static float RETICLE_RADIUS = 24;
    public final static float RETICLE_CENTERSIZE = 1;
    public final static float RETICLE_INNERRADIUS = 8;
    public final static float reloadIconRotationSpeed = 2;
    private final static RotationModifier iconRotator = new RotationModifier(reloadIconRotationSpeed, 0, 360);
    private final static LoopEntityModifier loopRotation = new LoopEntityModifier(iconRotator);

    private Reticle() {
        init();
    }

    public static Reticle getSharedInstance() {
        if (instance == null) {
            instance = new Reticle();
            //Log.d("com.cladophora.ssniper.Reticle.getSharedInstance()", String.valueOf(instance.hashCode()));
        }
        return instance;
    }

    public static void setColor(Color color) {
        center.setColor(color);
        top.setColor(color);
        right.setColor(color);
        bottom.setColor(color);
        left.setColor(color);
    }

    public static void hide() {
        center.setVisible(false);
    }

    public static void show() {
        center.setVisible(true);
    }

    public static void setVisible(boolean visibility) { 
        if (top == null || right == null || bottom == null || left == null) {
            init();
        }

        if (visibility) {
            center.setVisible(true);
        }
        top.setVisible(visibility);
        right.setVisible(visibility);
        bottom.setVisible(visibility);
        left.setVisible(visibility);
    }

    public static void init() {
        createReticleEntities();
        attachReticleChildren();
        setColor(Color.RED);
        setVisible(true);
        Reticle.center.setZIndex(BaseActivity.CAMERA_HEIGHT * 2);
    }

    private static void attachReticleChildren() {
        top.detachSelf();
        right.detachSelf();
        bottom.detachSelf();
        left.detachSelf();

        center.attachChild(top);
        center.attachChild(right);
        center.attachChild(bottom);
        center.attachChild(left);

        top.setPosition(0, -(RETICLE_INNERRADIUS + RETICLE_RADIUS));
        right.setPosition(RETICLE_INNERRADIUS, 0);
        bottom.setPosition(0, RETICLE_INNERRADIUS);
        left.setPosition(-(RETICLE_INNERRADIUS + RETICLE_RADIUS), 0);
    }

    private static void createReticleEntities() {
        final VertexBufferObjectManager vbom = BaseActivity.getSharedInstance().getVertexBufferObjectManager();
        center = new Rectangle(0, 0, RETICLE_CENTERSIZE, RETICLE_CENTERSIZE, vbom);
        top    = new Rectangle(0, 0, RETICLE_CENTERSIZE, RETICLE_RADIUS,     vbom);
        right  = new Rectangle(0, 0, RETICLE_RADIUS,     RETICLE_CENTERSIZE, vbom);
        bottom = new Rectangle(0, 0, RETICLE_CENTERSIZE, RETICLE_RADIUS,     vbom);
        left   = new Rectangle(0, 0, RETICLE_RADIUS,     RETICLE_CENTERSIZE, vbom);
    }

    private static void initializeReloadIndicator() {
        reloadIndicator = new Sprite(0, 0, SpriteManager.mReloadIconTR, BaseActivity.getSharedInstance().getVertexBufferObjectManager());
        if (center == null) {
            init();
        }
        reloadIndicator.detachSelf();
        center.attachChild(reloadIndicator);
        reloadIndicator.setPosition(-(reloadIndicator.getWidth()/2),-(reloadIndicator.getWidth()/2));
        reloadIndicator.registerEntityModifier(loopRotation);
    }

    public void aim(float x, float y) {
        if (!GameScene.userControl) { return; }
        center.setPosition(x, y);
    }

    public static void showReloadIndicator() {
        initializeReloadIndicator();
        HUDManager.slowTimeBar.setVisible(false);
        reloadIndicator.setVisible(true);
        setVisible(false);
    }

    public static void hideReloadIndicator() {
        setVisible(true);
        HUDManager.slowTimeBar.setVisible(true);
        if (reloadIndicator != null) {
            BaseActivity.getSharedInstance().getEngine().runOnUpdateThread(
                    new Runnable() {
                        @Override
                        public void run() {
                            reloadIndicator.detachSelf();
                        }
                    }
            );
        }
    }

}




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