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.

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