ControlsExample.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System.Linq;
  2. using Assets.HeroEditor4D.Common.Scripts.CharacterScripts;
  3. using Assets.HeroEditor4D.Common.Scripts.Enums;
  4. using UnityEngine;
  5. namespace Assets.HeroEditor4D.Common.Scripts.ExampleScripts
  6. {
  7. /// <summary>
  8. /// An example of how to handle user input, play animations and move a character.
  9. /// </summary>
  10. public class ControlsExample : MonoBehaviour
  11. {
  12. public Character4D Character;
  13. public FirearmFxExample FirearmFx;
  14. public bool InitDirection;
  15. public int MovementSpeed;
  16. public playerNetwork playerNetwork;
  17. [SerializeField]private bool _moving;
  18. public void Start()
  19. {
  20. Character.AnimationManager.SetState(CharacterState.Idle);
  21. if (InitDirection)
  22. {
  23. Character.SetDirection(Vector2.down);
  24. }
  25. }
  26. public void Update()
  27. {
  28. if(!playerNetwork.isLocalPlayer){return;}
  29. if (playerNetwork.isDead) {
  30. return;
  31. }
  32. SetDirection();
  33. Move();
  34. ChangeState();
  35. Actions();
  36. }
  37. private void SetDirection()
  38. {
  39. Vector2 direction;
  40. if(joystick.Input == Vector2.zero){
  41. if (Input.GetKeyDown(KeyCode.LeftArrow))
  42. {
  43. direction = Vector2.left;
  44. }
  45. else if (Input.GetKeyDown(KeyCode.RightArrow))
  46. {
  47. direction = Vector2.right;
  48. }
  49. else if (Input.GetKeyDown(KeyCode.UpArrow))
  50. {
  51. direction = Vector2.up;
  52. }
  53. else if (Input.GetKeyDown(KeyCode.DownArrow))
  54. {
  55. direction = Vector2.down;
  56. }
  57. else return;
  58. }else{
  59. direction = JoyToDir();
  60. // direction= joystick.Input;
  61. }
  62. // Debug.Log(direction);
  63. Character.SetDirection(direction);
  64. }
  65. Vector2 JoyToDir(){
  66. if(joystick.Input == Vector2.zero){return Vector2.zero;}
  67. Vector2 direction;
  68. if(Mathf.Abs(joystick.Input.x) > Mathf.Abs(joystick.Input.y)){
  69. if(joystick.Input.x > 0){
  70. direction = Vector2.right;
  71. }else{
  72. direction = Vector2.left;
  73. }
  74. }else{
  75. if(joystick.Input.y > 0){
  76. direction = Vector2.up;
  77. }else{
  78. direction = Vector2.down;
  79. }
  80. }
  81. return direction;
  82. }
  83. private void Move()
  84. {
  85. if (MovementSpeed == 0) return;
  86. var direction = Vector2.zero;
  87. if (Input.GetKey(KeyCode.LeftArrow))
  88. {
  89. direction += Vector2.left;
  90. }
  91. if (Input.GetKey(KeyCode.RightArrow))
  92. {
  93. direction += Vector2.right;
  94. }
  95. if (Input.GetKey(KeyCode.UpArrow))
  96. {
  97. direction += Vector2.up;
  98. }
  99. if (Input.GetKey(KeyCode.DownArrow))
  100. {
  101. direction += Vector2.down;
  102. }
  103. if(joystick.Input != Vector2.zero){
  104. direction = joystick.Input;
  105. }
  106. if (direction == Vector2.zero)
  107. {
  108. if (_moving)
  109. {
  110. Character.AnimationManager.SetState(CharacterState.Idle);
  111. _moving = false;
  112. }
  113. }
  114. else
  115. {
  116. Character.AnimationManager.SetState(CharacterState.Run);
  117. Character.transform.position += (Vector3) direction.normalized * MovementSpeed * Time.deltaTime;
  118. _moving = true;
  119. }
  120. }
  121. private void Actions()
  122. {
  123. if (Input.GetKeyDown(KeyCode.S))
  124. {
  125. switch (Character.WeaponType)
  126. {
  127. case WeaponType.Melee1H:
  128. Character.AnimationManager.Slash(twoHanded: false);
  129. break;
  130. case WeaponType.Melee2H:
  131. case WeaponType.Paired:
  132. Character.AnimationManager.Slash(twoHanded: true);
  133. break;
  134. case WeaponType.Bow:
  135. Character.AnimationManager.ShotBow();
  136. break;
  137. case WeaponType.Crossbow:
  138. Character.AnimationManager.CrossbowShot();
  139. break;
  140. }
  141. }
  142. else if (Input.GetKeyDown(KeyCode.J))
  143. {
  144. Character.AnimationManager.Jab();
  145. }
  146. else if (Input.GetKey(KeyCode.F))
  147. {
  148. if (Character.AnimationManager.IsAction) return;
  149. switch (Character.WeaponType)
  150. {
  151. case WeaponType.Crossbow:
  152. Character.AnimationManager.CrossbowShot();
  153. break;
  154. case WeaponType.Firearm1H:
  155. case WeaponType.Firearm2H:
  156. {
  157. Character.AnimationManager.Fire();
  158. if (Character.Parts[0].PrimaryWeapon != null)
  159. {
  160. var firearm = Character.SpriteCollection.Firearm1H.SingleOrDefault(i => i.Sprites.Contains(Character.Parts[0].PrimaryWeapon))
  161. ?? Character.SpriteCollection.Firearm2H.SingleOrDefault(i => i.Sprites.Contains(Character.Parts[0].PrimaryWeapon));
  162. if (firearm != null)
  163. {
  164. FirearmFx.CreateFireMuzzle(firearm.Name, firearm.Collection);
  165. }
  166. }
  167. break;
  168. }
  169. case WeaponType.Paired:
  170. Character.AnimationManager.SecondaryShot();
  171. break;
  172. }
  173. }
  174. }
  175. private void ChangeState()
  176. {
  177. if (Input.GetKeyDown(KeyCode.I))
  178. {
  179. Character.AnimationManager.SetState(CharacterState.Idle);
  180. }
  181. if (Input.GetKeyDown(KeyCode.E))
  182. {
  183. Character.AnimationManager.SetState(CharacterState.Ready);
  184. }
  185. if (Input.GetKeyDown(KeyCode.W))
  186. {
  187. Character.AnimationManager.SetState(CharacterState.Walk);
  188. }
  189. if (Input.GetKeyDown(KeyCode.R))
  190. {
  191. Character.AnimationManager.SetState(CharacterState.Run);
  192. }
  193. if (Input.GetKeyDown(KeyCode.U))
  194. {
  195. Character.AnimationManager.SetState(CharacterState.Jump);
  196. }
  197. if (Input.GetKeyDown(KeyCode.C))
  198. {
  199. Character.AnimationManager.SetState(CharacterState.Climb);
  200. }
  201. if (Input.GetKeyDown(KeyCode.D))
  202. {
  203. Character.AnimationManager.Die();
  204. }
  205. if (Input.GetKeyDown(KeyCode.H))
  206. {
  207. Character.AnimationManager.Hit();
  208. }
  209. if (Input.GetKeyDown(KeyCode.B))
  210. {
  211. Character.AnimationManager.SetState(CharacterState.ShieldBlock);
  212. }
  213. else if (Input.GetKeyUp(KeyCode.B))
  214. {
  215. Character.AnimationManager.SetState(CharacterState.Idle);
  216. }
  217. }
  218. public void TurnLeft()
  219. {
  220. Character.SetDirection(Vector2.left);
  221. }
  222. public void TurnRight()
  223. {
  224. Character.SetDirection(Vector2.right);
  225. }
  226. public void TurnUp()
  227. {
  228. Character.SetDirection(Vector2.up);
  229. }
  230. public void TurnDown()
  231. {
  232. Character.SetDirection(Vector2.down);
  233. }
  234. public void Show4Directions()
  235. {
  236. Character.SetDirection(Vector2.zero);
  237. }
  238. public void OpenLink(string url)
  239. {
  240. Application.OpenURL(url);
  241. }
  242. }
  243. }