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.

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