CockActionComp.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using System;
  2. using System.Collections;
  3. using Game;
  4. using Sound;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace Comp
  8. {
  9. public class CockActionComp : MonoBehaviour
  10. {
  11. private static readonly Color HpGreen = new Color(0.06f, 0.9f, 0.18f);
  12. private static readonly Color HpYellow = new Color(0.9f, 0.9f, 0.18f);
  13. private static readonly Color HpRed = new Color(0.9f, 0.18f, 0.18f);
  14. private const string CockTag = "cock";
  15. private ICockController _cockController;
  16. // 血量条组件
  17. private Image _backgroundImage, _foregroundImage;
  18. private int _maxHp, _curHp;
  19. public int reduceHp; // 需要扣除的Hp
  20. public int MaxHp
  21. {
  22. set
  23. {
  24. _maxHp = value;
  25. _curHp = _maxHp;
  26. }
  27. }
  28. private void ReduceCurHp(int hp)
  29. {
  30. _curHp -= hp;
  31. var hpPercent = (float)_curHp / _maxHp;
  32. if (hpPercent > 0.5f)
  33. {
  34. _foregroundImage.color = HpGreen;
  35. }
  36. else if (hpPercent is >= 0.25f and <= 0.5f)
  37. {
  38. _foregroundImage.color = HpYellow;
  39. }
  40. else
  41. {
  42. _foregroundImage.color = HpRed;
  43. }
  44. var backgroundSize = _backgroundImage.rectTransform.sizeDelta;
  45. var foregroundSize = _foregroundImage.rectTransform.sizeDelta;
  46. var newForegroundWidth = backgroundSize.x * hpPercent;
  47. _foregroundImage.rectTransform.sizeDelta = new Vector2(newForegroundWidth, foregroundSize.y);
  48. }
  49. // 跑步跟切换待机状态相关参数
  50. private bool _firstTrigger = true; // 第一次碰撞切换为待机
  51. private Vector3 _thrust;
  52. private const float MoveSpeed = 1f;
  53. // 锁定物体相关参数
  54. private float _miniY; // 最低Y坐标
  55. // 执行动作相关
  56. public Action curAction;
  57. private readonly WaitForSeconds _waitEverySecond = new WaitForSeconds(1f);
  58. // 水平与垂直方向运动相关
  59. private CockJumpData _lowData, _highData, _curData;
  60. public int playerId;
  61. public int cockId;
  62. public bool rightForward = false;
  63. // miss文本相关参数
  64. private GameObject _missPrefab;
  65. private bool _createMiss = false;
  66. public ICockController CockController
  67. {
  68. get
  69. {
  70. if (_cockController == null)
  71. {
  72. _cockController = new AnimatorCockController(animator);
  73. _cockController.OnRun += OnRun;
  74. _cockController.OnIdle += OnIdle;
  75. _cockController.OnAttack += OnAttack;
  76. _cockController.OnJumpAttack += OnJumpAttack;
  77. return _cockController;
  78. }
  79. return _cockController;
  80. }
  81. }
  82. public Animator animator;
  83. private void OnCollisionEnter(Collision collision)
  84. {
  85. if (collision.gameObject.CompareTag(CockTag) && _firstTrigger)
  86. {
  87. _firstTrigger = false;
  88. _cockController.IdleInBattle();
  89. GameCore.Instance.GetCurState().BattleStart();
  90. }
  91. }
  92. private void Start()
  93. {
  94. _missPrefab = Resources.Load<GameObject>("Prefab/prefab_miss");
  95. // 获取血条的填充图片
  96. var images = GetComponentsInChildren<Image>();
  97. foreach (var image in images)
  98. {
  99. if (image.name == "Background")
  100. {
  101. _backgroundImage = image;
  102. }
  103. if (image.name == "Foreground")
  104. {
  105. _foregroundImage = image;
  106. }
  107. }
  108. // 获取最低Y坐标
  109. _miniY = transform.position.y;
  110. // 跳跃相关
  111. _lowData = new CockJumpData(0.5f, 0.45f, false);
  112. _highData = new CockJumpData(1.0f, 0.3f, true);
  113. _curData = _highData;
  114. StartCoroutine(CallEverySeconds());
  115. }
  116. private IEnumerator CallEverySeconds()
  117. {
  118. while (true)
  119. {
  120. curAction?.Invoke();
  121. curAction = null;
  122. yield return _waitEverySecond;
  123. }
  124. }
  125. private void Update()
  126. {
  127. if (_createMiss)
  128. {
  129. _createMiss = false;
  130. CreateMissText();
  131. }
  132. if (reduceHp != 0)
  133. {
  134. ReduceCurHp(reduceHp);
  135. reduceHp = 0;
  136. }
  137. }
  138. private void FixedUpdate()
  139. {
  140. transform.Translate(_thrust * Time.deltaTime);
  141. // 移动游戏对象
  142. // if (GameCore.Instance.inBattleState)
  143. transform.position += _curData.GetJumpVector();
  144. if (GameCore.Instance.inBattleState)
  145. {
  146. if (rightForward)
  147. {
  148. transform.position += _curData.GetMoveVector();
  149. }
  150. else
  151. {
  152. transform.position -= _curData.GetMoveVector();
  153. }
  154. }
  155. if (transform.position.y < _miniY)
  156. {
  157. var position = transform.position;
  158. position = new Vector3(position.x, _miniY, position.z);
  159. transform.position = position;
  160. }
  161. }
  162. private void OnRun()
  163. {
  164. _thrust = new Vector3(0, 0, MoveSpeed);
  165. }
  166. private void OnIdle()
  167. {
  168. _thrust = new Vector3(0, 0, 0);
  169. }
  170. private void OnAttack()
  171. {
  172. _curData.StartMove();
  173. }
  174. private void OnJumpAttack()
  175. {
  176. SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
  177. _curData.StartJump();
  178. }
  179. public void OnAttackEnd()
  180. {
  181. _curData.StopMove();
  182. }
  183. public void OnJumpEnd()
  184. {
  185. _curData.StopJump();
  186. }
  187. public void SetHighJump(bool high)
  188. {
  189. _curData = high ? _highData : _lowData;
  190. }
  191. public void CreateMiss()
  192. {
  193. this._createMiss = true;
  194. }
  195. private void CreateMissText()
  196. {
  197. var obj = Instantiate(_missPrefab);
  198. obj.GetComponent<MissText>().SetTarget(transform);
  199. }
  200. }
  201. public class CockJumpData
  202. {
  203. private readonly float _gravity; // 重力
  204. private readonly float _jumpVelocity; // 跳跃速度
  205. private readonly bool _forward; // 是否前进
  206. private Vector3 _jumpDirection = Vector3.zero;
  207. private Vector3 _moveDirection = Vector3.zero;
  208. private bool _move = false; // 是否移动状态
  209. private float _timeToJumpApex;
  210. private float _curJumpTime;
  211. // 跳跃高度 到达跳跃高度的时间
  212. public CockJumpData(float jumpHeight, float timeToJumpApex, bool forward)
  213. {
  214. _forward = forward;
  215. _timeToJumpApex = timeToJumpApex;
  216. // 计算跳跃速度和重力
  217. _gravity = -(2 * jumpHeight) / Mathf.Pow(timeToJumpApex, 2);
  218. _jumpVelocity = Mathf.Abs(_gravity) * timeToJumpApex;
  219. }
  220. public Vector3 GetJumpVector()
  221. {
  222. if (_curJumpTime < 0)
  223. {
  224. return Vector3.zero;
  225. }
  226. // 计算重力
  227. _jumpDirection.y += _gravity * Time.deltaTime;
  228. _curJumpTime -= Time.deltaTime;
  229. // 移动游戏对象
  230. return _jumpDirection * Time.deltaTime;
  231. }
  232. public Vector3 GetMoveVector()
  233. {
  234. if (!_move)
  235. return Vector3.zero;
  236. // 计算位移
  237. _moveDirection.x = (_forward ? 30f : -30f) * Time.deltaTime;
  238. return _moveDirection * Time.deltaTime;
  239. }
  240. public void StartJump()
  241. {
  242. _jumpDirection.y = _jumpVelocity;
  243. _curJumpTime = _timeToJumpApex * 2; // 双倍时间
  244. _move = true;
  245. }
  246. public void StartMove()
  247. {
  248. _move = true;
  249. }
  250. public void StopJump()
  251. {
  252. _move = false;
  253. }
  254. public void StopMove()
  255. {
  256. _move = false;
  257. }
  258. }
  259. }