From 2a3ed54b707e98e3a99fea38f2668d8553c0a0db Mon Sep 17 00:00:00 2001 From: Matt Sorg Date: Wed, 3 Feb 2021 12:25:08 -0800 Subject: [PATCH] add an eventhandler for the end of the game and a metric that surfaces the score for every end game --- Assets/Match3/Scripts/Match3.cs | 4 ++ Assets/Match3/Scripts/Match3GameSimulation.cs | 56 ++++++++++++++++--- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Assets/Match3/Scripts/Match3.cs b/Assets/Match3/Scripts/Match3.cs index a119e38..0a79d54 100644 --- a/Assets/Match3/Scripts/Match3.cs +++ b/Assets/Match3/Scripts/Match3.cs @@ -16,6 +16,7 @@ public class Match3 : MonoBehaviour { public event EventHandler OnOutOfMoves; public event EventHandler OnScoreChanged; public event EventHandler OnWin; + public event EventHandler OnGameEnd; public class OnNewGemGridSpawnedEventArgs : EventArgs { public GemGrid gemGrid; @@ -523,6 +524,7 @@ public bool TryIsGameOver() { if (!HasMoveAvailable()) { // No more moves, game over! OnOutOfMoves?.Invoke(this, EventArgs.Empty); + OnGameEnd?.Invoke(this, EventArgs.Empty); return true; } @@ -532,6 +534,7 @@ public bool TryIsGameOver() { if (score >= levelSO.targetScore) { // Reached Target Score! OnWin?.Invoke(this, EventArgs.Empty); + OnGameEnd?.Invoke(this, EventArgs.Empty); return true; } break; @@ -539,6 +542,7 @@ public bool TryIsGameOver() { if (GetGlassAmount() <= 0) { // All glass destroyed! OnWin?.Invoke(this, EventArgs.Empty); + OnGameEnd?.Invoke(this, EventArgs.Empty); return true; } break; diff --git a/Assets/Match3/Scripts/Match3GameSimulation.cs b/Assets/Match3/Scripts/Match3GameSimulation.cs index 6a0c553..0b4d788 100644 --- a/Assets/Match3/Scripts/Match3GameSimulation.cs +++ b/Assets/Match3/Scripts/Match3GameSimulation.cs @@ -1,5 +1,7 @@ -using System.Collections.Generic; +using System.Collections; +using System.Collections.Generic; using UnityEngine; +using Unity.Simulation.Games; // TODO: Game Simulation uses the following namespace: Unity.Simulation.Games // Reference: https://docs.unity3d.com/Packages/com.unity.simulation.games@0.4/manual/ImplementationGuide.html @@ -23,9 +25,14 @@ private void Awake() { // This application loads level data by calling the SetLevelSO(LevelSO) method on the Match3 object. // Read the level from the game sim manager, find it in your levelList, and pass along the appropriate level to // your match3 instance. + GameSimManager.Instance.FetchConfig(OnFetchConfig); // TODO: Add event handler when winning/losing the game - // Match3 provides event handlers OnWin and OnOutOfMoves. + // Match3 provides event handlers OnWin and OnOutOfMoves + + match3.OnWin += Match3_OnWin; + match3.OnOutOfMoves += Match3_OnOutOfMoves; + match3.OnGameEnd += Match3_OnGameEnd; } private void Match3_OnWin(object sender, System.EventArgs e) { @@ -34,25 +41,56 @@ private void Match3_OnWin(object sender, System.EventArgs e) { // of them. // // GameSimManager.Instance.SetCounter(string name, long value) - + GameSimManager.Instance.SetCounter(levelSO.name + "_Win_MovesUsed", match3.GetUsedMoveCount()); + // GameSimManager.Instance.SetCounter(levelSO.name + "_Score", match3.GetScore()); // TODO: Gracefully exit + EndGameSimulation(); + } + + private void Match3_OnGameEnd(object sender, System.EventArgs e) { + // GameSimManager.Instance.SetCounter(levelSO.name + "_Win_MovesUsed", match3.GetUsedMoveCount()); + GameSimManager.Instance.SetCounter(levelSO.name + "_End_Score", match3.GetScore()); + EndGameSimulation(); } + private void Match3_OnOutOfMoves(object sender, System.EventArgs e) { // TODO: Enable tracking metric Lose when the game is lost // As above, you'll want to use SetCounter to track losing. - + GameSimManager.Instance.SetCounter(levelSO.name + "_Lose", 1); // TODO: Gracefully exit + EndGameSimulation(); + } + + private void OnFetchConfig(GameSimConfigResponse gameSimConfigResponse) { + string levelName = gameSimConfigResponse.GetString("level"); + + LevelSO loadLevel = null; + + foreach (LevelSO levelSO in levelList) { + if (levelSO.name == levelName) { + loadLevel = levelSO; + break; + } + } + + if (loadLevel == null) { + Debug.Log("Could not find level with name: " + levelName); + loadLevel = levelList[0]; + } + + this.levelSO = loadLevel; + match3.SetLevelSO(loadLevel); } // Gracefully end the simulation private void EndGameSimulation() { // It's convenient for this to function in the expected manner in editor.. -#if UNITY_EDITOR - UnityEditor.EditorApplication.isPlaying = false; -#else - Application.Quit(); -#endif + #if UNITY_EDITOR + UnityEditor.EditorApplication.isPlaying = false; + #else + Application.Quit(); + #endif } }