# 【跟我一起学Unity3D】做一个2D的90坦克大战之AI系统

    protected enum State
{
Idle,
LeftWalk,
RightWalk,
UpWalk,
DownWalk,
Fire
};

    protected void UpdateRotate(State state)
{
switch (state)
{
case State.LeftWalk:
this.transform.rotation = Quaternion.identity;
this.transform.Rotate(Vector3.forward * m_fAngle);
m_curAngle = Vector3.left;
break;
case State.RightWalk:
this.transform.rotation = Quaternion.identity;
this.transform.Rotate(Vector3.forward * -m_fAngle);
m_curAngle = Vector3.right;
break;
case State.UpWalk:
this.transform.rotation = Quaternion.identity;
m_curAngle = Vector3.up;
break;
case State.DownWalk:
this.transform.rotation = Quaternion.identity;
this.transform.Rotate(Vector3.forward * m_fAngle * 2);
m_curAngle = Vector3.down;
break;
}
}

    public virtual void UpdateState()
{
switch (m_curState)
{
case State.Idle:
break;
case State.LeftWalk:
this.transform.position += Vector3.left * Time.deltaTime * m_fMoveSpeed;
break;
case State.UpWalk:
this.transform.position += Vector3.up * Time.deltaTime * m_fMoveSpeed;
break;
case State.DownWalk:
this.transform.position += Vector3.down * Time.deltaTime * m_fMoveSpeed;
break;
case State.RightWalk:
this.transform.position += Vector3.right * Time.deltaTime * m_fMoveSpeed;
break;
case State.Fire:
doFire();
break;
}
}

    protected virtual void doFire()
{
GameObject bullet = Instantiate(m_gBullet) as GameObject;
bullet.name = m_sName + "Bullet";
bullet.GetComponent<CBullet>().m_vDirection = m_curAngle;
bullet.GetComponent<CBullet>().m_fMoveSpeed = m_fMoveSpeed + 1.0f;
bullet.transform.position = new Vector3(this.transform.position.x, this.transform.position.y,0);
Destroy(bullet, 5.0f);
}

    void Update()
{
SetCurState(State.Idle);
//转向
if (Input.GetKey(KeyCode.A))
{
UpdateRotate(State.LeftWalk);
SetCurState(State.LeftWalk);
}
if (Input.GetKey(KeyCode.W))
{
UpdateRotate(State.UpWalk);
SetCurState(State.UpWalk);
}
if (Input.GetKey(KeyCode.S))
{
UpdateRotate(State.DownWalk);
SetCurState(State.DownWalk);
}
if (Input.GetKey(KeyCode.D))
{
UpdateRotate(State.RightWalk);
SetCurState(State.RightWalk);
}
if (Input.GetKeyDown(KeyCode.K))
{
SetCurState(State.Fire);
}
}

    protected void SetCurState(State curState)
{
if (curState != m_curState)
m_lastState = m_curState;
m_curState = curState;
}

    void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(m_sName + " OnTriggerEnter : " + other.gameObject.name);
//被打中了
if (other.gameObject.name == "AIBullet")
{
Destroy(other.gameObject);
if (Camera.main.GetComponent<CCamera>().ReduceMyLeft() > 0)
{
GameObject temp = Instantiate(m_BoomAnimation, this.gameObject.transform.position, Quaternion.identity) as GameObject;
Destroy(temp, 0.5f);
string name = this.gameObject.name;
GameObject temp_tank = Instantiate(this.gameObject, m_initPosition, Quaternion.identity) as GameObject;
temp_tank.name = name;
}
else
{
//你输了
Camera.main.GetComponent<CCamera>().m_bIsLose = true;
}
Destroy(this.gameObject);

}
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    void OnCollisionEnter2D(Collision2D coll)
{
Debug.Log(m_sName + " OnCollisionEnter2D : " + coll.gameObject.name);
ChangeStateFromWall();
}

    void ChangeStateFromWall()
{
switch (m_curState)
{
case State.Idle:
SetCurState(State.DownWalk);
break;
case State.LeftWalk:
SetCurState(State.UpWalk);
break;
case State.UpWalk:
SetCurState(State.RightWalk);
break;
case State.DownWalk:
SetCurState(State.LeftWalk);
break;
case State.RightWalk:
SetCurState(State.DownWalk);
break;
case State.Fire:
SetCurState(m_lastState);
break;
}
this.UpdateRotate(m_curState);
}
}

void InvokeRepeating (string methodName,float time,float repeatRate) 

        //每隔2秒开一次火
InvokeRepeating("doFire", 1, 2);

    protected override void doFire()
{
base.doFire();
}