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.

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