MozambiqueHumanoidShotTarget.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using ShotSimulator.Tool;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace ShotSimulator.Target
  7. {
  8. public class MozambiqueHumanoidShotTarget : HumanoidShotTarget
  9. {
  10. public List<HumanoidPartType> m_NeedOrder = new List<HumanoidPartType>();
  11. public List<HumanoidPartShotTarget> hitParts = new List<HumanoidPartShotTarget>();
  12. private int next_MozambiqueIndex;
  13. public int currentHitIndex;
  14. public bool isCurFalseHit;
  15. public GameObject brokenFragment;
  16. public void InitMozambiqueHumanoidShotTarget()
  17. {
  18. next_MozambiqueIndex = 0;
  19. currentHitIndex = 0;
  20. hitParts.Clear();
  21. //SetCurrentView();
  22. gameObject.transform.LookAt(Camera.main.transform);
  23. }
  24. public override void CauseHumanoidPartReduceHP(HumanoidPartShotTarget part)
  25. {
  26. if (m_NeedOrder[next_MozambiqueIndex] == part.PartType&& !hitParts.Contains(part))
  27. {
  28. hitParts.Add(part);
  29. switch (part.PartType)
  30. {
  31. case HumanoidPartType.Head:
  32. Health = 0f;
  33. break;
  34. case HumanoidPartType.Body:
  35. Health--;
  36. break;
  37. }
  38. part.SetColor(Color.red);
  39. m_NeedOrder.Add(part.PartType);
  40. currentHitIndex = next_MozambiqueIndex;
  41. next_MozambiqueIndex++;
  42. //SetCurrentView();
  43. isCurFalseHit = false;
  44. }
  45. else
  46. {
  47. isCurFalseHit = true;
  48. }
  49. }
  50. private void SetCurrentView()
  51. {
  52. if (next_MozambiqueIndex < m_HumanoidParts.Count)
  53. {
  54. Text text = m_HumanoidParts[next_MozambiqueIndex].transform.Find("Canvas/OrderText").GetComponent<Text>();
  55. text.gameObject.SetActive(true);
  56. text.text = (next_MozambiqueIndex + 1).ToString();
  57. Vector3 m_IndexTextPos = m_HumanoidParts[next_MozambiqueIndex].transform.position;
  58. }
  59. }
  60. private void OnDestroy()
  61. {
  62. if (Health <= 0)
  63. {
  64. GameObject effectObj = Instantiate(Resources.Load<GameObject>("Prefabs/BrokenEffect"), transform.position + new Vector3(0, 2f, 0), Quaternion.identity);
  65. effectObj.transform.localScale = Vector3.one * 2;
  66. SpawnDebris();
  67. }
  68. }
  69. private void SpawnDebris()
  70. {
  71. brokenFragment.transform.SetParent(null);
  72. brokenFragment.SetActive(true);
  73. foreach(Transform child in brokenFragment.transform)
  74. {
  75. // 随机选择碎片预制体
  76. GameObject selectedDebris = child.gameObject;
  77. // 生成参数设置
  78. Vector3 spawnPos = transform.position + Random.insideUnitSphere * 0.5f;
  79. Quaternion spawnRot = Random.rotation;
  80. selectedDebris.transform.position = spawnPos;
  81. selectedDebris.transform.rotation = spawnRot;
  82. // 添加物理组件
  83. Rigidbody rb = selectedDebris.GetComponent<Rigidbody>();
  84. if (rb == null) rb = selectedDebris.AddComponent<Rigidbody>();
  85. // 施加随机方向力
  86. Vector3 randomDir = new Vector3(Random.Range(-1f, 1f), Random.Range(0.5f, 1f), Random.Range(-1f, 1f)).normalized;
  87. float force = Random.Range(5f, 15f);
  88. rb.AddForce(randomDir * force, ForceMode.Impulse);
  89. // 添加随机旋转
  90. rb.AddTorque(Random.insideUnitSphere * 50f, ForceMode.Impulse);
  91. }
  92. Destroy(brokenFragment, 0.5f);
  93. }
  94. }
  95. }