You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
8.1 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic; //input for lists
  4. using UnityEngine.UI;
  5. public class playerController : MonoBehaviour {
  6. public string HorizontalBtn; //holds string for horizontal input
  7. public string VerticalBtn; //holds string for Vertical input
  8. public string StrongAttackBtn;
  9. public string DashAttackBtn;
  10. public Animator animator; //Holds animator for Pinata
  11. public GameObject heart; //Health sprites
  12. public GameObject newConfetti; //confetti when hit
  13. public GameObject newCandy;
  14. public stickController stick; //controller for stick
  15. public GUIText scoreDisplay;
  16. public GUIText gameOverDisplay;
  17. public GameObject dashDisplay;
  18. public float runSpeed; //run speed
  19. public float jumpHeight; //jump height
  20. public float health; //holds health
  21. public int confettiOnHit; //amount of confetti created on hit
  22. public Vector2 healthPos;
  23. public float invulTime;
  24. public float attackWait;
  25. public bool isHit;
  26. public float dashDistance;
  27. public float dashTime;
  28. public float dashWait;
  29. public float minY;
  30. public float deathTimer = 3.0f;
  31. public Vector3 respawnArea;
  32. public Vector2 randRes;
  33. private float displayedHealth = 3; //currently displayed health
  34. private bool updateHealth = true; //if the health bar needs updating
  35. private float curDirection = -1; //direction player is pointing
  36. private bool canJump = true; // if player can jump
  37. private bool canSpin = true; // if player can spin
  38. private List<GameObject> hearts = new List<GameObject> (); //list which holds health sprites;
  39. private bool isSpinning = false; //checks if player is spin attacking
  40. private float lastHit;
  41. private float lastAttack;
  42. private int score;
  43. private bool isDashing =false;
  44. private bool canDash = true;
  45. private float lastDash = 0.0f;
  46. private bool stopDash = false;
  47. private bool droppedCandy = false;
  48. void Start(){
  49. if (transform.rotation.y == 180)
  50. curDirection = -1;
  51. else
  52. curDirection = 1;
  53. }
  54. // Update is called once per frame
  55. void Update () {
  56. displayHealth ();
  57. death ();
  58. applyPlayerDirection (Input.GetAxisRaw (HorizontalBtn));
  59. damage ();
  60. //if (transform.position.y < minY)
  61. // health = 0;
  62. if (score >= 10) {
  63. gameOverDisplay.enabled = true;
  64. gameOverDisplay.text = ""+ gameObject.name + " Wins";
  65. Time.timeScale = 0;
  66. }
  67. dashDisplay.GetComponent<Scrollbar> ().size = (Time.time - lastDash) / dashWait;
  68. }
  69. void FixedUpdate() {
  70. /*
  71. * Movement inputs
  72. */
  73. Vector2 velocity = rigidbody2D.velocity;
  74. velocity.x = Input.GetAxis (HorizontalBtn) * runSpeed; //Horizontal input
  75. if ((Input.GetAxisRaw (VerticalBtn) == 1) && (canJump)) //Vertical input
  76. {
  77. velocity.y = jumpHeight;
  78. canJump = false;
  79. }//end if
  80. if (Input.GetAxisRaw (StrongAttackBtn)==1) {
  81. if (!isSpinning && canSpin && Time.time - lastAttack > attackWait){
  82. velocity.y = jumpHeight;
  83. lastAttack = Time.time;
  84. StartCoroutine (spinAttack (Vector3.down * 360, 0.2f));
  85. }
  86. }
  87. if (Input.GetAxisRaw (DashAttackBtn) == 1) {
  88. //Debug.Log ("dashbutton pressed");
  89. if(!isDashing && canDash && Time.time - lastDash > dashWait){
  90. lastDash = Time.time;
  91. StartCoroutine (dashAttack (dashDistance,dashTime));
  92. }
  93. }
  94. rigidbody2D.velocity = velocity; //apply inputs
  95. animator.SetFloat("Velocity",velocity.magnitude);//inputs for animator
  96. animator.SetBool ("Fall", !canJump);
  97. }//end fixed update
  98. private void displayHealth(){
  99. if (displayedHealth != health) //check if health needs updating
  100. updateHealth = true;
  101. if (updateHealth){
  102. foreach (GameObject desHeart in hearts) //Destroy all heart sprites
  103. GameObject.Destroy (desHeart);
  104. hearts.Clear(); //sets list to zero
  105. for (int i=1; i<= health; i++) { //creates new heart sprite for each health
  106. GameObject heartCanister = Instantiate (heart) as GameObject; //creat heart sprite
  107. Vector3 heartPos = new Vector3 ();
  108. //set position
  109. if (healthPos.x == 1)
  110. heartPos.x = (i * 0.033f);
  111. else
  112. heartPos.x = 1-(i * 0.033f);
  113. if (healthPos.y == 1)
  114. heartPos.y = 0.95f;
  115. else
  116. heartPos.y = 0.05f;
  117. heartCanister.transform.position = heartPos;
  118. hearts.Add(heartCanister); //adds heart to list
  119. }//end for
  120. displayedHealth = health;
  121. updateHealth = false;
  122. }//end if
  123. }//end displayHealth
  124. //respawns player if they die
  125. private void death()
  126. {
  127. if (health <= 0)
  128. if (deathTimer > 0)
  129. {
  130. deathTimer -=Time.deltaTime;
  131. if (!droppedCandy){
  132. Instantiate (newCandy, transform.position + Vector3.up, transform.rotation);
  133. droppedCandy = true;
  134. }
  135. transform.position = new Vector3 (0,-50,0);
  136. //gameObject.active = false;
  137. if(deathTimer <=0)
  138. {
  139. //gameObject.active = true;
  140. //Vector3 spawnPos = new Vector3 (Random.Range (-20.0f, 20.0f), 15.0f, 0); //picks random position
  141. Vector3 spawnPos = respawnArea;
  142. spawnPos.x = Random.Range(randRes.x,randRes.y);
  143. health = 3; //resets life
  144. transform.position = spawnPos; //changes position
  145. rigidbody2D.velocity = Vector2.zero;
  146. deathTimer = deathTimer + 3.02f;
  147. droppedCandy = false;
  148. }
  149. }//end if
  150. }//end death
  151. private void applyPlayerDirection(float moveHorizontal)
  152. {
  153. if ((curDirection != moveHorizontal) && (moveHorizontal != 0) && !isSpinning) //if player movement direction vs displayed direction
  154. {
  155. transform.Rotate(0,180,0); //rotates player
  156. curDirection = moveHorizontal; //updates direction
  157. }
  158. }
  159. private void damage (){
  160. if (isHit) {
  161. gameObject.GetComponentInChildren<PolygonCollider2D> ().enabled = false;
  162. isHit = false;
  163. lastHit = Time.time;
  164. }
  165. if (lastHit+invulTime < Time.time)
  166. gameObject.GetComponentInChildren<PolygonCollider2D> ().enabled = true;
  167. }
  168. //spin
  169. IEnumerator spinAttack(Vector3 byAngles, float inTime) {
  170. isSpinning = true;
  171. canSpin = false;
  172. stick.isAttacking = true;
  173. Debug.Log ("" + gameObject.name + "spinning");
  174. Quaternion startAngle = transform.rotation;
  175. Quaternion endAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
  176. float direction = transform.eulerAngles.y;
  177. for(float i = 0; i < 1; i += Time.deltaTime/inTime) {
  178. transform.rotation = Quaternion.Euler(0,direction,Mathf.Lerp(0,360,i));
  179. yield return null;
  180. }
  181. transform.eulerAngles = new Vector3 (0,direction,0);
  182. //Debug.Log ("" + gameObject.name + "End spin");
  183. isSpinning = false;
  184. stick.isAttacking = false;
  185. }
  186. //dash
  187. IEnumerator dashAttack (float dashDistance,float dashTime) {
  188. float endPosition;
  189. int dashDirection;
  190. isDashing = true;
  191. canDash = false;
  192. stick.isAttacking = true;
  193. Debug.Log ("" + gameObject.name + "Dashing");
  194. rigidbody2D.gravityScale = 0;
  195. Vector3 startPosition = transform.position;
  196. //Debug.Log ("rotation Y: " + transform.eulerAngles.y);
  197. if (transform.eulerAngles.y >= 150)
  198. dashDirection = -1;
  199. else
  200. dashDirection = 1;
  201. endPosition = startPosition.x + (dashDirection * dashDistance);
  202. //Debug.Log ("Dash start: " + startPosition.x);
  203. //Debug.Log ("Dash Direction: " + (dashDirection ));
  204. //Debug.Log ("Dash end: " + endPosition);
  205. for (float i = 0; i < 1; i += Time.deltaTime/dashTime) {
  206. transform.position = new Vector3(dashDirection * Mathf.Lerp((dashDirection)*startPosition.x, (dashDirection)* endPosition, i), startPosition.y, startPosition.z);
  207. if ((transform.position.x < -30.5f && dashDirection == -1) || (transform.position.x > 26.5 && dashDirection == 1))
  208. stopDash = true;
  209. if (stopDash)
  210. break;
  211. yield return null;
  212. }
  213. stopDash = false;
  214. isDashing = false;
  215. canDash = true;
  216. stick.isAttacking = false;
  217. //Debug.Log ("" + gameObject.name + " finished Dashing");
  218. rigidbody2D.gravityScale = 4;
  219. }
  220. void OnCollisionEnter2D(Collision2D col){
  221. if (col.collider.tag == "ground") {
  222. canJump = true;
  223. canSpin = true;
  224. }
  225. else if (col.collider.tag == "Player") {
  226. canJump = true;
  227. canSpin = true;
  228. }
  229. if (col.collider.tag == "wall") {
  230. stopDash = true;
  231. }
  232. if (col.collider.tag == "failBox") {
  233. health = 0;
  234. }
  235. }
  236. void OnTriggerEnter2D(Collider2D trig)
  237. {
  238. Debug.Log (trig.gameObject.tag);
  239. if (trig.gameObject.tag == "candy")
  240. {
  241. trig.gameObject.SetActive (false);
  242. score++;
  243. scoreDisplay.text = "score: " + score.ToString ();
  244. }
  245. }
  246. }