70 lines
2.7 KiB
C#
70 lines
2.7 KiB
C#
|
|
using JetBrains.Annotations;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
public class HitboxController : MonoBehaviour {
|
|||
|
|
public BoxCollider2D boxCollider; // Pass-thru reference
|
|||
|
|
public CircleCollider2D circleCollider; // Pass-thru reference
|
|||
|
|
|
|||
|
|
[SerializeField] private GameObject hitEffect;
|
|||
|
|
|
|||
|
|
[SerializeField] public float activeTime = 1.0f; // Seconds
|
|||
|
|
[SerializeField] public float damageTick = 0.08f; // Seconds
|
|||
|
|
public int maxMonstersHit = 1;
|
|||
|
|
public int numLines = 1;
|
|||
|
|
public float damageMultiplier = 1.0f;
|
|||
|
|
|
|||
|
|
[SerializeField] public bool flip = false;
|
|||
|
|
[SerializeField] public AudioClip hitClip;
|
|||
|
|
[SerializeField] public AudioClip tickClip;
|
|||
|
|
|
|||
|
|
private int numMonstersHit = 0;
|
|||
|
|
|
|||
|
|
// Start is called before the first frame update
|
|||
|
|
void Start() {
|
|||
|
|
// boxCollider = GetComponent<BoxCollider2D>();
|
|||
|
|
// circleCollider = GetComponent<CircleCollider2D>();
|
|||
|
|
Invoke("Die", activeTime);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Setup(int maxMonstersHit, int numLines, float damageMultiplier) {
|
|||
|
|
this.maxMonstersHit = maxMonstersHit;
|
|||
|
|
this.numLines = numLines;
|
|||
|
|
this.damageMultiplier = damageMultiplier;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void OnTriggerEnter2D(Collider2D collision) {
|
|||
|
|
Debug.Log("Entered collision");
|
|||
|
|
// If the hitbox collides with a monster...
|
|||
|
|
if (collision.gameObject.CompareTag("Monster") && numMonstersHit < maxMonstersHit) {
|
|||
|
|
// Do nothing if the monster is still spawning.
|
|||
|
|
if (collision.GetComponent<MonsterController>().isSpawning) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
// Calculate the damage dealt against the monster and generate damage lines.
|
|||
|
|
float BASE_DAMAGE = State.state.stats.cache.baseDamage;
|
|||
|
|
float STABILITY = State.state.stats.cache.totalStb;
|
|||
|
|
float MIN_MULTIPLIER = 0.5f + 0.4f * Mathf.Min(STABILITY, 100f) / 100f;
|
|||
|
|
List<float> finalDamageLines = new List<float>();
|
|||
|
|
for (int i = 0; i < numLines; i++) {
|
|||
|
|
finalDamageLines.Add(BASE_DAMAGE * damageMultiplier * Random.Range(MIN_MULTIPLIER, 1.0f));
|
|||
|
|
};
|
|||
|
|
bool hit = collision.gameObject.GetComponent<DamageController>().IssueDamage(finalDamageLines, damageTick, !flip, hitClip, tickClip);
|
|||
|
|
if (hit) {
|
|||
|
|
numMonstersHit += 1;
|
|||
|
|
GameObject effect = Instantiate(hitEffect);
|
|||
|
|
Destroy(effect, 5f);
|
|||
|
|
effect.transform.position = collision.transform.position;
|
|||
|
|
if (numMonstersHit >= maxMonstersHit) {
|
|||
|
|
Destroy(gameObject);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Die() {
|
|||
|
|
Destroy(gameObject);
|
|||
|
|
}
|
|||
|
|
}
|