Implemented end of game screen
This commit is contained in:
parent
ac10c8a2fc
commit
3651adbef8
23 changed files with 20035 additions and 19365 deletions
|
|
@ -9,5 +9,7 @@ namespace DungeonSelection
|
|||
public string DungeonName;
|
||||
public Sprite Thumbnail;
|
||||
public string SceneName;
|
||||
public string WinMessage;
|
||||
public string DieMessage;
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,5 @@ namespace DungeonSelection
|
|||
public static Action NextDungeonClicked;
|
||||
public static Action PreviousDungeonClicked;
|
||||
public static Action<DungeonData> EnterDungeonClicked;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -3,9 +3,33 @@ using UnityEngine;
|
|||
using Object = UnityEngine.Object;
|
||||
using System.Collections.Generic;
|
||||
using Abilities;
|
||||
using DungeonSelection;
|
||||
using TMPro;
|
||||
using Unity.VisualScripting;
|
||||
|
||||
public class EndOfGameEventArgs : EventArgs
|
||||
{
|
||||
public EndOfGameEventArgs(
|
||||
string message,
|
||||
bool died,
|
||||
int diamonds,
|
||||
int pointsFromHealth,
|
||||
int finalScore)
|
||||
{
|
||||
Message = message;
|
||||
Died = died;
|
||||
Diamonds = diamonds;
|
||||
PointsFromHealth = pointsFromHealth;
|
||||
FinalScore = finalScore;
|
||||
}
|
||||
|
||||
public bool Died { get; private set; }
|
||||
public int Diamonds { get; private set; }
|
||||
public readonly int DiamondsMultiplier = 3;
|
||||
public int PointsFromHealth { get; private set; }
|
||||
public int FinalScore { get; private set; }
|
||||
public string Message { get; private set; }
|
||||
}
|
||||
public enum GameState
|
||||
{
|
||||
RollDice,
|
||||
|
|
@ -21,12 +45,28 @@ public class GameManager : MonoBehaviour
|
|||
[SerializeField] private DiceRoller diceRoller;
|
||||
[SerializeField] private PassManager passManager;
|
||||
[SerializeField] private Player player;
|
||||
[SerializeField] private DungeonData dungeonData;
|
||||
|
||||
private DicePair _dicePairOne = new();
|
||||
private DicePair _dicePairTwo = new();
|
||||
|
||||
private int monstersLeft = 0; // Includes boss monster. Used for detecting end of game.
|
||||
public static event Action<GameState> StateChanged;
|
||||
public static event Action<DicePair> DiceSelected;
|
||||
public static event Action DiceUnselected;
|
||||
public static event EventHandler<EndOfGameEventArgs> EndOfGame;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
MonsterRoom.MonsterRoomExplored += HandleMonsterRoomExplored;
|
||||
player.Died += HandlePlayerDied;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
MonsterRoom.MonsterRoomExplored -= HandleMonsterRoomExplored;
|
||||
player.Died -= HandlePlayerDied;
|
||||
}
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
|
|
@ -36,7 +76,12 @@ public class GameManager : MonoBehaviour
|
|||
rooms = GameObject.FindWithTag("RoomsParent");
|
||||
foreach (Transform roomTransform in rooms.transform)
|
||||
{
|
||||
roomTransform.gameObject.GetComponent<Room>().RoomExploredByDice += HandleRoomExploredByDice;
|
||||
Room roomComponent = roomTransform.gameObject.GetComponent<Room>();
|
||||
roomComponent.RoomExploredByDice += HandleRoomExploredByDice;
|
||||
if (roomComponent is MonsterRoom)
|
||||
{
|
||||
monstersLeft++;
|
||||
}
|
||||
}
|
||||
foreach (Transform diceTransform in diceRoller.dice.transform)
|
||||
{
|
||||
|
|
@ -149,4 +194,49 @@ public class GameManager : MonoBehaviour
|
|||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleMonsterRoomExplored()
|
||||
{
|
||||
monstersLeft--;
|
||||
if (monstersLeft <= 0)
|
||||
{
|
||||
ResolveEndOfGame(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandlePlayerDied()
|
||||
{
|
||||
ResolveEndOfGame(false);
|
||||
}
|
||||
|
||||
private void ResolveEndOfGame(bool allMonstersKilled)
|
||||
{
|
||||
string message;
|
||||
if (allMonstersKilled)
|
||||
{
|
||||
message = dungeonData.WinMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
message = dungeonData.DieMessage;
|
||||
}
|
||||
|
||||
int finalScore = CalculateScore();
|
||||
Debug.Log($"Final score: {finalScore}");
|
||||
EndOfGame?.Invoke(this, new EndOfGameEventArgs(
|
||||
message,
|
||||
!allMonstersKilled,
|
||||
player.GetDiamondCount(),
|
||||
player.GetHealthScore(),
|
||||
finalScore));
|
||||
}
|
||||
|
||||
private int CalculateScore()
|
||||
{
|
||||
int sum = 0;
|
||||
sum += player.GetDiamondCount() * 3;
|
||||
sum += player.GetHealthScore();
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
30
PuzzleGameProject/Assets/Scripts/LeaveDungeonHandler.cs
Normal file
30
PuzzleGameProject/Assets/Scripts/LeaveDungeonHandler.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using DungeonSelection;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class LeaveDungeonHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private DungeonData dungeonData;
|
||||
private void OnEnable()
|
||||
{
|
||||
EndGameController.LeaveDungeonClicked += LeaveDungeon;
|
||||
EndGameController.SaveScoreClicked += SaveScore;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
EndGameController.LeaveDungeonClicked -= LeaveDungeon;
|
||||
EndGameController.SaveScoreClicked -= SaveScore;
|
||||
}
|
||||
|
||||
private void SaveScore(string name, int score)
|
||||
{
|
||||
Debug.Log($"{name} | {score}");
|
||||
}
|
||||
|
||||
private void LeaveDungeon()
|
||||
{
|
||||
SceneManager.LoadScene(0);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c900817c8cb721946b8efc4fbe6f7ef0
|
||||
|
|
@ -8,6 +8,7 @@ public class Player : MonoBehaviour
|
|||
public event Action<int> DiamondCountUpdated;
|
||||
public event Action<int> HealthUpdated;
|
||||
public event Action<int> DamageTaken;
|
||||
public event Action Died;
|
||||
|
||||
[SerializeField] private int maxHealth;
|
||||
[SerializeField] private int diamonds;
|
||||
|
|
@ -32,6 +33,16 @@ public class Player : MonoBehaviour
|
|||
UpdateGUI();
|
||||
}
|
||||
|
||||
public int GetDiamondCount()
|
||||
{
|
||||
return diamonds;
|
||||
}
|
||||
|
||||
public int GetHealthScore()
|
||||
{
|
||||
return _healthBar[_healthIndex];
|
||||
}
|
||||
|
||||
public void AddDiamonds(int count)
|
||||
{
|
||||
diamonds += count;
|
||||
|
|
@ -44,6 +55,14 @@ public class Player : MonoBehaviour
|
|||
{
|
||||
_healthIndex += damage;
|
||||
UpdateGUI();
|
||||
if (_healthIndex == _healthBar.Length - 1)
|
||||
{
|
||||
Died?.Invoke();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Died?.Invoke();
|
||||
}
|
||||
DamageTaken?.Invoke(damage);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TMPro;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine.UIElements;
|
||||
using Toggle = UnityEngine.UI.Toggle;
|
||||
|
||||
|
|
@ -10,7 +12,9 @@ public class MonsterRoom : Room
|
|||
[SerializeField] private GameObject numberTextObject;
|
||||
[SerializeField] private GameObject healthTickObject;
|
||||
[SerializeField] private int _health; // Number of times the room needs to be unlocked before becoming explored.
|
||||
|
||||
|
||||
public static event Action MonsterRoomExplored;
|
||||
|
||||
private GameObject[] _healthTicks;
|
||||
protected override void InitializeRoom() {
|
||||
base.InitializeRoom();
|
||||
|
|
@ -53,10 +57,12 @@ public class MonsterRoom : Room
|
|||
if (_health == 0)
|
||||
{
|
||||
_isExplored = true;
|
||||
|
||||
UnhighlightRoomAsOption();
|
||||
TriggerRoomRewards();
|
||||
|
||||
SetExploredGUI();
|
||||
MonsterRoomExplored?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
83
PuzzleGameProject/Assets/Scripts/UI/EndGameController.cs
Normal file
83
PuzzleGameProject/Assets/Scripts/UI/EndGameController.cs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
using System;
|
||||
using DungeonSelection;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
public class EndGameController : MonoBehaviour
|
||||
{
|
||||
public static event Action LeaveDungeonClicked;
|
||||
public static event Action<string, int> SaveScoreClicked;
|
||||
|
||||
private const string WIN_MESSAGE = "You Won";
|
||||
private const string DIE_MESSAGE = "You Died";
|
||||
[SerializeField] private Sprite diamondSprite;
|
||||
[SerializeField] private Sprite healthSprite;
|
||||
private Label _title;
|
||||
private Label _message;
|
||||
private Label _diamondPoints;
|
||||
private Label _pointsFromHealth;
|
||||
private Label _scoreSum;
|
||||
private VisualElement _diamondImageBox;
|
||||
private VisualElement _healthImageBox;
|
||||
private TextField _nameField;
|
||||
|
||||
private int _score;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
VisualElement root = GetComponent<UIDocument>().rootVisualElement;
|
||||
|
||||
Button save = root.Q<Button>("Save");
|
||||
save.clicked += HandleSaveClicked;
|
||||
Button close = root.Q<Button>("Close");
|
||||
close.clicked += HandleCloseClicked;
|
||||
_diamondPoints = root.Q<Label>("PointsFromDiamond");
|
||||
_pointsFromHealth = root.Q<Label>("PointsFromHealth");
|
||||
_scoreSum = root.Q<Label>("ScoreSum");
|
||||
_diamondImageBox = root.Q<VisualElement>("DiamondImageBox");
|
||||
_healthImageBox = root.Q<VisualElement>("HealthImageBox");
|
||||
_title = root.Q<Label>("Title");
|
||||
_message = root.Q<Label>("Message");
|
||||
_nameField = root.Q<TextField>("NameField");
|
||||
|
||||
Image diamondImage = new Image();
|
||||
diamondImage.image = diamondSprite.texture;
|
||||
diamondImage.scaleMode = ScaleMode.ScaleToFit;
|
||||
_diamondImageBox.Add(diamondImage);
|
||||
|
||||
Image healthImage = new Image();
|
||||
healthImage.image = healthSprite.texture;
|
||||
healthImage.scaleMode = ScaleMode.ScaleToFit;
|
||||
_healthImageBox.Add(healthImage);
|
||||
}
|
||||
|
||||
public void PopulateEndOfGamePopUp(EndOfGameEventArgs args)
|
||||
{
|
||||
if (args.Died)
|
||||
{
|
||||
_title.text = DIE_MESSAGE;
|
||||
}
|
||||
else
|
||||
{
|
||||
_title.text = WIN_MESSAGE;
|
||||
}
|
||||
|
||||
_message.text = args.Message;
|
||||
_diamondPoints.text = $"{args.Diamonds} X {args.DiamondsMultiplier}";
|
||||
_pointsFromHealth.text = args.PointsFromHealth.ToString();
|
||||
_score = args.FinalScore;
|
||||
_scoreSum.text = _score.ToString();
|
||||
}
|
||||
|
||||
private void HandleCloseClicked()
|
||||
{
|
||||
LeaveDungeonClicked?.Invoke();
|
||||
}
|
||||
|
||||
private void HandleSaveClicked()
|
||||
{
|
||||
SaveScoreClicked?.Invoke(_nameField.value, _score);
|
||||
LeaveDungeonClicked?.Invoke();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ad2e64e6a9233f54a90f666bbbc029d8
|
||||
|
|
@ -12,6 +12,7 @@ namespace UI
|
|||
[SerializeField] private GameObject canvas;
|
||||
[FormerlySerializedAs("hud")] [SerializeField] private InGameHUDController hudController;
|
||||
[SerializeField] private GameObject chestPopUpControllerGO;
|
||||
[SerializeField] private GameObject endOfGamePupUpGO;
|
||||
[SerializeField] private ChestPopUpController chestPopUpController;
|
||||
[SerializeField] private GameObject diceGO;
|
||||
[SerializeField] private DiceRoller diceRoller;
|
||||
|
|
@ -45,6 +46,8 @@ namespace UI
|
|||
|
||||
RoomRewards.ChestRewarded += HandleChestRewarded;
|
||||
|
||||
GameManager.EndOfGame += HandleEndOfGame;
|
||||
|
||||
chestPopUpController.BlackDieSelected += chestRewardSelection.HandleBlackDiceSelected;
|
||||
chestPopUpController.KeySelected += chestRewardSelection.HandleTorchSelected;
|
||||
chestPopUpController.ArmorAndDiamondSelected += chestRewardSelection.HandleDiamondAndLifeSelected;
|
||||
|
|
@ -136,5 +139,11 @@ namespace UI
|
|||
{
|
||||
chestPopUpControllerGO.SetActive(true);
|
||||
}
|
||||
|
||||
private void HandleEndOfGame(object sender, EndOfGameEventArgs args)
|
||||
{
|
||||
endOfGamePupUpGO.SetActive(true);
|
||||
endOfGamePupUpGO.GetComponent<EndGameController>().PopulateEndOfGamePopUp(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue