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.

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