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.

363 lines
9.0 KiB

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