Android Open Source - ssniper-andengine Rifle






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;
/*from w  w  w  . j  av  a 2s .c om*/
import com.cladophora.ssniper.SoundManager;
import com.cladophora.ssniper.SpriteManager;
import com.cladophora.ssniper.scene.GameScene;
import org.andengine.audio.sound.exception.SoundReleasedException;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.RotationModifier;

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

public class Rifle {
    private boolean roundInChamber = true;
    public static int damage = 1;
  private static Timer timer;
    private static final long MUZZLE_FLASH_DURATION = 300;
    private static final long CHAMBER_DELAY = 300;
    private static final long RELOAD_DELAY = 2000;
    public static final int MAG_CAPACITY = 5;
    public static int roundsInMag;
    public static int roundsLeft;
  private static Rifle instance;
    public static boolean isReloading;

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

  private Rifle() {
    timer = new Timer();
        roundsLeft = 0;
    roundInChamber = false;
        SpriteManager.initializeRifleSprites();
        resetValues();
  }

    public static void init() {
        SpriteManager.sprite.detachSelf();
        SpriteManager.flash.detachSelf();

        SpriteManager.flash.setPosition(SpriteManager.sprite.getWidth() * 0.61f, SpriteManager.sprite.getY() + (SpriteManager.sprite.getHeight() * 0.17f));
        SpriteManager.sprite.setZIndex(10);
        SpriteManager.flash.setZIndex(9);
        GameScene.getSharedInstance().sortChildren();
    }

    public void resetValues() {
        if (roundsLeft > MAG_CAPACITY) {
            roundsInMag = MAG_CAPACITY;
        } else {
            roundsInMag = roundsLeft;
        }
        roundInChamber = false;
        updateBulletDisplay();
        chamberRound();
    }

    public boolean reload() {
        if (roundsLeft < 1) {
            //Log.i("Rifle.reload()", "0 rounds remaining");
            return false;
        } else {
            isReloading = true;
            Reticle.showReloadIndicator();
            //Log.i("Rifle.reload()", "Reloading");

            try {
                SoundManager.mRifleReloadSound.play();
            } catch (SoundReleasedException e) {
                //Log.d("com.cladophora.ssniper.reload()", "caught SoundReleasedException for mRifleReloadSound");
            }
            timer.schedule(new ReloadTask(), RELOAD_DELAY);
            return true;
        }
    }

    public void setRoundsLeft(final int rounds) {
        roundsLeft = rounds;
    }

    // returns true if rifle is able to fire
    public boolean pullTrigger() {
        if (!GameScene.inGame) {return false;}

        if (magEmpty()) {
            return false;
        }
        if (roundInChamber) {
            roundsInMag--;
            roundsLeft--;
            try {
                SoundManager.mRifleSound.play();
            } catch (SoundReleasedException e) {
                //Log.d("com.cladophora.ssniper.Rifle.pullTrigger()", "caught SoundReleasedException for mRifleSound");
            }
            roundInChamber = false;
            showMuzzleFlash();
            updateBulletDisplay();
            ejectCasing();
            if (roundsInMag > 0) {
                chamberRound();
            }
            return true;
        } else {
            return false;
        }
    }

    public void chamberRound() {
        timer.schedule(new ChamberTask(), CHAMBER_DELAY);
    }

    public void showMuzzleFlash() {
        SpriteManager.flash.setVisible(true);
        timer.schedule(new MuzzleFlash(), MUZZLE_FLASH_DURATION);
    }

    private static void ejectCasing() {
        final float x0 = SpriteManager.sprite.getX() + 190 - SpriteManager.casing.getWidthScaled();
        final float x1 = -SpriteManager.casing.getWidth();
        final float y0 = SpriteManager.sprite.getY() + 170 - SpriteManager.casing.getHeightScaled();
        final float y1 = SpriteManager.sprite.getY() + 60;
        final RotationModifier casingEjectionRotator = new RotationModifier(0.2f,0,360);
        final MoveModifier casingEjectionModifier = new MoveModifier(0.4f,x0,x1,y0,y1) {
            @Override
            protected void onModifierFinished(IEntity pItem) {
                SpriteManager.casing.setVisible(false);
            }
        };
        SpriteManager.casing.registerEntityModifier(new LoopEntityModifier(casingEjectionRotator));
        SpriteManager.casing.setVisible(true);
        SpriteManager.casing.registerEntityModifier(casingEjectionModifier);
    }

    public static boolean magEmpty() {
        return (roundsInMag < 1);
    }

    class MuzzleFlash extends TimerTask {
        public void run() {
            SpriteManager.flash.setVisible(false);
        }
    }

    class ChamberTask extends TimerTask {
    public void run() {
            try {
                SoundManager.mRifleBoltSound.play();
            } catch (SoundReleasedException e) {
                //Log.d("com.cladophora.ssniper.Rifle.ChamberTask.run()", "caught SoundReleasedException for mRifleBoltSound");
            }
            roundInChamber = true;
            //Log.d("com.cladophora.ssniper.Rifle.ChamberTask.run()", "Round has been chambered");
    }
  }

    private class ReloadTask extends TimerTask {
        public void run() {
            isReloading = false;
            if (roundsLeft >= MAG_CAPACITY) {
                roundsInMag = MAG_CAPACITY;
            } else {
                roundsInMag = roundsLeft;
            }
            timer.schedule(new ChamberTask(), CHAMBER_DELAY);
            Reticle.hideReloadIndicator();
            updateBulletDisplay();
            // Log.i("com.cladophora.ssniper.Rifle.ReloadTask.run()", "Reload complete" );
        }
    }

    public static void updateBulletDisplay() {
        switch (roundsInMag) {
            case 0:
                SpriteManager.bullet0.setVisible(false);
                SpriteManager.bullet1.setVisible(false);
                SpriteManager.bullet2.setVisible(false);
                SpriteManager.bullet3.setVisible(false);
                SpriteManager.bullet4.setVisible(false);
                break;
            case 1:
                SpriteManager.bullet1.setVisible(false);
                SpriteManager.bullet2.setVisible(false);
                SpriteManager.bullet3.setVisible(false);
                SpriteManager.bullet4.setVisible(false);
                break;
            case 2:
                SpriteManager.bullet2.setVisible(false);
                SpriteManager.bullet3.setVisible(false);
                SpriteManager.bullet4.setVisible(false);
                break;
            case 3:
                SpriteManager.bullet3.setVisible(false);
                SpriteManager.bullet4.setVisible(false);
                break;
            case 4:
                SpriteManager.bullet4.setVisible(false);
                break;
            case 5:
                SpriteManager.bullet0.setVisible(true);
                SpriteManager.bullet1.setVisible(true);
                SpriteManager.bullet2.setVisible(true);
                SpriteManager.bullet3.setVisible(true);
                SpriteManager.bullet4.setVisible(true);
                break;
            default:
                SpriteManager.bullet0.setVisible(true);
                SpriteManager.bullet1.setVisible(true);
                SpriteManager.bullet2.setVisible(true);
                SpriteManager.bullet3.setVisible(true);
                SpriteManager.bullet4.setVisible(true);
                break;
        }
    }
}




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