Advertisement
gotoheavenbro

DragonAI.cs

Nov 20th, 2014
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.59 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class DragonAI : MonoBehaviour
  5. {
  6.     public GameObject player;
  7.     public GameObject[] prefabModels;
  8.     public float RunSpeed;
  9.     public float RotSpeed;
  10.  
  11.     private float _energy;
  12.     private float _hunger;
  13.     private GameObject _currentModel;
  14.     private GameObject[] path;
  15.     private GameObject home;
  16.     private FSMSystem _fsm;
  17.     private GameObject _myGameObject;
  18.     private string _text;
  19.  
  20.     public void SetTransition(Transition t)
  21.     {
  22.         _fsm.PerformTransition(t);
  23.     }
  24.  
  25.     private void InitializeFSM()
  26.     {
  27.         IdleState idle = new IdleState();
  28.         idle.AddTransition(Transition.Shaking, StateID.Shake);
  29.         _fsm.AddState(idle);
  30.  
  31.         ShakeState shake = new ShakeState();
  32.         shake.AddTransition(Transition.Hatching, StateID.Hatch);
  33.         shake.AddTransition(Transition.Idling, StateID.Idle);
  34.         _fsm.AddState(shake);
  35.  
  36.         HatchingState hatch = new HatchingState();
  37.         hatch.AddTransition(Transition.Growing1, StateID.Walk);
  38.         _fsm.AddState(hatch);
  39.  
  40.         WalkState walk = new WalkState(path, home, RunSpeed, RotSpeed);
  41.         walk.AddTransition(Transition.Sleepy, StateID.Sleep);
  42.         walk.AddTransition(Transition.FoodClose, StateID.Eat);
  43.         walk.AddTransition(Transition.Hungry, StateID.SearchFood);
  44.         _fsm.AddState(walk);
  45.  
  46.         SleepState sleep = new SleepState();
  47.         sleep.AddTransition(Transition.Awake, StateID.Walk);
  48.         _fsm.AddState(sleep);
  49.  
  50.         EatState eat = new EatState();
  51.         eat.AddTransition(Transition.Hungry, StateID.SearchFood);
  52.         eat.AddTransition(Transition.FinishEat, StateID.Walk);
  53.         _fsm.AddState(eat);
  54.  
  55.         SearchFoodState searcfood = new SearchFoodState(path, RunSpeed, RotSpeed);
  56.         searcfood.AddTransition(Transition.FoodClose, StateID.Eat);
  57.         _fsm.AddState(searcfood);
  58.     }
  59.     // Use this for initialization
  60.     void Start()
  61.     {
  62.         InitializeMember();
  63.         InitializeModel();
  64.         InitializeFSM();
  65.     }
  66.  
  67.     // Update is called once per frame
  68.     void Update()
  69.     {
  70.         FSMUpdate();
  71.     }
  72.  
  73.     void OnGUI()
  74.     {
  75.         GUI.Box(new Rect(100, 10, 500, 50), _text);
  76.     }
  77.  
  78.     public void ChangeModel(int i)
  79.     {
  80.         Destroy(_currentModel);
  81.         GameObject thisModel = Instantiate(prefabModels[i], _myGameObject.transform.position, _myGameObject.transform.rotation) as GameObject;
  82.         _currentModel = thisModel;
  83.         _currentModel.transform.parent = _myGameObject.transform;
  84.  
  85.     }
  86.     private void FSMUpdate()
  87.     {
  88.         _fsm.CurrentState.Reason(player, _myGameObject);
  89.         _fsm.CurrentState.Act(player, _myGameObject);
  90.     }
  91.     private void InitializeMember()
  92.     {
  93.         _myGameObject = gameObject;
  94.         _fsm = new FSMSystem();
  95.         path = GameObject.FindGameObjectsWithTag("WaypointTag");
  96.         home = GameObject.FindGameObjectWithTag("HomeTag");
  97.         _text = "";
  98.         _energy = 100.0f;
  99.         _hunger = 0.0f;
  100.     }
  101.     private void InitializeModel()
  102.     {
  103.         _currentModel = Instantiate(prefabModels[0], _myGameObject.transform.position, _myGameObject.transform.rotation) as GameObject;
  104.         _currentModel.transform.parent = _myGameObject.transform;
  105.     }
  106.     public string Text
  107.     {
  108.         set
  109.         {
  110.             _text = value;
  111.         }
  112.         get
  113.         {
  114.             return _text;
  115.         }
  116.     }
  117.     public float Energy
  118.     {
  119.         set
  120.         {
  121.             _energy = value;
  122.         }
  123.         get
  124.         {
  125.             return _energy;
  126.         }
  127.     }
  128.     public float Hunger
  129.     {
  130.         set
  131.         {
  132.             _hunger = value;
  133.         }
  134.         get
  135.         {
  136.             return _hunger;
  137.         }
  138.     }
  139.  
  140. }
  141.  
  142. public class IdleState : FSMState
  143. {
  144.     private float _elapsedTime;
  145.     public IdleState()
  146.     {
  147.         stateID = StateID.Idle;
  148.         _elapsedTime = 0.0f;
  149.     }
  150.     public override void Reason(GameObject player, GameObject npc)
  151.     {
  152.         _elapsedTime += Time.deltaTime;
  153.         if (_elapsedTime >= 2.0f)
  154.         {
  155.             _elapsedTime = 0.0f;
  156.             int rnd = Random.Range(0, 100);
  157.             if (rnd <= 50)
  158.             {
  159.                 npc.GetComponent<DragonAI>().Text = "The Egg is Shaking";
  160.                 npc.GetComponent<DragonAI>().SetTransition(Transition.Shaking);
  161.             }
  162.             else
  163.             {
  164.                 npc.GetComponent<DragonAI>().Text = "The Egg is still tired";
  165.             }
  166.         }
  167.     }
  168.     public override void Act(GameObject player, GameObject npc)
  169.     {
  170.         npc.GetComponentInChildren<MeshRenderer>().renderer.material.color = Color.blue;
  171.     }
  172.  
  173. }
  174.  
  175. public class ShakeState : FSMState
  176. {
  177.     private float _elapsedTime;
  178.     private float _elapsedTime2;
  179.     public ShakeState()
  180.     {
  181.         stateID = StateID.Shake;
  182.         _elapsedTime = 0.0f;
  183.         _elapsedTime2 = 0.0f;
  184.     }
  185.     public override void Reason(GameObject player, GameObject npc)
  186.     {
  187.         _elapsedTime2 += Time.deltaTime;
  188.         if (_elapsedTime2 >= 5.0f)
  189.         {
  190.             _elapsedTime2 = 0.0f;
  191.             int rnd = Random.Range(0, 100);
  192.             if (rnd <= 25)
  193.             {
  194.                 npc.GetComponent<DragonAI>().Text = "The Egg is Hatching";
  195.                 npc.GetComponentInChildren<MeshRenderer>().renderer.material.color = Color.green;
  196.                 npc.GetComponent<DragonAI>().SetTransition(Transition.Hatching);
  197.             }
  198.             else if (rnd <= 75)
  199.             {
  200.                 npc.GetComponent<DragonAI>().Text = "The Egg is still Shaking";
  201.  
  202.             }
  203.             else
  204.             {
  205.                 npc.GetComponent<DragonAI>().Text = "The Egg is tired, Go Back to Idle";
  206.                 npc.GetComponent<DragonAI>().SetTransition(Transition.Idling);
  207.             }
  208.         }
  209.  
  210.     }
  211.     public override void Act(GameObject player, GameObject npc)
  212.     {
  213.         npc.GetComponentInChildren<MeshRenderer>().renderer.material.color = Color.red;
  214.         _elapsedTime += Time.deltaTime;
  215.         if (_elapsedTime >= 1.0f)
  216.         {
  217.             _elapsedTime = 0.0f;
  218.             int rnd = Random.Range(1, 6);
  219.             switch (rnd)
  220.             {
  221.                 case 1:
  222.                     npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.up * 150.0f);
  223.                     break;
  224.                 case 2:
  225.                     npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.right * 150.0f);
  226.                     break;
  227.                 case 3:
  228.                     npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.left * 150.0f);
  229.                     break;
  230.                 case 4:
  231.                     npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.forward * 150.0f);
  232.                     break;
  233.                 case 5:
  234.                     npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.back * 150.0f);
  235.                     break;
  236.                 case 6:
  237.                     npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.down * 150.0f);
  238.                     break;
  239.                 default:
  240.                     npc.GetComponentInChildren<Rigidbody>().rigidbody.AddForce(Vector3.zero * 150.0f);
  241.                     break;
  242.             }
  243.         }
  244.  
  245.     }
  246.  
  247. }
  248.  
  249. public class HatchingState : FSMState
  250. {
  251.     private float _elapsedTime;
  252.     private bool _alreadyChange;
  253.     public HatchingState()
  254.     {
  255.         stateID = StateID.Hatch;
  256.         _alreadyChange = false;
  257.         _elapsedTime = 0.0f;
  258.     }
  259.     public override void Reason(GameObject player, GameObject npc)
  260.     {
  261.         if (_alreadyChange)
  262.         {
  263.             {
  264.                 _elapsedTime = 0.0f;
  265.                 npc.GetComponent<DragonAI>().SetTransition(Transition.Growing1);
  266.             }
  267.         }
  268.     }
  269.     public override void Act(GameObject player, GameObject npc)
  270.     {
  271.         _elapsedTime += Time.deltaTime;
  272.         if (_elapsedTime >= 3.0f)
  273.         {
  274.             _elapsedTime = 0;
  275.             if (!_alreadyChange)
  276.             {
  277.                 npc.GetComponent<DragonAI>().ChangeModel(1);
  278.                 _alreadyChange = true;
  279.             }
  280.         }
  281.  
  282.     }
  283. }
  284.  
  285. public class WalkState : FSMState
  286. {
  287.  
  288.     private float _rotSpeed;
  289.     private float _runSpeed;
  290.     private GameObject[] _waypoints;
  291.     private GameObject _currentWP;
  292.     private GameObject _home;
  293.     private List<GameObject> _foodList;
  294.     private bool _foodAvailable;
  295.     private int _count;
  296.     public WalkState(GameObject[] wp, GameObject hm, float runSpeed, float rotSpeed)
  297.     {
  298.  
  299.         _foodList = new List<GameObject>();
  300.         stateID = StateID.Walk;
  301.         _waypoints = wp;
  302.         _count = 0;
  303.         _currentWP = _waypoints[_count];
  304.         _rotSpeed = rotSpeed;
  305.         _runSpeed = runSpeed;
  306.         _foodAvailable = false;
  307.         _home = hm;
  308.         _foodList = GameObject.Find("Food Holder").GetComponent<FoodHolder>()._foodList;
  309.     }
  310.     public override void Reason(GameObject player, GameObject npc)
  311.     {
  312.  
  313.         if (npc.GetComponent<DragonAI>().Energy <= 0 && (Vector3.Distance(npc.transform.position, _currentWP.transform.position) <= 5.0f && _currentWP == _home))
  314.         {
  315.             npc.GetComponent<DragonAI>().Text = "Going to Sleep";
  316.             npc.GetComponent<DragonAI>().SetTransition(Transition.Sleepy);
  317.  
  318.         }
  319.         if (npc.GetComponent<DragonAI>().Hunger >= 15.0f && _foodAvailable)
  320.         {
  321.             Debug.Log("Makanan tersedia");
  322.             GameObject temp = FindNearestFood(npc);
  323.             if (Vector3.Distance(npc.transform.position, temp.transform.position) <= 5.0f)
  324.             {
  325.                 npc.GetComponent<DragonAI>().Text = "Hmm Food.....";
  326.                 npc.GetComponent<DragonAI>().SetTransition(Transition.FoodClose);
  327.  
  328.             }
  329.  
  330.         }
  331.         if (npc.GetComponentInChildren<DragonAI>().Hunger >= 40.0f)
  332.         {
  333.             npc.GetComponent<DragonAI>().SetTransition(Transition.Hungry);
  334.         }
  335.     }
  336.     public override void Act(GameObject player, GameObject npc)
  337.     {
  338.      
  339.         npc.GetComponent<DragonAI>().Text = "Energy : " + npc.GetComponent<DragonAI>().Energy + "% Hunger : " + npc.GetComponent<DragonAI>().Hunger + "%";
  340.         npc.GetComponent<DragonAI>().Energy -= Time.deltaTime;
  341.         npc.GetComponent<DragonAI>().Hunger += Time.deltaTime;
  342.         if (npc.GetComponent<DragonAI>().Energy > 0)
  343.         {
  344.             npc.GetComponentInChildren<Animation>().animation.CrossFade("Run");
  345.             if (Vector3.Distance(npc.transform.position, _currentWP.transform.position) <= 5.0f)
  346.             {
  347.                 FindNextWP();
  348.             }
  349.  
  350.         }
  351.         else
  352.         {
  353.             npc.GetComponent<DragonAI>().Text = "Finding Home";
  354.             FindHome();
  355.         }
  356.         FindingFood();
  357.         Quaternion targetRotation = Quaternion.LookRotation(_currentWP.transform.position - npc.transform.position);
  358.         npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation, targetRotation, Time.deltaTime * _rotSpeed);
  359.         npc.transform.Translate(Vector3.forward * Time.deltaTime * _runSpeed);
  360.     }
  361.     private void FindNextWP()
  362.     {
  363.         _count++;
  364.         if (_count >= _waypoints.Length)
  365.         {
  366.             _count = 0;
  367.         }
  368.         _currentWP = _waypoints[_count];
  369.     }
  370.     private void FindHome()
  371.     {
  372.         _currentWP = _home;
  373.     }
  374.     private void FindingFood()
  375.     {
  376.         _foodAvailable = _foodList.Count != 0;
  377.     }
  378.     private GameObject FindNearestFood(GameObject npc)
  379.     {
  380.         float minDist = 1000000.0f;
  381.         GameObject tempGO = new GameObject();
  382.         foreach (GameObject i in _foodList)
  383.         {
  384.             float tempDist = Vector3.Distance(npc.transform.position, i.transform.position);
  385.             if (minDist > tempDist)
  386.             {
  387.                 minDist = tempDist;
  388.                 tempGO = i;
  389.             }
  390.         }
  391.         return tempGO;
  392.     }
  393. }
  394.  
  395. public class SleepState : FSMState
  396. {
  397.  
  398.     public SleepState()
  399.     {
  400.         stateID = StateID.Sleep;
  401.  
  402.     }
  403.     public override void Reason(GameObject player, GameObject npc)
  404.     {
  405.         npc.GetComponent<DragonAI>().Energy += Time.deltaTime;
  406.         if (npc.GetComponent<DragonAI>().Energy >= 100.0f)
  407.         {
  408.             npc.GetComponent<DragonAI>().SetTransition(Transition.Awake);
  409.         }
  410.     }
  411.     public override void Act(GameObject player, GameObject npc)
  412.     {
  413.         npc.GetComponentInChildren<Animation>().animation.CrossFade("Idle01");
  414.         npc.GetComponent<DragonAI>().Text = "Sleeping Zzzz Zzzz Zzzz, Restoring energy :" + npc.GetComponent<DragonAI>().Energy;
  415.  
  416.     }
  417. }
  418.  
  419.  
  420. public class EatState : FSMState
  421. {
  422.     private List<GameObject> _foodList;
  423.     private bool _foodAvailable;
  424.     private GameObject _chosenFood;
  425.     public EatState()
  426.     {
  427.         stateID = StateID.Eat;
  428.         _foodAvailable = false;
  429.         _foodList = GameObject.Find("Food Holder").GetComponent<FoodHolder>()._foodList;
  430.     }
  431.     public override void Reason(GameObject player, GameObject npc)
  432.     {
  433.         if (npc.GetComponent<DragonAI>().Hunger <= 0.0f)
  434.         {
  435.             npc.GetComponent<DragonAI>().SetTransition(Transition.FinishEat);
  436.         }
  437.         if (!_foodAvailable && npc.GetComponent<DragonAI>().Hunger > 40.0f)
  438.         {
  439.             npc.GetComponent<DragonAI>().SetTransition(Transition.Hungry);
  440.         }
  441.         else if (!_foodAvailable && npc.GetComponent<DragonAI>().Hunger < 15.0f)
  442.         {
  443.             npc.GetComponent<DragonAI>().SetTransition(Transition.FinishEat);
  444.         }
  445.     }
  446.     public override void Act(GameObject player, GameObject npc)
  447.     {
  448.         FindingFood();
  449.         if (_foodAvailable)
  450.         {
  451.  
  452.             npc.GetComponent<DragonAI>().Text = "Nom Nom Nom. Hunger : " + npc.GetComponent<DragonAI>().Hunger;
  453.             _chosenFood = FindNearestFood(npc);
  454.             _chosenFood.GetComponent<Food>().Eaten(5 * Time.deltaTime);
  455.             npc.GetComponent<DragonAI>().Hunger -= 5 * Time.deltaTime;
  456.             npc.GetComponentInChildren<Animation>().animation.CrossFade("Attack01");
  457.         }
  458.     }
  459.     private void FindingFood()
  460.     {
  461.         _foodAvailable = _foodList.Count != 0;
  462.  
  463.     }
  464.     private GameObject FindNearestFood(GameObject npc)
  465.     {
  466.         float minDist = 1000000.0f;
  467.         GameObject tempGO = new GameObject();
  468.         foreach (GameObject i in _foodList)
  469.         {
  470.             float tempDist = Vector3.Distance(npc.transform.position, i.transform.position);
  471.             if (minDist > tempDist)
  472.             {
  473.                 minDist = tempDist;
  474.                 tempGO = i;
  475.             }
  476.         }
  477.         return tempGO;
  478.     }
  479. }
  480.  
  481.  
  482. public class SearchFoodState : FSMState
  483. {
  484.     private GameObject[] _waypoints;
  485.     private GameObject _currentWP;
  486.     private float _rotSpeed;
  487.     private float _runSpeed;
  488.     private List<GameObject> _foodList;
  489.     private bool _foodAvailable;
  490.     private int _count;
  491.     public SearchFoodState(GameObject[] wp, float runSpeed, float rotSpeed)
  492.     {
  493.         stateID = StateID.SearchFood;
  494.         _waypoints = wp;
  495.         _count = 0;
  496.         _currentWP = _waypoints[_count];
  497.         _rotSpeed = rotSpeed;
  498.         _runSpeed = runSpeed;
  499.         _foodList = GameObject.Find("Food Holder").GetComponent<FoodHolder>()._foodList;
  500.     }
  501.     public override void Reason(GameObject player, GameObject npc)
  502.     {
  503.         if (_foodAvailable)
  504.         {
  505.             if (Vector3.Distance(npc.transform.position, FindNearestFood(npc).transform.position) <= 2.5f)
  506.             {
  507.                 npc.GetComponent<DragonAI>().Text = "Finally Food.....";
  508.                 npc.GetComponent<DragonAI>().SetTransition(Transition.FoodClose);
  509.             }
  510.         }
  511.     }
  512.     public override void Act(GameObject player, GameObject npc)
  513.     {
  514.         npc.GetComponent<DragonAI>().Text = "Finding Food...Hungry";
  515.         FindingFood();
  516.         if (_foodAvailable)
  517.         {
  518.             if (Vector3.Distance(npc.transform.position, FindNearestFood(npc).transform.position) <= 45.0f)
  519.             {
  520.                 npc.GetComponent<DragonAI>().Text = "I saw Food...Go for it";
  521.                 _currentWP = FindNearestFood(npc);
  522.             }
  523.             else if (Vector3.Distance(npc.transform.position, _currentWP.transform.position) <= 5.0f)
  524.             {
  525.                 FindNextWP();
  526.             }
  527.         }
  528.         else if (Vector3.Distance(npc.transform.position, _currentWP.transform.position) <= 5.0f)
  529.         {
  530.             FindNextWP();
  531.         }
  532.         Quaternion targetRotation = Quaternion.LookRotation(_currentWP.transform.position - npc.transform.position);
  533.         npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation, targetRotation, Time.deltaTime * _rotSpeed);
  534.         npc.transform.Translate(Vector3.forward * Time.deltaTime * _runSpeed);
  535.     }
  536.     private void FindNextWP()
  537.     {
  538.         _count++;
  539.         if (_count >= _waypoints.Length)
  540.         {
  541.             _count = 0;
  542.         }
  543.         _currentWP = _waypoints[_count];
  544.     }
  545.     private void FindingFood()
  546.     {
  547.         _foodAvailable = _foodList.Count != 0;
  548.  
  549.     }
  550.     private GameObject FindNearestFood(GameObject npc)
  551.     {
  552.         float minDist = 1000000.0f;
  553.         GameObject tempGO = new GameObject();
  554.         foreach (GameObject i in _foodList)
  555.         {
  556.             float tempDist = Vector3.Distance(npc.transform.position, i.transform.position);
  557.             if (minDist > tempDist)
  558.             {
  559.                 minDist = tempDist;
  560.                 tempGO = i;
  561.             }
  562.         }
  563.         return tempGO;
  564.     }
  565. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement