Guest User

hero.cs

a guest
Mar 5th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Hero : Actor
  7. {
  8. public Walker walker;
  9. public bool isAutoPiloting;
  10. public bool controllable = true;
  11.  
  12. public float walkSpeed = 2;
  13. public float runSpeed = 4;
  14.  
  15. bool isRunning;
  16. bool isMoving;
  17. float lastWalk;
  18. public bool canRun = true;
  19. float tapAgainToRunTime = 0.2f;
  20. Vector3 lastWalkVector;
  21.  
  22. Vector3 currentDir;
  23. bool isFacingLeft;
  24.  
  25. bool isJumpLandAnim;
  26. bool isJumpingAnim;
  27.  
  28. public InputHandler input;
  29.  
  30. public float jumpForce = 1750;
  31. private float jumpDuration = 0.2f;
  32. private float lastJumpTime;
  33.  
  34. bool isAttackingAnim;
  35. float lastAttackTime;
  36. float attackLimit = 0.14f;
  37.  
  38. public bool canJumpAttack = true;
  39. private int currentAttackChain = 1;
  40. public int evaluatedAttackChain = 0;
  41. public AttackData jumpAttack;
  42.  
  43. bool isHurtAnim;
  44.  
  45. // New jump mechanics
  46. int jumpPower = 15;
  47. float fallMultiplier = -10;
  48. float jumpMultiplier = 30.0f;
  49. Vector3 vecGravity = new Vector3(0, -1.0F, 0);
  50. //
  51.  
  52. public override void Update()
  53. {
  54. base.Update();
  55.  
  56. if (!isAlive)
  57. {
  58. return;
  59. }
  60.  
  61. isAttackingAnim = baseAnim.GetCurrentAnimatorStateInfo(0).IsName("attack1") || baseAnim.GetCurrentAnimatorStateInfo(0).IsName("jump_attack");
  62. isJumpLandAnim = baseAnim.GetCurrentAnimatorStateInfo(0).IsName("jump_land");
  63. isJumpingAnim = baseAnim.GetCurrentAnimatorStateInfo(0).IsName("jump_rise") || baseAnim.GetCurrentAnimatorStateInfo(0).IsName("jump_fall");
  64. isHurtAnim = baseAnim.GetCurrentAnimatorStateInfo(0).IsName("hurt");
  65.  
  66. if (isAutoPiloting)
  67. {
  68. return;
  69. }
  70. float h = input.GetHorizontalAxis();
  71. float v = input.GetVerticalAxis();
  72. bool jump = input.GetJumpButtonDown();
  73. bool attack = input.GetAttackButtonDown();
  74.  
  75. currentDir = new Vector3(h, 0, v);
  76. currentDir.Normalize();
  77.  
  78. if (!isAttackingAnim)
  79. {
  80. if ((v == 0 && h == 0))
  81. {
  82. Stop();
  83. isMoving = false;
  84. }
  85. else if (!isMoving && (v != 0 || h != 0))
  86. {
  87. isMoving = true;
  88. float dotProduct = Vector3.Dot(currentDir, lastWalkVector);
  89. if (canRun && Time.time < lastWalk + tapAgainToRunTime && dotProduct > 0)
  90. {
  91. Run();
  92. }
  93. else
  94. {
  95. Walk();
  96. if (h != 0)
  97. {
  98. lastWalkVector = currentDir;
  99. lastWalk = Time.time;
  100. }
  101. }
  102. }
  103. }
  104.  
  105. // Jump mechanics
  106. if (jump && !isKnockedOut && !isJumpLandAnim && !isAttackingAnim && !isHurtAnim && (isGrounded || (isJumpingAnim && Time.time < lastJumpTime + jumpDuration)))
  107. {
  108. if (!isJumpingAnim && !isHurtAnim)
  109. {
  110. baseAnim.SetTrigger("Jump");
  111. lastJumpTime = Time.time;
  112. body.velocity = new Vector3(speed * currentDir.x, jumpPower, 0);
  113. //body.velocity = new Vector3(speed, jumpPower, currentDir.z);
  114. }
  115.  
  116. }
  117.  
  118. if (body.velocity.y > 0 && isJumpingAnim)
  119. {
  120. body.velocity += vecGravity * jumpMultiplier * Time.deltaTime;
  121. }
  122.  
  123. if (body.velocity.y < 0)
  124. {
  125. body.velocity -= vecGravity * fallMultiplier * Time.deltaTime;
  126. }
  127.  
  128. if (attack && Time.time >= lastAttackTime + attackLimit && !isKnockedOut)
  129. {
  130. lastAttackTime = Time.time;
  131. Attack();
  132. }
  133. }
  134.  
  135. public void Stop()
  136. {
  137. speed = 0;
  138. baseAnim.SetFloat("Speed", speed);
  139. isRunning = false;
  140. baseAnim.SetBool("IsRunning", isRunning);
  141. }
  142.  
  143. public void Walk()
  144. {
  145. speed = walkSpeed;
  146. baseAnim.SetFloat("Speed", speed);
  147. isRunning = false;
  148. baseAnim.SetBool("IsRunning", isRunning);
  149. }
  150.  
  151. void FixedUpdate()
  152. {
  153. if (!isAlive)
  154. {
  155. return;
  156. }
  157. if (!isAutoPiloting)
  158. {
  159. Vector3 moveVector = currentDir * speed;
  160. if (isGrounded && !isAttackingAnim && !isJumpLandAnim && !isKnockedOut && !isHurtAnim)
  161. {
  162. body.MovePosition(transform.position + moveVector * Time.fixedDeltaTime);
  163. baseAnim.SetFloat("Speed", moveVector.magnitude);
  164. }
  165.  
  166. if (moveVector != Vector3.zero && isGrounded && !isKnockedOut && !isAttackingAnim)
  167. {
  168. if (moveVector.x != 0)
  169. {
  170. isFacingLeft = moveVector.x < 0;
  171. }
  172. FlipSprite(isFacingLeft);
  173. }
  174. }
  175. }
  176.  
  177. public void Run()
  178. {
  179. speed = runSpeed;
  180. isRunning = true;
  181. baseAnim.SetBool("IsRunning", isRunning);
  182. baseAnim.SetFloat("Speed", speed);
  183. }
  184.  
  185. protected override void DidLand()
  186. {
  187. base.DidLand();
  188. //Walk();
  189. }
  190.  
  191. public override void Attack()
  192. {
  193. if (!isGrounded)
  194. {
  195. if (isJumpingAnim && canJumpAttack)
  196. {
  197. canJumpAttack = false;
  198. currentAttackChain = 1;
  199. evaluatedAttackChain = 0;
  200. baseAnim.SetInteger("EvaluatedChain", evaluatedAttackChain);
  201. baseAnim.SetInteger("CurrentChain", currentAttackChain);
  202.  
  203. body.velocity = Vector3.zero;
  204. body.useGravity = false;
  205. }
  206. }
  207. else
  208. {
  209. currentAttackChain = 1;
  210. evaluatedAttackChain = 0;
  211. baseAnim.SetInteger("EvaluatedChain", evaluatedAttackChain);
  212. baseAnim.SetInteger("CurrentChain", currentAttackChain);
  213. }
  214. }
  215.  
  216. public void DidChain(int chain)
  217. {
  218. baseAnim.SetInteger("EvaluatedChain", 1);
  219. }
  220.  
  221. public void AnimateTo(Vector3 position, bool shouldRun, Action callback)
  222. {
  223. if (shouldRun)
  224. {
  225. Run();
  226. }
  227. else
  228. {
  229. Walk();
  230. }
  231. walker.MoveTo(position, callback);
  232. }
  233.  
  234. public void UseAutopilot(bool useAutopilot)
  235. {
  236. isAutoPiloting = useAutopilot;
  237. walker.enabled = useAutopilot;
  238. }
  239.  
  240. protected override void OnCollisionEnter(Collision collision)
  241. {
  242. base.OnCollisionEnter(collision);
  243. if (collision.collider.name == "Floor")
  244. {
  245. canJumpAttack = true;
  246. }
  247. }
  248.  
  249. public void DidJumpAttack()
  250. {
  251. body.useGravity = true;
  252. }
  253.  
  254. private void AnalyzeSpecialAttack(AttackData attackData, Actor actor, Vector3 hitPoint, Vector3 hitVector)
  255. {
  256. actor.EvaluateAttackData(attackData, hitVector, hitPoint);
  257. }
  258.  
  259. protected override void HitActor(Actor actor, Vector3 hitPoint, Vector3 hitVector)
  260. {
  261. if (baseAnim.GetCurrentAnimatorStateInfo(0).IsName("attack1"))
  262. {
  263. base.HitActor(actor, hitPoint, hitVector);
  264. }
  265. else if (baseAnim.GetCurrentAnimatorStateInfo(0).IsName("jump_attack"))
  266. {
  267. AnalyzeSpecialAttack(jumpAttack, actor, hitPoint, hitVector);
  268. }
  269. }
  270.  
  271. public override void TakeDamage(float value, Vector3 hitVector, bool knockdown = false)
  272. {
  273. if (!isGrounded)
  274. {
  275. knockdown = true;
  276. }
  277. base.TakeDamage(value, hitVector, knockdown);
  278. }
  279.  
  280. public override bool CanWalk()
  281. {
  282. return (isGrounded && !isAttackingAnim && !isJumpLandAnim && !isKnockedOut && !isHurtAnim);
  283. }
  284.  
  285. protected override IEnumerator KnockdownRoutine()
  286. {
  287. body.useGravity = true;
  288. return base.KnockdownRoutine();
  289. }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment