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

54 lines
1.3 KiB
C#
Raw Permalink Normal View History

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