Files
CartoonCopChase/Assets/Scripts/GameManager.cs

268 lines
7.5 KiB
C#

using System.Collections;
using UnityEngine;
using TMPro; // TextMeshPro
using UnityEngine.SceneManagement;
using ArcadeVP;
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; private set; }
[Header("Level Settings")]
public string locationName = "the Downtown Bank";
public float startingStolenMoney = 50000f;
[Tooltip("How much money is lost for every 1 point of damage the player takes.")]
public float moneyLostPerHitMultiplier = 100f;
[Header("Text Templates")]
[TextArea]
public string introTextTemplate = "You have robbed {0} and stole ${1:N0}.\nReach your safe point.";
[TextArea]
public string successTextTemplate = "You successfully escaped {0}!\nYou safely secured ${1:N0}!\nTotal Cash Pile: ${2:N0}";
[TextArea]
public string raidedTextTemplate = "SAFE HOUSE RAIDED!\nYou led the cops right to your front door after robbing {0}.\nAll previously collected money has been seized by the police.";
[TextArea]
public string bustedTextTemplate = "BUSTED!\nYou were pinned by the police and arrested.\nAll previously collected money has been seized.";
[Header("Current State (Read Only)")]
public float currentStolenMoney;
public float totalCashPile;
public bool isGameOver = false;
public bool isGameActive = false; // Controls player and AI input
[Header("UI References")]
[Tooltip("A UI Panel with an Image component set to Black to fade in from.")]
public CanvasGroup fadePanel;
[Tooltip("The text element showing the intro text.")]
public TextMeshProUGUI introText;
[Space(10)]
public TextMeshProUGUI moneyDisplay;
[Space(10)]
public GameObject gameOverPanel;
[Space(10)]
public GameObject levelCompletePanel;
public TextMeshProUGUI levelCompleteTitle;
public TextMeshProUGUI levelCompleteDetails;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
// Load persistent cash pile from previous levels
totalCashPile = PlayerPrefs.GetFloat("TotalCashPile", 0f);
currentStolenMoney = startingStolenMoney;
isGameOver = false;
isGameActive = false; // Disable controls during intro
// Initialize UI panels
if (gameOverPanel) gameOverPanel.SetActive(false);
if (levelCompletePanel) levelCompletePanel.SetActive(false);
UpdateMoneyDisplay();
StartCoroutine(IntroSequence());
}
private IEnumerator IntroSequence()
{
if (fadePanel == null || introText == null) yield break;
// Set screen to black
fadePanel.alpha = 1f;
fadePanel.gameObject.SetActive(true);
introText.gameObject.SetActive(true);
// Setup intro text using the template
introText.text = string.Format(introTextTemplate, locationName, startingStolenMoney);
// Set text alpha to 0 manually
Color c = introText.color;
c.a = 0f;
introText.color = c;
// Fade out black screen
float t = 0f;
while (t < 1f)
{
t += Time.deltaTime * 0.5f;
fadePanel.alpha = Mathf.Lerp(1f, 0f, t);
yield return null;
}
fadePanel.blocksRaycasts = false;
// Fade IN text
t = 0f;
while (t < 1f)
{
t += Time.deltaTime * 0.8f;
c.a = Mathf.Lerp(0f, 1f, t);
introText.color = c;
yield return null;
}
yield return new WaitForSeconds(4f);
// Fade OUT text
t = 0f;
while (t < 1f)
{
t += Time.deltaTime * 0.8f;
c.a = Mathf.Lerp(1f, 0f, t);
introText.color = c;
yield return null;
}
introText.gameObject.SetActive(false);
isGameActive = true; // Give control back to player and AI
}
public void OnPlayerHit(float damageTaken)
{
if (isGameOver || !isGameActive) return;
float moneyToLose = damageTaken * moneyLostPerHitMultiplier;
currentStolenMoney -= moneyToLose;
if (currentStolenMoney < 0) currentStolenMoney = 0;
UpdateMoneyDisplay();
}
public void GameOver()
{
if (isGameOver) return;
isGameOver = true;
isGameActive = false;
if (gameOverPanel != null)
{
gameOverPanel.SetActive(true);
}
}
public void Busted()
{
if (isGameOver) return;
isGameOver = true;
isGameActive = false;
// Lose all money
totalCashPile = 0f;
PlayerPrefs.SetFloat("TotalCashPile", 0f);
PlayerPrefs.Save();
// Show the level complete panel but with the Busted text
if (levelCompletePanel != null)
{
levelCompletePanel.SetActive(true);
}
if (levelCompleteTitle)
{
levelCompleteTitle.text = "BUSTED!";
levelCompleteTitle.color = Color.red;
}
if (levelCompleteDetails)
{
levelCompleteDetails.text = bustedTextTemplate;
}
}
public void EnterSafeHouse()
{
if (isGameOver) return;
isGameOver = true;
isGameActive = false;
// Determine if cops are actively chasing (Ignore searching if player is hidden)
bool isBeingChased = false;
AIController[] allAI = FindObjectsByType<AIController>(FindObjectsInactive.Exclude);
foreach (AIController ai in allAI)
{
if (ai.currentState == AIController.AIState.Pursue)
{
isBeingChased = true;
break;
}
}
// Show End Screen
if (levelCompletePanel != null)
{
levelCompletePanel.SetActive(true);
}
if (isBeingChased)
{
// RAIDED - Lost all collected money
totalCashPile = 0f;
PlayerPrefs.SetFloat("TotalCashPile", 0f); // Wipe the save file
if (levelCompleteTitle)
{
levelCompleteTitle.text = "BUSTED!";
levelCompleteTitle.color = Color.red;
}
if (levelCompleteDetails)
{
levelCompleteDetails.text = string.Format(raidedTextTemplate, locationName);
}
}
else
{
// SUCCESS - Keep the loot
totalCashPile += currentStolenMoney;
PlayerPrefs.SetFloat("TotalCashPile", totalCashPile);
if (levelCompleteTitle)
{
levelCompleteTitle.text = "ESCAPE SUCCESSFUL";
levelCompleteTitle.color = Color.green;
}
if (levelCompleteDetails)
{
levelCompleteDetails.text = string.Format(successTextTemplate, locationName, currentStolenMoney, totalCashPile);
}
}
PlayerPrefs.Save();
}
private void UpdateMoneyDisplay()
{
if (moneyDisplay != null)
{
moneyDisplay.text = $"Loot: ${currentStolenMoney:N0}";
}
}
// --- BUTTON METHODS ---
public void RestartLevel()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void QuitGame()
{
Application.Quit();
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
}
}