using UnityEngine; using System.Collections; using System.Collections.Generic; //input for lists using UnityEngine.UI; public class playerController : MonoBehaviour { public string HorizontalBtn; //holds string for horizontal input public string VerticalBtn; //holds string for Vertical input public string StrongAttackBtn; public string DashAttackBtn; public Animator animator; //Holds animator for Pinata public GameObject heart; //Health sprites public GameObject newConfetti; //confetti when hit public GameObject newCandy; public stickController stick; //controller for stick public GameObject scoreDisplay; public GUIText gameOverDisplay; public GameObject dashDisplay; //public GameObject playerFrac; public GameObject[] hearts; public Color playerColor; //public GameObject heart2; //public GameObject heart3; public float runSpeed; //run speed public float jumpHeight; //jump height public float health; //holds health public int confettiOnHit; //amount of confetti created on hit public Vector2 healthPos; public float invulTime; public float attackWait; public bool isHit; public float dashDistance; public float dashTime; public float dashWait; public float minY; public bool heartBlack = false; public bool heartColor = true; public bool dead = false; public float heartTimer = 0.0f; public int maxScore = 10; public bool debugHeartUpdate = false; private float displayedHealth = 3; //currently displayed health private bool updateHealth = true; //if the health bar needs updating private float curDirection = -1; //direction player is pointing private bool canJump = true; // if player can jump private bool canSpin = true; // if player can spin //private List hearts = new List (); //list which holds health sprites; private bool isSpinning = false; //checks if player is spin attacking private float lastHit; private float lastAttack; public int score; private bool isDashing =false; private bool canDash = true; private float lastDash = 0.0f; private bool stopDash = false; public float deathTimer = 3.0f; public bool droppedCandy = false; private float volume = 1.0f; private bool exploded = false; private bool failCheck = false; void Start(){ if (transform.rotation.y == 180) curDirection = -1; else curDirection = 1; } // Update is called once per frame void Update () { displayHealth (); death (); applyPlayerDirection (Input.GetAxisRaw (HorizontalBtn)); damage (); //if (transform.position.y < minY) // health = 0; if (score >= maxScore) { gameOverDisplay.enabled = true; gameOverDisplay.text = ""+ gameObject.name + " Wins"; Time.timeScale = 0; if (Input.anyKeyDown){ Time.timeScale = 1.0f; Application.LoadLevel("Menu_Scene"); } } dashDisplay.GetComponent ().size = (Time.time - lastDash) / dashWait; if (Input.GetKeyUp (KeyCode.F11)) { AudioListener.volume = 0.0F; } if (Input.GetKeyUp (KeyCode.F12)) { AudioListener.volume = 1.0F; } } void FixedUpdate() { /* * Movement inputs */ Vector2 velocity = rigidbody2D.velocity; velocity.x = Input.GetAxis (HorizontalBtn) * runSpeed; //Horizontal input if ((Input.GetAxisRaw (VerticalBtn) == 1) && (canJump)) //Vertical input { velocity.y = jumpHeight; canJump = false; }//end if if (Input.GetAxisRaw (StrongAttackBtn)==1) { if (!isSpinning && canSpin && Time.time - lastAttack > attackWait){ velocity.y = jumpHeight; lastAttack = Time.time; StartCoroutine (spinAttack (Vector3.down * 360, 0.2f)); } } if (Input.GetAxisRaw (DashAttackBtn) == 1) { //Debug.Log ("dashbutton pressed"); if(!isDashing && canDash && Time.time - lastDash > dashWait){ lastDash = Time.time; StartCoroutine (dashAttack (dashDistance,dashTime)); } } rigidbody2D.velocity = velocity; //apply inputs animator.SetFloat("Velocity",velocity.magnitude);//inputs for animator animator.SetBool ("Fall", !canJump); }//end fixed update private void displayHealth(){ if (displayedHealth != health || debugHeartUpdate) //check if health needs updating updateHealth = true; if (updateHealth){ foreach (GameObject heart in hearts) //Destroy all heart sprites heart.SetActive(false); //sets list to zero for (int i=0; i< health; i++) { //creates new heart sprite for each health hearts[i].SetActive(true); if (heartBlack == true && heartColor == false){ //heartCanister.guiTexture.color = Color.black; hearts[i].GetComponent().color = Color.black; } else hearts[i].GetComponent().color = playerColor; }//end for displayedHealth = health; updateHealth = false; }//end if }//end displayHealth //respawns player if they die private void death() { if (health <= 0|| dead ==true) { dead = true; if (dead == true){ heartColor = false; heartBlack = true; heartTimer += Time.deltaTime; if (heartTimer >=1.5f){ health = 1; } if (heartTimer >=2.5f){ health = 2; } /*if (!exploded){ exploded = true; Vector3 fracRotation = transform.rotation.eulerAngles; fracRotation.y +=90; GameObject explosion = Instantiate (playerFrac, transform.position+(Vector3.up*-3), Quaternion.Euler(fracRotation)) as GameObject; }*/ } if (deathTimer > 0) { deathTimer -= Time.deltaTime; if (!droppedCandy) { if (!failCheck){ Instantiate (newCandy, transform.position + Vector3.up, transform.rotation); Debug.Log ("Exploded position: " + transform.position); } else { Vector3 randCandyPos = new Vector3 (Random.Range (-20.0f, 20.0f),Random.Range (-11.0f, 14.0f),0); Instantiate (newCandy, randCandyPos, transform.rotation); failCheck = false; } droppedCandy = true; } transform.position = new Vector3 (10, -50, 0); if (deathTimer <= 0) { Vector3 spawnPos = new Vector3 (Random.Range (-20.0f, 20.0f), 15.0f, 0); //picks random position health = 3; //resets life transform.position = spawnPos; //changes position rigidbody2D.velocity = Vector2.zero; deathTimer = deathTimer + 3.02f; heartTimer = heartTimer - 3.00f; droppedCandy = false; heartBlack = false; dead = false; heartColor = true; exploded = false; } }//end if /*else if (hearts <= 0) { dead = true; }*/ }//end death } private void applyPlayerDirection(float moveHorizontal) { if ((curDirection != moveHorizontal) && (moveHorizontal != 0) && !isSpinning) //if player movement direction vs displayed direction { transform.Rotate(0,180,0); //rotates player curDirection = moveHorizontal; //updates direction } } private void damage (){ if (isHit) { gameObject.GetComponentInChildren ().enabled = false; isHit = false; lastHit = Time.time; } if (lastHit+invulTime < Time.time) gameObject.GetComponentInChildren ().enabled = true; } //spin IEnumerator spinAttack(Vector3 byAngles, float inTime) { isSpinning = true; canSpin = false; stick.isAttacking = true; Debug.Log ("" + gameObject.name + "spinning"); Quaternion startAngle = transform.rotation; Quaternion endAngle = Quaternion.Euler(transform.eulerAngles + byAngles); float direction = transform.eulerAngles.y; for(float i = 0; i < 1; i += Time.deltaTime/inTime) { transform.rotation = Quaternion.Euler(0,direction,Mathf.Lerp(0,360,i)); yield return null; } transform.eulerAngles = new Vector3 (0,direction,0); //Debug.Log ("" + gameObject.name + "End spin"); isSpinning = false; stick.isAttacking = false; } //dash IEnumerator dashAttack (float dashDistance,float dashTime) { float endPosition; int dashDirection; isDashing = true; canDash = false; stick.isAttacking = true; Debug.Log ("" + gameObject.name + "Dashing"); rigidbody2D.gravityScale = 0; Vector3 startPosition = transform.position; //Debug.Log ("rotation Y: " + transform.eulerAngles.y); if (transform.eulerAngles.y >= 150) dashDirection = -1; else dashDirection = 1; endPosition = startPosition.x + (dashDirection * dashDistance); //Debug.Log ("Dash start: " + startPosition.x); //Debug.Log ("Dash Direction: " + (dashDirection )); //Debug.Log ("Dash end: " + endPosition); for (float i = 0; i < 1; i += Time.deltaTime/dashTime) { transform.position = new Vector3(dashDirection * Mathf.Lerp((dashDirection)*startPosition.x, (dashDirection)* endPosition, i), startPosition.y, startPosition.z); if ((transform.position.x < -30.5f && dashDirection == -1) || (transform.position.x > 26.5 && dashDirection == 1)) stopDash = true; if (stopDash) break; yield return null; } stopDash = false; isDashing = false; canDash = true; stick.isAttacking = false; //Debug.Log ("" + gameObject.name + " finished Dashing"); rigidbody2D.gravityScale = 4; } void OnCollisionEnter2D(Collision2D col){ if (col.collider.tag == "ground") { canJump = true; canSpin = true; } else if (col.collider.tag == "Player") { canJump = true; canSpin = true; } if (col.collider.tag == "wall") { stopDash = true; } if (col.collider.tag == "failBox") { failCheck = true; health = 0; if (score > 0) scoreDisplay.GetComponent().text = "score: " + score.ToString (); } } void OnTriggerEnter2D(Collider2D trig) { Debug.Log (trig.gameObject.tag); if (trig.gameObject.tag == "candy") { Destroy (trig.gameObject, 0.0f); score++; scoreDisplay.GetComponent().text = "score: " + score.ToString (); } } }