TargetCtrl.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using James.Util;
  6. namespace JamesGame {
  7. public class TargetCtrl : MonoBehaviour {
  8. public enum ObstacleType
  9. {
  10. None,
  11. Woman,
  12. Bee,
  13. OilDrum,
  14. }
  15. public enum TargetPromptType
  16. {
  17. None,
  18. CanAttack, //可攻击
  19. ForbidAttack //禁止攻击
  20. }
  21. public bool autoDestoryOnAnimEnd = false;
  22. public System.Action OnAutoDestory;
  23. private float _destoryCD = float.MaxValue;
  24. private System.Action _customCallback;
  25. public bool destoryTimerEnable = false;
  26. [Tooltip("女战士马蜂窝属于障碍物,其他目标不属于none")]
  27. public ObstacleType TcType = ObstacleType.None;
  28. [Tooltip("打中障碍物的target不进行相机跟踪特效")]
  29. public bool CameraFollow = true;
  30. [Tooltip("打中障碍物后间隔多久进行结算ui的显示")]
  31. public float DelayMatchedTime = 3f;
  32. [Tooltip("靶子首次出现需要进行提示 None为默认不提示")]
  33. public TargetPromptType promptType = TargetPromptType.None;
  34. public List<TargetScoreCollider> colliders = new List<TargetScoreCollider>();
  35. [SerializeField]
  36. private bool _isCrownTarget = false;
  37. public bool IsCrownTarget
  38. {
  39. get { return _isCrownTarget; }
  40. }
  41. private void FixedUpdate()
  42. {
  43. if (destoryTimerEnable)
  44. {
  45. _destoryCD -= Time.deltaTime;
  46. if (_destoryCD < 0f)
  47. {
  48. _customCallback?.Invoke();
  49. OnAutoDestory?.Invoke();
  50. _destoryCD = float.MaxValue;
  51. _customCallback = null;
  52. destoryTimerEnable = false;
  53. }
  54. }
  55. }
  56. public int GetScore(Collider col) {
  57. var idx = colliders.FindIndex((entity) => entity.collider == col);
  58. return idx == -1 ? 0 : colliders[idx].score;
  59. }
  60. public void DestoryDelay(float sec, System.Action callback) {
  61. _destoryCD = sec;
  62. _customCallback = callback;
  63. destoryTimerEnable = true;
  64. //callback?.Invoke();
  65. //OnAutoDestory?.Invoke();
  66. }
  67. public void OnSpeedChanged(float animSpeedRatio,float preTime,float delayTime)
  68. {
  69. if (preTime - delayTime >= _destoryCD)
  70. {
  71. _destoryCD /= animSpeedRatio;
  72. }
  73. else
  74. {
  75. float temp = preTime - delayTime;
  76. float delya_Time = _destoryCD - temp;
  77. _destoryCD = delya_Time + (temp/animSpeedRatio);
  78. }
  79. }
  80. public void ActiveCollider(bool activeFlag)
  81. {
  82. for (int i = 0; i < colliders.Count; ++i)
  83. {
  84. colliders[i].collider.enabled = activeFlag;
  85. }
  86. }
  87. }
  88. [System.Serializable]
  89. public struct TargetScoreCollider {
  90. public Collider collider;
  91. public int score;
  92. }
  93. #if UNITY_EDITOR
  94. [CustomEditor(typeof(TargetCtrl))]
  95. public class TargetControllerInspector : Editor {
  96. private TargetCtrl _target;
  97. private void OnEnable() {
  98. _target = target as TargetCtrl;
  99. }
  100. public override void OnInspectorGUI() {
  101. base.OnInspectorGUI();
  102. //
  103. GUILayout.BeginHorizontal();
  104. if(GUILayout.Button("Auto Fill")) {
  105. _target.colliders.Clear();
  106. var colliders = _target.transform.GetComponentsInChildren<Collider>();
  107. foreach(var col in colliders) {
  108. int score;
  109. if(! int.TryParse(col.name, out score)) {
  110. score = 0;
  111. }
  112. _target.colliders.Add(new TargetScoreCollider {
  113. collider = col,
  114. score = score,
  115. });
  116. }
  117. }
  118. if(GUILayout.Button("Remove Empty")) {
  119. var tmpList = new List<TargetScoreCollider>();
  120. _target.colliders.ForEach((entity) => {
  121. if (entity.collider != null) tmpList.Add(entity);
  122. });
  123. _target.colliders = tmpList;
  124. }
  125. if(GUILayout.Button("Remove All")) {
  126. _target.colliders.Clear();
  127. }
  128. //
  129. GUILayout.EndHorizontal();
  130. }
  131. }
  132. #endif
  133. }