using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { private MathsFTW maths; public Rigidbody2D rB; public SpriteRenderer sR; public CapsuleCollider2D coll; public Animator anim; public GameObject hitEffect; public GameObject flash; private Database database; private GameManager manager; public List tools; private int currentTool; public GameObject bullet; public enum PlayerState { Standing, Hanging, Boosting, Climbing }; private PlayerState state; private bool isInvincible; private bool canUseTool; private bool canJump; private bool canBoost; private bool isJumping; private bool canMove; private bool canHang; private bool facingRight; public float health; private float fullHealth; public float jumpPower; public float slidePower; public float climbSpeed; private bool canHit; private float vertKnockback; private float horizKnockback; private float knockbackPower; private Vector3 ledgePos; public float attackKnockback; private bool controlEnabled; private bool isHealing; private GameObject groundTransform; private GameObject rightTransform; private GameObject leftTransform; private GameObject leftWalk; private GameObject rightWalk; private GameObject attack; private GameObject attackPoint; private GameObject attackPoint2; private GameObject attackPoint3; private Animator attackAnim; private GameObject downGrabTransform; private Ladder activeLadder; public float baseSpeed; public float baseDamage; public float baseAttackSpeed; public float baseJump; public float baseMaxHealth; private float statSpeed; private float statDamage; private float statAttackSpeed; private float statJump; private float statMaxHealth; private float statDefence; // Use this for initialization void Start () { database = GameObject.FindGameObjectWithTag ("Database").GetComponent (); maths = GetComponent (); rB = GetComponent (); sR = transform.Find ("BodySprite").GetComponent (); coll = GetComponent (); manager = GameObject.FindGameObjectWithTag ("GameManager").GetComponent (); anim = transform.Find ("BodySprite").GetComponent(); database.GetPlayerStats (ref statSpeed, ref statDamage, ref statAttackSpeed, ref statJump, ref statMaxHealth, ref statDefence); groundTransform = transform.Find ("FloorTransform").gameObject; rightTransform = transform.Find ("RightTransform").gameObject; leftTransform = transform.Find ("LeftTransform").gameObject; leftWalk = transform.Find ("LeftWalk").gameObject; rightWalk = transform.Find ("RightWalk").gameObject; attack = transform.Find ("Attack").gameObject; attackPoint = attack.transform.Find ("AttackPoint").gameObject; attackPoint2 = attack.transform.Find ("AttackPoint2").gameObject; attackPoint3 = attack.transform.Find ("AttackPoint3").gameObject; attackAnim = attack.transform.Find ("Sprite").gameObject.GetComponent(); downGrabTransform = transform.Find ("DownGrabTransform").gameObject; FillTools(); rB.freezeRotation = true; state = PlayerState.Standing; isInvincible = false; canUseTool = true; isJumping = false; canMove = true; canHang = true; canBoost = true; facingRight = true; canHit = true; fullHealth = (baseMaxHealth + ((baseMaxHealth * statMaxHealth) * 1.5f)); health = fullHealth; vertKnockback = 10.0f; horizKnockback = 50.0f; controlEnabled = true; currentTool = 0; isHealing = false; manager.SetToolName (tools[currentTool].ToolName.ToString()); manager.SetPlayerHealth (health); } // Update is called once per frame void Update () { database.GetPlayerStats (ref statSpeed, ref statDamage, ref statAttackSpeed, ref statJump, ref statMaxHealth, ref statDefence); if (state == PlayerState.Standing) { rB.gravityScale = 3; } else if (state == PlayerState.Hanging || state == PlayerState.Boosting || state == PlayerState.Climbing) { rB.gravityScale = 0; } if (state == PlayerState.Boosting) { sR.color = new Color (1.0f, 1.0f, 0.5f); } else { if (isInvincible) { if (sR.color.g < Color.gray.g) { sR.color = new Color (sR.color.r + 0.02f, sR.color.g + 0.02f, sR.color.b + 0.02f); } } else { if (sR.color != Color.white) { sR.color = new Color (sR.color.r + 0.02f, sR.color.g + 0.02f, sR.color.b + 0.02f); } } } if (GetCanJump() && state != PlayerState.Boosting) { canBoost = true; } if (isHealing) { if (health < fullHealth) { if (health + 1.0f >= fullHealth) { health = fullHealth; } else { health += 2.0f; } } else { isHealing = false; controlEnabled = true; } manager.SetPlayerHealth (health); } if (controlEnabled) { if (health <= 0) { Die (); } else { float horizontalAxis = Input.GetAxis ("Horizontal"); if (horizontalAxis > 0.0f && state == PlayerState.Standing) { facingRight = true; anim.SetBool("PlayerMoving", true); } else if (horizontalAxis < 0.0f && state == PlayerState.Standing) { facingRight = false; anim.SetBool("PlayerMoving", true); } else { anim.SetBool("PlayerMoving", false); } if (Input.GetKeyDown (KeyCode.Q)) { if (currentTool + 1 >= tools.Count) { currentTool = 0; } else { currentTool++; } manager.SetToolName (tools[currentTool].ToolName.ToString()); } if (state == PlayerState.Hanging) { Hanging (); EnableAttackUI (false); } else if (state == PlayerState.Climbing) { Climbing(); EnableAttackUI (false); } else { MovePlayer (horizontalAxis); EnableAttackUI (true); Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); float angle = Mathf.Atan2 (mousePos.y - transform.position.y, mousePos.x - transform.position.x) * Mathf.Rad2Deg; attack.transform.rotation = Quaternion.AngleAxis (angle, transform.forward); if (Input.GetMouseButton(0) && canHit) { Attack (); canHit = false; } if (Input.GetMouseButtonDown(1) && canUseTool) { UseTool (); canUseTool = false; StartCoroutine (ToolTimer()); } } } } UpdateAnimation(); } void UpdateAnimation() { if (facingRight) { sR.transform.localScale = new Vector3(0.3f, 0.3f, 1.0f); sR.transform.localPosition = new Vector3(0.127f, 0.04f, 0.0f); if (anim.GetCurrentAnimatorClipInfo(0)[0].clip.name == "HumpHit") { } } else { sR.transform.localScale = new Vector3(-0.3f, 0.3f, 1.0f); sR.transform.localPosition = new Vector3(-0.127f, 0.04f, 0.0f); if (anim.GetCurrentAnimatorClipInfo(0)[0].clip.name == "HumpHit") { } } if (GetIsHanging()) { anim.SetBool("PlayerHanging", true); } else { anim.SetBool("PlayerHanging", false); } if (!GetCanJump()) { anim.SetBool("PlayerJumping", true); } else if (GetCanJump()) { anim.SetBool("PlayerJumping", false); } } void EnableAttackUI(bool newEnabled) { attackPoint.GetComponent ().enabled = newEnabled; attackPoint2.GetComponent ().enabled = newEnabled; attackPoint3.GetComponent ().enabled = newEnabled; } void UseTool() { if (tools [currentTool].GetToolName() == Tool.ToolNames.Gun) { Instantiate (bullet, attackPoint.transform.position, attack.transform.rotation); } } public void SetClimbingLadder(bool newIsClimbing, Ladder newLadder) { if (newIsClimbing) { state = PlayerState.Climbing; activeLadder = newLadder; rB.gravityScale = 0; //Vector2 newPosition = new Vector2(activeLadder.gameObject.transform.position.x, transform.position.y); Vector3 newPos = Vector3.zero; newPos = maths.ClosestPointOnLine (activeLadder.GetBottomLimit (), activeLadder.GetTopLimit (), transform.position); transform.position = newPos; } else { state = PlayerState.Standing; } } public void SetEnableControl(bool newEnabled) { controlEnabled = newEnabled; if (!newEnabled) { rB.velocity = Vector3.zero; } } public void Die() { manager.PlayerDead (); Destroy (this.gameObject); } public void FillHealth() { controlEnabled = false; isHealing = true; } public void FillTools() { tools = GameObject.FindGameObjectWithTag("Database").GetComponent().GetUnlockedTools(); } void Attack() { attackAnim.SetTrigger("Attack"); anim.SetTrigger("Hit"); List hitObjects = new List (); AttackCheck (transform.position, attackPoint.transform.position, ref hitObjects); AttackCheck (transform.position, attackPoint2.transform.position, ref hitObjects); AttackCheck (transform.position, attackPoint3.transform.position, ref hitObjects); if (hitObjects.Count > 0) { rB.simulated = false; StartCoroutine(FreezeTimer()); } else { StartCoroutine(HitTimer()); } } void AttackCheck(Vector2 startPos, Vector2 endPos, ref List hitObjects) { float distance = Vector2.Distance(startPos, endPos); foreach (RaycastHit2D hit in Physics2D.RaycastAll(startPos, endPos - startPos, distance)) { if (hit.collider.isTrigger == false && hit.collider.gameObject != this.gameObject) { break; } Enemy enemy = hit.collider.gameObject.GetComponent (); Button button = hit.collider.gameObject.GetComponent