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.

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