Pacman/Assets/Scripts/GameManager.cs
2025-04-20 21:10:16 +05:30

131 lines
2.9 KiB
C#

using UnityEngine;
public class GameManager : MonoBehaviour
{
public Ghost[] ghosts;
public Pacman pacman;
public Transform pellets;
public int ghostMultiplier { get; private set; } = 1;
public int score { get; private set;}
public int lives { get ; private set;}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
NewGame();
}
private void NewGame()
{
SetScore(0);
Setlives(3);
NewRound();
}
private void NewRound()
{
foreach ( Transform pellet in this.pellets)
{
pellet.gameObject.SetActive(true);
}
ResetState();
}
private void ResetState()
{
ResetGhostMultiplier();
for ( int i = 0; i < this.ghosts.Length; i++)
{
this.ghosts[i].ResetState();
}
this.pacman.ResetState();
}
private void GameOver()
{
for ( int i = 0; i < this.ghosts.Length; i++)
{
this.ghosts[i].gameObject.SetActive(false);
}
this.pacman.gameObject.SetActive(false);
}
private void SetScore(int score)
{
this.score = score;
}
private void Setlives(int lives){
this.lives =lives;
}
// Update is called once per frame
void Update()
{
if(this.lives <= 0 && Input.anyKeyDown)
{
NewGame();
}
}
public void GhostEaten(Ghost ghost)
{
int points = ghost.points * this.ghostMultiplier;
SetScore(this.score + points);
this.ghostMultiplier++;
}
public void PacmanEaten()
{
this.pacman.gameObject.SetActive(false);
Setlives(this.lives - 1);
if(this.lives > 0)
{
Invoke(nameof(ResetState), 3.0f);
}else
{
GameOver();
}
}
public void PelletEaten(Pellet pellet)
{
pellet.gameObject.SetActive(false);
SetScore(this.score + pellet.points);
if(!HasRemainigPellets())
{
this.pacman.gameObject.SetActive(false);
Invoke(nameof(NewRound), 3.0f);
}
}
public void Power_PelletEaten(Power_Pellet pellet)
{
for( int i= 0; i < this.ghosts.Length; i++)
{
this.ghosts[i].Frightened.Enable(pellet.duration);
}
PelletEaten(pellet);
CancelInvoke();
Invoke (nameof(ResetGhostMultiplier), pellet.duration);
}
public bool HasRemainigPellets()
{
foreach (Transform pellet in this.pellets)
{
if(pellet.gameObject.activeSelf)
{
return true;
}
}
return false;
}
private void ResetGhostMultiplier()
{
this.ghostMultiplier = 1;
}
}