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.

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