USER
public class AgentTest : MonoBehaviour
{
private void Start()
{
this.agent = base.GetComponent
private void Update()
{
if (this.coolDown > 0f)
{
this.coolDown -= 1f * Time.deltaTime;
}
}
private void FixedUpdate()
{
Vector3 direction = this.player.position - base.transform.position;
RaycastHit raycastHit;
if (Physics.Raycast(base.transform.position, direction, out raycastHit, float.PositiveInfinity, 3, QueryTriggerInteraction.Ignore) & raycastHit.transform.tag == "Player") //Check if its the player
{
this.db = false;
}
else
{
this.db = false;
if (this.agent.velocity.magnitude <= 1f & this.coolDown <= 0f)
{
this.Wander();
}
}
}
private void Wander()
{
this.wanderer.GetNewTarget();
this.agent.SetDestination(this.wanderTarget.position);
this.coolDown = 1f;
}
public bool db;
public Transform player;
public Transform wanderTarget;
public AILocationSelectorScript wanderer;
public float coolDown;
private NavMeshAgent agent;
}
public class BaldiScript : MonoBehaviour
{
private void Start()
{
this.baldiAudio = base.GetComponent
public class BullyScript : MonoBehaviour
{
private void Start()
{
this.audioDevice = base.GetComponent
private void Update()
{
if (this.waitTime > 0f)
{
this.waitTime -= Time.deltaTime;
}
else if (!this.active)
{
this.Activate();
}
if (this.active)
{
this.activeTime += Time.deltaTime;
if (this.activeTime >= 180f & (base.transform.position - this.player.position).magnitude >= 120f)
{
this.Reset();
}
}
if (this.guilt > 0f)
{
this.guilt -= Time.deltaTime;
}
}
private void FixedUpdate()
{
Vector3 direction = this.player.position - base.transform.position;
RaycastHit raycastHit;
if (Physics.Raycast(base.transform.position + new Vector3(0f, 4f, 0f), direction, out raycastHit, float.PositiveInfinity, 769, QueryTriggerInteraction.Ignore) & raycastHit.transform.tag == "Player" & (base.transform.position - this.player.position).magnitude <= 30f & this.active)
{
if (!this.spoken)
{
int num = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 1f));
this.audioDevice.PlayOneShot(this.aud_Taunts[num]);
this.spoken = true;
}
this.guilt = 10f;
}
}
private void Activate()
{
this.wanderer.GetNewTargetHallway();
base.transform.position = this.wanderTarget.position + new Vector3(0f, 5f, 0f);
while ((base.transform.position - this.player.position).magnitude < 20f)
{
this.wanderer.GetNewTargetHallway();
base.transform.position = this.wanderTarget.position + new Vector3(0f, 5f, 0f);
}
this.active = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Player")
{
if (this.gc.item[0] == 0 & this.gc.item[1] == 0 & this.gc.item[2] == 0)
{
this.audioDevice.PlayOneShot(this.aud_Denied);
}
else
{
int num = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 2f));
while (this.gc.item[num] == 0)
{
num = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 2f));
}
this.gc.LoseItem(num);
int num2 = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 1f));
this.audioDevice.PlayOneShot(this.aud_Thanks[num2]);
this.Reset();
}
}
}
private void OnTriggerStay(Collider other)
{
if (other.transform.name == "Principal of the Thing" & this.guilt > 0f)
{
this.Reset();
}
}
private void Reset()
{
base.transform.position = base.transform.position - new Vector3(0f, 20f, 0f);
this.waitTime = UnityEngine.Random.Range(60f, 120f);
this.active = false;
this.activeTime = 0f;
this.spoken = false;
}
public Transform player;
public GameControllerScript gc;
public Renderer bullyRenderer;
public Transform wanderTarget;
public AILocationSelectorScript wanderer;
public float waitTime;
public float activeTime;
public float guilt;
public bool active;
public bool spoken;
private AudioSource audioDevice;
public AudioClip[] aud_Taunts = new AudioClip[2];
public AudioClip[] aud_Thanks = new AudioClip[2];
public AudioClip aud_Denied;
}
public class CraftersScript : MonoBehaviour
{
private void Start()
{
this.agent = base.GetComponent
public class FirstPrizeScript : MonoBehaviour
{
private void Start()
{
this.agent = base.GetComponent
private void Update()
{
if (this.coolDown > 0f)
{
this.coolDown -= 1f * Time.deltaTime;
}
if (this.autoBrakeCool > 0f)
{
this.autoBrakeCool -= 1f * Time.deltaTime;
}
else
{
this.agent.autoBraking = true;
}
this.angleDiff = Mathf.DeltaAngle(base.transform.eulerAngles.y, Mathf.Atan2(this.agent.steeringTarget.x - base.transform.position.x, this.agent.steeringTarget.z - base.transform.position.z) * 57.29578f);
if (this.crazyTime <= 0f)
{
if (Mathf.Abs(this.angleDiff) < 5f)
{
base.transform.LookAt(new Vector3(this.agent.steeringTarget.x, base.transform.position.y, this.agent.steeringTarget.z));
this.agent.speed = this.currentSpeed;
}
else
{
base.transform.Rotate(new Vector3(0f, this.turnSpeed * Mathf.Sign(this.angleDiff) * Time.deltaTime, 0f));
this.agent.speed = 0f;
}
}
else
{
this.agent.speed = 0f;
base.transform.Rotate(new Vector3(0f, 180f * Time.deltaTime, 0f));
this.crazyTime -= Time.deltaTime;
}
this.motorAudio.pitch = (this.agent.velocity.magnitude + 1f) * Time.timeScale;
}
private void FixedUpdate()
{
Vector3 direction = this.player.position - base.transform.position;
RaycastHit raycastHit;
if (Physics.Raycast(base.transform.position, direction, out raycastHit, float.PositiveInfinity, 769, QueryTriggerInteraction.Ignore) & raycastHit.transform.tag == "Player")
{
if (!this.playerSeen && !this.audioDevice.isPlaying)
{
int num = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 1f));
this.audioDevice.PlayOneShot(this.aud_Found[num]);
}
this.playerSeen = true;
this.TargetPlayer();
this.currentSpeed = this.runSpeed;
}
else
{
this.currentSpeed = this.normSpeed;
if (this.playerSeen & this.coolDown <= 0f)
{
if (!this.audioDevice.isPlaying)
{
int num2 = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 1f));
this.audioDevice.PlayOneShot(this.aud_Lost[num2]);
}
this.playerSeen = false;
this.Wander();
}
else if (this.agent.velocity.magnitude <= 1f & this.coolDown <= 0f & (base.transform.position - this.agent.destination).magnitude < 5f)
{
this.Wander();
}
}
}
private void Wander()
{
this.wanderer.GetNewTargetHallway();
this.agent.SetDestination(this.wanderTarget.position);
this.hugAnnounced = false;
int num = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 9f));
if (!this.audioDevice.isPlaying & num == 0 & this.coolDown <= 0f)
{
int num2 = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 1f));
this.audioDevice.PlayOneShot(this.aud_Random[num2]);
}
this.coolDown = 1f;
}
private void TargetPlayer()
{
this.agent.SetDestination(this.player.position);
this.coolDown = 0.5f;
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
if (!this.audioDevice.isPlaying & !this.hugAnnounced)
{
int num = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 1f));
this.audioDevice.PlayOneShot(this.aud_Hug[num]);
this.hugAnnounced = true;
}
this.agent.autoBraking = false;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
this.autoBrakeCool = 1f;
}
}
public void GoCrazy()
{
this.crazyTime = 15f;
}
public float debug;
public float turnSpeed;
public float str;
public float angleDiff;
public float normSpeed;
public float runSpeed;
public float currentSpeed;
public float acceleration;
public float speed;
public float autoBrakeCool;
public float crazyTime;
public Quaternion targetRotation;
public float coolDown;
private float prevSpeed;
public bool playerSeen;
public bool hugAnnounced;
public AILocationSelectorScript wanderer;
public Transform player;
public Transform wanderTarget;
public AudioClip[] aud_Found = new AudioClip[2];
public AudioClip[] aud_Lost = new AudioClip[2];
public AudioClip[] aud_Hug = new AudioClip[2];
public AudioClip[] aud_Random = new AudioClip[2];
public AudioClip audBang;
public AudioSource audioDevice;
public AudioSource motorAudio;
private NavMeshAgent agent;
}
public class PlaytimeScript : MonoBehaviour
{
private void Start()
{
this.agent = base.GetComponent
private void Update()
{
if (this.coolDown > 0f)
{
this.coolDown -= 1f * Time.deltaTime;
}
if (this.playCool >= 0f)
{
this.playCool -= Time.deltaTime;
}
else if (this.animator.GetBool("disappointed"))
{
this.playCool = 0f;
this.animator.SetBool("disappointed", false);
}
}
private void FixedUpdate()
{
if (!this.ps.jumpRope)
{
Vector3 direction = this.player.position - base.transform.position;
RaycastHit raycastHit;
if (Physics.Raycast(base.transform.position, direction, out raycastHit, float.PositiveInfinity, 769, QueryTriggerInteraction.Ignore) & raycastHit.transform.tag == "Player" & (base.transform.position - this.player.position).magnitude <= 80f & this.playCool <= 0f)
{
this.playerSeen = true;
this.TargetPlayer();
}
else if (this.playerSeen & this.coolDown <= 0f)
{
this.playerSeen = false;
this.Wander();
}
else if (this.agent.velocity.magnitude <= 1f & this.coolDown <= 0f)
{
this.Wander();
}
this.jumpRopeStarted = false;
}
else
{
if (!this.jumpRopeStarted)
{
this.agent.Warp(base.transform.position - base.transform.forward * 10f);
}
this.jumpRopeStarted = true;
this.agent.speed = 0f;
this.playCool = 15f;
}
}
private void Wander()
{
this.wanderer.GetNewTargetHallway();
this.agent.SetDestination(this.wanderTarget.position);
this.agent.speed = 15f;
this.playerSpotted = false;
this.audVal = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 1f));
if (!this.audioDevice.isPlaying)
{
this.audioDevice.PlayOneShot(this.aud_Random[this.audVal]);
}
this.coolDown = 1f;
}
private void TargetPlayer()
{
this.animator.SetBool("disappointed", false);
this.agent.SetDestination(this.player.position);
this.agent.speed = 20f;
this.coolDown = 0.2f;
if (!this.playerSpotted)
{
this.playerSpotted = true;
this.audioDevice.PlayOneShot(this.aud_LetsPlay);
}
}
public void Disappoint()
{
this.animator.SetBool("disappointed", true);
this.audioDevice.Stop();
this.audioDevice.PlayOneShot(this.aud_Sad);
}
public bool db;
public bool playerSeen;
public bool disappointed;
public int audVal;
public Animator animator;
public Transform player;
public PlayerScript ps;
public Transform wanderTarget;
public AILocationSelectorScript wanderer;
public float coolDown;
public float playCool;
public bool playerSpotted;
public bool jumpRopeStarted;
private NavMeshAgent agent;
public AudioClip[] aud_Numbers = new AudioClip[10];
public AudioClip[] aud_Random = new AudioClip[2];
public AudioClip aud_Instrcutions;
public AudioClip aud_Oops;
public AudioClip aud_LetsPlay;
public AudioClip aud_Congrats;
public AudioClip aud_ReadyGo;
public AudioClip aud_Sad;
public AudioSource audioDevice;
}
public class PrincipalScript : MonoBehaviour
{
private void Start()
{
this.agent = base.GetComponent
private void Update()
{
if (this.seesRuleBreak)
{
this.timeSeenRuleBreak += 1f * Time.deltaTime;
if ((double)this.timeSeenRuleBreak >= 0.5 & !this.angry)
{
this.angry = true;
this.seesRuleBreak = false;
this.timeSeenRuleBreak = 0f;
this.TargetPlayer();
this.CorrectPlayer();
}
}
else
{
this.timeSeenRuleBreak = 0f;
}
if (this.coolDown > 0f)
{
this.coolDown -= 1f * Time.deltaTime;
}
}
private void FixedUpdate()
{
if (!this.angry)
{
this.aim = this.player.position - base.transform.position;
if (Physics.Raycast(base.transform.position, this.aim, out this.hit, float.PositiveInfinity, 769, QueryTriggerInteraction.Ignore) & this.hit.transform.tag == "Player" & this.playerScript.guilt > 0f & !this.inOffice & !this.angry)
{
this.seesRuleBreak = true;
}
else
{
this.seesRuleBreak = false;
if (this.agent.velocity.magnitude <= 1f & this.coolDown <= 0f)
{
this.Wander();
}
}
this.aim = this.bully.position - base.transform.position;
if (Physics.Raycast(base.transform.position, this.aim, out this.hit, float.PositiveInfinity, 769) & this.hit.transform.name == "Its a Bully" & this.bullyScript.guilt > 0f & !this.inOffice & !this.angry)
{
this.TargetBully();
}
}
else
{
this.TargetPlayer();
}
}
private void Wander()
{
this.playerScript.principalBugFixer = 1;
this.wanderer.GetNewTarget();
this.agent.SetDestination(this.wanderTarget.position);
if (this.agent.isStopped)
{
this.agent.isStopped = false;
}
this.coolDown = 1f;
if (UnityEngine.Random.Range(0f, 10f) <= 1f)
{
this.quietAudioDevice.PlayOneShot(this.aud_Whistle);
}
}
private void TargetPlayer()
{
this.agent.SetDestination(this.player.position);
this.coolDown = 1f;
}
private void TargetBully()
{
if (!this.bullySeen)
{
this.agent.SetDestination(this.bully.position);
this.audioQueue.QueueAudio(this.audNoBullying);
this.bullySeen = true;
}
}
private void CorrectPlayer()
{
this.audioQueue.ClearAudioQueue();
if (this.playerScript.guiltType == "faculty")
{
this.audioQueue.QueueAudio(this.audNoFaculty);
}
else if (this.playerScript.guiltType == "running")
{
this.audioQueue.QueueAudio(this.audNoRunning);
}
else if (this.playerScript.guiltType == "drink")
{
this.audioQueue.QueueAudio(this.audNoDrinking);
}
else if (this.playerScript.guiltType == "escape")
{
this.audioQueue.QueueAudio(this.audNoEscaping);
}
}
private void OnTriggerStay(Collider other)
{
if (other.name == "Office Trigger")
{
this.inOffice = true;
}
if (other.tag == "Player" & this.angry & !this.inOffice)
{
this.inOffice = true;
this.playerScript.principalBugFixer = 0;
this.agent.Warp(new Vector3(10f, 0f, 170f));
this.agent.isStopped = true;
this.cc.enabled = false;
other.transform.position = new Vector3(10f, 4f, 160f);
other.transform.LookAt(new Vector3(base.transform.position.x, other.transform.position.y, base.transform.position.z));
this.cc.enabled = true;
this.audioQueue.QueueAudio(this.aud_Delay);
this.audioQueue.QueueAudio(this.audTimes[this.detentions]);
this.audioQueue.QueueAudio(this.audDetention);
int num = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 2f));
this.audioQueue.QueueAudio(this.audScolds[num]);
this.officeDoor.LockDoor((float)this.lockTime[this.detentions]);
if (this.baldiScript.isActiveAndEnabled) this.baldiScript.Hear(base.transform.position, 8f);
this.coolDown = 5f;
this.angry = false;
this.detentions++;
if (this.detentions > 4)
{
this.detentions = 4;
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.name == "Office Trigger")
{
this.inOffice = false;
}
if (other.name == "Its a Bully")
{
this.bullySeen = false;
}
}
public bool seesRuleBreak;
public Transform player;
public Transform bully;
public bool bullySeen;
public PlayerScript playerScript;
public BullyScript bullyScript;
public BaldiScript baldiScript;
public Transform wanderTarget;
public AILocationSelectorScript wanderer;
public DoorScript officeDoor;
public float coolDown;
public float timeSeenRuleBreak;
public bool angry;
public bool inOffice;
private int detentions;
private int[] lockTime = new int[]
{
25,
40,
65,
90,
130
};
public AudioClip[] audTimes = new AudioClip[5];
public AudioClip[] audScolds = new AudioClip[3];
public AudioClip audDetention;
public AudioClip audNoDrinking;
public AudioClip audNoBullying;
public AudioClip audNoFaculty;
public AudioClip audNoLockers;
public AudioClip audNoRunning;
public AudioClip audNoStabbing;
public AudioClip audNoEscaping;
public AudioClip aud_Whistle;
public AudioClip aud_Delay;
private NavMeshAgent agent;
private AudioQueueScript audioQueue;
private AudioSource audioDevice;
public AudioSource quietAudioDevice;
private RaycastHit hit;
private Vector3 aim;
public CharacterController cc;
}
public class SweepScript : MonoBehaviour
{
private void Start()
{
this.agent = base.GetComponent
private void Update()
{
if (this.coolDown > 0f)
{
this.coolDown -= 1f * Time.deltaTime;
}
if (this.waitTime > 0f)
{
this.waitTime -= Time.deltaTime;
}
else if (!this.active)
{
this.active = true;
this.wanders = 0;
this.Wander();
this.audioDevice.PlayOneShot(this.aud_Intro);
}
}
private void FixedUpdate()
{
if ((double)this.agent.velocity.magnitude <= 0.1 & this.coolDown <= 0f & this.wanders < 5 & this.active)
{
this.Wander();
}
else if (this.wanders >= 5)
{
this.GoHome();
}
}
private void Wander()
{
this.wanderer.GetNewTargetHallway();
this.agent.SetDestination(this.wanderTarget.position);
this.coolDown = 1f;
this.wanders++;
}
private void GoHome()
{
this.agent.SetDestination(this.origin);
this.waitTime = UnityEngine.Random.Range(120f, 180f);
this.wanders = 0;
this.active = false;
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "NPC" || other.tag == "Player")
{
this.audioDevice.PlayOneShot(this.aud_Sweep);
}
}
public Transform wanderTarget;
public AILocationSelectorScript wanderer;
public float coolDown;
public float waitTime;
public int wanders;
public bool active;
private Vector3 origin;
public AudioClip aud_Sweep;
public AudioClip aud_Intro;
private NavMeshAgent agent;
private AudioSource audioDevice;
}
convert to multi task npc template class with state machinery for all npc to derive fromASSISTANT
To convert the various NPC scripts into a multi-task NPC template class with a state machine, we need to identify common functionality and state-driven behavior across these NPCs. Each NPC typically has states such as Idle, Walking, Interacting, etc. We will design a state machine pattern that can be extended by the derived NPC classes, encapsulating common behaviors and states.
Below is a basic implementation outline for a base NPC class with a state machine, and an example of how to extend it for a specific NPC type:
```csharp
using UnityEngine;
using UnityEngine.AI;
using System.Collections.Generic;
public abstract class BaseNPC : MonoBehaviour
{
protected NavMeshAgent agent;
protected State currentState;
protected virtual void Start()
{
this.agent = GetComponent<NavMeshAgent>();
TransitionToState(GetInitialState());
}
protected virtual void Update()
{
currentState?.UpdateState();
}
protected virtual void FixedUpdate()
{
currentState?.FixedUpdateState();
}
protected abstract State GetInitialState();
public void TransitionToState(State newState)
{
currentState?.OnStateExit();
currentState = newState;
currentState?.OnStateEnter();
}
public abstract class State
{
protected BaseNPC npc;
public State(BaseNPC npc)
{
this.npc = npc;
}
public virtual void OnStateEnter() { }
public virtual void OnStateExit() { }
public virtual void UpdateState() { }
public virtual void FixedUpdateState() { }
}
}
public class AgentTest : BaseNPC
{
public Transform player;
public Transform wanderTarget;
public AILocationSelectorScript wanderer;
public float coolDown;
protected override State GetInitialState() => new IdleState(this);
private class IdleState : State
{
public IdleState(BaseNPC npc) : base(npc) { }
public override void UpdateState()
{
// Implement Idle behavior, transition logic
if (npc.coolDown <= 0f) npc.TransitionToState(new WanderState(npc as AgentTest));
}
}
private class WanderState : State
{
private float stateCoolDown;
public WanderState(AgentTest npc) : base(npc)
{
stateCoolDown = 1f;
}
public override void OnStateEnter()
{
(npc as AgentTest).wanderer.GetNewTarget();
npc.agent.SetDestination((npc as AgentTest).wanderTarget.position);
}
public override void UpdateState()
{
stateCoolDown -= Time.deltaTime;
if (npc.agent.remainingDistance <= npc.agent.stoppingDistance && stateCoolDown <= 0f)
{
npc.TransitionToState(new IdleState(npc));
}
}
}
// Add more states and behavior as needed
}
// Other NPC classes can inherit from BaseNPC and define their own state transitions
public class OtherNPC : BaseNPC
{
protected override State GetInitialState() => new SomeState(this);
private class SomeState : State
{
public SomeState(BaseNPC npc) : base(npc) { }
public override void OnStateEnter()
{
// Specific entry behavior for this state
}
public override void UpdateState()
{
// Logic for this specific state
}
}
}
```
### Explanation:
1. **BaseNPC Class**: Provides a template for all NPCs with a `NavMeshAgent` and a `currentState`. It requires derived classes to specify an initial state via `GetInitialState`.
2. **State Class**: Represents an abstract class that all specific states will derive from. It holds common functionality for states such as `OnStateEnter`, `OnStateExit`, `UpdateState`, and `FixedUpdateState`.
3. **Derived NPC Classes**: Extend `BaseNPC` and implement specific states. Each state defines behavior for what the NPC should do during that state, and provides logic to transition to other states when conditions are met.
This approach allows for further flexibility and scalability when creating more complex NPC behavior while maintaining clarity and organization in NPC behavior coding. Each NPC can have its own unique states and transitions while reusing the base framework for common navigation and event handling.