71 lines
1.4 KiB
C#
71 lines
1.4 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
public class Projectile : MonoBehaviour
|
|
{
|
|
[Header("Flags")]
|
|
public bool deflectable = true;
|
|
[SerializeField] private bool isPiercing;
|
|
public bool playerBullet; //bad code
|
|
[Header("Stats")]
|
|
[SerializeField] private int projectileHealth = 2;
|
|
public int damage;
|
|
public float speed;
|
|
public float lifetime;
|
|
[Header("CACHE")]
|
|
public Vector2 direction;
|
|
public AudioClip projectileShootSFX;
|
|
public float volume;
|
|
[SerializeField] private Rigidbody2D rb;
|
|
public SpriteRenderer sprite;
|
|
public bool willUnparent;
|
|
public float unparentTime;
|
|
|
|
protected virtual void Start()
|
|
{
|
|
Destroy(gameObject, lifetime);
|
|
direction = transform.right;
|
|
}
|
|
|
|
protected virtual void Update()
|
|
{
|
|
if (willUnparent && unparentTime > 0)
|
|
{
|
|
unparentTime -= Time.deltaTime;
|
|
if (unparentTime <= 0)
|
|
{
|
|
transform.SetParent(null);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
rb.linearVelocity = direction * speed;
|
|
}
|
|
|
|
protected virtual void OnTriggerEnter2D(Collider2D other)
|
|
{
|
|
if (!other.CompareTag(tag) && other.TryGetComponent(out Entity isEntity))
|
|
{
|
|
if (playerBullet)
|
|
{
|
|
GameManager.instance.projectilesHit++; //bad. very bad. but i don't care right now
|
|
}
|
|
isEntity.TakeDamage(damage);
|
|
if (!isPiercing)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Deflected()
|
|
{
|
|
projectileHealth--;
|
|
if (projectileHealth <= 0)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|