Advertisement
AnomalousUnderdog

Louie AIBehaviour

Nov 3rd, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AIBehaviour : MonoBehaviour
  5. {
  6.     public enum AIState
  7.     {
  8.         Going, // going to nearest vacant building
  9.         Waiting, // waiting for building to be vacant
  10.         Inside, // inside building
  11.         Exiting // finished occupying building and leaving
  12.     };
  13.  
  14.     public AIState state;
  15.  
  16.     Transform buildingToGoTo;
  17.  
  18.     const float WAIT_DISTANCE = 1.0f;
  19.     const float INSIDE_DISTANCE = 0.1f;
  20.  
  21.     const float WAIT_DURATION = 5;
  22.     const float INSIDE_DURATION = 10;
  23.  
  24.     void Start()
  25.     {
  26.         SetState(AIState.Going);
  27.     }
  28.    
  29.     void Update()
  30.     {
  31.         if (state == AIState.Going)
  32.         {
  33.             // go to building
  34.             transform.position = Vector3.MoveTowards(transform.position, buildingToGoTo.position, 0.05f);
  35.  
  36.             float distanceToBuilding = Vector3.Distance(transform.position, buildingToGoTo.position);
  37.  
  38.             if (buildingToGoTo.tag == "OccupiedBuilding" && distanceToBuilding <= WAIT_DISTANCE)
  39.             {
  40.                 // someone got there ahead of us
  41.                 SetState(AIState.Waiting);
  42.             }
  43.             // check if inside already
  44.             else if (buildingToGoTo.tag == "VacantBuilding" && distanceToBuilding <= INSIDE_DISTANCE)
  45.             {
  46.                 // mark it as occupied so others won't go here
  47.                 buildingToGoTo.tag = "OccupiedBuilding";
  48.  
  49.                 SetState(AIState.Inside);
  50.             }
  51.         }
  52.         else if (state == AIState.Exiting)
  53.         {
  54.             // add code to move to left side
  55.             Destroy(gameObject);
  56.         }
  57.     }
  58.  
  59.     void SetState(AIState s)
  60.     {
  61.         if (s == AIState.Going)
  62.         {
  63.             buildingToGoTo = FindClosestBuilding().transform;
  64.         }
  65.         else if (s == AIState.Inside)
  66.         {
  67.             // call FinishedInside() after 10 secs
  68.             Invoke("FinishedInside", INSIDE_DURATION);
  69.         }
  70.         else if (s == AIState.Waiting)
  71.         {
  72.             // call FinishedWaiting() after 5 secs
  73.             Invoke("FinishedWaiting", WAIT_DURATION);
  74.         }
  75.         state = s;
  76.     }
  77.  
  78.     void FinishedInside()
  79.     {
  80.         buildingToGoTo.tag = "VacantBuilding";
  81.         SetState(AIState.Exiting);
  82.     }
  83.  
  84.     void FinishedWaiting()
  85.     {
  86.         SetState(AIState.Going);
  87.     }
  88.  
  89.     GameObject FindClosestBuilding()
  90.     {
  91.         GameObject[] gos;
  92.         gos = GameObject.FindGameObjectsWithTag("VacantBuilding");
  93.         GameObject closest = null;
  94.         float distance = Mathf.Infinity;
  95.         Vector3 position = transform.position;
  96.         foreach (GameObject go in gos)
  97.         {
  98.             Vector3 diff = go.transform.position - position;
  99.             float curDistance = diff.sqrMagnitude;
  100.             if (curDistance < distance)
  101.             {
  102.                 closest = go;
  103.                 distance = curDistance;
  104.             }
  105.         }
  106.         return closest;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement