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.

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