Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using BehaviorDesigner.Runtime;
- using Pathfinding;
- using Gameplay.Agents;
- using Gameplay.Agents.Controllers;
- namespace Gameplay.AI {
- public class NPCPath : MonoBehaviour {
- public BehaviorDesigner.Runtime.BehaviorTree BehaviorTree;
- public AIPath AIPath;
- public NPCController NPCController;
- AgentBase Agent { get { return NPCController.Agent; } }
- SharedBool _isRunning;
- bool IsRunning {
- get {
- if (_isRunning == null) { _isRunning = (SharedBool)BehaviorTree.GetVariable("IsRunning"); }
- return _isRunning.Value;
- }
- }
- Vector3 _desiredPosition;
- Quaternion _desiredRotation;
- void Update() {
- ResetAIPath();
- }
- void FixedUpdate() {
- ResetAIPath();
- if (GameplayManager.IsPaused) { return; }
- UpdateMovement();
- }
- void ResetAIPath() {
- AIPath.canMove = AIPath.updatePosition = AIPath.updateRotation = false;
- AIPath.rvoController = NPCController.AgentBehaviour.RVOController;
- }
- void UpdateMovement() {
- if (NPCController.State == NPCState.Dead) {
- Agent.MoveDelta(Vector2.zero);
- return;
- }
- AIPath.maxSpeed = Agent.GetMoveSpeed(Agent.Health, IsRunning);
- AIPath.MovementUpdate(Time.deltaTime, out _desiredPosition, out _desiredRotation);
- Agent.MoveDelta((Vector2)_desiredPosition - Agent.Position);
- AIPath.FinalizeMovement(Agent.Position, Quaternion.identity);
- Agent.Position = AIPath.position; // Update Agent position in case any external forces were applied during FinalizeMovement.
- }
- public void Stop() {
- AIPath.isStopped = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement