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 w w. ja v a2 s . co m import com.cladophora.ssniper.entity.*; 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.IEntityModifier; import org.andengine.entity.modifier.RotationModifier; import org.andengine.util.modifier.IModifier; import java.util.Iterator; /** * Created by jmar on 1/25/14. */ public class GameEvents { public static final float ENEMY_DEATH_ANIMATION_DURATION = 0.9f; public static void fire() { synchronized (GameScene.getSharedInstance()) { if (!GameScene.inGame) {return;} boolean roundFired = Rifle.getSharedInstance().pullTrigger(); if (!roundFired) { return; } else { GameScene.roundsFired++; } final float x0 = Reticle.center.getX(); final float y0 = Reticle.center.getY(); final int multiEnemyMultiplier = 1; boolean hitSomething = false; int enemiesHit = 0; // Check for hit Iterator<Enemy> enemyIterator = EnemyLayer.getIterator(); while (enemyIterator.hasNext()) { Enemy enemy = enemyIterator.next(); if (Reticle.center.collidesWith(enemy.head)) { hitSomething = true; enemiesHit++; GameScene.totalHeadshots++; float[] coords = enemy.sprite.convertSceneToLocalCoordinates(x0,y0); GameScene.getSharedInstance().showHeadshotIndicator(); final boolean enemySurvived = enemy.applyDamageHead(); if (!enemySurvived) { GameEffects.createExplosion(coords[0], coords[1], enemy.sprite, BaseActivity.getSharedInstance()); GameScene.getSharedInstance().addBonusTime(GameScene.headShotBonus); destroy(enemy, 1); enemyIterator.remove(); } else { GameEffects.createSmallExplosion(coords[0], coords[1], enemy.sprite, BaseActivity.getSharedInstance()); } } else if (Reticle.center.collidesWith(enemy.body)) { hitSomething = true; enemiesHit++; float[] coords = enemy.sprite.convertSceneToLocalCoordinates(x0,y0); GameScene.getSharedInstance().showBodyshotIndicator(); final boolean enemySurvived = enemy.applyDamage(); if (!enemySurvived) { GameEffects.createExplosion(coords[0], coords[1], enemy.sprite, BaseActivity.getSharedInstance()); GameScene.getSharedInstance().addBonusTime(GameScene.killBonus); destroy(enemy, 0); enemyIterator.remove(); } else { GameEffects.createSmallExplosion(coords[0], coords[1], enemy.sprite, BaseActivity.getSharedInstance()); } } } // Check for item hit Iterator<Item> itemIterator = ItemLayer.getIterator(); while (itemIterator.hasNext()) { Item item = itemIterator.next(); if (Item.isHit(item)) { hitSomething = true; } } if (hitSomething) { GameScene.roundsOnTarget++; } if (!hitSomething && !SurvivorScene.inSurvivorMode) { GameScene.getSharedInstance().takeDamage(GameScene.MISS_DAMAGE); } if (enemiesHit > 1) { GameScene.getSharedInstance().addBonusTime(enemiesHit * multiEnemyMultiplier); } checkForMissionEnd(); } } public static void checkForMissionEnd() { if (GameScene.isEnding) { return; } if (EnemyLayer.isEmpty() && !SurvivorScene.inSurvivorMode) { GameScene.getSharedInstance().scheduleMissionEnd(); return; } if (Rifle.magEmpty()) { final boolean reload = Rifle.getSharedInstance().reload(); if (!reload) { GameScene.getSharedInstance().scheduleMissionEnd(); return; } } } private static void destroy(final Enemy enemy, final int modifier) { enemy.body.clearEntityModifiers(); enemy.body.clearUpdateHandlers(); enemy.head.clearEntityModifiers(); enemy.head.clearUpdateHandlers(); enemy.sprite.clearEntityModifiers(); enemy.sprite.clearUpdateHandlers(); enemy.body.setRotationCenter(0,0); final RotationModifier enemyFallingModifier = new RotationModifier(ENEMY_DEATH_ANIMATION_DURATION,0,90); final IEntityModifier.IEntityModifierListener entityModifierListener = new IEntityModifier.IEntityModifierListener() { @Override public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) { } @Override public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) { BaseActivity.getSharedInstance().runOnUpdateThread(new Runnable() { @Override public void run() { ItemLayer.spawnItem(enemy.sprite.getX() + (enemy.sprite.getWidthScaled() / 2), enemy.sprite.getY() + enemy.sprite.getHeightScaled(), modifier, enemy.enemyType); EnemyPool.sharedEnemyPool().recyclePoolItem(enemy); } }); } }; enemy.sprite.registerEntityModifier(enemyFallingModifier); enemy.sprite.registerEntityModifier(new AlphaModifier(ENEMY_DEATH_ANIMATION_DURATION, 1, 0, entityModifierListener)); } }