using UnityEngine; using System.Collections; using System.Text; /// /// This is the data collected during a game. It will not be persisted. /// The data is reset on every game played. /// public class GameDataManager : MonoBehaviour { #region Unity Singleton private static GameDataManager _instance; public static GameDataManager Instance { get { if (_instance == null) { _instance = FindObjectOfType(typeof(GameDataManager)) as GameDataManager; if (_instance == null) { AVDebug.LogError(string.Format("No gameObject with {0} component exists. Make sure to create a gameObject with {0} component", new System.Diagnostics.StackFrame().GetMethod().DeclaringType)); } } return _instance; } } void Awake() { if (_instance != null && _instance != this) { AVDebug.LogWarning(string.Format("{0} Instance already exists on another gameObject. Destroying this gameObject {1}", this.GetType().Name, gameObject.name)); Destroy(gameObject); return; } _instance = this; DontDestroyOnLoad(gameObject); RegisterListeners(); } void OnDestroy() { UnregisterListeners(); } #endregion int _score; public int Score { get { return _score; } } int[][] _enemiesCaughtWithWeapon; int[] _weaponUpgradesPickedUp; int[] _enemiesGoneThrough; public bool HasStartedSpawningEnemies; const string bestScoreKeyPrefix = "bestScore_"; public int SpecialSpermsGoneThrough { get { return GetEnemyTypeGoneThrough(EnemyType.bronzeSperm) + GetEnemyTypeGoneThrough(EnemyType.silverSperm) + GetEnemyTypeGoneThrough(EnemyType.goldSperm); } } public int SpermsGoneThrough { get { return SpecialSpermsGoneThrough + GetEnemyTypeGoneThrough(EnemyType.standardSperm); } } float _timePassed; public float TimePassed { get { return _timePassed; } } int _biggestCountOfSpermsHitBySpermicidalWave; public int BiggestCountOfSpermsHitBySpermicidalWave { get { return _biggestCountOfSpermsHitBySpermicidalWave; } } int _spermsHitInCurrentSpermicidalWave; int _biggestCountOfEnemiesHitByRocket; public int BiggestCountOfEnemiesHitByRocket { get { return _biggestCountOfEnemiesHitByRocket; } } int _biggestCountOfEnemiesCaughtByGiantCondom; public int BiggestCountOfEnemiesCaughtByGiantCondom { get { return _biggestCountOfEnemiesCaughtByGiantCondom; } } #region Unity Callbacks void RegisterListeners() { NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart); NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver); NotificationCenter.AddListener(OnAddToScore, NotificationType.AddToScore); NotificationCenter.AddListener(OnHitSomething, NotificationType.HitSomething); NotificationCenter.AddListener(OnPickedUpWeaponUpgrade, NotificationType.PickedUpWeaponUpgrade); NotificationCenter.AddListener(OnEnemyGoneThrough, NotificationType.EnemyGoneThrough); NotificationCenter.AddListener(OnFiredSpermidicalWave, NotificationType.FireSpermicidalWave); NotificationCenter.AddListener(OnSpermHitBySpermicidalWave, NotificationType.SpermHitBySpermicidalWave); NotificationCenter.AddListener(OnRocketHitEnemies, NotificationType.RocketHitEnemies); NotificationCenter.AddListener(OnGiantCondomCaughtEnemies, NotificationType.GiantCondomCaughtEnemies); DebugViewManager.OnDebugView += OnDebugView; CoreNotificationCenter.AddListener(OnResetData, CoreNotificationType.ResetData); NotificationCenter.AddListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies); } void UnregisterListeners() { NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart); NotificationCenter.RemoveListener(OnGameOver, NotificationType.GameOver); NotificationCenter.RemoveListener(OnAddToScore, NotificationType.AddToScore); NotificationCenter.RemoveListener(OnHitSomething, NotificationType.HitSomething); NotificationCenter.RemoveListener(OnPickedUpWeaponUpgrade, NotificationType.PickedUpWeaponUpgrade); NotificationCenter.RemoveListener(OnEnemyGoneThrough, NotificationType.EnemyGoneThrough); NotificationCenter.RemoveListener(OnFiredSpermidicalWave, NotificationType.FireSpermicidalWave); NotificationCenter.RemoveListener(OnSpermHitBySpermicidalWave, NotificationType.SpermHitBySpermicidalWave); NotificationCenter.RemoveListener(OnRocketHitEnemies, NotificationType.RocketHitEnemies); NotificationCenter.RemoveListener(OnGiantCondomCaughtEnemies, NotificationType.GiantCondomCaughtEnemies); DebugViewManager.OnDebugView -= OnDebugView; CoreNotificationCenter.RemoveListener(OnResetData, CoreNotificationType.ResetData); NotificationCenter.RemoveListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies); } void Update() { if (HasStartedSpawningEnemies && GameUpdateManager.Instance._isPlaying) { _timePassed += Time.deltaTime; } else { //Debug.Log(_timePassed); } } #endregion void OnDebugView() { if (GUILayout.Button("Fake Game Data for Simulating Achievements")) { EnemyType[] enemyTypes = (EnemyType[])System.Enum.GetValues(typeof(EnemyType)); foreach (EnemyType e in enemyTypes) { _enemiesCaughtWithWeapon[(int)e][(int)PickupType.DefaultWeapon] = 100; _enemiesGoneThrough[(int)e] = 0; } PickupType[] weaponTypes = (PickupType[])System.Enum.GetValues(typeof(PickupType)); foreach (PickupType w in weaponTypes) { _weaponUpgradesPickedUp[(int)w] = 100; } _timePassed = 100 * 60; _biggestCountOfEnemiesCaughtByGiantCondom = 100; _biggestCountOfEnemiesHitByRocket = 100; _biggestCountOfSpermsHitBySpermicidalWave = 100; } } #region Notifications void OnGameStart(Notification note) { ResetData(); NotificationCenter.Post(NotificationType.UpdateScoreLabel); } void OnStartSpawningEnemies(Notification note) { HasStartedSpawningEnemies = true; } void OnGameOver(Notification note) { //Output the stats int levelIndex = LevelsManager.Instance.CurrentLevelIndex; StringBuilder sb = new StringBuilder(); sb.AppendLine("Game Stats"); sb.AppendLine("=========="); sb.AppendLine("Level:"+levelIndex); sb.AppendLine("Score: "+_score); sb.AppendLine("Time: "+_timePassed); sb.AppendLine("Biggest Spermicidal Wave: " + _biggestCountOfSpermsHitBySpermicidalWave); sb.AppendLine("Biggest Rocket hit: " + _biggestCountOfEnemiesHitByRocket); sb.AppendLine("Biggest Giant Condom catch: " + _biggestCountOfEnemiesCaughtByGiantCondom); sb.AppendLine("Enemies Caught/PassedThrough:"); EnemyType[] enemyTypes = (EnemyType[])System.Enum.GetValues(typeof(EnemyType)); foreach (EnemyType e in enemyTypes) { sb.AppendLine(string.Format("-{0}: {1}/{2}", e, GetEnemyTypeCaught(e), GetEnemyTypeGoneThrough(e))); } sb.AppendLine("Weapon Upgrades picked up"); PickupType[] weaponTypes = (PickupType[])System.Enum.GetValues(typeof(PickupType));; foreach (PickupType w in weaponTypes) { sb.AppendLine(string.Format("-{0}: {1}", w, GetWeaponUpgradeTypePickedUp(w))); } Logger.LogFlurryEvent("Game Stats", "summary", sb.ToString()); if (LevelsManager.Instance.CurrentLevel.AreAllObjectivesMet()) { Logger.LogFlurryEvent("Level completed", "level", LevelsManager.Instance.CurrentLevelIndex.ToString()); } AVDebug.Log(sb.ToString()); if (_score > GetBestScoreForCurrentLevel()) { PlayerPrefs.SetInt(bestScoreKeyPrefix+levelIndex, _score); } } void OnResetData(CoreNotification note) { AVDebug.Log("Resetting Game Data"); ResetData(); } void OnAddToScore(Notification note) { ScoreChangeData data = (ScoreChangeData)note.data; _score += data.scoreDelta; NotificationCenter.Post(NotificationType.UpdateScoreLabel); } void OnHitSomething(Notification note) { HitEvent hitEvent = (HitEvent)note.data; Enemy enemy = hitEvent.hitObject.GetComponent(); int enemyType = (int)enemy.type; ++_enemiesCaughtWithWeapon[enemyType][(int)hitEvent.projectile.type]; ++_enemiesCaughtWithWeapon[enemyType][(int)PickupType.AnyWeapon]; ++_enemiesCaughtWithWeapon[(int)EnemyType.anyEnemy][(int)PickupType.AnyWeapon]; ++_enemiesCaughtWithWeapon[(int)EnemyType.anyEnemy][(int)hitEvent.projectile.type]; //if it's a sperm if (enemyType >= (int)EnemyType.standardSperm && enemyType <= (int)EnemyType.goldSperm) { ++_enemiesCaughtWithWeapon[(int)EnemyType.anySperm][(int)PickupType.AnyWeapon]; ++_enemiesCaughtWithWeapon[(int)EnemyType.anySperm][(int)hitEvent.projectile.type]; } //if it's a virus if (enemyType < (int)EnemyType.standardSperm) { ++_enemiesCaughtWithWeapon[(int)EnemyType.anyVirus][(int)PickupType.AnyWeapon]; ++_enemiesCaughtWithWeapon[(int)EnemyType.anyVirus][(int)hitEvent.projectile.type]; } } void OnPickedUpWeaponUpgrade(Notification note) { PickupType pickupType = (PickupType)note.data; ++_weaponUpgradesPickedUp[(int)pickupType]; } void OnEnemyGoneThrough(Notification note) { EnemyType enemyType = (EnemyType)note.data; //AVDebug.Log("enemyType passed through: "+enemyType); ++_enemiesGoneThrough[(int)enemyType]; Logger.LogFlurryEvent("Enemy Gone Through", "Virus", enemyType.ToString(), "Level", LevelsManager.Instance.CurrentLevelIndex.ToString()); } void OnFiredSpermidicalWave(Notification note) { _spermsHitInCurrentSpermicidalWave = 0; } void OnSpermHitBySpermicidalWave(Notification note) { Enemy enemy = (Enemy)note.data; ++_enemiesCaughtWithWeapon[(int)enemy.type][(int)PickupType.SpermicidalWave]; ++_spermsHitInCurrentSpermicidalWave; if (_spermsHitInCurrentSpermicidalWave > _biggestCountOfSpermsHitBySpermicidalWave) { _biggestCountOfSpermsHitBySpermicidalWave = _spermsHitInCurrentSpermicidalWave; } } void OnRocketHitEnemies(Notification note) { int enemiesHit = (int)note.data; if (enemiesHit > _biggestCountOfEnemiesHitByRocket) { _biggestCountOfEnemiesHitByRocket = enemiesHit; } } void OnGiantCondomCaughtEnemies(Notification note) { int enemiesCaught = (int)note.data; if (enemiesCaught > _biggestCountOfEnemiesCaughtByGiantCondom) { _biggestCountOfEnemiesCaughtByGiantCondom = enemiesCaught; } } #endregion void ResetData() { _score = 0; _timePassed = 0; HasStartedSpawningEnemies = false; _biggestCountOfSpermsHitBySpermicidalWave = 0; _spermsHitInCurrentSpermicidalWave = 0; _biggestCountOfEnemiesHitByRocket = 0; _biggestCountOfEnemiesCaughtByGiantCondom = 0; if (_enemiesCaughtWithWeapon == null) { int numOfEnemyTypes = System.Enum.GetNames(typeof(EnemyType)).Length; _enemiesGoneThrough = new int[numOfEnemyTypes]; int numOfWeaponUpgrades = System.Enum.GetNames(typeof(PickupType)).Length; _weaponUpgradesPickedUp = new int[numOfWeaponUpgrades]; _enemiesCaughtWithWeapon = new int[numOfEnemyTypes][]; for (int i = 0; i < numOfEnemyTypes; i++) { _enemiesCaughtWithWeapon[i] = new int[numOfWeaponUpgrades]; } } else { for (int i = 0; i < _enemiesCaughtWithWeapon.Length; i++) { for (int j = 0; j < _enemiesCaughtWithWeapon[i].Length; j++) { _enemiesCaughtWithWeapon[i][j] = 0; } _enemiesGoneThrough[i] = 0; } for (int i = 0; i < _weaponUpgradesPickedUp.Length; i++) { _weaponUpgradesPickedUp[i] = 0; } } } public int GetEnemyTypeCaught(EnemyType enemyType) { return _enemiesCaughtWithWeapon[(int)enemyType][(int)PickupType.AnyWeapon]; } public int GetEnemyTypeCaughtWithWeapon(EnemyType enemyType, PickupType weaponType) { return _enemiesCaughtWithWeapon[(int)enemyType][(int)weaponType]; } public int GetEnemyTypeGoneThrough(EnemyType enemyType) { return _enemiesGoneThrough[(int)enemyType]; } public int GetWeaponUpgradeTypePickedUp(PickupType pickupType) { return _weaponUpgradesPickedUp[(int)pickupType]; } public int GetBestScoreForCurrentLevel() { return PlayerPrefs.GetInt(bestScoreKeyPrefix+LevelsManager.Instance.CurrentLevelIndex, 0); } }