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.

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