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

51 lines
1.2 KiB
C#
Raw 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-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-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;
UpdateRigidbody();
}
public void Resume()
{
IsPaused = false;
UpdateRigidbody();
}
private void UpdateRigidbody()
{
2022-02-26 18:46:49 +03:00
Rigidbody.simulated = !IsPaused;
2022-02-22 11:25:25 +03:00
}
}
}