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.

362 lines
8.9 KiB

9 years ago
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. //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.GetComponent<Text>().text = "score: " + score.ToString ();
  273. }
  274. }
  275. }