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.

375 lines
9.7 KiB

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