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.

212 lines
5.3 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic; //input for lists
  4. public class playerController : MonoBehaviour {
  5. public string HorizontalBtn; //holds string for horizontal input
  6. public string VerticalBtn; //holds string for Vertical input
  7. public string StrongAttackBtn;
  8. public Animator animator; //Holds animator for Pinata
  9. public GameObject heart; //Health sprites
  10. public GameObject newConfetti; //confetti when hit
  11. public GameObject newCandy;
  12. public stickController stick; //controller for stick
  13. public GUIText scoreDisplay;
  14. public GUIText gameOverDisplay;
  15. public float runSpeed; //run speed
  16. public float jumpHeight; //jump height
  17. public float health; //holds health
  18. public int confettiOnHit; //amount of confetti created on hit
  19. public Vector2 healthPos;
  20. public float invulTime;
  21. public float attackWait;
  22. public bool isHit;
  23. private float displayedHealth = 3; //currently displayed health
  24. private bool updateHealth = true; //if the health bar needs updating
  25. private float curDirection = -1; //direction player is pointing
  26. private bool canJump = true; // if player can jump
  27. private List<GameObject> hearts = new List<GameObject> (); //list which holds health sprites;
  28. private bool isSpinning = false; //checks if player is spin attacking
  29. private float lastHit;
  30. private float lastAttack;
  31. private int score;
  32. void Start(){
  33. if (transform.rotation.y == 180)
  34. curDirection = -1;
  35. else
  36. curDirection = 1;
  37. }
  38. // Update is called once per frame
  39. void Update () {
  40. displayHealth ();
  41. death ();
  42. applyPlayerDirection (Input.GetAxisRaw (HorizontalBtn));
  43. damage ();
  44. if (transform.position.y < -3)
  45. health = 0;
  46. if (score >= 10) {
  47. gameOverDisplay.enabled = true;
  48. gameOverDisplay.text = ""+ gameObject.name + " Wins";
  49. }
  50. }
  51. void FixedUpdate() {
  52. /*
  53. * Movement inputs
  54. */
  55. Vector2 velocity = rigidbody2D.velocity;
  56. velocity.x = Input.GetAxis (HorizontalBtn) * runSpeed; //Horizontal input
  57. if ((Input.GetAxisRaw (VerticalBtn) == 1) && (canJump)) //Vertical input
  58. {
  59. velocity.y = jumpHeight;
  60. canJump = false;
  61. }//end if
  62. if (Input.GetAxisRaw (StrongAttackBtn)==1) {
  63. if (!isSpinning && (lastAttack+attackWait) < Time.time){
  64. lastAttack = Time.time;
  65. StartCoroutine (spinAttack (Vector3.down * 360, 0.2f));
  66. }
  67. }
  68. rigidbody2D.velocity = velocity; //apply inputs
  69. animator.SetFloat("Velocity",velocity.magnitude);//inputs for animator
  70. animator.SetBool ("Fall", !canJump);
  71. }//end fixed update
  72. private void displayHealth(){
  73. if (displayedHealth != health) //check if health needs updating
  74. updateHealth = true;
  75. if (updateHealth){
  76. foreach (GameObject desHeart in hearts) //Destroy all heart sprites
  77. GameObject.Destroy (desHeart);
  78. hearts.Clear(); //sets list to zero
  79. for (int i=1; i<= health; i++) { //creates new heart sprite for each health
  80. GameObject heartCanister = Instantiate (heart) as GameObject; //creat heart sprite
  81. Vector3 heartPos = new Vector3 ();
  82. //set position
  83. if (healthPos.x == 1)
  84. heartPos.x = (i * 0.033f);
  85. else
  86. heartPos.x = 1-(i * 0.033f);
  87. if (healthPos.y == 1)
  88. heartPos.y = 0.95f;
  89. else
  90. heartPos.y = 0.05f;
  91. heartCanister.transform.position = heartPos;
  92. hearts.Add(heartCanister); //adds heart to list
  93. }//end for
  94. displayedHealth = health;
  95. updateHealth = false;
  96. }//end if
  97. }//end displayHealth
  98. //respawns player if they die
  99. private void death(){
  100. if (health <= 0) {
  101. Instantiate (newCandy, transform.position + Vector3.up, transform.rotation);
  102. Vector3 spawnPos = new Vector3 (Random.Range (-20.0f, 20.0f), 15.0f, 0); //picks random position
  103. health = 3; //resets life
  104. transform.position = spawnPos; //changes position
  105. rigidbody2D.velocity = Vector2.zero;
  106. }//end if
  107. }//end death
  108. private void applyPlayerDirection(float moveHorizontal)
  109. {
  110. if ((curDirection != moveHorizontal) && (moveHorizontal != 0) && !isSpinning) //if player movement direction vs displayed direction
  111. {
  112. transform.Rotate(0,180,0); //rotates player
  113. curDirection = moveHorizontal; //updates direction
  114. }
  115. }
  116. private void damage (){
  117. if (isHit) {
  118. gameObject.GetComponent<PolygonCollider2D> ().enabled = false;
  119. isHit = false;
  120. lastHit = Time.time;
  121. }
  122. if (lastHit+invulTime < Time.time)
  123. gameObject.GetComponent<PolygonCollider2D> ().enabled = true;
  124. }
  125. IEnumerator spinAttack(Vector3 byAngles, float inTime) {
  126. isSpinning = true;
  127. stick.isAttacking = true;
  128. Debug.Log ("" + gameObject.name + "spinning");
  129. Quaternion startAngle = transform.rotation;
  130. Quaternion endAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
  131. float direction = transform.eulerAngles.y;
  132. for(float i = 0; i < 1; i += Time.deltaTime/inTime) {
  133. transform.rotation = Quaternion.Euler(0,direction,Mathf.Lerp(0,360,i));
  134. yield return null;
  135. }
  136. transform.eulerAngles = new Vector3 (0,direction,0);
  137. Debug.Log ("" + gameObject.name + "End spin");
  138. isSpinning = false;
  139. stick.isAttacking = false;
  140. }
  141. void OnCollisionEnter2D(Collision2D col){
  142. if (col.collider.tag == "ground")
  143. canJump = true;
  144. else if (col.collider.tag == "Player") {
  145. canJump = true;
  146. }
  147. }
  148. void OnTriggerEnter2D(Collider2D trig)
  149. {
  150. Debug.Log (trig.gameObject.tag);
  151. if (trig.gameObject.tag == "candy")
  152. {
  153. trig.gameObject.SetActive (false);
  154. score++;
  155. scoreDisplay.text = "score: " + score.ToString ();
  156. }
  157. }
  158. }