finish the state machine for idle and wander
This commit is contained in:
parent
9956b9286d
commit
3d5d94f897
@ -1,107 +0,0 @@
|
|||||||
using UnityEngine;
|
|
||||||
using System.Collections;
|
|
||||||
|
|
||||||
public class hover : MonoBehaviour
|
|
||||||
{
|
|
||||||
|
|
||||||
public LayerMask enemyMask;
|
|
||||||
public float speed = 1;
|
|
||||||
public bool flyPattern; // false is x-axis, true is y axis YOU HAPPY???? good
|
|
||||||
public float maxFlightDistance = 0; // if flight distance is 0, fly foreva, probably should never do.. lol
|
|
||||||
float distanceFlown;
|
|
||||||
Rigidbody2D myBody;
|
|
||||||
Transform myTrans;
|
|
||||||
float myWidth, myHeight;
|
|
||||||
float lastPos;
|
|
||||||
|
|
||||||
|
|
||||||
// Use this for initialization
|
|
||||||
void Start()
|
|
||||||
{
|
|
||||||
myTrans = this.transform;
|
|
||||||
myBody = this.GetComponent<Rigidbody2D>();
|
|
||||||
SpriteRenderer mySprite = this.GetComponent<SpriteRenderer>();
|
|
||||||
myWidth = mySprite.bounds.extents.x;
|
|
||||||
myHeight = mySprite.bounds.extents.y;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Update is called once per frame
|
|
||||||
void FixedUpdate()
|
|
||||||
{
|
|
||||||
Vector2 lineCastPos;
|
|
||||||
//check to see if there's something in front of us before moving forward
|
|
||||||
if (flyPattern == false)
|
|
||||||
{
|
|
||||||
lineCastPos = myTrans.position.toVector2() - myTrans.right.toVector2() * myWidth + Vector2.up * myHeight;
|
|
||||||
lineCastPos.y = lineCastPos.y - (myHeight * 1.2f);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lineCastPos = myTrans.position.toVector2() - myTrans.up.toVector2() * myWidth + Vector2.right * myHeight;
|
|
||||||
lineCastPos.x -= myWidth;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down * 0.05f);
|
|
||||||
bool isBlockedY = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down * 0.05f, enemyMask);
|
|
||||||
Debug.DrawLine(lineCastPos, lineCastPos - myTrans.right.toVector2() * 0.05f);
|
|
||||||
bool isBlockedX = Physics2D.Linecast(lineCastPos, lineCastPos - myTrans.right.toVector2() * 0.05f, enemyMask);
|
|
||||||
|
|
||||||
//if hit wall on x-axis ground turn around
|
|
||||||
if ((isBlockedX && flyPattern == false) || (distanceFlown > maxFlightDistance && maxFlightDistance != 0 && flyPattern == false))
|
|
||||||
{
|
|
||||||
Vector3 currentRot = myTrans.eulerAngles;
|
|
||||||
currentRot.y += 180;
|
|
||||||
print(currentRot.x);
|
|
||||||
myTrans.eulerAngles = currentRot;
|
|
||||||
distanceFlown = 0;
|
|
||||||
print("did it");
|
|
||||||
}
|
|
||||||
|
|
||||||
//if hit wall on y-axis ground turn around
|
|
||||||
else if((isBlockedY && flyPattern == true) || (distanceFlown > maxFlightDistance && maxFlightDistance != 0 && flyPattern == true))
|
|
||||||
{
|
|
||||||
Vector3 currentRot = myTrans.eulerAngles;
|
|
||||||
currentRot.x += 180;
|
|
||||||
print(currentRot.x);
|
|
||||||
myTrans.eulerAngles = currentRot;
|
|
||||||
print(myTrans.eulerAngles);
|
|
||||||
distanceFlown = 0;
|
|
||||||
print("did it");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Horizontal movement
|
|
||||||
if (flyPattern == false)
|
|
||||||
{
|
|
||||||
Vector2 myVel = myBody.velocity;
|
|
||||||
myVel.x = -myTrans.right.x * speed;
|
|
||||||
distanceFlown += Mathf.Abs(lastPos - transform.position.x);
|
|
||||||
//print(distanceFlown);
|
|
||||||
myBody.velocity = myVel;
|
|
||||||
lastPos = transform.position.x;
|
|
||||||
|
|
||||||
}
|
|
||||||
//Vertickle movement
|
|
||||||
if(flyPattern == true)
|
|
||||||
{
|
|
||||||
Vector2 myVel = myBody.velocity;
|
|
||||||
myVel.y = -myTrans.up.y * speed;
|
|
||||||
distanceFlown += Mathf.Abs(lastPos - transform.position.y);
|
|
||||||
//print(distanceFlown);
|
|
||||||
myBody.velocity = myVel;
|
|
||||||
lastPos = transform.position.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 7feb214d1ef985b4dac3410715f7e42c
|
|
||||||
timeCreated: 1454129535
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
Binary file not shown.
@ -3,18 +3,21 @@ using System.Collections;
|
|||||||
using GameUtils;
|
using GameUtils;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
public class Enemy : Controller, ISceneObject{
|
public class Enemy : Controller{
|
||||||
|
public bool isAir;
|
||||||
private state currentState;
|
private state currentState;
|
||||||
// Use this for initialization
|
public LayerMask enemyMask;
|
||||||
void Start ()
|
// Use this for initialization
|
||||||
|
void Start ()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
isAir = false;
|
||||||
print("start");
|
print("start");
|
||||||
changestate(new idle());
|
changestate(new idle());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
void Update ()
|
void FixedUpdate ()
|
||||||
{
|
{
|
||||||
currentState.Execute();
|
currentState.Execute();
|
||||||
}
|
}
|
||||||
@ -25,13 +28,4 @@ public class Enemy : Controller, ISceneObject{
|
|||||||
currentState = newstate;
|
currentState = newstate;
|
||||||
currentState.Enter(this);
|
currentState.Enter(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ObjectUpdate()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Initialize()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,28 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System;
|
using System;
|
||||||
|
using GameUtils;
|
||||||
|
|
||||||
public class idle : state {
|
public class idle : state
|
||||||
|
{
|
||||||
|
private ISceneObject manager;
|
||||||
private Enemy enemy;
|
private Enemy enemy;
|
||||||
private float time = 0;
|
private float time = 0;
|
||||||
public void Execute()
|
public void Execute()
|
||||||
{
|
{
|
||||||
time += Time.deltaTime;
|
Idle();
|
||||||
if (time >= 1)
|
|
||||||
enemy.changestate(new wander());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Enter(Enemy enemy)
|
public void Enter(Enemy enemy)
|
||||||
{
|
{
|
||||||
Debug.Log("idle");
|
Debug.Log("idle");
|
||||||
time = 0;
|
|
||||||
this.enemy = enemy;
|
this.enemy = enemy;
|
||||||
}
|
}
|
||||||
|
public void Exit(){}
|
||||||
public void Exit()
|
public void onTriggerEnter(Collider2D other){}
|
||||||
{
|
public void Idle()
|
||||||
}
|
|
||||||
|
|
||||||
public void onTriggerEnter(Collider2D other)
|
|
||||||
{
|
{
|
||||||
|
time += Time.deltaTime;
|
||||||
|
if (time >= 3)
|
||||||
|
enemy.changestate(new wander());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System;
|
using System;
|
||||||
|
using GameUtils;
|
||||||
|
|
||||||
public class seeking : state {
|
public class seeking : state
|
||||||
|
{
|
||||||
private Enemy enemy;
|
private Enemy enemy;
|
||||||
public void Enter(Enemy enemy)
|
public void Enter(Enemy enemy)
|
||||||
{
|
{
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System;
|
using System;
|
||||||
|
using GameUtils;
|
||||||
|
|
||||||
public class wander : state {
|
public class wander : state
|
||||||
|
{
|
||||||
private Enemy enemy;
|
private Enemy enemy;
|
||||||
public LayerMask enemyMask;
|
public LayerMask enemyMask;
|
||||||
public float speed = 5;
|
public float speed;
|
||||||
Rigidbody2D myBody;
|
Rigidbody2D myBody;
|
||||||
Transform myTrans;
|
Transform myTrans;
|
||||||
float myWidth, myHeight;
|
float myWidth, myHeight;
|
||||||
@ -14,8 +16,29 @@ public class wander : state {
|
|||||||
public void Execute()
|
public void Execute()
|
||||||
{
|
{
|
||||||
time += Time.deltaTime;
|
time += Time.deltaTime;
|
||||||
if (time >= 5)
|
if (time >= 10)
|
||||||
enemy.changestate(new idle());
|
enemy.changestate(new idle());
|
||||||
|
CheckHead();
|
||||||
|
Move();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Enter(Enemy enemy)
|
||||||
|
{
|
||||||
|
Debug.Log("Wander");
|
||||||
|
time = 0;
|
||||||
|
this.enemy = enemy;
|
||||||
|
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 Exit(){}
|
||||||
|
public void onTriggerEnter(Collider2D other){}
|
||||||
|
public void CheckHead()
|
||||||
|
{
|
||||||
//check to see if there's ground in front of us before moving forward
|
//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;
|
Vector2 lineCastPos = myTrans.position.toVector2() - myTrans.right.toVector2() * myWidth + Vector2.up * myHeight;
|
||||||
lineCastPos.y = lineCastPos.y - (myHeight * 1.2f);
|
lineCastPos.y = lineCastPos.y - (myHeight * 1.2f);
|
||||||
@ -30,29 +53,13 @@ public class wander : state {
|
|||||||
currentRot.y += 180;
|
currentRot.y += 180;
|
||||||
myTrans.eulerAngles = currentRot;
|
myTrans.eulerAngles = currentRot;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
public void Move()
|
||||||
|
{
|
||||||
//always move forward
|
//always move forward
|
||||||
Vector2 myVel = myBody.velocity;
|
Vector2 myVel = myBody.velocity;
|
||||||
myVel.x = -myTrans.right.x * speed;
|
myVel.x = -myTrans.right.x * speed;
|
||||||
myBody.velocity = myVel;
|
myBody.velocity = myVel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Enter(Enemy enemy)
|
|
||||||
{
|
|
||||||
Debug.Log("Wander");
|
|
||||||
time = 0;
|
|
||||||
this.enemy = enemy;
|
|
||||||
myTrans = enemy.transform;
|
|
||||||
myBody = enemy.GetComponent<Rigidbody2D>();
|
|
||||||
SpriteRenderer mySprite = enemy.GetComponent<SpriteRenderer>();
|
|
||||||
myWidth = mySprite.bounds.extents.x;
|
|
||||||
myHeight = mySprite.bounds.extents.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Exit()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onTriggerEnter(Collider2D other)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Reference in New Issue
Block a user