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.

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