66 lines
2.3 KiB
C#
66 lines
2.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class RocketShell : ProjectileBase
|
|
{
|
|
[SerializeField] private float visualDamageAmount = 1000.0f;
|
|
[SerializeField] private float visualDamageRadius = 10.0f;
|
|
[SerializeField] private LayerMask whatCanBeHitByRocket;
|
|
private float splitAmount = 5f;
|
|
private HashSet<NPC> hitNPCs = new HashSet<NPC>();
|
|
private bool hasHit = false;
|
|
|
|
|
|
protected override void OnHitEntity(Collision col)
|
|
{
|
|
SpawnDamageVisuals(col);
|
|
TimedPooledDestroy();
|
|
}
|
|
|
|
private void SpawnDamageVisuals(Collision col)
|
|
{
|
|
if (!hasHit)
|
|
{
|
|
Collider[] hitEnemies = Physics.OverlapSphere(col.contacts[0].point, visualDamageRadius, whatCanBeHitByRocket);
|
|
foreach (Collider collider in hitEnemies)
|
|
{
|
|
if (collider.transform.root.TryGetComponent(out NPC npc) && !hitNPCs.Contains(npc))
|
|
{
|
|
hitNPCs.Add(npc);
|
|
SplitDamage(npc.getNA().GetHeadPosition());
|
|
}
|
|
}
|
|
}
|
|
hasHit = true;
|
|
|
|
}
|
|
|
|
private void SplitDamage(Vector3 spawnPos)
|
|
{
|
|
float damage = visualDamageAmount / splitAmount;
|
|
for (int i = 0; i < splitAmount; i++)
|
|
{
|
|
GameObject damageTextClone = NetworkPooler.Instance.PooledInstantiate("FloatingDamageText", spawnPos, Quaternion.identity);
|
|
FloatingDamageText text = damageTextClone.GetComponent<FloatingDamageText>();
|
|
text.MaxWaitTime = 3.0f;
|
|
text.GetText().color = Color.red;
|
|
|
|
float visualDamageVariance = Random.Range(damage - 30.0f, damage + 30.0f);
|
|
text.GetText().SetText(Mathf.RoundToInt(visualDamageVariance).ToString());
|
|
}
|
|
}
|
|
|
|
private void TimedPooledDestroy()
|
|
{
|
|
//we want it to put back into the pool after time, so the particle effect doesnt disapear abruptly. so stop its movement until returned to pool.
|
|
rb.velocity = Vector3.zero;
|
|
rb.angularVelocity = Vector3.zero;
|
|
|
|
string baseName = NetworkPooler.GetBaseName(transform.name);
|
|
if (NetworkPooler.Instance.poolDictionary.ContainsKey(baseName))
|
|
NetworkPooler.Instance.PooledDestroy(baseName, gameObject, 5.0f);
|
|
}
|
|
}
|
|
|