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.

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