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.6 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. transform.position = new Vector3 (0,30,0);
  129. Instantiate (newCandy, transform.position + Vector3.up, transform.rotation);
  130. Vector3 spawnPos = new Vector3 (Random.Range (-20.0f, 20.0f), 15.0f, 0); //picks random position
  131. health = 3; //resets life
  132. transform.position = spawnPos; //changes position
  133. rigidbody2D.velocity = Vector2.zero;
  134. deathTimer = deathTimer + 3.02f;
  135. }
  136. }//end if
  137. }//end death
  138. private void applyPlayerDirection(float moveHorizontal)
  139. {
  140. if ((curDirection != moveHorizontal) && (moveHorizontal != 0) && !isSpinning) //if player movement direction vs displayed direction
  141. {
  142. transform.Rotate(0,180,0); //rotates player
  143. curDirection = moveHorizontal; //updates direction
  144. }
  145. }
  146. private void damage (){
  147. if (isHit) {
  148. gameObject.GetComponentInChildren<PolygonCollider2D> ().enabled = false;
  149. isHit = false;
  150. lastHit = Time.time;
  151. }
  152. if (lastHit+invulTime < Time.time)
  153. gameObject.GetComponentInChildren<PolygonCollider2D> ().enabled = true;
  154. }
  155. //spin
  156. IEnumerator spinAttack(Vector3 byAngles, float inTime) {
  157. isSpinning = true;
  158. canSpin = false;
  159. stick.isAttacking = true;
  160. Debug.Log ("" + gameObject.name + "spinning");
  161. Quaternion startAngle = transform.rotation;
  162. Quaternion endAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
  163. float direction = transform.eulerAngles.y;
  164. for(float i = 0; i < 1; i += Time.deltaTime/inTime) {
  165. transform.rotation = Quaternion.Euler(0,direction,Mathf.Lerp(0,360,i));
  166. yield return null;
  167. }
  168. transform.eulerAngles = new Vector3 (0,direction,0);
  169. //Debug.Log ("" + gameObject.name + "End spin");
  170. isSpinning = false;
  171. stick.isAttacking = false;
  172. }
  173. //dash
  174. IEnumerator dashAttack (float dashDistance,float dashTime) {
  175. float endPosition;
  176. int dashDirection;
  177. isDashing = true;
  178. canDash = false;
  179. stick.isAttacking = true;
  180. Debug.Log ("" + gameObject.name + "Dashing");
  181. rigidbody2D.gravityScale = 0;
  182. Vector3 startPosition = transform.position;
  183. //Debug.Log ("rotation Y: " + transform.eulerAngles.y);
  184. if (transform.eulerAngles.y >= 150)
  185. dashDirection = -1;
  186. else
  187. dashDirection = 1;
  188. endPosition = startPosition.x + (dashDirection * dashDistance);
  189. //Debug.Log ("Dash start: " + startPosition.x);
  190. //Debug.Log ("Dash Direction: " + (dashDirection ));
  191. //Debug.Log ("Dash end: " + endPosition);
  192. for (float i = 0; i < 1; i += Time.deltaTime/dashTime) {
  193. transform.position = new Vector3(dashDirection * Mathf.Lerp((dashDirection)*startPosition.x, (dashDirection)* endPosition, i), startPosition.y, startPosition.z);
  194. if ((transform.position.x < -30.5f && dashDirection == -1) || (transform.position.x > 26.5 && dashDirection == 1))
  195. stopDash = true;
  196. if (stopDash)
  197. break;
  198. yield return null;
  199. }
  200. stopDash = false;
  201. isDashing = false;
  202. canDash = true;
  203. stick.isAttacking = false;
  204. //Debug.Log ("" + gameObject.name + " finished Dashing");
  205. rigidbody2D.gravityScale = 4;
  206. }
  207. void OnCollisionEnter2D(Collision2D col){
  208. if (col.collider.tag == "ground") {
  209. canJump = true;
  210. canSpin = true;
  211. }
  212. else if (col.collider.tag == "Player") {
  213. canJump = true;
  214. canSpin = true;
  215. }
  216. if (col.collider.tag == "wall") {
  217. stopDash = true;
  218. }
  219. }
  220. void OnTriggerEnter2D(Collider2D trig)
  221. {
  222. Debug.Log (trig.gameObject.tag);
  223. if (trig.gameObject.tag == "candy")
  224. {
  225. trig.gameObject.SetActive (false);
  226. score++;
  227. scoreDisplay.text = "score: " + score.ToString ();
  228. }
  229. }
  230. }