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 a v a 2s . c om*/ import com.cladophora.ssniper.BaseActivity; import com.cladophora.ssniper.HUDManager; import com.cladophora.ssniper.SoundManager; import com.cladophora.ssniper.SpriteManager; import com.cladophora.ssniper.andengine.IRandomTimerCallBack; import com.cladophora.ssniper.andengine.RandomTimerHandler; import com.cladophora.ssniper.scene.GameScene; import com.cladophora.ssniper.scene.SurvivorScene; import org.andengine.entity.IEntity; import org.andengine.entity.modifier.AlphaModifier; import org.andengine.entity.modifier.MoveModifier; import org.andengine.entity.primitive.Rectangle; import org.andengine.entity.sprite.Sprite; import org.andengine.opengl.vbo.VertexBufferObjectManager; import org.andengine.util.color.Color; public class Enemy { private static final float MAX_MOVE_TIME = 4; public Rectangle body; public Rectangle head; public Sprite sprite; public Sprite flash; public static BaseActivity activity; public int hp; public boolean isMoving; public int enemyType; public float bodyWidth; public float bodyHeight; private float bodyWidthRatio; private float bodyHeightRatio; private float bodyOffsetRatioX; private float bodyOffsetRatioY; public float bodyOffsetX; public float bodyOffsetY; public float headWidth; public float headHeight; private float headWidthRatio; private float headHeightRatio; private float headOffsetRatioX; private float headOffsetRatioY; public float headOffsetX; public float headOffsetY; private float flashOffsetRatioX; private float flashOffsetRatioY; private static final Color spriteColor = Color.TRANSPARENT; private RandomTimerHandler mRandomTimerHandler; private IRandomTimerCallBack mRandomTimerCallBack; private VertexBufferObjectManager vbom; public Enemy() { activity = BaseActivity.getSharedInstance(); vbom = activity.getVertexBufferObjectManager(); init(); initializeCallBack(); } /* Method for initializing a new instance * Used by constructor and pool class */ public void init() { isMoving = false; enemyType = getRandomEnemyType(GameScene.levelID); initializeSprite(enemyType); getEnemySpriteProperties(enemyType); calculateHitboxLocations(); initializeHitBoxes(); attachChildren(); sprite.setScale(GameScene.enemyScale); sprite.setRotationCenter(sprite.getWidthScaled()* 0.8f,sprite.getHeightScaled()* 0.8f); setInitialHP(enemyType); setRandomInitialPosition(); setIgnoreUpdate(false); } private void setRandomInitialPosition() { float y0; float x0; if (SurvivorScene.inSurvivorMode) { y0 = -(sprite.getHeightScaled()) - (sprite.getHeightScaled() * BaseActivity.r.nextFloat()); x0 = BaseActivity.SCOPE_SIZE + (BaseActivity.CAMERA_WIDTH * BaseActivity.r.nextFloat()); } else { x0 = BaseActivity.SCOPE_SIZE + (BaseActivity.CAMERA_WIDTH * BaseActivity.r.nextFloat()); if (x0 > BaseActivity.CAMERA_WIDTH) { y0 = (BaseActivity.CAMERA_HEIGHT - sprite.getHeightScaled()) + (BaseActivity.CAMERA_HEIGHT * BaseActivity.r.nextFloat()); } else { y0 = -(sprite.getHeightScaled() + (sprite.getHeightScaled() * BaseActivity.r.nextFloat())); } } setPosition(x0, y0); } public void setIgnoreUpdate(final boolean bool) { sprite.setIgnoreUpdate(bool); body.setIgnoreUpdate(bool); head.setIgnoreUpdate(bool); } private static int getRandomEnemyType(int levelID) { if (SurvivorScene.inSurvivorMode) { return getRandomEnemyTypeSM(); } int normalEnemyRange = 999 - (levelID * 25); int bossEnemyRange = 999 - (levelID * 10); int randomInt = BaseActivity.r.nextInt(999); if (GameScene.difficultyLevel > 1) { normalEnemyRange -= GameScene.difficultyLevel * 50; bossEnemyRange -= GameScene.difficultyLevel * 50; } if (normalEnemyRange < 200) { normalEnemyRange = 200; } if (bossEnemyRange < 600) { bossEnemyRange = 200; } if (levelID <= 1) { return 1; } if (randomInt < normalEnemyRange) { return 1; } if (randomInt > bossEnemyRange) { if (levelID < 4) { return 2; } if (levelID > 5) { int secondRoll = BaseActivity.r.nextInt(99); if (secondRoll > (90 - levelID )) { return 5; } else if (secondRoll > (70 - levelID)) { return 4; } } return 3; } else { return 2; } } private static int getRandomEnemyTypeSM() { float bossEnemyRange = 1 - SurvivorScene.bossEnemyPct; float megaBossRange = 1 - SurvivorScene.megaBossEnemyPct; float superBossRange = 1 - (megaBossRange + SurvivorScene.superBossEnemyPct); final float firstRoll = BaseActivity.r.nextFloat(); final float secondRoll = BaseActivity.r.nextFloat(); if (firstRoll < bossEnemyRange) { if (firstRoll > bossEnemyRange / 2) { return 2; } return 1; } if (secondRoll > (megaBossRange)) { return 5; } else if (secondRoll > (superBossRange)) { return 4; } else { return 3; } } private void initializeSprite(int enemyType) { switch (enemyType) { case 1: sprite = new Sprite(0, 0, SpriteManager.mf1TR, vbom); flash = new Sprite(0, 0, SpriteManager.mflash1TR, vbom); break; case 2: sprite = new Sprite(0, 0, SpriteManager.mf2TR, vbom); flash = new Sprite(0, 0, SpriteManager.mflash2TR, vbom); break; case 3: sprite = new Sprite(0, 0, SpriteManager.mf3TR, vbom); flash = new Sprite(0, 0, SpriteManager.mflash3TR, vbom); break; case 4: sprite = new Sprite(0, 0, SpriteManager.mf4TR, vbom); flash = new Sprite(0, 0, SpriteManager.mflash4TR, vbom); break; case 5: sprite = new Sprite(0, 0, SpriteManager.mf5TR, vbom); flash = new Sprite(0, 0, SpriteManager.mflash5TR, vbom); break; default: sprite = new Sprite(0, 0, SpriteManager.mf1TR, vbom); flash = new Sprite(0, 0, SpriteManager.mflash1TR, vbom); break; } sprite.setScaleCenter(0,0); } private void getEnemySpriteProperties(int enemyType) { switch (enemyType) { case 1: headWidthRatio = 0.450f; headHeightRatio = 0.150f; headOffsetRatioX = 0.230f; headOffsetRatioY = 0.000f; // head should be at top of sprite bodyWidthRatio = headWidthRatio * 1.61f; bodyHeightRatio = 0.750f; bodyOffsetRatioX = 0.200f; // usually less than headOffsetRatioX bodyOffsetRatioY = headHeightRatio; // set equal to headHeightRatioY to connect to head flashOffsetRatioX = 0.001f; flashOffsetRatioY = 0.340f; break; case 2: headWidthRatio = 0.300f; headHeightRatio = 0.200f; headOffsetRatioX = 0.290f; headOffsetRatioY = 0.000f; bodyWidthRatio = 0.650f; bodyHeightRatio = 0.600f; bodyOffsetRatioX = 0.200f; bodyOffsetRatioY = 0.200f; flashOffsetRatioX = 0.018f; flashOffsetRatioY = 0.281f; break; case 3: headWidthRatio = 0.300f; headHeightRatio = 0.200f; headOffsetRatioX = 0.290f; headOffsetRatioY = 0.000f; bodyWidthRatio = 0.550f; bodyHeightRatio = 0.600f; bodyOffsetRatioX = 0.300f; bodyOffsetRatioY = 0.200f; flashOffsetRatioX = 0.021f; flashOffsetRatioY = 0.129f; break; case 4: headWidthRatio = 0.350f; headHeightRatio = 0.190f; headOffsetRatioX = 0.230f; headOffsetRatioY = 0.000f; bodyWidthRatio = 0.650f; bodyHeightRatio = 0.570f; bodyOffsetRatioX = 0.250f; bodyOffsetRatioY = 0.190f; flashOffsetRatioX = 0.202f; flashOffsetRatioY = 0.156f; break; case 5: headWidthRatio = 0.340f; headHeightRatio = 0.210f; headOffsetRatioX = 0.200f; headOffsetRatioY = 0.000f; bodyWidthRatio = 0.650f; bodyHeightRatio = 0.650f; bodyOffsetRatioX = 0.250f; bodyOffsetRatioY = 0.210f; flashOffsetRatioX = 0.093f; flashOffsetRatioY = 0.426f; break; default: headWidthRatio = 0.350f; headHeightRatio = 0.190f; headOffsetRatioX = 0.230f; headOffsetRatioY = 0.000f; bodyWidthRatio = 0.650f; bodyHeightRatio = 0.570f; bodyOffsetRatioX = 0.250f; bodyOffsetRatioY = 0.190f; flashOffsetRatioX = 0.400f; flashOffsetRatioY = 0.400f; break; } } public float getHeadCenterX() { return (sprite.getX() + headOffsetX / 2) + ((sprite.getWidthScaled() * headWidthRatio) / 2); } public float getHeadCenterY() { return (sprite.getY() + headOffsetY / 2) + ((sprite.getHeightScaled() * headHeightRatio) / 2) + 5; } private void calculateHitboxLocations() { headOffsetX = sprite.getWidth() * headOffsetRatioX; headOffsetY = sprite.getHeight() * headOffsetRatioY; headWidth = sprite.getWidth() * headWidthRatio; headHeight = sprite.getHeight() * headHeightRatio; bodyOffsetX = sprite.getWidth() * bodyOffsetRatioX; bodyOffsetY = sprite.getHeight() * bodyOffsetRatioY; bodyWidth = sprite.getWidth() * bodyWidthRatio; bodyHeight = sprite.getHeight() * bodyHeightRatio; } private void initializeHitBoxes() { body = new Rectangle(0 + bodyOffsetX, 0 + bodyOffsetY, bodyWidth, bodyHeight, vbom); head = new Rectangle(0 + headOffsetX, 0 + headOffsetY, headWidth, headHeight, vbom); flash.setPosition((sprite.getWidth() * flashOffsetRatioX) - (flash.getWidth()/2),(sprite.getHeight() * flashOffsetRatioY) - (flash.getHeight()/2)); body.setColor(spriteColor); head.setColor(spriteColor); flash.setVisible(false); } private void attachChildren() { body.detachSelf(); head.detachSelf(); flash.detachSelf(); sprite.attachChild(flash); sprite.attachChild(body); sprite.attachChild(head); } private void setInitialHP(int enemyType) { switch (enemyType) { case 1: hp = 1; break; case 2: hp = 3; break; case 3: hp = 5; break; case 4: hp = 10; break; case 5: hp = 15; break; default: hp = 7; break; } } public void setPosition(float x1, float y1) { sprite.setPosition(x1, y1); } public static float getRestModifier(int enemyType) { switch(enemyType) { case 1: return 0.7f; case 2: return 0.5f; case 3: return 0.2f; case 4: return 0.0f; case 5: return 0.0f; default: return 1.0f; } } public void move(float x1, float y1) { if (isMoving) { return; } isMoving = true; if (y1 > BaseActivity.CAMERA_HEIGHT) { y1 = BaseActivity.CAMERA_HEIGHT; } float moveDistanceX = (x1 - sprite.getX()); float moveDistanceY = (y1 - sprite.getY()); final float moveDistance = (float) Math.sqrt((moveDistanceX * moveDistanceX) + (moveDistanceY * moveDistanceY)); final float moveDuration = MAX_MOVE_TIME * (moveDistance / (BaseActivity.CAMERA_WIDTH * EnemyLayer.ENEMY_BOUNDS_WIDTHRATIO)); // add movement time to the RandomTimerHandler if (mRandomTimerHandler != null) { mRandomTimerHandler.addTime(moveDuration + 0.5f); } final MoveModifier spriteMoveModifier = new MoveModifier(moveDuration,sprite.getX(), x1, sprite.getY(), y1) { @Override protected void onModifierFinished(IEntity pItem) { isMoving = false; if (mRandomTimerHandler != null) { mRandomTimerHandler.generateRandomTime(); mRandomTimerHandler.addTime(moveDuration * getRestModifier(enemyType)); // increase resting time for longer moves } final boolean reachedGoal = sprite.getY() + sprite.getHeightScaled() > BaseActivity.CAMERA_HEIGHT; if (SurvivorScene.inSurvivorMode && reachedGoal) { reachGoal(); GameScene.getSharedInstance().unregisterUpdateHandler(mRandomTimerHandler); } } }; sprite.clearEntityModifiers(); sprite.registerEntityModifier(spriteMoveModifier); } public void beginFiringRandomly() { IRandomTimerCallBack fireWeaponCallBack = new IRandomTimerCallBack() { @Override public void onTimePassed(final RandomTimerHandler pRandomTimerHandler) { fireWeapon(); } }; float pTimerSecondsMin = 1.0f; float pTimerSecondsMax = 6.0f; sprite.registerUpdateHandler(new RandomTimerHandler(pTimerSecondsMin, pTimerSecondsMax, true, fireWeaponCallBack)); } private void fireWeapon() { if (hp <= 0) { return; } int enemiesLeft = EnemyLayer.size(); if (enemiesLeft <= 0) { return; } if (BaseActivity.r.nextFloat() < 1/enemiesLeft) { return; } flash.setVisible(true); flash.registerEntityModifier(new AlphaModifier(0.4f, 1, 0)); switch (enemyType) { case 1: SoundManager.mGun1Sound.play(); break; case 2: SoundManager.mGun2Sound.play(); break; case 3: SoundManager.mGun3Sound.play(); break; case 4: SoundManager.mGun4Sound.play(); break; case 5: SoundManager.mGun5Sound.play(); break; } } public void beginAttackMovement() { IRandomTimerCallBack attackCallback = new IRandomTimerCallBack() { @Override public void onTimePassed(final RandomTimerHandler pRandomTimerHandler) { advance(); } }; mRandomTimerHandler = new RandomTimerHandler(5.0f, 7.0f, true, attackCallback); sprite.registerUpdateHandler(mRandomTimerHandler); } public void beginRandomMovement(final float minWait, final float maxWait) { initializeRandomTimer(minWait, maxWait); sprite.registerUpdateHandler(mRandomTimerHandler); } public void initializeCallBack () { mRandomTimerCallBack = new IRandomTimerCallBack() { @Override public void onTimePassed(final RandomTimerHandler pRandomTimerHandler) { moveRandomly(); } }; } public void initializeRandomTimer(final float minWait, final float maxWait) { mRandomTimerHandler = new RandomTimerHandler(minWait, maxWait, true, mRandomTimerCallBack); } public void clean() { sprite.setVisible(false); sprite.clearEntityModifiers(); sprite.clearUpdateHandlers(); setIgnoreUpdate(true); } /* method for applying damage returns false if enemy is destroyed */ public boolean applyDamage() { synchronized (this) { hp -= Rifle.damage; if (enemyType >= 3) { moveRandomly(); } if (hp <= 0) { giveCash(); return false; } else { return true; } } } private void giveCash() { int cashBonus = 0; switch (enemyType) { case 1: cashBonus = 200; break; case 2: cashBonus = 750; break; case 3: cashBonus = 2500; break; case 4: cashBonus = 8888; break; case 5: cashBonus = 25000; break; } GameScene.cash += cashBonus; HUDManager.updateCashCount(); } public boolean applyDamageHead() { synchronized (this) { hp -= Rifle.damage * 3; if (enemyType >= 3) { moveRandomly(); } if (hp <= 0) { giveCash(); return false; } else { return true; } } } public void moveRandomly() { if (isMoving) { return; } if (SurvivorScene.inSurvivorMode) { advance(); return; } final float xr1 = EnemyLayer.leftBound + (EnemyLayer.ENEMY_BOUNDS_WIDTHRATIO * BaseActivity.r.nextFloat()); final float yr1 = EnemyLayer.upperBound + (EnemyLayer.ENEMY_BOUNDS_HEIGHTRATIO * BaseActivity.r.nextFloat()); float x1 = BaseActivity.CAMERA_WIDTH * xr1; float y1 = BaseActivity.CAMERA_HEIGHT * yr1; if (SurvivorScene.inSurvivorMode) { y1 = 0 + (BaseActivity.CAMERA_HEIGHT * 0.10f) * BaseActivity.r.nextFloat(); } move(x1, y1); } public void advance() { if (isMoving) { return; } final float xr1 = EnemyLayer.leftBound + (EnemyLayer.ENEMY_BOUNDS_WIDTHRATIO * BaseActivity.r.nextFloat()); final float x1 = BaseActivity.CAMERA_WIDTH * xr1; final float maxMovementY = SurvivorScene.currentMaxMovementSpeed * BaseActivity.CAMERA_HEIGHT; final float minMovementY = SurvivorScene.currentMinMovementSpeed * BaseActivity.CAMERA_HEIGHT; float movementY = minMovementY + (BaseActivity.r.nextFloat() * (maxMovementY - minMovementY)); float y0 = sprite.getY(); if (y0 < 0) { y0 = 0; } float y1 = y0 + movementY; move(x1, y1); } private void reachGoal() { this.setRandomInitialPosition(); this.init(); this.beginAttackMovement(); GameScene.getSharedInstance().takeDamage(1); } }