using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CsPlayer : MonoBehaviour {
public enum STATE{IDLE, ATTACK, MOVE, DIE};
public Button restartButton;
private Animator anim;
private bool isDead;
private int heroMaxHP;
private int heroHP;
private int heroDamage;
public float moveSpeed = 0.5f;
// Use this for initialization
void Start () {
isDead = false;
var gameManagerObj = GameObject.Find("GameManager");
CsGameManager GMInstance = gameManagerObj.GetComponent<CsGameManager>();
heroMaxHP = heroHP = GMInstance.GetHeroHP();
heroDamage = GMInstance.GetHeroDamage();
anim = GetComponent<Animator>();
StartCoroutine("AutoHeal");
StartCoroutine("ChangeState", STATE.IDLE);
}
public int GetMaxHP()
{
return heroMaxHP;
}
public int GetHP()
{
return heroHP;
}
public void AddHP(int _hp)
{
heroHP += _hp;
if(heroHP <= 0)
{
isDead = true;
restartButton.gameObject.active = true;
transform.GetComponent<CsPlayer>().enabled = false;
GameObject[] objs = GameObject.FindGameObjectsWithTag("ENEMY");
for (int i = 0; i < objs.Length; i++)
{
Destroy(objs[i]);
}
Destroy(gameObject);
}
}
public int GetDamage()
{
return heroDamage;
}
// Update is called once per frame
void FixedUpdate () {
if (isDead) return;
float amtMove = moveSpeed * Time.smoothDeltaTime;
float inputX = Input.GetAxis("Horizontal");
float inputZ = Input.GetAxis("Vertical");
if(inputX < -0.1f)
{
transform.localScale = new Vector3(1, 1, 1);
}
if(inputX >= 0.1f)
{
transform.localScale = new Vector3(-1, 1, 1);
}
if (inputX != 0 || inputZ != 0)
{
Vector3 pos = new Vector3(inputX, 0, inputZ);
transform.Translate(pos * amtMove, Space.World);
StartCoroutine("ChangeState", STATE.MOVE);
}
else
{
StartCoroutine("ChangeState", STATE.IDLE);
}
}
public void MovePlayer(float inputX, float inputZ)
{
if (inputX < -0.1f)
{
transform.localScale = new Vector3(1, 1, 1);
}
if (inputX >= 0.1f)
{
transform.localScale = new Vector3(-1, 1, 1);
}
if (inputX != 0 || inputZ != 0)
{
StartCoroutine("ChangeState", STATE.MOVE);
}
else
{
StartCoroutine("ChangeState", STATE.IDLE);
}
}
void OnCollisionEnter(Collision coll)
{
if(coll.transform.tag == "ENEMY")
{
StartCoroutine("ChangeState", STATE.ATTACK);
Vector3 force = CalculateDistance(coll.transform);
coll.rigidbody.AddForce(force * 40);
transform.GetComponent<Rigidbody>().AddForce((-force) * 40);
}
}
Vector3 CalculateDistance(Transform obj)
{
Vector3 dist = obj.position - transform.position;
dist = Vector3.Normalize(dist);
return dist;
}
IEnumerator ChangeState(STATE _state)
{
switch(_state)
{
case STATE.IDLE:
StartCoroutine("IdleProc");
break;
case STATE.MOVE:
StartCoroutine("MoveProc");
break;
case STATE.ATTACK:
StartCoroutine("AttackProc");
break;
case STATE.DIE:
StartCoroutine("DieProc");
break;
}
yield return null;
}
IEnumerator IdleProc()
{
anim.SetTrigger("tIdle");
yield return new WaitForSeconds(1.0f);
}
IEnumerator MoveProc()
{
anim.SetTrigger("tMove");
yield return new WaitForSeconds(1.0f);
}
IEnumerator AttackProc()
{
anim.SetTrigger("tAttack");
yield return new WaitForSeconds(0.5f);
StartCoroutine("ChangeState", STATE.IDLE);
}
IEnumerator AutoHeal()
{
while(true)
{
yield return new WaitForSeconds(0.5f);
heroHP++;
if(heroHP > heroMaxHP)
{
heroHP = heroMaxHP;
}
}
}
}