BGJ-2022.1/Assets/Scripts/AI/Projectile.cs

54 lines
1.3 KiB
C#

using Pausable;
using UnityEngine;
namespace AI
{
[RequireComponent(typeof(Rigidbody2D))]
public class Projectile : MonoBehaviour, IPausable
{
public Rigidbody2D Rigidbody { get; private set; } = null;
private AudioSource audioSource = null;
private Animator animator = null;
private void Awake()
{
Rigidbody = GetComponent<Rigidbody2D>();
audioSource = GetComponent<AudioSource>();
animator = GetComponent<Animator>();
}
public void SetVelocity(Vector2 velocity)
{
Rigidbody.velocity = velocity;
}
private void OnCollisionEnter2D(Collision2D other)
{
ProjectilePool.Instance.Return(this);
audioSource.Play();
if (other.transform.CompareTag("Player"))
other.gameObject.GetComponent<Player.Death>().Die();
}
public bool IsPaused { get; protected set; } = false;
public void Pause()
{
IsPaused = true;
UpdateComponents();
}
public void Resume()
{
IsPaused = false;
UpdateComponents();
}
private void UpdateComponents()
{
Rigidbody.simulated = !IsPaused;
animator.enabled = !IsPaused;
}
}
}