1
0

state machine

This commit is contained in:
dakota 2016-01-30 11:16:38 -05:00
parent ceff1eb851
commit d9b9f7208e
14 changed files with 310 additions and 0 deletions

View File

@ -0,0 +1,37 @@
using UnityEngine;
using System.Collections;
using GameUtils;
using System;
public class Enemy : Controller, ISceneObject{
private state currentState;
// Use this for initialization
void Start ()
{
print("start");
changestate(new idle());
}
// Update is called once per frame
void Update ()
{
currentState.Execute();
}
public void changestate(state newstate)
{
if (currentState != null)
currentState.Exit();
currentState = newstate;
currentState.Enter(this);
}
public void ObjectUpdate()
{
}
public void Initialize()
{
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5a356d60991bf2f4aae52e89b8389562
timeCreated: 1454135814
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
using UnityEngine;
using System.Collections;
public static class ExtensionMethods
{
public static Vector2 toVector2(this Vector3 vec3)
{
return new Vector2(vec3.x, vec3.y);
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9a2a3bd0eab8bd44a8b62c66b491b74f
timeCreated: 1454122812
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,49 @@
using UnityEngine;
using System.Collections;
public class FlyingAI : MonoBehaviour {
public LayerMask enemyMask;
public float speed = 1;
Rigidbody2D myBody;
Transform myTrans;
float myWidth;
// Use this for initialization
void Start()
{
myTrans = this.transform;
myBody = this.GetComponent<Rigidbody2D>();
myWidth = this.GetComponent<SpriteRenderer>().bounds.extents.x;
}
// Update is called once per frame
void FixedUpdate ()
{
//check to see if there's ground in front of us before moving forward
Vector2 lineCastPos = myTrans.position - myTrans.right * myWidth;
Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask);
Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
//bool isBlocked = Physics2D.Linecast(lineCastPos, lineCastPos - myTrans.right.toVector2(), enemyMask);
//if no ground turn around
if(!isGrounded)
{
Vector3 currentRot = myTrans.eulerAngles;
currentRot.y += 180;
myTrans.eulerAngles = currentRot;
}
//always move forward
Vector2 myVel = myBody.velocity;
myVel.x = -myTrans.right.x * speed;
myBody.velocity = myVel;
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: fdfeca06308042847a6fb2cd3d736f58
timeCreated: 1454115530
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,29 @@
using UnityEngine;
using System.Collections;
using System;
public class idle : state {
private Enemy enemy;
private float time = 0;
public void Execute()
{
time += Time.deltaTime;
if (time >= 1)
enemy.changestate(new wander());
}
public void Enter(Enemy enemy)
{
Debug.Log("idle");
time = 0;
this.enemy = enemy;
}
public void Exit()
{
}
public void onTriggerEnter(Collider2D other)
{
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d4219c53f2febb84ba5d0ef41a0dc841
timeCreated: 1454141097
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,34 @@
using UnityEngine;
using System.Collections;
using System;
public class seeking : state {
private Enemy enemy;
public void Enter(Enemy enemy)
{
this.enemy = enemy;
}
public void Execute()
{
}
public void Exit()
{
}
public void onTriggerEnter(Collider2D other)
{
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: a135daaaadfba3441aceef4152682823
timeCreated: 1454140854
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
using UnityEngine;
using System.Collections;
public interface state {
void Execute();
void Enter(Enemy enemy);
void Exit();
void onTriggerEnter(Collider2D other);
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 591ef13b39612874391352f28b303b66
timeCreated: 1454139743
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,58 @@
using UnityEngine;
using System.Collections;
using System;
public class wander : state {
private Enemy enemy;
public LayerMask enemyMask;
public float speed = 5;
Rigidbody2D myBody;
Transform myTrans;
float myWidth, myHeight;
private float time=0;
public void Execute()
{
time += Time.deltaTime;
if (time >= 5)
enemy.changestate(new idle());
//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;
currentRot.y += 180;
myTrans.eulerAngles = currentRot;
}
//always move forward
Vector2 myVel = myBody.velocity;
myVel.x = -myTrans.right.x * speed;
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)
{
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 8182b79a4527187438b0d57630925652
timeCreated: 1454115530
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: