Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class DragonAI : MonoBehaviour
- {
- public GameObject player;
- public GameObject[] prefabModels;
- public float RunSpeed;
- public float RotSpeed;
- private float _energy;
- private float _hunger;
- private GameObject _currentModel;
- private GameObject[] path;
- private GameObject home;
- private FSMSystem _fsm;
- private GameObject _myGameObject;
- private string _text;
- public void SetTransition(Transition t)
- {
- _fsm.PerformTransition(t);
- }
- private void InitializeFSM()
- {
- IdleState idle = new IdleState();
- idle.AddTransition(Transition.Shaking, StateID.Shake);
- _fsm.AddState(idle);
- ShakeState shake = new ShakeState();
- shake.AddTransition(Transition.Hatching, StateID.Hatch);
- shake.AddTransition(Transition.Idling, StateID.Idle);
- _fsm.AddState(shake);
- HatchingState hatch = new HatchingState();
- hatch.AddTransition(Transition.Growing1, StateID.Walk);
- _fsm.AddState(hatch);
- WalkState walk = new WalkState(path, home, RunSpeed, RotSpeed);
- walk.AddTransition(Transition.Sleepy, StateID.Sleep);
- walk.AddTransition(Transition.FoodClose, StateID.Eat);
- walk.AddTransition(Transition.Hungry, StateID.SearchFood);
- _fsm.AddState(walk);
- SleepState sleep = new SleepState();
- sleep.AddTransition(Transition.Awake, StateID.Walk);
- _fsm.AddState(sleep);
- EatState eat = new EatState();
- eat.AddTransition(Transition.Hungry, StateID.SearchFood);
- eat.AddTransition(Transition.FinishEat, StateID.Walk);
- _fsm.AddState(eat);
- SearchFoodState searcfood = new SearchFoodState(path, RunSpeed, RotSpeed);
- searcfood.AddTransition(Transition.FoodClose, StateID.Eat);
- _fsm.AddState(searcfood);
- }
- // Use this for initialization
- void Start()
- {
- InitializeMember();
- InitializeModel();
- InitializeFSM();
- }
- // Update is called once per frame
- void Update()
- {
- FSMUpdate();
- }
- void OnGUI()
- {
- GUI.Box(new Rect(100, 10, 500, 50), _text);
- }
- public void ChangeModel(int i)
- {
- Destroy(_currentModel);
- GameObject thisModel = Instantiate(prefabModels[i], _myGameObject.transform.position, _myGameObject.transform.rotation) as GameObject;
- _currentModel = thisModel;
- _currentModel.transform.parent = _myGameObject.transform;
- }
- private void FSMUpdate()
- {
- _fsm.CurrentState.Reason(player, _myGameObject);
- _fsm.CurrentState.Act(player, _myGameObject);
- }
- private void InitializeMember()
- {
- _myGameObject = gameObject;
- _fsm = new FSMSystem();
- path = GameObject.FindGameObjectsWithTag("WaypointTag");
- home = GameObject.FindGameObjectWithTag("HomeTag");
- _text = "";
- _energy = 100.0f;
- _hunger = 0.0f;
- }
- private void InitializeModel()
- {
- _currentModel = Instantiate(prefabModels[0], _myGameObject.transform.position, _myGameObject.transform.rotation) as GameObject;
- _currentModel.transform.parent = _myGameObject.transform;
- }
- public string Text
- {
- set
- {
- _text = value;
- }
- get
- {
- return _text;
- }
- }
- public float Energy
- {
- set
- {
- _energy = value;
- }
- get
- {
- return _energy;
- }
- }
- public float Hunger
- {
- set
- {
- _hunger = value;
- }
- get
- {
- return _hunger;
- }
- }
- }
- public class IdleState : FSMState
- {
- private float _elapsedTime;
- public IdleState()
- {
- stateID = StateID.Idle;
- _elapsedTime = 0.0f;
- }
- public override void Reason(GameObject player, GameObject npc)
- {
- _elapsedTime += Time.deltaTime;
- if (_elapsedTime >= 2.0f)
- {
- _elapsedTime = 0.0f;
- int rnd = Random.Range(0, 100);
- if (rnd <= 50)
- {
- npc.GetComponent<DragonAI>().Text = "The Egg is Shaking";
- npc.GetComponent<DragonAI>().SetTransition(Transition.Shaking);
- }
- else
- {
- npc.GetComponent<DragonAI>().Text = "The Egg is still tired";
- }
- }
- }
- public override void Act(GameObject player, GameObject npc)
- {
- npc.GetComponentInChildren<MeshRenderer>().renderer.material.color = Color.blue;
- }
- }
- public class ShakeState : FSMState
- {
- private float _elapsedTime;
- private float _elapsedTime2;
- public ShakeState()
- {
- stateID = StateID.Shake;
- _elapsedTime = 0.0f;
- _elapsedTime2 = 0.0f;
- }
- public override void Reason(GameObject player, GameObject npc)
- {
- _elapsedTime2 += Time.deltaTime;
- if (_elapsedTime2 >= 5.0f)
- {
- _elapsedTime2 = 0.0f;
- int rnd = Random.Range(0, 100);
- if (rnd <= 25)
- {
- npc.GetComponent<DragonAI>().Text = "The Egg is Hatching";
- npc.GetComponentInChildren<MeshRenderer>().renderer.material.color = Color.green;
- npc.GetComponent<DragonAI>().SetTransition(Transition.Hatching);
- }
- else if (rnd <= 75)
- {
- npc.GetComponent<DragonAI>().Text = "The Egg is still Shaking";
- }
- else
- {
- npc.GetComponent<DragonAI>().Text = "The Egg is tired, Go Back to Idle";
- npc.GetComponent<DragonAI>().SetTransition(Transition.Idling);
- }
- }
- }
- public override void Act(GameObject player, GameObject npc)
- {
- npc.GetComponentInChildren<MeshRenderer>().renderer.material.color = Color.red;
- _elapsedTime += Time.deltaTime;
- if (_elapsedTime >= 1.0f)
- {
- _elapsedTime = 0.0f;
- int rnd = Random.Range(1, 6);
- switch (rnd)
- {
- case 1:
- npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.up * 150.0f);
- break;
- case 2:
- npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.right * 150.0f);
- break;
- case 3:
- npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.left * 150.0f);
- break;
- case 4:
- npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.forward * 150.0f);
- break;
- case 5:
- npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.back * 150.0f);
- break;
- case 6:
- npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.down * 150.0f);
- break;
- default:
- npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.zero * 150.0f);
- break;
- }
- }
- }
- }
- public class HatchingState : FSMState
- {
- private float _elapsedTime;
- private bool _alreadyChange;
- public HatchingState()
- {
- stateID = StateID.Hatch;
- _alreadyChange = false;
- _elapsedTime = 0.0f;
- }
- public override void Reason(GameObject player, GameObject npc)
- {
- if (_alreadyChange)
- {
- {
- _elapsedTime = 0.0f;
- npc.GetComponent<DragonAI>().SetTransition(Transition.Growing1);
- }
- }
- }
- public override void Act(GameObject player, GameObject npc)
- {
- _elapsedTime += Time.deltaTime;
- if (_elapsedTime >= 3.0f)
- {
- _elapsedTime = 0;
- if (!_alreadyChange)
- {
- npc.GetComponent<DragonAI>().ChangeModel(1);
- _alreadyChange = true;
- }
- }
- }
- }
- public class WalkState : FSMState
- {
- private float _rotSpeed;
- private float _runSpeed;
- private GameObject[] _waypoints;
- private GameObject _currentWP;
- private GameObject _home;
- private List<GameObject> _foodList;
- private bool _foodAvailable;
- private int _count;
- public WalkState(GameObject[] wp, GameObject hm, float runSpeed, float rotSpeed)
- {
- _foodList = new List<GameObject>();
- stateID = StateID.Walk;
- _waypoints = wp;
- _count = 0;
- _currentWP = _waypoints[_count];
- _rotSpeed = rotSpeed;
- _runSpeed = runSpeed;
- _foodAvailable = false;
- _home = hm;
- _foodList = GameObject.Find("Food Holder").GetComponent<FoodHolder>()._foodList;
- }
- public override void Reason(GameObject player, GameObject npc)
- {
- if (npc.GetComponent<DragonAI>().Energy <= 0 && (Vector3.Distance(npc.transform.position, _currentWP.transform.position) <= 5.0f && _currentWP == _home))
- {
- npc.GetComponent<DragonAI>().Text = "Going to Sleep";
- npc.GetComponent<DragonAI>().SetTransition(Transition.Sleepy);
- }
- if (npc.GetComponent<DragonAI>().Hunger >= 15.0f && _foodAvailable)
- {
- Debug.Log("Makanan tersedia");
- GameObject temp = FindNearestFood(npc);
- if (Vector3.Distance(npc.transform.position, temp.transform.position) <= 5.0f)
- {
- npc.GetComponent<DragonAI>().Text = "Hmm Food.....";
- npc.GetComponent<DragonAI>().SetTransition(Transition.FoodClose);
- }
- }
- if (npc.GetComponentInChildren<DragonAI>().Hunger >= 40.0f)
- {
- npc.GetComponent<DragonAI>().SetTransition(Transition.Hungry);
- }
- }
- public override void Act(GameObject player, GameObject npc)
- {
- npc.GetComponent<DragonAI>().Text = "Energy : " + npc.GetComponent<DragonAI>().Energy + "% Hunger : " + npc.GetComponent<DragonAI>().Hunger + "%";
- npc.GetComponent<DragonAI>().Energy -= Time.deltaTime;
- npc.GetComponent<DragonAI>().Hunger += Time.deltaTime;
- if (npc.GetComponent<DragonAI>().Energy > 0)
- {
- npc.GetComponentInChildren<Animation>().animation.CrossFade("Run");
- if (Vector3.Distance(npc.transform.position, _currentWP.transform.position) <= 5.0f)
- {
- FindNextWP();
- }
- }
- else
- {
- npc.GetComponent<DragonAI>().Text = "Finding Home";
- FindHome();
- }
- FindingFood();
- Quaternion targetRotation = Quaternion.LookRotation(_currentWP.transform.position - npc.transform.position);
- npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation, targetRotation, Time.deltaTime * _rotSpeed);
- npc.transform.Translate(Vector3.forward * Time.deltaTime * _runSpeed);
- }
- private void FindNextWP()
- {
- _count++;
- if (_count >= _waypoints.Length)
- {
- _count = 0;
- }
- _currentWP = _waypoints[_count];
- }
- private void FindHome()
- {
- _currentWP = _home;
- }
- private void FindingFood()
- {
- _foodAvailable = _foodList.Count != 0;
- }
- private GameObject FindNearestFood(GameObject npc)
- {
- float minDist = 1000000.0f;
- GameObject tempGO = new GameObject();
- foreach (GameObject i in _foodList)
- {
- float tempDist = Vector3.Distance(npc.transform.position, i.transform.position);
- if (minDist > tempDist)
- {
- minDist = tempDist;
- tempGO = i;
- }
- }
- return tempGO;
- }
- }
- public class SleepState : FSMState
- {
- public SleepState()
- {
- stateID = StateID.Sleep;
- }
- public override void Reason(GameObject player, GameObject npc)
- {
- npc.GetComponent<DragonAI>().Energy += Time.deltaTime;
- if (npc.GetComponent<DragonAI>().Energy >= 100.0f)
- {
- npc.GetComponent<DragonAI>().SetTransition(Transition.Awake);
- }
- }
- public override void Act(GameObject player, GameObject npc)
- {
- npc.GetComponentInChildren<Animation>().animation.CrossFade("Idle01");
- npc.GetComponent<DragonAI>().Text = "Sleeping Zzzz Zzzz Zzzz, Restoring energy :" + npc.GetComponent<DragonAI>().Energy;
- }
- }
- public class EatState : FSMState
- {
- private List<GameObject> _foodList;
- private bool _foodAvailable;
- private GameObject _chosenFood;
- public EatState()
- {
- stateID = StateID.Eat;
- _foodAvailable = false;
- _foodList = GameObject.Find("Food Holder").GetComponent<FoodHolder>()._foodList;
- }
- public override void Reason(GameObject player, GameObject npc)
- {
- if (npc.GetComponent<DragonAI>().Hunger <= 0.0f)
- {
- npc.GetComponent<DragonAI>().SetTransition(Transition.FinishEat);
- }
- if (!_foodAvailable && npc.GetComponent<DragonAI>().Hunger > 40.0f)
- {
- npc.GetComponent<DragonAI>().SetTransition(Transition.Hungry);
- }
- else if (!_foodAvailable && npc.GetComponent<DragonAI>().Hunger < 15.0f)
- {
- npc.GetComponent<DragonAI>().SetTransition(Transition.FinishEat);
- }
- }
- public override void Act(GameObject player, GameObject npc)
- {
- FindingFood();
- if (_foodAvailable)
- {
- npc.GetComponent<DragonAI>().Text = "Nom Nom Nom. Hunger : " + npc.GetComponent<DragonAI>().Hunger;
- _chosenFood = FindNearestFood(npc);
- _chosenFood.GetComponent<Food>().Eaten(5 * Time.deltaTime);
- npc.GetComponent<DragonAI>().Hunger -= 5 * Time.deltaTime;
- npc.GetComponentInChildren<Animation>().animation.CrossFade("Attack01");
- }
- }
- private void FindingFood()
- {
- _foodAvailable = _foodList.Count != 0;
- }
- private GameObject FindNearestFood(GameObject npc)
- {
- float minDist = 1000000.0f;
- GameObject tempGO = new GameObject();
- foreach (GameObject i in _foodList)
- {
- float tempDist = Vector3.Distance(npc.transform.position, i.transform.position);
- if (minDist > tempDist)
- {
- minDist = tempDist;
- tempGO = i;
- }
- }
- return tempGO;
- }
- }
- public class SearchFoodState : FSMState
- {
- private GameObject[] _waypoints;
- private GameObject _currentWP;
- private float _rotSpeed;
- private float _runSpeed;
- private List<GameObject> _foodList;
- private bool _foodAvailable;
- private int _count;
- public SearchFoodState(GameObject[] wp, float runSpeed, float rotSpeed)
- {
- stateID = StateID.SearchFood;
- _waypoints = wp;
- _count = 0;
- _currentWP = _waypoints[_count];
- _rotSpeed = rotSpeed;
- _runSpeed = runSpeed;
- _foodList = GameObject.Find("Food Holder").GetComponent<FoodHolder>()._foodList;
- }
- public override void Reason(GameObject player, GameObject npc)
- {
- if (_foodAvailable)
- {
- if (Vector3.Distance(npc.transform.position, FindNearestFood(npc).transform.position) <= 2.5f)
- {
- npc.GetComponent<DragonAI>().Text = "Finally Food.....";
- npc.GetComponent<DragonAI>().SetTransition(Transition.FoodClose);
- }
- }
- }
- public override void Act(GameObject player, GameObject npc)
- {
- npc.GetComponent<DragonAI>().Text = "Finding Food...Hungry";
- FindingFood();
- if (_foodAvailable)
- {
- if (Vector3.Distance(npc.transform.position, FindNearestFood(npc).transform.position) <= 45.0f)
- {
- npc.GetComponent<DragonAI>().Text = "I saw Food...Go for it";
- _currentWP = FindNearestFood(npc);
- }
- else if (Vector3.Distance(npc.transform.position, _currentWP.transform.position) <= 5.0f)
- {
- FindNextWP();
- }
- }
- else if (Vector3.Distance(npc.transform.position, _currentWP.transform.position) <= 5.0f)
- {
- FindNextWP();
- }
- Quaternion targetRotation = Quaternion.LookRotation(_currentWP.transform.position - npc.transform.position);
- npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation, targetRotation, Time.deltaTime * _rotSpeed);
- npc.transform.Translate(Vector3.forward * Time.deltaTime * _runSpeed);
- }
- private void FindNextWP()
- {
- _count++;
- if (_count >= _waypoints.Length)
- {
- _count = 0;
- }
- _currentWP = _waypoints[_count];
- }
- private void FindingFood()
- {
- _foodAvailable = _foodList.Count != 0;
- }
- private GameObject FindNearestFood(GameObject npc)
- {
- float minDist = 1000000.0f;
- GameObject tempGO = new GameObject();
- foreach (GameObject i in _foodList)
- {
- float tempDist = Vector3.Distance(npc.transform.position, i.transform.position);
- if (minDist > tempDist)
- {
- minDist = tempDist;
- tempGO = i;
- }
- }
- return tempGO;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement