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.

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