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.scene; // www. j a v a 2 s.c o m import com.cladophora.ssniper.*; import com.cladophora.ssniper.entity.EnemyLayer; import com.cladophora.ssniper.entity.ItemLayer; import com.cladophora.ssniper.entity.Reticle; import com.cladophora.ssniper.entity.Rifle; import org.andengine.engine.camera.Camera; import org.andengine.engine.camera.hud.HUD; import org.andengine.entity.IEntity; import org.andengine.entity.modifier.DelayModifier; import org.andengine.entity.primitive.Rectangle; import org.andengine.entity.scene.CameraScene; import org.andengine.entity.scene.IOnSceneTouchListener; import org.andengine.entity.scene.Scene; import org.andengine.entity.sprite.Sprite; import org.andengine.entity.text.Text; import org.andengine.input.touch.TouchEvent; import org.andengine.opengl.vbo.VertexBufferObjectManager; import org.andengine.util.color.Color; public class ShopScene extends CameraScene implements IOnSceneTouchListener { private static boolean done = false; private static HUD shopHUD; private static int price1 = 100000; private static int price2 = 200000; private static int price3 = 500000; private static int price4 = 750000; private static int price5 = 2000000; public static Text item1; public static Text item1price; public static Text item2; public static Text item2price; public static Text item3; public static Text item3price; public static Text item4; public static Text item4price; public static Text item5; public static Text item5price; public static Text cashAvailable; public ShopScene(Camera mCamera) { super(mCamera); done = false; setBackgroundEnabled(false); setOnSceneTouchListener(this); Reticle.hide(); GameScene.inGame = false; GameScene.pause(); loadResources(); } private void loadResources() { shopHUD = new HUD(); final VertexBufferObjectManager vbom = BaseActivity.getSharedInstance().getVertexBufferObjectManager(); final BaseActivity activity = BaseActivity.getSharedInstance(); final float storePadding = 10; final Text storeTitle = new Text(BaseActivity.SCOPE_SIZE + storePadding,storePadding,BaseActivity.mNotoSansFont,activity.getString(R.string.store), vbom); storeTitle.setColor(Color.BLACK); float storeLeftBound = BaseActivity.SCOPE_SIZE + storePadding; cashAvailable = new Text(0, 0,BaseActivity.mDigitFont,"$" + String.valueOf(GameScene.cash), vbom); final float pX = storeTitle.getX() + storeTitle.getWidth() * 2; final float pY = storeTitle.getY() + (storeTitle.getHeight() / 2) - (cashAvailable.getHeight() / 2); cashAvailable.setPosition(pX,pY); cashAvailable.setColor(Color.BLACK); final Sprite item1S = new Sprite(storeLeftBound,storeTitle.getY() + storeTitle.getHeight() + (storePadding * 2), SpriteManager.mAmmoTR,vbom); final Sprite item2S = new Sprite(storeLeftBound,item1S.getY() + item1S.getHeight() + storePadding, SpriteManager.mMedkitTR,vbom); final Sprite item3S = new Sprite(storeLeftBound,item2S.getY() + item2S.getHeight() + storePadding, SpriteManager.mClockTR,vbom); final Sprite item4S = new Sprite(storeLeftBound,item3S.getY() + item3S.getHeight() + storePadding, SpriteManager.mBCaseTR,vbom); item4S.setScaleCenter(0,0); item4S.setScale(2.0f); final Sprite item5S = new Sprite(storeLeftBound,item4S.getY() + (item4S.getHeight() * 2) + storePadding, SpriteManager.mCrateTR,vbom); final Sprite door = new Sprite(0,0, SpriteManager.mDoorTR,vbom); door.setPosition(storePadding * 2,BaseActivity.CAMERA_HEIGHT - (door.getHeight() + (storePadding * 2))); item1 = new Text(0,0,BaseActivity.mNotoSansFont,activity.getString(R.string.ammo), vbom); item2 = new Text(0,0,BaseActivity.mNotoSansFont,activity.getString(R.string.health), vbom); item3 = new Text(0,0,BaseActivity.mNotoSansFont,activity.getString(R.string.slowtime0), vbom); item4 = new Text(0,0,BaseActivity.mNotoSansFont,activity.getString(R.string.slowtime1), vbom); item5 = new Text(0,0,BaseActivity.mNotoSansFont,activity.getString(R.string.damage), vbom); item1.setPosition(item1S.getX() + item1S.getWidth(),item1S.getY()); item2.setPosition(item2S.getX() + item2S.getWidth(),item2S.getY()); item3.setPosition(item3S.getX() + item3S.getWidth(),item3S.getY()); item4.setPosition(item4S.getX() + (item4S.getWidth() * 2),item4S.getY()); item5.setPosition(item5S.getX() + item5S.getWidth(),item5S.getY()); item1price = new Text(item1.getX(),item1.getY() + item1.getHeight(),BaseActivity.mDigitFont,"$" + String.valueOf(price1), vbom); item2price = new Text(item2.getX(),item2.getY() + item2.getHeight(),BaseActivity.mDigitFont,"$" + String.valueOf(price2), vbom); item3price = new Text(item3.getX(),item3.getY() + item3.getHeight(),BaseActivity.mDigitFont,"$" + String.valueOf(price3), vbom); item4price = new Text(item4.getX(),item4.getY() + item4.getHeight(),BaseActivity.mDigitFont,"$" + String.valueOf(price4), vbom); item5price = new Text(item5.getX(),item5.getY() + item5.getHeight(),BaseActivity.mDigitFont,"$" + String.valueOf(price5), vbom); item1.setColor(Color.BLACK); item2.setColor(Color.BLACK); item3.setColor(Color.BLACK); item4.setColor(Color.BLACK); item5.setColor(Color.BLACK); item1price.setColor(Color.BLACK); item2price.setColor(Color.BLACK); item3price.setColor(Color.BLACK); item4price.setColor(Color.BLACK); item5price.setColor(Color.BLACK); updateMenu(); final Rectangle storeBG = new Rectangle(item1S.getX() - storePadding,item1.getY() - storePadding, BaseActivity.CAMERA_WIDTH - (item1S.getX() + storePadding),BaseActivity.CAMERA_HEIGHT - (item1.getY() + storePadding),vbom); storeBG.setColor(Color.WHITE); storeBG.setAlpha(GameScene.P_ALPHA); final Rectangle buy1 = new Rectangle(item1S.getX(), item1S.getY(), item1.getX() + item1.getWidth() - item1S.getX(), item1S.getHeight(), vbom) { @Override public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y) { if (pSceneTouchEvent.isActionUp()) { buyItem1(); } return true; }; }; this.registerTouchArea(buy1); final Rectangle buy2 = new Rectangle(item2S.getX(), item2S.getY(), item2.getX() + item2.getWidth() - item2S.getX(), item2S.getHeight(), vbom) { @Override public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y) { if (pSceneTouchEvent.isActionUp()) { buyItem2(); } return true; }; }; this.registerTouchArea(buy2); final Rectangle buy3 = new Rectangle(item3S.getX(), item3S.getY(), item3.getX() + item3.getWidth() - item3S.getX(), item3S.getHeight(), vbom) { @Override public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y) { if (pSceneTouchEvent.isActionUp()) { buyItem3(); } return true; }; }; this.registerTouchArea(buy3); final Rectangle buy4 = new Rectangle(item4S.getX(), item4S.getY(), item4.getX() + item4.getWidth() - item4S.getX(), item4S.getHeight(), vbom) { @Override public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y) { if (pSceneTouchEvent.isActionUp()) { buyItem4(); } return true; }; }; this.registerTouchArea(buy4); final Rectangle buy5 = new Rectangle(item5S.getX(), item5S.getY(), item5.getX() + item5.getWidth() - item5S.getX(), item5S.getHeight(), vbom) { @Override public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y) { if (pSceneTouchEvent.isActionUp()) { buyItem5(); } return true; }; }; this.registerTouchArea(buy5); storeTitle.detachSelf(); item1S.detachSelf(); item2S.detachSelf(); item3S.detachSelf(); item4S.detachSelf(); item5S.detachSelf(); door.detachSelf(); storeBG.detachSelf(); cashAvailable.detachSelf(); item1.detachSelf(); item2.detachSelf(); item3.detachSelf(); item4.detachSelf(); item5.detachSelf(); item1price.detachSelf(); item2price.detachSelf(); item3price.detachSelf(); item4price.detachSelf(); item5price.detachSelf(); shopHUD.attachChild(storeTitle); shopHUD.attachChild(cashAvailable); shopHUD.attachChild(storeBG); shopHUD.attachChild(item1); shopHUD.attachChild(item2); shopHUD.attachChild(item3); shopHUD.attachChild(item4); shopHUD.attachChild(item5); shopHUD.attachChild(item1price); shopHUD.attachChild(item2price); shopHUD.attachChild(item3price); shopHUD.attachChild(item4price); shopHUD.attachChild(item5price); shopHUD.attachChild(item1S); shopHUD.attachChild(item2S); shopHUD.attachChild(item3S); shopHUD.attachChild(item4S); shopHUD.attachChild(item5S); shopHUD.attachChild(door); mCamera.setHUD(shopHUD); storeTitle.registerEntityModifier(new DelayModifier(1.0f) { @Override protected void onModifierFinished(IEntity pItem) { done = true; } }); } private static void updateMenu() { final float pAlpha = 0.4f; cashAvailable.setText("$" + String.valueOf(GameScene.cash)); if (GameScene.cash < price1) { item1.setAlpha(pAlpha); item1price.setAlpha(pAlpha); } if (GameScene.cash < price2) { item2.setAlpha(pAlpha); item2price.setAlpha(pAlpha); } if (GameScene.cash < price3) { item3.setAlpha(pAlpha); item3price.setAlpha(pAlpha); } if (GameScene.cash < price4) { item4.setAlpha(pAlpha); item4price.setAlpha(pAlpha); } if (GameScene.cash < price5) { item5.setAlpha(pAlpha); item5price.setAlpha(pAlpha); } } private void buyItem1() { if (!done) { return; } if (GameScene.cash > price1) { GameScene.cash -= price1; Rifle.roundsLeft += 5; SoundManager.mBagSound.play(); updateMenu(); } else { SoundManager.mErrorSound.play(); } } private void buyItem2() { if (!done) { return; } if (GameScene.cash > price2) { GameScene.cash -= price2; GameScene.setHP(GameScene.playerHP + 1); SoundManager.mBagSound.play(); updateMenu(); } else { SoundManager.mErrorSound.play(); } } private void buyItem3() { if (!done) { return; } if (GameScene.cash > price3 && GameScene.SLOW_TIME_RECHARGE_RATE < 1.0f) { GameScene.cash -= price3; GameScene.SLOW_TIME_RECHARGE_RATE += 0.25f; SoundManager.mBagSound.play(); updateMenu(); } else { SoundManager.mErrorSound.play(); } } private void buyItem4() { if (!done) { return; } if (GameScene.cash > price4 && GameScene.slowTimeSpeedModifier > 0.15f) { GameScene.cash -= price4; GameScene.slowTimeSpeedModifier -= 0.10; SoundManager.mBagSound.play(); updateMenu(); } else { SoundManager.mErrorSound.play(); } } private void buyItem5() { if (!done) { return; } if (GameScene.cash > price5 || Rifle.damage >= 3) { GameScene.cash -= price5; Rifle.damage += 5; SoundManager.mBagSound.play(); updateMenu(); } else { SoundManager.mErrorSound.play(); } } @Override public boolean onSceneTouchEvent(Scene arg0, TouchEvent arg1) { if (!done) { return true; } GameScene.inGame = true; GameScene.tStart = BaseActivity.getSharedInstance().getEngine().getSecondsElapsedTotal(); GameScene.initialCash = GameScene.cash; // Prevent player from spending money then receiving it back upon mission failure GameScene.resume(); mCamera.setHUD(HUDManager.mHUD); if (SurvivorScene.inSurvivorMode) { GameScene.loadChildScene(new SurvivorScene()); } else { GameScene.getSharedInstance().clearChildScene(); } Rifle.getSharedInstance().resetValues(); Reticle.show(); EnemyLayer.show(); ItemLayer.show(); return false; } }