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

108 lines
2.7 KiB
C#

using UnityEngine;
public class Ghost_Frightened : Ghost_behaviour
{
public SpriteRenderer body;
public SpriteRenderer eye;
public SpriteRenderer blue;
public SpriteRenderer white;
public bool eaten { get; private set;}
public override void Enable(float duration)
{
base.Enable(duration);
this.body.enabled = false;
this.eye.enabled = false;
this.blue.enabled = true;
this.white.enabled = false;
Invoke(nameof(Flash), duration / 2.0f);
}
public override void Disable()
{
base.Disable();
this.body.enabled = true;
this.eye.enabled = true;
this.blue.enabled = false;
this.white.enabled = false;
}
private void Flash()
{
if(this.eaten)
{
this.blue.enabled = false;
this.white.enabled = true;
this.white.GetComponent<Ani>().Restart();
}
}
private void Eaten()
{
this.eaten = true;
Vector3 position = this.ghost.home.inside.position;
position.z = this.ghost.transform.position.z;
this.ghost.transform.position = position;
this.ghost.home.Enable(this.duration);
this.body.enabled = false;
this.eye.enabled = true;
this.blue.enabled = false;
this.white.enabled = false;
}
private void OnEnable()
{
this.ghost.movement.speedMultiplier = 0.5f;
this.eaten = false;
}
public void OnDisable()
{
this.ghost.movement.speedMultiplier = 1.0f;
this.eaten = false;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.layer == LayerMask.NameToLayer("Pacman"))
{
if(this.enabled)
{
Eaten();
}
}
}
private void OnTriggerEnter2D(Collider2D other)
{
Nodes node = other .GetComponent<Nodes>();
if(node != null && this.enabled)
{
Vector2 direction = Vector2.zero;
float maxDistance = float.MinValue;
foreach (Vector2 availableDirection in node.availableDirections)
{
Vector3 newPosition = this.transform.position + new Vector3(availableDirection.x , availableDirection.y , 0.0f);
float distance = (this.ghost.target.position - newPosition).sqrMagnitude;
if( distance > maxDistance)
{
direction = availableDirection;
maxDistance = distance;
}
}
this.ghost.movement.SetDirection(direction);
}
}
}