Advertisement
ensiferum888

CitizenExample

May 22nd, 2014
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1. public class CitizenAI : MonoBehaviour {   
  2.  
  3.     public Transform _myTransform;
  4.     public Transform workLocation;
  5.     public Transform target;   
  6.     public float idleTime;
  7.  
  8.     public enum State{
  9.         Hungry,
  10.         LookingForWork,
  11.         Working,
  12.         BringingRessourcesToWork,
  13.         Hunting,
  14.         Idle,
  15.         Waiting,
  16.         //Many more states but you get the idea
  17.     };
  18.  
  19.     private float speed;
  20.     private float idleTimer;
  21.  
  22.     void Update()
  23.         {          
  24.         //Run any movement code that hasn't ran this frame          
  25.                 MoveTorwards();
  26.        
  27.         switch (state){
  28.         //There's a case for every possible State, only listing relevant one here
  29.         //When entering a state, a villager should not have a target
  30.  
  31.         //Villager just picked up resources from a storage barn and is bringing them
  32.         //to his workplace.
  33.         case State.BringingRessourcesToWork:
  34.                         if(!target) //If there is no destination, acquire one.
  35.                         {
  36.                                 target = workLocation;
  37.                 //Make a call to StartPath only once
  38.                                 seeker.StartPath(_myTransform.position, target.position, OnPathComplete);
  39.                                 currentStateString = "Bringing needed ressources to work.";
  40.                         }//villager has a valid target, only check if within range
  41.                        
  42.             //Here I check to see if the unit is within range of his destination
  43.             //I could probably check if the waypoint is path.vectorPath[path.vectorPath.Length - 1]
  44.                         if(SquareDistance (_myTransform.position, target.position) < 4.5f)
  45.                         {
  46.                                 dropRessoucesAtWork(); //drop work related resources
  47.                                 target = null; //clear the destination reference
  48.                                 state = assesNeeds(); //get a new state
  49.                         }//end if distance is short enough
  50.                 break;
  51.  
  52.         //Here is another case where I perdiodically need to refresh the path.
  53.         case State.HuntingAnimal:
  54.                         if(!target){ //Same principle as above, acquire a target and call StartPath
  55.                                 target = workLocation.parent.GetComponent<HunterScript>().getNextTarget();
  56.                                 if(target){
  57.                                         seeker.StartPath (_myTransform.position, target.position, OnPathComplete);
  58.                                         idleTimer = 8.0f; //Here I set a timer at 8 seconds
  59.                                         currentStateString = "Hunting";
  60.                                 }
  61.                 else{
  62.                     state = assessNeeds();
  63.                     return;
  64.                 }
  65.                         }
  66.                        
  67.                         idleTimer -= Time.deltaTime; //Every frame deduct deltaTime from the timer
  68.                         if(idleTimer <= 0){//Once the timer reaches 0 recall StartPath (the deer might have moved)
  69.                                 seeker.StartPath (_myTransform.position, target.position, OnPathComplete);
  70.                                 idleTimer = 8.0f;
  71.                         }
  72.                        
  73.             //if we're within a certain distance of the deer, do stuff
  74.                         if(SquareDistance (_myTransform.position, target.position) < 500){
  75.                                 path = null;
  76.                                 target.GetComponent<DeerAI>().MarkForDeath();
  77.                 //The code below simply sets the AI not to do anything for 3 seconds and play an archery animation
  78.                                 idleTimer = 3.0f;
  79.                                 state = State.Waiting;
  80.                                 playArcheryAnim();
  81.                                 lastState = State.GoingForKill;
  82.                                 idleTimer = idleTime;
  83.                         }
  84.                 break;
  85.  
  86.         }//end switch (state)
  87.     }//end update
  88.  
  89.     private void MoveTorwards(){
  90.         if (path == null) {
  91.                     return;
  92.             }
  93.        
  94.             if (currentWaypoint >= path.vectorPath.Count) {
  95.                     //End of path reached
  96.                         path = null;
  97.                     return;
  98.             }
  99.                 if(!bentDown){
  100.                     playWalkCycle();
  101.                 }
  102.  
  103.                 _myTransform.position = Vector3.Slerp (_myTransform.position, path.vectorPath[currentWaypoint],
  104.                         speed * Time.deltaTime);
  105.        
  106.             //Check if we are close enough to the next waypoint
  107.             //If we are, proceed to follow the next waypoint
  108.             if (SquareDistance(_myTransform.position,path.vectorPath[currentWaypoint]) < 4) {
  109.                     currentWaypoint++;
  110.                         if (currentWaypoint < path.vectorPath.Count) {
  111.                                 Vector3 lookDirection = path.vectorPath[currentWaypoint];
  112.                                 lookDirection.y = this._myTransform.position.y;
  113.                                 transform.LookAt(lookDirection);
  114.                         }
  115.                     return;
  116.             }
  117.     }
  118.  
  119.     private float SquareDistance(Vector3 a, Vector3 b){
  120.                 Vector3 diff = b - a;
  121.                 return diff.sqrMagnitude;
  122.         }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement