nPC decision

This commit is contained in:
Pedro Gomes 2024-06-16 11:05:37 +01:00
parent 2c099c15fe
commit 90d52d491e
8 changed files with 121 additions and 9 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 56939ec6b1c7ba4418f60df6bf032cd8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class NPCDecision : ScriptableObject
{
protected Coroutine executeRoutine;
public abstract IEnumerator ExecuteCoroutine(NPCBrain npc);
public void Execute(NPCBrain npc)
{
executeRoutine = npc.StartCoroutine(ExecuteCoroutine(npc));
}
public abstract void Interrupt(NPCBrain npc);
public bool IsRunning() => executeRoutine != null;
}

View File

@ -0,0 +1,69 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Events;
[CreateAssetMenu(fileName = "PatrolDecision", menuName = "RiftMayhem/AI/Decisions/Patrol")]
public class NPCPatrolDecision : NPCDecision
{
[Header("Settings:")]
[SerializeField] private float distanceToChangePatrolDestination;
[SerializeField] private float patrolAgentSpeed;
NavMeshAgent agent;
Vector3 patrolDestination = new Vector3();
public UnityEvent OnPatrolReachedDestination = new UnityEvent();
public override IEnumerator ExecuteCoroutine(NPCBrain npc)
{
SetupAgent(npc);
StartPatrollingToNewPosition();
yield return new WaitUntil(() => IsPathComplete());
OnPatrolReachedDestination.Invoke();
}
public override void Interrupt(NPCBrain npc)
{
if (executeRoutine != null)
npc.StopCoroutine(executeRoutine);
}
//Helpers
protected void StartPatrollingToNewPosition()
{
agent.speed = patrolAgentSpeed;
patrolDestination.x = Random.Range(-5, 5);
patrolDestination.y = 0f;
patrolDestination.z = Random.Range(-5, 5);
agent.SetDestination(agent.transform.position + patrolDestination);
agent.isStopped = false;
}
protected bool IsPathComplete()
{
if (Vector3.Distance(agent.destination, agent.transform.position) <= agent.stoppingDistance)
{
if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)
{
return true;
}
}
return false;
}
protected void SetupAgent(NPCBrain npc)
{
agent = npc.Agent;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cc8abedb55d773e46aab9bba344516df
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,10 +1,25 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NPCBrain : MonoBehaviour
{
[Header("Decision Profile")]
[SerializeField] private NPCDecisionProfile decisionProfile;
NavMeshAgent agent;
public NavMeshAgent Agent => agent;
private void Start()
{
StartCoroutine(Decide());
}
IEnumerator Decide()
{
yield return null;
}
}

View File

@ -1,8 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class NPCDecision : ScriptableObject
{
public abstract void Execute(NPCController npc);
}

View File

@ -94,7 +94,7 @@ Material:
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _Rotation: 11.08514
- _Rotation: 12.507187
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1