Android Open Source - ssniper-andengine Game Events






From Project

Back to project page ssniper-andengine.

License

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.

Java Source Code

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));
    }
}




Java Source Code List

com.cladophora.ssniper.BaseActivity.java
com.cladophora.ssniper.DeviceUtil.java
com.cladophora.ssniper.EnemyPool.java
com.cladophora.ssniper.GameEffects.java
com.cladophora.ssniper.GameEvents.java
com.cladophora.ssniper.GameLoopUpdateHandler.java
com.cladophora.ssniper.GameUtil.java
com.cladophora.ssniper.HUDManager.java
com.cladophora.ssniper.HoverListener.java
com.cladophora.ssniper.ItemPool.java
com.cladophora.ssniper.LevelLoader.java
com.cladophora.ssniper.PlantPool.java
com.cladophora.ssniper.SPenHoverView.java
com.cladophora.ssniper.ScopeEngine.java
com.cladophora.ssniper.SoundManager.java
com.cladophora.ssniper.SpriteManager.java
com.cladophora.ssniper.andengine.IRandomTimerCallBack.java
com.cladophora.ssniper.andengine.RandomTimerHandler.java
com.cladophora.ssniper.entity.EnemyLayer.java
com.cladophora.ssniper.entity.Enemy.java
com.cladophora.ssniper.entity.ItemLayer.java
com.cladophora.ssniper.entity.Item.java
com.cladophora.ssniper.entity.PlantLayer.java
com.cladophora.ssniper.entity.Plant.java
com.cladophora.ssniper.entity.Reticle.java
com.cladophora.ssniper.entity.Rifle.java
com.cladophora.ssniper.entity.SPen.java
com.cladophora.ssniper.scene.GameScene.java
com.cladophora.ssniper.scene.InstructionScene.java
com.cladophora.ssniper.scene.IntroScene.java
com.cladophora.ssniper.scene.MainMenuScene.java
com.cladophora.ssniper.scene.PauseScene.java
com.cladophora.ssniper.scene.ResultScene.java
com.cladophora.ssniper.scene.ShopScene.java
com.cladophora.ssniper.scene.SplashScene.java
com.cladophora.ssniper.scene.SurvivorIntroScene.java
com.cladophora.ssniper.scene.SurvivorScene.java
com.cladophora.ssniper.scene.TutorialScene.java
com.cladophora.ssniper.scene.WeaponInfoScene.java