2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

52 lines
2.1 KiB

using System.Collections.Generic;
using UnityEngine;
namespace Unity.FPS.Game
{
public class DamageArea : MonoBehaviour
{
[Tooltip("Area of damage when the projectile hits something")]
public float AreaOfEffectDistance = 5f;
[Tooltip("Damage multiplier over distance for area of effect")]
public AnimationCurve DamageRatioOverDistance;
[Header("Debug")] [Tooltip("Color of the area of effect radius")]
public Color AreaOfEffectColor = Color.red * 0.5f;
public void InflictDamageInArea(float damage, Vector3 center, LayerMask layers,
QueryTriggerInteraction interaction, GameObject owner)
{
Dictionary<Health, Damageable> uniqueDamagedHealths = new Dictionary<Health, Damageable>();
// Create a collection of unique health components that would be damaged in the area of effect (in order to avoid damaging a same entity multiple times)
Collider[] affectedColliders = Physics.OverlapSphere(center, AreaOfEffectDistance, layers, interaction);
foreach (var coll in affectedColliders)
{
Damageable damageable = coll.GetComponent<Damageable>();
if (damageable)
{
Health health = damageable.GetComponentInParent<Health>();
if (health && !uniqueDamagedHealths.ContainsKey(health))
{
uniqueDamagedHealths.Add(health, damageable);
}
}
}
// Apply damages with distance falloff
foreach (Damageable uniqueDamageable in uniqueDamagedHealths.Values)
{
float distance = Vector3.Distance(uniqueDamageable.transform.position, transform.position);
uniqueDamageable.InflictDamage(
damage * DamageRatioOverDistance.Evaluate(distance / AreaOfEffectDistance), true, owner);
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = AreaOfEffectColor;
Gizmos.DrawSphere(transform.position, AreaOfEffectDistance);
}
}
}