HumanoidShotTarget.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using UnityEngine.UI;
  6. namespace ShotSimulator.Target
  7. {
  8. public class HumanoidShotTarget : MonoBehaviour
  9. {
  10. public bool IsRunning { get; set; }
  11. private float m_Health;
  12. public float m_MaxHealth;
  13. [SerializeField]
  14. private Canvas m_Canvas;
  15. [SerializeField]
  16. private Slider healthBar;
  17. public Image healthBarImage;
  18. [SerializeField]
  19. protected List<HumanoidPartShotTarget> m_HumanoidParts = new List<HumanoidPartShotTarget>();
  20. public List<HumanoidPartType> m_CauseHumanoidPartReduceHPOrder = new List<HumanoidPartType>();
  21. public float Health
  22. {
  23. get { return m_Health; }
  24. protected set
  25. {
  26. m_Health = value;
  27. if (m_Health > 1)
  28. {
  29. healthBarImage.color = new Color(0, 1, 1);
  30. }
  31. else
  32. {
  33. healthBarImage.color = Color.red;
  34. }
  35. healthBar.value = m_Health / m_MaxHealth;
  36. }
  37. }
  38. public void InitHumanoidShotTarget(float health, Vector3 origin)
  39. {
  40. m_MaxHealth = health;
  41. Health = health;
  42. m_CauseHumanoidPartReduceHPOrder.Clear();
  43. gameObject.transform.position = origin;
  44. foreach(var part in m_HumanoidParts)
  45. {
  46. part.IsRunning = true;
  47. }
  48. IsRunning = true;
  49. }
  50. protected void Update()
  51. {
  52. if (!IsRunning) return;
  53. m_Canvas.gameObject.transform.LookAt(Camera.main.transform);
  54. }
  55. public virtual void CauseHumanoidPartReduceHP(HumanoidPartShotTarget part)
  56. {
  57. switch (part.PartType)
  58. {
  59. case HumanoidPartType.Head:
  60. Health = 0f;
  61. break;
  62. case HumanoidPartType.Body:
  63. Health--;
  64. break;
  65. }
  66. m_CauseHumanoidPartReduceHPOrder.Add(part.PartType);
  67. }
  68. }
  69. }