finish state machine
This commit is contained in:
parent
0b9a54c664
commit
2e16acb527
@ -7,23 +7,23 @@ public class Enemy : Controller{
|
||||
public bool isAir;
|
||||
private state currentState;
|
||||
public LayerMask enemyMask;
|
||||
public bool rightdirection;
|
||||
// Use this for initialization
|
||||
void Start ()
|
||||
{
|
||||
|
||||
rightdirection = true;
|
||||
isAir = false;
|
||||
print("start");
|
||||
changestate(new idle());
|
||||
|
||||
}
|
||||
// Update is called once per frame
|
||||
void FixedUpdate ()
|
||||
void Update ()
|
||||
{
|
||||
currentState.Execute();
|
||||
}
|
||||
public void changestate(state newstate)
|
||||
{
|
||||
if (currentState != null)
|
||||
if(currentState != null)
|
||||
currentState.Exit();
|
||||
currentState = newstate;
|
||||
currentState.Enter(this);
|
||||
|
@ -5,32 +5,68 @@ using GameUtils;
|
||||
|
||||
public class seeking : state
|
||||
{
|
||||
private Enemy enemy;
|
||||
public Enemy enemy;
|
||||
public LayerMask enemyMask;
|
||||
public float speed;
|
||||
Rigidbody2D myBody;
|
||||
Transform myTrans;
|
||||
float myWidth, myHeight;
|
||||
private int dis = 5;
|
||||
public bool isSee;
|
||||
public Collider2D p;
|
||||
|
||||
public void Enter(Enemy enemy)
|
||||
{
|
||||
Debug.Log("seeking");
|
||||
this.enemy = enemy;
|
||||
init();
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
|
||||
seeplayer();
|
||||
if (isSee)
|
||||
enemy.changestate(new idle());
|
||||
else
|
||||
Chase();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void Exit()
|
||||
public void Exit(){}
|
||||
public void onTriggerEnter(Collider2D other){}
|
||||
public void init()
|
||||
{
|
||||
this.enemyMask = enemy.enemyMask;
|
||||
myTrans = enemy.transform;
|
||||
myBody = enemy.GetComponent<Rigidbody2D>();
|
||||
SpriteRenderer mySprite = enemy.GetComponent<SpriteRenderer>();
|
||||
myWidth = mySprite.bounds.extents.x;
|
||||
myHeight = mySprite.bounds.extents.y;
|
||||
speed = enemy.movementSpeed;
|
||||
}
|
||||
|
||||
public void onTriggerEnter(Collider2D other)
|
||||
public void seeplayer()
|
||||
{
|
||||
Vector2 lineCastPos = myTrans.position.toVector2() - myTrans.right.toVector2() * myWidth + Vector2.up * myHeight;
|
||||
lineCastPos.y = lineCastPos.y - (myHeight * 1.2f);
|
||||
Vector3 currentRot = myTrans.eulerAngles;
|
||||
if (enemy.rightdirection)
|
||||
{
|
||||
Debug.DrawLine(lineCastPos, lineCastPos - myTrans.right.toVector2() * dis);
|
||||
isSee = Physics2D.Linecast(lineCastPos, lineCastPos + myTrans.right.toVector2() * dis, enemy.enemyMask);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.DrawLine(lineCastPos, lineCastPos - myTrans.right.toVector2() * dis);
|
||||
isSee = Physics2D.Linecast(lineCastPos, lineCastPos - myTrans.right.toVector2() * dis, enemy.enemyMask);
|
||||
}
|
||||
}
|
||||
public void Chase()
|
||||
{
|
||||
Vector2 myVel = myBody.velocity;
|
||||
if(!enemy.rightdirection)
|
||||
myVel.x = myTrans.right.x * speed;
|
||||
else
|
||||
myVel.x = myTrans.right.x * -speed;
|
||||
myBody.velocity = myVel;
|
||||
}
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -12,12 +12,15 @@ public class wander : state
|
||||
Transform myTrans;
|
||||
float myWidth, myHeight;
|
||||
private float time=0;
|
||||
public bool isSee;
|
||||
private int dis;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
time += Time.deltaTime;
|
||||
if (time >= 10)
|
||||
enemy.changestate(new idle());
|
||||
seeplayer();
|
||||
CheckHead();
|
||||
Move();
|
||||
}
|
||||
@ -26,6 +29,7 @@ public class wander : state
|
||||
{
|
||||
Debug.Log("Wander");
|
||||
time = 0;
|
||||
dis = 5;
|
||||
this.enemy = enemy;
|
||||
this.enemyMask = enemy.enemyMask;
|
||||
myTrans = enemy.transform;
|
||||
@ -39,14 +43,12 @@ public class wander : state
|
||||
public void onTriggerEnter(Collider2D other){}
|
||||
public void CheckHead()
|
||||
{
|
||||
//check to see if there's ground in front of us before moving forward
|
||||
Vector2 lineCastPos = myTrans.position.toVector2() - myTrans.right.toVector2() * myWidth + Vector2.up * myHeight;
|
||||
lineCastPos.y = lineCastPos.y - (myHeight * 1.2f);
|
||||
Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
|
||||
bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask);
|
||||
Debug.DrawLine(lineCastPos, lineCastPos - myTrans.right.toVector2() * 0.05f);
|
||||
bool isBlocked = Physics2D.Linecast(lineCastPos, lineCastPos - myTrans.right.toVector2() * 0.05f, enemyMask);
|
||||
//if no ground turn around
|
||||
if (!isGrounded || isBlocked)
|
||||
{
|
||||
Vector3 currentRot = myTrans.eulerAngles;
|
||||
@ -56,10 +58,27 @@ public class wander : state
|
||||
}
|
||||
public void Move()
|
||||
{
|
||||
//always move forward
|
||||
Vector2 myVel = myBody.velocity;
|
||||
myVel.x = -myTrans.right.x * speed;
|
||||
myBody.velocity = myVel;
|
||||
}
|
||||
|
||||
public void seeplayer()
|
||||
{
|
||||
Vector2 lineCastPos = myTrans.position.toVector2() - myTrans.right.toVector2() * myWidth + Vector2.up * myHeight;
|
||||
lineCastPos.y = lineCastPos.y - (myHeight * 1.2f);
|
||||
if(enemy.rightdirection)
|
||||
{
|
||||
Debug.DrawLine(lineCastPos, lineCastPos + myTrans.right.toVector2() * -dis);
|
||||
isSee = Physics2D.Linecast(lineCastPos, lineCastPos + myTrans.right.toVector2() * -dis, enemy.enemyMask);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.DrawLine(lineCastPos, lineCastPos + myTrans.right.toVector2() * dis);
|
||||
isSee = Physics2D.Linecast(lineCastPos, lineCastPos - myTrans.right.toVector2() * dis, enemy.enemyMask);
|
||||
}
|
||||
if (isSee)
|
||||
{
|
||||
enemy.changestate(new seeking());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user