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; //from w ww . ja v a 2 s. co m import com.cladophora.ssniper.entity.Reticle; import com.cladophora.ssniper.entity.Rifle; import com.cladophora.ssniper.scene.GameScene; import org.andengine.engine.camera.hud.HUD; import org.andengine.engine.handler.timer.ITimerCallback; import org.andengine.engine.handler.timer.TimerHandler; import org.andengine.entity.primitive.Rectangle; import org.andengine.entity.sprite.Sprite; import org.andengine.entity.text.Text; import org.andengine.opengl.vbo.VertexBufferObjectManager; import org.andengine.util.color.Color; /** * Created by jmar on 1/30/14. */ public class HUDManager { // HUD Entities public static Sprite healthIcon; public static Sprite targetIcon; public static Sprite ammoIcon; public static Rectangle targetHead; public static Rectangle targetBody; public static Text healthCount; public static Text ammoCount; public static Text clock; public static Text cashCounter; public static Rectangle slowTimeBar; public static HUD mHUD; public static void initializeHUD() { createHUDEntities(); positionHUDChildren(); prepareHUDChildren(); attachHUDChildren(); updateHUDAmmoCount(); registerClockUpdateHandler(); GameScene.mCamera.setHUD(mHUD); BaseActivity.mScopeCamera.setChaseEntity(Reticle.center); BaseActivity.mScopeCamera.setZoomFactor(3); } private static void createHUDEntities() { mHUD = new HUD(); final VertexBufferObjectManager vbom = BaseActivity.getSharedInstance().getVertexBufferObjectManager(); slowTimeBar = new Rectangle(0,0, GameScene.SLOW_TIME_BAR_WIDTH,0,vbom); targetHead = new Rectangle(0,0, GameScene.TARGET_INDICATOR_SIZE, GameScene.TARGET_INDICATOR_SIZE,vbom); targetBody = new Rectangle(0,0, GameScene.TARGET_INDICATOR_SIZE, GameScene.TARGET_INDICATOR_SIZE,vbom); healthIcon = new Sprite(0, 0, SpriteManager.mHealthTR, vbom); ammoIcon = new Sprite(0, 0, SpriteManager.mAmmoTR, vbom); targetIcon = new Sprite(0, 0, SpriteManager.mTargetTR, vbom); healthCount = new Text(0, 0, BaseActivity.mNotoSansFont, "x 3", "x XXX".length(), vbom); ammoCount = new Text(0, 0, BaseActivity.mNotoSansFont, "x 15", "x XXX".length(), vbom); clock = new Text(0, 0, BaseActivity.mDigitFontL, "000.00", "XXX.XX".length(), vbom); cashCounter = new Text(0, 0, BaseActivity.mDigitFont, "$ 0", "$ 000000000".length(), vbom); healthCount.setColor(Color.BLACK); ammoCount.setColor(Color.BLACK); clock.setColor(Color.BLACK); clock.setAlpha(GameScene.P_ALPHA); cashCounter.setColor(Color.BLACK); } private static void positionHUDChildren() { final float hudMargin = 10; final float hudPadding = 20; final float bottomHudHeight = BaseActivity.CAMERA_HEIGHT - SpriteManager.mHealthTR.getHeight() - 10; final float healthAreaX = SpriteManager.sprite.getWidth(); final float healthCountX = healthAreaX + SpriteManager.mHealthTR.getWidth(); final float healthCountY = bottomHudHeight + (SpriteManager.mHealthTR.getHeight() - healthCount.getHeight()); final float ammoIconX = healthCountX + healthCount.getWidth() + (hudPadding * 3); final float ammoCountX = ammoIconX + (SpriteManager.mAmmoTR.getWidth() + hudPadding); final float ammoCountY = bottomHudHeight + (SpriteManager.mAmmoTR.getHeight() - ammoCount.getHeight()); final float clockX = (BaseActivity.CAMERA_WIDTH / 2) - 180; final float cashCounterX0 = clockX + (clock.getWidth() + 100); final float bulletSpacingY = SpriteManager.bullet0.getHeight() + 8; final float bullet0X = (BaseActivity.CAMERA_WIDTH - hudMargin) - SpriteManager.mBulletIconTR.getWidth(); final float bullet0Y = (BaseActivity.CAMERA_HEIGHT - hudMargin) - (5 * bulletSpacingY); final float slowTimeBarX0 = -GameScene.SLOW_TIME_BAR_WIDTH; final float slowTimeBarY0 = -(Reticle.RETICLE_RADIUS + Reticle.RETICLE_INNERRADIUS); healthIcon.setPosition(healthAreaX, bottomHudHeight); healthCount.setPosition(healthCountX, healthCountY); healthCount.setText("x" + String.format("%d", GameScene.playerHP)); SpriteManager.bullet0.setPosition(bullet0X, bullet0Y); SpriteManager.bullet1.setPosition(bullet0X, bullet0Y + bulletSpacingY * 1); SpriteManager.bullet2.setPosition(bullet0X, bullet0Y + bulletSpacingY * 2); SpriteManager.bullet3.setPosition(bullet0X, bullet0Y + bulletSpacingY * 3); SpriteManager.bullet4.setPosition(bullet0X, bullet0Y + bulletSpacingY * 4); ammoIcon.setPosition(ammoIconX, bottomHudHeight); ammoCount.setPosition(ammoCountX, ammoCountY); clock.setPosition(clockX, hudMargin); clock.setColor(Color.BLACK); slowTimeBar.setScaleCenter(0, 0); slowTimeBar.setPosition(slowTimeBarX0,slowTimeBarY0); cashCounter.setPosition(cashCounterX0, hudMargin); // Coords on unscaled image final float thX = 42; final float thY = 15; final float tbX = 42; final float tbY = 90; targetHead.registerEntityModifier(GameScene.blinkModifier); targetBody.registerEntityModifier(GameScene.blinkModifier); targetIcon.setVisible(false); targetHead.setVisible(false); targetBody.setVisible(false); targetHead.setPosition(thX, thY); targetBody.setPosition(tbX, tbY); targetIcon.setPosition(BaseActivity.CAMERA_WIDTH - (SpriteManager.mTargetTR.getWidth() + hudMargin), hudMargin); targetIcon.setAlpha(GameScene.P_ALPHA); } private static void prepareHUDChildren() { healthIcon.detachSelf(); healthCount.detachSelf(); ammoIcon.detachSelf(); ammoCount.detachSelf(); SpriteManager.casing.detachSelf(); SpriteManager.bullet0.detachSelf(); SpriteManager.bullet1.detachSelf(); SpriteManager.bullet2.detachSelf(); SpriteManager.bullet3.detachSelf(); SpriteManager.bullet4.detachSelf(); SpriteManager.sprite.detachSelf(); SpriteManager.flash.detachSelf(); clock.detachSelf(); cashCounter.detachSelf(); Reticle.center.detachSelf(); targetIcon.detachSelf(); targetHead.detachSelf(); targetBody.detachSelf(); slowTimeBar.detachSelf(); mHUD.detachChildren(); } private static void attachHUDChildren() { GameScene.getSharedInstance().attachChild(Reticle.center); Reticle.center.attachChild(slowTimeBar); mHUD.attachChild(healthIcon); mHUD.attachChild(healthCount); mHUD.attachChild(ammoIcon); mHUD.attachChild(ammoCount); mHUD.attachChild(SpriteManager.casing); mHUD.attachChild(SpriteManager.bullet0); mHUD.attachChild(SpriteManager.bullet1); mHUD.attachChild(SpriteManager.bullet2); mHUD.attachChild(SpriteManager.bullet3); mHUD.attachChild(SpriteManager.bullet4); mHUD.attachChild(clock); mHUD.attachChild(cashCounter); mHUD.attachChild(SpriteManager.sprite); mHUD.attachChild(SpriteManager.flash); mHUD.attachChild(targetIcon); targetIcon.attachChild(targetHead); targetIcon.attachChild(targetBody); } public static void registerClockUpdateHandler() { clock.registerUpdateHandler(new TimerHandler(GameScene.CLOCK_RESOLUTION, true, getClockCallback())); } private static ITimerCallback getClockCallback() { return new ITimerCallback() { float timePassed; @Override public void onTimePassed(final TimerHandler pTimerHandler) { timePassed = BaseActivity.getSharedInstance().getEngine().getSecondsElapsedTotal(); GameScene.timeRemaining = (GameScene.missionTime + GameScene.bonusTime + GameScene.slowTime) - (timePassed - GameScene.tStart); if (GameScene.timeRemaining >= 0) { clock.setText(String.format("%.1f", GameScene.timeRemaining)); updateSlowTimeBar(); GameScene.sortEnemies(); } else { BaseActivity.getSharedInstance().getEngine().runOnUpdateThread( new Runnable() { @Override public void run() { GameScene.getSharedInstance().endMission(); } } ); } } }; } public static void updateHUDAmmoCount() { if (ammoCount != null) { ammoCount.setText("x" + String.format("%d", Rifle.roundsLeft)); } } public static void updateCashCount() { if (GameScene.cash > 0) { cashCounter.setText("$ " + String.format("%d", GameScene.cash)); } } public static void updateSlowTimeBar() { float slowTimeBankPCT = (GameScene.slowTimeBank / GameScene.SLOW_TIME_BANK_MAX) * 100; slowTimeBar.setHeight(slowTimeBankPCT * (GameScene.SLOW_TIME_BAR_MAXHEIGHT / 100)); int pct = (int) slowTimeBankPCT; if (pct >= 100) { slowTimeBar.setColor(Color.BLUE); return; } if (pct < 100) { slowTimeBar.setColor(Color.RED); return; } } public static void hideTarget() { targetIcon.setVisible(false); targetBody.setVisible(false); targetHead.setVisible(false); } }