Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Player_Control : MonoBehaviour
  5. {
  6. public SoundManager sound;
  7.  
  8. // Tapping
  9. float startTime;
  10. bool isJump;
  11.  
  12. // Debug Control
  13. public bool endless;
  14. int mistakeCount;
  15. int lastMistakeCount;
  16.  
  17. // Control Variables
  18.  
  19. [HideInInspector] public bool gameOver;
  20. [HideInInspector] public bool victory;
  21. [HideInInspector] public bool dead;
  22. [HideInInspector] public bool diedInLiquid;
  23. [HideInInspector] public bool fullStop;
  24.  
  25. // The RidigBody 2D
  26. Rigidbody2D Rb2D;
  27.  
  28. // The Movement Class
  29. Movement MV;
  30.  
  31. // Initialization
  32. void Start ()
  33. {
  34. MV = GetComponent<Movement> ();
  35. Rb2D = GetComponent<Rigidbody2D> ();
  36. mistakeCount = 0;
  37. lastMistakeCount = 0;
  38. gameOver = false;
  39. dead = false;
  40. diedInLiquid = false;
  41. fullStop = false;
  42. victory = false;
  43. }
  44.  
  45. // Fixed Update of the Player
  46. void FixedUpdate ()
  47. {
  48. if (fullStop)
  49. {
  50. Rb2D.velocity = new Vector2 (0, 0);
  51. return;
  52. }
  53.  
  54. // Mistake Mechanics
  55. processMistakes();
  56.  
  57. if (dead) {
  58. if(diedInLiquid)
  59. Rb2D.velocity = new Vector2 (0, 0);
  60. else
  61. Rb2D.velocity = new Vector2 (0, Rb2D.velocity.y);
  62. } else {
  63.  
  64. // Horizontal Update
  65. Tap ();
  66.  
  67. if (endless)
  68. Rb2D.velocity = new Vector2 (5, Rb2D.velocity.y);
  69. else
  70. MV.Horizontal_Velocity_Update (Rb2D);
  71.  
  72. // Check for player controlled jumps
  73. MV.Jump (Rb2D);
  74.  
  75. // Tells the movement script to update his axis
  76. MV.UpdateAxis ();
  77. }
  78.  
  79. // Animate
  80. MV.Animate(Rb2D, dead);
  81. }
  82.  
  83.  
  84. public void Jump()
  85. {
  86. if(MV.slide == 1)
  87. MV.slide = 0;
  88.  
  89. MV.jump = 1;
  90. }
  91.  
  92. public void Slide()
  93. {
  94. sound.play = Sound.SLIDE;
  95. MV.slide = 1;
  96. }
  97.  
  98. public void processMistakes()
  99. {
  100. if (!endless || dead)
  101. return;
  102.  
  103. // Mistake Mechanics
  104. if (Rb2D.velocity.x < 1)
  105. mistakeCount++;
  106.  
  107. if (mistakeCount == lastMistakeCount)
  108. mistakeCount = 0;
  109.  
  110. lastMistakeCount = mistakeCount;
  111.  
  112. if (mistakeCount > 1) {
  113. Debug.Log ("sound");
  114. sound.play = Sound.DEAD;
  115. dead = true;
  116. }
  117. }
  118.  
  119. public void setGameOver()
  120. {
  121. gameOver = true;
  122. }
  123.  
  124. public void setVictory()
  125. {
  126. victory = true;
  127. }
  128.  
  129. void OnTriggerEnter2D(Collider2D other)
  130. {
  131. if (dead)
  132. return;
  133.  
  134. GameObject go = other.gameObject;
  135.  
  136. if (go.tag == "Exit") {
  137. UnityAds.ShowAd ();
  138. sound.play = Sound.VICTORY;
  139. victory = true;
  140. } else if ((go.tag == "Enemy") || (go.tag == "Water")) {
  141. sound.play = Sound.DEAD;
  142. dead = true;
  143. }
  144.  
  145. if (go.tag == "Water")
  146. diedInLiquid = true;
  147. }
  148.  
  149. public void Reset()
  150. {
  151. gameOver = false;
  152. dead = false;
  153. diedInLiquid = false;
  154. fullStop = false;
  155. victory = false;
  156. mistakeCount = 0;
  157. lastMistakeCount = 0;
  158. MV.Animate (Rb2D, dead);
  159. MV.ResetAxis ();
  160. }
  161.  
  162. public void Tap()
  163. {
  164.  
  165. if (Input.touchCount > 0)
  166. {
  167. Touch touch = Input.touches[0];
  168.  
  169. switch (touch.phase)
  170. {
  171. case TouchPhase.Began:
  172.  
  173. isJump = true;
  174. startTime = Time.time;
  175. break;
  176.  
  177. case TouchPhase.Moved:
  178.  
  179. isJump = false;
  180. break;
  181.  
  182. case TouchPhase.Stationary:
  183.  
  184. Slide ();
  185. isJump = false;
  186. break;
  187.  
  188. case TouchPhase.Ended:
  189.  
  190. if (isJump)
  191. Jump;
  192.  
  193. break;
  194. }
  195. }
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement