Back to project page ssniper-andengine.
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.
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; } } }