Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Assets/Match3/Scripts/Match3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -532,13 +534,15 @@ public bool TryIsGameOver() {
if (score >= levelSO.targetScore) {
// Reached Target Score!
OnWin?.Invoke(this, EventArgs.Empty);
OnGameEnd?.Invoke(this, EventArgs.Empty);
return true;
}
break;
case LevelSO.GoalType.Glass:
if (GetGlassAmount() <= 0) {
// All glass destroyed!
OnWin?.Invoke(this, EventArgs.Empty);
OnGameEnd?.Invoke(this, EventArgs.Empty);
return true;
}
break;
Expand Down
56 changes: 47 additions & 9 deletions Assets/Match3/Scripts/Match3GameSimulation.cs
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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) {
Expand All @@ -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
}

}