DamageController.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using QFSW.MOP2;
  6. using KairoEngine.Core;
  7. using KairoEngine.Inventory;
  8. namespace KairoEngine.CharacterSystem
  9. {
  10. public class DamageController : MonoBehaviour
  11. {
  12. public CharacterController character;
  13. [SerializeField, ShowInInspector] private int hitPoints = 100;
  14. [SerializeField, ShowInInspector]private int maxHitPoints = 100;
  15. [SerializeField, ShowInInspector] private int armorPoints = 4;
  16. //public GameObject hitPrefab;
  17. public string hitFxName = "BloodSpillFx";
  18. public int HP { get { return hitPoints; } }
  19. public int maxHP { get { return maxHitPoints; } }
  20. public int armor { get { return armorPoints; } }
  21. private List<Bullet> bulletHits = new List<Bullet>();
  22. void Start()
  23. {
  24. if(HP <= 0)
  25. {
  26. character.Die();
  27. DamageEvents.DeathEvent(this, null);
  28. }
  29. }
  30. public void OnCollisionEnter(Collision collision)
  31. {
  32. Vector3 point = collision.contacts[0].point;
  33. if (collision.gameObject.tag == "Bullet")
  34. {
  35. Bullet bullet = collision.gameObject.GetComponent<Bullet>();
  36. Damage(bullet, point);
  37. }
  38. DamageEmitter damageEmitter = collision.gameObject.GetComponent<DamageEmitter>();
  39. if(damageEmitter != null)
  40. {
  41. if(damageEmitter.character != character && damageEmitter.enabled == true)
  42. {
  43. Damage(damageEmitter, point);
  44. }
  45. }
  46. }
  47. public void Damage(Bullet bullet, Vector3 point, DamagePoint damagePoint = null)
  48. {
  49. float damageMultiplier = 1f;
  50. if(damagePoint != null) damageMultiplier = damagePoint.damageMultiplier;
  51. // Don't damage the shooter
  52. if(bullet.character == character) return;
  53. // Each bullet only damages once
  54. for (int i = 0; i < bulletHits.Count; i++)
  55. {
  56. if(bulletHits[i] == bullet) return;
  57. }
  58. bulletHits.Add(bullet);
  59. StartCoroutine(Timer.Start(0.5f, false, () => { bulletHits.Remove(bullet); }));
  60. // Get firearm properties to calculate damage
  61. ItemBaseFirearm firearmItem = (ItemBaseFirearm)bullet.firearmRef.item;
  62. int firearmDamage = firearmItem.CalculateDamage(bullet.firearmRef, bullet.startPos, point);
  63. // Distance damage falloff calculation
  64. float travelDistance = Vector3.Distance(bullet.startPos, bullet.transform.position);
  65. if(travelDistance < firearmItem.minDistance)
  66. {
  67. firearmDamage = (int)((float)firearmDamage * 1.25f);
  68. }
  69. else if(travelDistance > firearmItem.halfDistance)
  70. {
  71. float modifier = ((firearmItem.maxDistance - firearmItem.halfDistance)/100) * (travelDistance - firearmItem.halfDistance);
  72. firearmDamage = (int)((float)firearmDamage - (float)firearmDamage * modifier);
  73. if(firearmDamage < 0) firearmDamage = 0;
  74. }
  75. else if(travelDistance > firearmItem.maxDistance)
  76. {
  77. firearmDamage = 0;
  78. }
  79. // Critical Hit calculation
  80. bool criticalHit = false;
  81. float roll = Random.Range(0, 100);
  82. if(roll <= firearmItem.criticalChanceModifier) criticalHit = true;
  83. // Calculate damage
  84. int armorPiercing = bullet.firearmRef.ammoType.armorPiercing;
  85. int damage = CalculateDamage(firearmDamage, armorPiercing);
  86. if(criticalHit) damage = damage * 2;
  87. damage = Mathf.FloorToInt(damage * damageMultiplier);
  88. // Remove hitpoints and start events
  89. //if(damage == 0) return;
  90. hitPoints -= damage;
  91. GameObject obj = bullet.character.gameObject;
  92. DamageData damageData = new DamageData(this, damagePoint, damage, obj, bullet.character, bullet, null, point, bullet.startPos, hitPoints <= 0);
  93. DamageEvents.DamageEvent(damageData);
  94. if(character != null)
  95. {
  96. character.Injury();
  97. if(hitPoints <= 0)
  98. {
  99. character.Die();
  100. DamageEvents.DeathEvent(this, bullet.character);
  101. }
  102. }
  103. SpawnHitFX(point);
  104. }
  105. public void Damage(DamageEmitter damageEmitter, Vector3 point, DamagePoint damagePoint = null)
  106. {
  107. int damage = CalculateDamage(damageEmitter.damage, damageEmitter.armorPiercing);
  108. hitPoints -= damage;
  109. GameObject obj = damageEmitter.character.gameObject;
  110. DamageData damageData = new DamageData(this, damagePoint, damage, obj, damageEmitter.character, null, damageEmitter, point, obj.transform.position, hitPoints <= 0);
  111. DamageEvents.DamageEvent(damageData);
  112. if(character != null)
  113. {
  114. character.Injury();
  115. if(hitPoints <= 0)
  116. {
  117. character.Die();
  118. if(damageEmitter.character != null) DamageEvents.DeathEvent(this, damageEmitter.character);
  119. }
  120. }
  121. SpawnHitFX(point);
  122. }
  123. private int CalculateDamage(int baseDamage, int armorPiercing)
  124. {
  125. int defense = armor - armorPiercing;
  126. if(defense < 0) defense = 0;
  127. int damage = baseDamage - defense;
  128. if(damage < 0) damage = 0;
  129. return damage;
  130. }
  131. private void SpawnHitFX(Vector3 position)
  132. {
  133. //if(hitPrefab == null) return;
  134. //var prefab = Instantiate(hitPrefab, position, transform.rotation) as GameObject;
  135. MasterObjectPooler.Instance.GetPool(hitFxName).GetObject(position, transform.rotation);
  136. }
  137. public void Heal(int value)
  138. {
  139. if(hitPoints + value < maxHitPoints) hitPoints += value;
  140. else
  141. {
  142. value = maxHitPoints - hitPoints;
  143. hitPoints = maxHitPoints;
  144. }
  145. DamageEvents.HealEvent(this, value);
  146. }
  147. public void SetHP(int value) => hitPoints = value;
  148. public void SetMaxHP(int value) => maxHitPoints = value;
  149. public void SetArmor(int value) => armorPoints = value;
  150. }
  151. }