Advertisement
Guest User

Untitled

a guest
May 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.SceneManagement;
  4.  
  5. [RequireComponent (typeof (Controller2D))]
  6. public class Player : MonoBehaviour {
  7.  
  8. public float automatic_knife_trigger_distance;
  9. public float knife_speed;
  10. public float moveSpeed;
  11. public float min_jump_height;
  12. public float max_jump_height;
  13. public float timeToJumpApex;
  14. public float accelerationTimeAirborne;
  15. public float accelerationTimeGrounded;
  16. float min_jump_velocity;
  17. float max_jump_velocity;
  18.  
  19. [Space]
  20.  
  21. public float step_dust_rate;
  22. private float step_dust_rate_count;
  23. public float knife_dust_rate;
  24. private float knife_dust_rate_count;
  25. public float knife_dust_time;
  26. private float knife_dust_time_count;
  27.  
  28. float gravity;
  29.  
  30. [HideInInspector] public Vector3 velocity;
  31. float velocityXSmoothing;
  32.  
  33. bool savedFlip;
  34.  
  35. Controller2D controller;
  36. SpriteRenderer sprite;
  37. AvatarStates avatar_states;
  38. Animator animator;
  39. LevelManager level_manager;
  40. GameObject slash;
  41. GameObject dust;
  42.  
  43. Vector3 knife_dir;
  44. GameObject child;
  45.  
  46. Vector2 input;
  47. float input_save;
  48.  
  49. void Start() {
  50. child = transform.Find("Graphics").gameObject;
  51. animator = child.GetComponent<Animator>();
  52. sprite = child.GetComponent<SpriteRenderer>();
  53. controller = GetComponent<Controller2D> ();
  54. dust = Resources.Load<GameObject>("Prefabs/Switch_Dust");
  55. slash = Resources.Load<GameObject>("Prefabs/Slash");
  56. level_manager = FindObjectOfType<LevelManager>();
  57.  
  58. gravity = -(2 * max_jump_height) / Mathf.Pow (timeToJumpApex, 2);
  59. min_jump_velocity = Mathf.Sqrt(2 * Mathf.Abs(gravity) * min_jump_height);
  60. max_jump_velocity = Mathf.Abs(gravity) * timeToJumpApex;
  61.  
  62. avatar_states = new AvatarStates();
  63. AvatarStates.OnLand += reset_step_dust;
  64. Ballon.OnKill += OnBallonKill;
  65. }
  66.  
  67. void reset_step_dust() {
  68. step_dust_rate_count = 0F;
  69. }
  70.  
  71. bool is_ballon_in_avatar_angle(Ballon ballon) {
  72. var mouse_pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  73. mouse_pos.z = 0F;
  74. var mouse_dir = (mouse_pos - transform.position).normalized;
  75. var ballon_dir = (ballon.transform.position - transform.position).normalized;
  76.  
  77. var angle = 20F;
  78. if (Vector2.Angle(mouse_dir, ballon_dir) <= angle) {
  79. return true;
  80. }
  81. return false;
  82. }
  83.  
  84. void OnBallonKill() {
  85. if (AvatarStates.states == AvatarStates.States.KNIFING) {
  86. Ballon closest_ballon = level_manager.get_closest_ballon();
  87. if (closest_ballon != null) {
  88. if (is_ballon_in_avatar_angle(closest_ballon)) {
  89. if (Vector2.Distance(closest_ballon.transform.position, transform.position) < automatic_knife_trigger_distance) {
  90. knife_dir = (closest_ballon.transform.position - transform.position).normalized;
  91. StartCoroutine(reset_knife_dir());
  92. } else {
  93. var mouse_pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  94. mouse_pos.z = 0F;
  95. var mouse_dir = (mouse_pos - transform.position).normalized;
  96.  
  97. if (Mathf.Sign(mouse_dir.x) > 0) {
  98. input_save = 1;
  99. } else {
  100. input_save = -1;
  101. }
  102.  
  103. if (mouse_dir.y < 0F) {
  104. mouse_dir.y = 0F;
  105. }
  106.  
  107.  
  108. knife_dir = mouse_dir;
  109. StartCoroutine(reset_knife_dir());
  110. }
  111. } else {
  112. var mouse_pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  113. mouse_pos.z = 0F;
  114. var mouse_dir = (mouse_pos - transform.position).normalized;
  115.  
  116. if (Mathf.Sign(mouse_dir.x) > 0) {
  117. input_save = 1;
  118. } else {
  119. input_save = -1;
  120. }
  121. //input_save = Mathf.Sign(mouse_dir.x);
  122.  
  123. if (mouse_dir.y < 0F) {
  124. mouse_dir.y = 0F;
  125. }
  126. knife_dir = mouse_dir;
  127. StartCoroutine(reset_knife_dir());
  128. }
  129. } else {
  130. knife_dir = Vector3.zero;
  131. }
  132. } else {
  133. knife_dir = Vector3.zero;
  134. }
  135. }
  136.  
  137. IEnumerator reset_knife_dir() {
  138. yield return new WaitForSeconds(0.1F);
  139. knife_dir = Vector3.zero;
  140. }
  141.  
  142. void Update() {
  143.  
  144. if (controller.collisions.above || controller.collisions.below) {
  145. velocity.y = 0;
  146. }
  147.  
  148. input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
  149.  
  150. if (AvatarStates.states == AvatarStates.States.KNIFING) {
  151. input.x = 0F;
  152. } else {
  153. if (Input.GetKeyDown (KeyCode.Space) && controller.collisions.below) {
  154. AvatarAnimations.GroundedJumpDust(this);
  155. velocity.y = max_jump_velocity;
  156. AvatarStates.states = AvatarStates.States.JUMPING;
  157. }
  158.  
  159. if (Input.GetKeyUp(KeyCode.Space)) {
  160. if (velocity.y > min_jump_velocity) {
  161. velocity.y = min_jump_velocity;
  162. }
  163. }
  164. }
  165.  
  166. if (Input.GetKeyDown(KeyCode.Mouse0)) {
  167. input_save = input.x;
  168.  
  169. // Dust et reset
  170. Instantiate(dust, transform.position, Quaternion.identity);
  171. step_dust_rate_count = step_dust_rate;
  172. knife_dust_rate_count = knife_dust_rate;
  173. knife_dust_time_count = knife_dust_time;
  174. }
  175.  
  176. if (controller.collisions.below) {
  177. input_save = 0;
  178. }
  179.  
  180.  
  181. if (knife_dir == Vector3.zero) {
  182. float targetVelocityX = (input_save != 0 ? input_save : input.x) * moveSpeed;
  183. velocity.x = Mathf.SmoothDamp (velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne);
  184. velocity.y += gravity * Time.deltaTime;
  185. controller.Move (velocity * Time.deltaTime);
  186. } else {
  187. if (knife_dir.y < 0F) {
  188. knife_dir.y = 0F;
  189. }
  190. velocity = knife_dir * knife_speed;
  191. controller.Move(velocity * Time.deltaTime);
  192. }
  193.  
  194. // Dust step
  195. if (controller.collisions.below && input.x != 0) {
  196. if (step_dust_rate_count <= 0F) {
  197. AvatarAnimations.GroundedRunDust(this);
  198. step_dust_rate_count = step_dust_rate;
  199. } else {
  200. step_dust_rate_count -= Time.deltaTime;
  201. }
  202. }
  203.  
  204. // Dust knife
  205. if (AvatarStates.states == AvatarStates.States.KNIFING) {
  206. if (knife_dust_time_count > 0F) {
  207. knife_dust_time_count -= Time.deltaTime;
  208. if (knife_dust_rate_count <= 0F) {
  209. AvatarAnimations.GroundedRunDust(this);
  210. knife_dust_rate_count = knife_dust_rate;
  211. } else {
  212. knife_dust_rate_count -= Time.deltaTime;
  213. }
  214. }
  215. }
  216.  
  217. if (input.x < 0) {
  218. sprite.flipX = true;
  219. }
  220. else if (input.x > 0) {
  221. sprite.flipX = false;
  222. }
  223. else {
  224. sprite.flipX = savedFlip;
  225. }
  226.  
  227. savedFlip = sprite.flipX;
  228.  
  229. if (Input.GetKeyDown(KeyCode.R)) {
  230. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  231. }
  232.  
  233. if (Input.GetKey(KeyCode.Mouse0)) {
  234. metamorphose();
  235. }
  236.  
  237. if (Input.GetKeyUp(KeyCode.Mouse0)) {
  238. input_save = 0;
  239. child.transform.up = Vector3.zero;
  240. var switch_dust = Instantiate(Resources.Load<GameObject>("Prefabs/Switch_Dust"), transform.position, Quaternion.identity);
  241. }
  242.  
  243. avatar_states.StateChange();
  244. AnimationUpdate();
  245. }
  246.  
  247. private void metamorphose() {
  248. AvatarStates.states = AvatarStates.States.KNIFING;
  249. child.transform.up = velocity;
  250. }
  251.  
  252. private void AnimationUpdate()
  253. {
  254. switch (AvatarStates.states)
  255. {
  256. case AvatarStates.States.IDLE:
  257. AvatarAnimations.AnimationIdle(animator);
  258. break;
  259.  
  260. case AvatarStates.States.JUMPING:
  261. AvatarAnimations.AnimationJumping(animator);
  262. break;
  263.  
  264. case AvatarStates.States.RUNNING:
  265. AvatarAnimations.AnimationRunning(animator);
  266. break;
  267.  
  268. case AvatarStates.States.KNIFING:
  269. AvatarAnimations.AnimationKnifing(animator);
  270. break;
  271.  
  272. case AvatarStates.States.FALLING:
  273. AvatarAnimations.AnimationFalling(animator);
  274. break;
  275.  
  276. case AvatarStates.States.DEATH:
  277. AvatarAnimations.AnimationDeath(animator);
  278. break;
  279. }
  280. }
  281.  
  282. void OnDestroy() {
  283. AvatarStates.OnLand -= reset_step_dust;
  284. Ballon.OnKill -= OnBallonKill;
  285. }
  286.  
  287. void OnTriggerEnter2D(Collider2D col) {
  288.  
  289. if (col.gameObject.CompareTag("NextLevel")) {
  290. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
  291. }
  292.  
  293. if (AvatarStates.states != AvatarStates.States.KNIFING) {
  294. if (col.gameObject.CompareTag("Lethal")) {
  295. Instantiate(Resources.Load<GameObject>("Prefabs/Death_Particules"), transform.position, Quaternion.identity);
  296.  
  297. GameObject obj = Instantiate(dust, transform.position, Quaternion.identity) as GameObject;
  298.  
  299. AvatarStates.states = AvatarStates.States.DEATH;
  300. }
  301. }
  302.  
  303. if (AvatarStates.states == AvatarStates.States.KNIFING) {
  304. if (col.gameObject.CompareTag("Knifable")) {
  305. Instantiate(slash, col.gameObject.transform.position, Quaternion.identity);
  306. Instantiate(dust, col.gameObject.transform.position, Quaternion.identity);
  307. Destroy(col.gameObject);
  308. }
  309. }
  310. }
  311.  
  312. void OnTriggerStay2D(Collider2D col) {
  313. if (AvatarStates.states == AvatarStates.States.KNIFING) {
  314. if (col.gameObject.CompareTag("Knifable")) {
  315. Instantiate(slash, col.gameObject.transform.position, Quaternion.identity);
  316. Instantiate(dust, col.gameObject.transform.position, Quaternion.identity);
  317. Destroy(col.gameObject);
  318. }
  319. }
  320.  
  321. if (AvatarStates.states != AvatarStates.States.KNIFING) {
  322. if (col.gameObject.CompareTag("Lethal")) {
  323. Instantiate(Resources.Load<GameObject>("Prefabs/Death_Particules"), transform.position, Quaternion.identity);
  324.  
  325. GameObject obj = Instantiate(dust, transform.position, Quaternion.identity) as GameObject;
  326.  
  327. AvatarStates.states = AvatarStates.States.DEATH;
  328. }
  329. }
  330. }
  331.  
  332. void OnTriggerExit2D(Collider2D col) {
  333. if (AvatarStates.states != AvatarStates.States.KNIFING) {
  334. if (col.gameObject.CompareTag("Lethal")) {
  335. Instantiate(Resources.Load<GameObject>("Prefabs/Death_Particules"), transform.position, Quaternion.identity);
  336.  
  337. GameObject obj = Instantiate(dust, transform.position, Quaternion.identity) as GameObject;
  338.  
  339. AvatarStates.states = AvatarStates.States.DEATH;
  340. }
  341. }
  342. }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement