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.

312 lines
7.7 KiB

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