Initial Commit
This commit is contained in:
commit
ee5c2f922d
2255 changed files with 547750 additions and 0 deletions
8
Assets/Scripts/AI/AttackPlayerGoapAction.cs
Normal file
8
Assets/Scripts/AI/AttackPlayerGoapAction.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
using Unity.Entities;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct AttackPlayerGoapAction : IComponentData
|
||||
{
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/AttackPlayerGoapAction.cs.meta
Normal file
3
Assets/Scripts/AI/AttackPlayerGoapAction.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 90a049afea3f43b2b5df5e855985a475
|
||||
timeCreated: 1532860293
|
||||
105
Assets/Scripts/AI/AttackPlayerGoapSystem.cs
Normal file
105
Assets/Scripts/AI/AttackPlayerGoapSystem.cs
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
using AI.FSM;
|
||||
using DefaultNamespace;
|
||||
using System;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
using Zenject;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
[UpdateAfter(typeof(PerformActionStateSystem)), UpdateBefore(typeof(IdleSystem)), UpdateInGroup(typeof(PresentationSystemGroup))]
|
||||
public class AttackPlayerGoapSystem : GoapActionInjectableSystem<AttackPlayerGoapAction>
|
||||
{
|
||||
[Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public ParticleSystem StabBloodParticles;
|
||||
public SoundLibrary StabSounds;
|
||||
}
|
||||
|
||||
[Inject] private readonly Settings settings;
|
||||
[Inject] private readonly SoundManager soundManager;
|
||||
|
||||
protected override void OnProcess(
|
||||
ref AttackPlayerGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
GoapAction goapAction,
|
||||
GoapActionActor actor,
|
||||
ref GoapActiveAction active)
|
||||
{
|
||||
if (active.InRange)
|
||||
{
|
||||
var animationEvent = GetComponentDataFromEntity<ActorAnimationEventData>();
|
||||
var animationGetter = GetComponentDataFromEntity<ActorAnimationData>();
|
||||
var actorGetter = GetComponentDataFromEntity<ActorData>();
|
||||
var meleeGetter = GetComponentDataFromEntity<ActorMeleeData>();
|
||||
|
||||
var actorEntity = actor.Actor;
|
||||
var animation = animationGetter[actorEntity];
|
||||
|
||||
var targetEntity = goapAction.Target;
|
||||
var animEvent = animationEvent[actorEntity];
|
||||
var prefab = EntityManager.GetComponentObject<EnemyPrefabComponent>(actorEntity);
|
||||
var rigidBody = EntityManager.GetComponentObject<Rigidbody2D>(actorEntity);
|
||||
animation.Triggers |= AnimationTriggerType.Attack;
|
||||
if (animEvent.Attacked)
|
||||
{
|
||||
var assetLoadingOperation = prefab.Prefab.OperationHandle.Convert<EnemyPrefab>();
|
||||
if (assetLoadingOperation.IsDone)
|
||||
{
|
||||
if (actorGetter.Exists(targetEntity) && EntityManager.HasComponent<Transform>(targetEntity))
|
||||
{
|
||||
var actorData = actorGetter[targetEntity];
|
||||
var transform = EntityManager.GetComponentObject<Transform>(targetEntity);
|
||||
var hit = Physics2D.Linecast(rigidBody.position, transform.position, LayerMask.GetMask("Player"));
|
||||
if (hit && hit.distance <= assetLoadingOperation.Result.AttackRange)
|
||||
{
|
||||
actorData.Health -= assetLoadingOperation.Result.Damage;
|
||||
EntityManager.SetComponentData(targetEntity, actorData);
|
||||
soundManager.PlayClip(PostUpdateCommands, settings.StabSounds, hit.point);
|
||||
settings.StabBloodParticles.Emit(new ParticleSystem.EmitParams { position = hit.point }, 1);
|
||||
}
|
||||
|
||||
active.Done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
active.Fail = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
animationGetter[actorEntity] = animation;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnInitialize(ref AttackPlayerGoapAction action, ref GoapSharedAction goapSharedAction)
|
||||
{
|
||||
goapSharedAction.Cost = 10;
|
||||
goapSharedAction.Effects.Add(((int)GoapKeys.Attacks, true));
|
||||
goapSharedAction.RequiresInRange = true;
|
||||
}
|
||||
|
||||
protected override bool OnValidate(
|
||||
ref AttackPlayerGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
ref GoapAction goapAction,
|
||||
GoapActionActor actor)
|
||||
{
|
||||
if (!EntityManager.HasComponents<ActorAnimationEventData, Rigidbody2D, EnemyPrefabComponent>(actor.Actor))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var playerEntity = GetSingletonEntity<PlayerData>();
|
||||
var playerRigidbody = EntityManager.GetComponentObject<Rigidbody2D>(playerEntity);
|
||||
|
||||
var actorRigidbody = EntityManager.GetComponentObject<Rigidbody2D>(actor.Actor);
|
||||
var d = Vector2.Distance(actorRigidbody.position, playerRigidbody.position);
|
||||
goapSharedAction.Cost = d;
|
||||
goapAction.Target = playerEntity;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/AttackPlayerGoapSystem.cs.meta
Normal file
3
Assets/Scripts/AI/AttackPlayerGoapSystem.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dea9267da89148aba9fabbb278c4521f
|
||||
timeCreated: 1532860315
|
||||
88
Assets/Scripts/AI/AttackTargetMeleeGoapAction.cs
Normal file
88
Assets/Scripts/AI/AttackTargetMeleeGoapAction.cs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
using DefaultNamespace;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct AttackTargetMeleeGoapAction : IComponentData
|
||||
{
|
||||
}
|
||||
|
||||
public class AttackPlayerMeleeGoapActionSystem : GoapActionSystem<AttackTargetMeleeGoapAction>
|
||||
{
|
||||
protected override void OnInitialize(ref AttackTargetMeleeGoapAction action, ref GoapSharedAction goapSharedAction)
|
||||
{
|
||||
goapSharedAction.Cost = 10;
|
||||
goapSharedAction.Preconditions.Add((GoapKeys.HasTarget, true));
|
||||
goapSharedAction.Effects.Add((GoapKeys.Attacks, true));
|
||||
}
|
||||
|
||||
protected override void OnProcess(
|
||||
ref AttackTargetMeleeGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
GoapAction goapAction,
|
||||
GoapActionActor actor,
|
||||
ref GoapActiveAction active)
|
||||
{
|
||||
var actorEntity = actor.Actor;
|
||||
var animation = EntityManager.GetComponentData<ActorAnimationData>(actorEntity);
|
||||
var melee = EntityManager.GetComponentData<ActorMeleeData>(actorEntity);
|
||||
var meleeShared = EntityManager.GetSharedComponentData<ActorMeleeSharedData>(actor.Actor);
|
||||
var target = EntityManager.GetComponentData<ActorTargetData>(actorEntity);
|
||||
|
||||
if (!EntityManager.Exists(target.Target))
|
||||
{
|
||||
active.MarkFailed();
|
||||
return;
|
||||
}
|
||||
|
||||
if (melee.MeleeTimer <= 0)
|
||||
{
|
||||
animation.Triggers |= AnimationTriggerType.Melee;
|
||||
melee.MeleeTimer += meleeShared.Cooldown;
|
||||
EntityManager.SetComponentData(actorEntity, melee);
|
||||
EntityManager.SetComponentData(actorEntity, animation);
|
||||
}
|
||||
else
|
||||
{
|
||||
active.MarkFailed();
|
||||
}
|
||||
|
||||
active.MarkDone();
|
||||
}
|
||||
|
||||
protected override bool OnValidate(
|
||||
ref AttackTargetMeleeGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
ref GoapAction goapAction,
|
||||
GoapActionActor actor)
|
||||
{
|
||||
if (!EntityManager.HasComponents<ActorMeleeData, ActorMeleeSharedData, Rigidbody2D, ActorAnimationData, ActorTargetData>(actor.Actor))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var melee = EntityManager.GetComponentData<ActorMeleeData>(actor.Actor);
|
||||
|
||||
if (melee.MeleeTimer > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var meleeShared = EntityManager.GetSharedComponentData<ActorMeleeSharedData>(actor.Actor);
|
||||
var actorRigidBody = EntityManager.GetComponentObject<Rigidbody2D>(actor.Actor);
|
||||
var target = EntityManager.GetComponentData<ActorTargetData>(actor.Actor);
|
||||
|
||||
if (EntityManager.TryGetComponentObject<Rigidbody2D>(target.Target, out var targetRigidBody))
|
||||
{
|
||||
var d = Vector2.Distance(actorRigidBody.position, targetRigidBody.position);
|
||||
if (d < meleeShared.Range)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/AttackTargetMeleeGoapAction.cs.meta
Normal file
3
Assets/Scripts/AI/AttackTargetMeleeGoapAction.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d50ffa59d77a40ed86cef71992500ce7
|
||||
timeCreated: 1533311558
|
||||
3
Assets/Scripts/AI/FSM.meta
Normal file
3
Assets/Scripts/AI/FSM.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e412c313c4bf462083c745b6c8b124ac
|
||||
timeCreated: 1532858449
|
||||
290
Assets/Scripts/AI/FSM/IdleSystem.cs
Normal file
290
Assets/Scripts/AI/FSM/IdleSystem.cs
Normal file
|
|
@ -0,0 +1,290 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace AI.FSM
|
||||
{
|
||||
public class IdleSystem : ComponentSystem
|
||||
{
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
var toRemove = new NativeList<KeyValuePair<Entity, ComponentType>>(8, Allocator.Temp);
|
||||
var toAdd = new List<ValueTuple<Entity, NativeArray<GoapActionReference>>>();
|
||||
var actionValidation = GetComponentDataFromEntity<GoapActionValidation>();
|
||||
|
||||
Entities.ForEach(
|
||||
(Entity entity, GoapAgentData agent, ref IdleInitializedState idleInitialized) =>
|
||||
{
|
||||
var hasValidatingFlag = false;
|
||||
for (var j = 0; j < agent.Actions.Count; j++)
|
||||
{
|
||||
if (EntityManager.Exists(agent.Actions[j]) &&
|
||||
actionValidation.Exists(agent.Actions[j]) &&
|
||||
actionValidation[agent.Actions[j]].Validating)
|
||||
{
|
||||
hasValidatingFlag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasValidatingFlag)
|
||||
{
|
||||
//remove validation component as all elements have been validated
|
||||
agent.Actions.ForEach(PostUpdateCommands.RemoveComponent<GoapActionValidation>);
|
||||
|
||||
var usableActions = new List<GoapActionReference>(agent.Actions.Count);
|
||||
for (var j = 0; j < agent.Actions.Count; j++)
|
||||
{
|
||||
if (actionValidation[agent.Actions[j]].Valid)
|
||||
{
|
||||
usableActions.Add(new GoapActionReference { Entity = agent.Actions[j] });
|
||||
}
|
||||
}
|
||||
|
||||
var leaves = new List<Node>();
|
||||
var worldState = new HashSet<(GoapKeys, object)>(agent.States);
|
||||
var goal = new HashSet<(GoapKeys, object)>(agent.Goals);
|
||||
var start = new Node(null, 0, worldState, new GoapActionReference { Entity = Entity.Null });
|
||||
var success = buildGraph(start, leaves, usableActions, goal);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
//no valid action path, reset to idle
|
||||
PostUpdateCommands.RemoveComponent<IdleInitializedState>(entity);
|
||||
PostUpdateCommands.AddComponent(entity, new IdleState());
|
||||
return;
|
||||
}
|
||||
|
||||
Node cheapest = null;
|
||||
foreach (var leaf in leaves)
|
||||
{
|
||||
if (cheapest == null)
|
||||
{
|
||||
cheapest = leaf;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (leaf.RunningCost < cheapest.RunningCost)
|
||||
{
|
||||
cheapest = leaf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get its node and work back through the parents
|
||||
var result = new List<GoapActionReference>();
|
||||
var n = cheapest;
|
||||
while (n != null)
|
||||
{
|
||||
if (n.Action.Entity != Entity.Null)
|
||||
{
|
||||
result.Insert(0, n.Action); // insert the action in the front
|
||||
}
|
||||
n = n.Parent;
|
||||
}
|
||||
|
||||
//start processing the action chain
|
||||
PostUpdateCommands.RemoveComponent<IdleInitializedState>(entity);
|
||||
PostUpdateCommands.AddComponent(entity, new PerformActionState());
|
||||
var ar = new NativeArray<GoapActionReference>(result.Count, Allocator.Temp);
|
||||
for (var j = 0; j < result.Count; j++)
|
||||
{
|
||||
if (j == 0)
|
||||
{
|
||||
PostUpdateCommands.AddComponent(result[j].Entity, new GoapActiveAction());
|
||||
}
|
||||
ar[j] = result[j];
|
||||
}
|
||||
|
||||
toAdd.Add(new ValueTuple<Entity, NativeArray<GoapActionReference>>(entity, ar));
|
||||
}
|
||||
});
|
||||
|
||||
Entities.ForEach(
|
||||
(Entity agentEntity, GoapAgentData agent, ref IdleState idleState) =>
|
||||
{
|
||||
for (var j = 0; j < agent.Actions.Count; j++)
|
||||
{
|
||||
var actionEntity = agent.Actions[j];
|
||||
//only add validation if none is present
|
||||
if (!actionValidation.Exists(actionEntity))
|
||||
{
|
||||
var validationData = new GoapActionValidation { Validating = true };
|
||||
PostUpdateCommands.AddComponent(actionEntity, validationData);
|
||||
//reset the action
|
||||
PostUpdateCommands.SetComponent(actionEntity, new GoapAction());
|
||||
if (EntityManager.HasComponent<GoapActiveAction>(actionEntity))
|
||||
{
|
||||
PostUpdateCommands.RemoveComponent<GoapActiveAction>(actionEntity);
|
||||
}
|
||||
if (EntityManager.HasComponent<GoapProcessingAction>(actionEntity))
|
||||
{
|
||||
PostUpdateCommands.RemoveComponent<GoapProcessingAction>(actionEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PostUpdateCommands.RemoveComponent<IdleState>(agentEntity);
|
||||
PostUpdateCommands.AddComponent(agentEntity, new IdleInitializedState());
|
||||
});
|
||||
|
||||
for (var i = 0; i < toRemove.Length; i++)
|
||||
{
|
||||
EntityManager.RemoveComponent(toRemove[i].Key, toRemove[i].Value);
|
||||
}
|
||||
|
||||
for (var i = 0; i < toAdd.Count; i++)
|
||||
{
|
||||
EntityManager.AddBuffer<GoapActionReference>(toAdd[i].Item1);
|
||||
var ar = EntityManager.GetBuffer<GoapActionReference>(toAdd[i].Item1);
|
||||
ar.CopyFrom(toAdd[i].Item2);
|
||||
toAdd[i].Item2.Dispose();
|
||||
}
|
||||
|
||||
toRemove.Dispose();
|
||||
}
|
||||
|
||||
private bool buildGraph(Node parent, List<Node> leaves, List<GoapActionReference> usableActions, HashSet<(GoapKeys, object)> goal)
|
||||
{
|
||||
var foundOne = false;
|
||||
|
||||
// go through each action available at this node and see if we can use it here
|
||||
for (var i = 0; i < usableActions.Count; i++)
|
||||
{
|
||||
var actionReference = usableActions[i];
|
||||
var action = EntityManager.GetSharedComponentData<GoapSharedAction>(actionReference.Entity);
|
||||
|
||||
// if the parent state has the conditions for this action's preconditions, we can use it here
|
||||
if (inState(action.Preconditions, parent.State))
|
||||
{
|
||||
// apply the action's effects to the parent state
|
||||
var currentState = populateState(parent.State, action.Effects);
|
||||
//Debug.Log(GoapAgent.prettyPrint(currentState));
|
||||
var node = new Node(parent, parent.RunningCost + action.Cost, currentState, actionReference);
|
||||
|
||||
if (inState(goal, currentState))
|
||||
{
|
||||
// we found a solution!
|
||||
leaves.Add(node);
|
||||
foundOne = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// not at a solution yet, so test all the remaining actions and branch out the tree
|
||||
var subset = actionSubset(usableActions, actionReference);
|
||||
var found = buildGraph(node, leaves, subset, goal);
|
||||
if (found)
|
||||
{
|
||||
foundOne = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return foundOne;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a subset of the actions excluding the removeMe one. Creates a new set.
|
||||
*/
|
||||
private List<GoapActionReference> actionSubset(List<GoapActionReference> actions, GoapActionReference removeMe)
|
||||
{
|
||||
var subset = new List<GoapActionReference>();
|
||||
foreach (var a in actions)
|
||||
{
|
||||
if (!a.Equals(removeMe))
|
||||
{
|
||||
subset.Add(a);
|
||||
}
|
||||
}
|
||||
return subset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that all items in 'test' are in 'state'. If just one does not match or is not there
|
||||
* then this returns false.
|
||||
*/
|
||||
private bool inState(HashSet<(GoapKeys, object)> test, HashSet<(GoapKeys, object)> state)
|
||||
{
|
||||
var allMatch = true;
|
||||
foreach (var t in test)
|
||||
{
|
||||
var match = false;
|
||||
foreach (var s in state)
|
||||
{
|
||||
if (s.Equals(t))
|
||||
{
|
||||
match = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!match)
|
||||
{
|
||||
allMatch = false;
|
||||
}
|
||||
}
|
||||
|
||||
return allMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the stateChange to the currentState
|
||||
*/
|
||||
private HashSet<(GoapKeys, object)> populateState(HashSet<(GoapKeys, object)> currentState, HashSet<(GoapKeys, object)> stateChange)
|
||||
{
|
||||
var state = new HashSet<(GoapKeys, object)>();
|
||||
// copy the KVPs over as new objects
|
||||
foreach (var s in currentState)
|
||||
{
|
||||
state.Add((s.Item1, s.Item2));
|
||||
}
|
||||
|
||||
foreach (var change in stateChange)
|
||||
{
|
||||
// if the key exists in the current state, update the Value
|
||||
var exists = false;
|
||||
|
||||
foreach (var s in state)
|
||||
{
|
||||
if (s.Equals(change))
|
||||
{
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (exists)
|
||||
{
|
||||
state.RemoveWhere(kvp => kvp.Item1.Equals(change.Item1));
|
||||
var updated = (change.Item1, change.Item2);
|
||||
state.Add(updated);
|
||||
}
|
||||
// if it does not exist in the current state, add it
|
||||
else
|
||||
{
|
||||
state.Add((change.Item1, change.Item2));
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
private class Node
|
||||
{
|
||||
public readonly GoapActionReference Action;
|
||||
public readonly Node Parent;
|
||||
public readonly float RunningCost;
|
||||
public readonly HashSet<(GoapKeys, object)> State;
|
||||
|
||||
public Node(Node parent, float runningCost, HashSet<(GoapKeys, object)> state, GoapActionReference action)
|
||||
{
|
||||
Parent = parent;
|
||||
RunningCost = runningCost;
|
||||
State = state;
|
||||
Action = action;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/FSM/IdleSystem.cs.meta
Normal file
3
Assets/Scripts/AI/FSM/IdleSystem.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ec32660a0628497583d8fa68b96a6045
|
||||
timeCreated: 1532865956
|
||||
111
Assets/Scripts/AI/FSM/MoveStateSystem.cs
Normal file
111
Assets/Scripts/AI/FSM/MoveStateSystem.cs
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
using DefaultNamespace;
|
||||
using DefaultNamespace.Navigation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
using Zenject;
|
||||
|
||||
namespace AI.FSM
|
||||
{
|
||||
public class MoveStateSystem : InjectableComponentSystem
|
||||
{
|
||||
[Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float AirControl;
|
||||
public float DefaultReachTime;
|
||||
public float JumpMinHeight;
|
||||
public float MaxForce;
|
||||
public float MinTargetDistance;
|
||||
public float MoveForce;
|
||||
public Vector3 MovePidParams;
|
||||
public float NodeMinDistance;
|
||||
public float RepathCooldown;
|
||||
public float ViewDistance;
|
||||
public LayerMask VisibilityLayerMask;
|
||||
}
|
||||
|
||||
[Inject] private readonly Settings settings;
|
||||
|
||||
protected override void OnSystemUpdate()
|
||||
{
|
||||
var timeDelta = Time.DeltaTime;
|
||||
var typesToRemove = new List<ValueTuple<Entity, ComponentType>>();
|
||||
|
||||
Entities.ForEach(
|
||||
(
|
||||
Entity entity,
|
||||
Rigidbody2D rigidBody,
|
||||
ref ActorNpcData npc,
|
||||
ref MoveState move,
|
||||
ref ActorData actor,
|
||||
ref ActorAnimationData animation,
|
||||
ref NavigationAgentData navigationAgent) =>
|
||||
{
|
||||
var actions = EntityManager.GetBuffer<GoapActionReference>(entity);
|
||||
|
||||
for (var j = 0; j < actions.Length; j++)
|
||||
{
|
||||
var actionEntity = actions[j].Entity;
|
||||
if (EntityManager.Exists(actionEntity) &&
|
||||
EntityManager.TryGetComponentData<GoapAction>(actionEntity, out var action) &&
|
||||
EntityManager.TryGetComponentData<GoapActiveAction>(actionEntity, out var active) &&
|
||||
EntityManager.TryGetSharedComponentData<GoapSharedAction>(actionEntity, out var sharedAction))
|
||||
{
|
||||
if (sharedAction.RequiresInRange && !EntityManager.Exists(action.Target))
|
||||
{
|
||||
//Action requires a target but has none. Planning failed.
|
||||
ReturnToPlanning(typesToRemove, entity, actions);
|
||||
continue;
|
||||
}
|
||||
|
||||
var targetTransform = EntityManager.GetComponentObject<Transform>(action.Target);
|
||||
|
||||
var distanceToTarget = Vector2.Distance(targetTransform.position, rigidBody.position);
|
||||
|
||||
if (distanceToTarget <= settings.MinTargetDistance && npc.JumpingTimer <= 0 && actor.Grounded && navigationAgent.Grounded)
|
||||
{
|
||||
active.InRange = true;
|
||||
PostUpdateCommands.SetComponent(actionEntity, active);
|
||||
PostUpdateCommands.RemoveComponent<MoveState>(entity);
|
||||
PostUpdateCommands.AddComponent(entity, new PerformActionState());
|
||||
navigationAgent.destination = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
navigationAgent.destination = (Vector2)targetTransform.position;
|
||||
}
|
||||
|
||||
animation.AttackSpeed = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
foreach (var tuple in typesToRemove)
|
||||
{
|
||||
EntityManager.RemoveComponent(tuple.Item1, tuple.Item2);
|
||||
}
|
||||
}
|
||||
|
||||
private void ReturnToPlanning(
|
||||
IList<ValueTuple<Entity, ComponentType>> typesToRemove,
|
||||
Entity entity,
|
||||
DynamicBuffer<GoapActionReference> actions)
|
||||
{
|
||||
PostUpdateCommands.RemoveComponent<MoveState>(entity);
|
||||
PostUpdateCommands.AddComponent(entity, new IdleState());
|
||||
RemoveCurrentActions(typesToRemove, entity, actions);
|
||||
}
|
||||
|
||||
private void RemoveCurrentActions(
|
||||
IList<ValueTuple<Entity, ComponentType>> typesToRemove,
|
||||
Entity entity,
|
||||
DynamicBuffer<GoapActionReference> actions)
|
||||
{
|
||||
var bufferType = ComponentType.ReadWrite<GoapActionReference>();
|
||||
typesToRemove.Add(new ValueTuple<Entity, ComponentType>(entity, bufferType));
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/FSM/MoveStateSystem.cs.meta
Normal file
3
Assets/Scripts/AI/FSM/MoveStateSystem.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 964301ce9f5d410cb3b2dabccdb0ec6d
|
||||
timeCreated: 1532858466
|
||||
116
Assets/Scripts/AI/FSM/PerformActionStateSystem.cs
Normal file
116
Assets/Scripts/AI/FSM/PerformActionStateSystem.cs
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
using DefaultNamespace;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace AI.FSM
|
||||
{
|
||||
[UpdateInGroup(typeof(SimulationSystemGroup)), UpdateAfter(typeof(EventRemovalSystem))]
|
||||
public class PerformActionStateSystem : ComponentSystem
|
||||
{
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
using (var typesToRemove = new NativeList<KeyValuePair<Entity, ComponentType>>(8, Allocator.Temp))
|
||||
{
|
||||
Entities.ForEach(
|
||||
(Entity entity, GoapAgentData agent, ref PerformActionState performAction) =>
|
||||
{
|
||||
var actions = EntityManager.GetBuffer<GoapActionReference>(entity);
|
||||
|
||||
var hasActions = actions.Length > 0;
|
||||
|
||||
if (!hasActions)
|
||||
{
|
||||
//finished actions
|
||||
PostUpdateCommands.RemoveComponent<PerformActionState>(entity);
|
||||
PostUpdateCommands.AddComponent(entity, new IdleState());
|
||||
ClearCurrentActions(typesToRemove, entity, actions);
|
||||
return;
|
||||
}
|
||||
|
||||
for (var j = 0; j < actions.Length; j++)
|
||||
{
|
||||
var actionEntity = actions[j].Entity;
|
||||
if (EntityManager.Exists(actionEntity) &&
|
||||
EntityManager.TryGetComponentData<GoapActiveAction>(actionEntity, out var active) &&
|
||||
EntityManager.TryGetSharedComponentData<GoapSharedAction>(actionEntity, out var action))
|
||||
{
|
||||
if (!EntityManager.HasComponent<GoapProcessingAction>(actionEntity))
|
||||
{
|
||||
PostUpdateCommands.AddComponent(actionEntity, new GoapProcessingAction());
|
||||
}
|
||||
|
||||
if (active.Done)
|
||||
{
|
||||
var nextActionIndex = j + 1;
|
||||
hasActions = nextActionIndex < actions.Length;
|
||||
if (hasActions)
|
||||
{
|
||||
//make next tha action active
|
||||
PostUpdateCommands.AddComponent(actions[nextActionIndex].Entity, new GoapActiveAction());
|
||||
//remove active and processing from current action
|
||||
PostUpdateCommands.RemoveComponent<GoapActiveAction>(actionEntity);
|
||||
PostUpdateCommands.RemoveComponent<GoapProcessingAction>(actionEntity);
|
||||
}
|
||||
else
|
||||
{
|
||||
//finished actions
|
||||
PostUpdateCommands.RemoveComponent<PerformActionState>(entity);
|
||||
PostUpdateCommands.AddComponent(entity, new IdleState());
|
||||
ClearCurrentActions(typesToRemove, entity, actions);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (action.RequiresInRange && !active.InRange)
|
||||
{
|
||||
// we need to move there first
|
||||
PostUpdateCommands.RemoveComponent<PerformActionState>(entity);
|
||||
PostUpdateCommands.RemoveComponent<GoapProcessingAction>(actionEntity);
|
||||
PostUpdateCommands.AddComponent(entity, new MoveState());
|
||||
break;
|
||||
}
|
||||
|
||||
if (active.Fail)
|
||||
{
|
||||
// action failed, we need to plan again
|
||||
PostUpdateCommands.RemoveComponent<PerformActionState>(entity);
|
||||
PostUpdateCommands.AddComponent(entity, new IdleState());
|
||||
ClearCurrentActions(typesToRemove, entity, actions);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (var i = 0; i < typesToRemove.Length; i++)
|
||||
{
|
||||
EntityManager.RemoveComponent(typesToRemove[i].Key, typesToRemove[i].Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearCurrentActions(
|
||||
NativeList<KeyValuePair<Entity, ComponentType>> typesToRemove,
|
||||
Entity entity,
|
||||
DynamicBuffer<GoapActionReference> actions)
|
||||
{
|
||||
var componentType = ComponentType.ReadWrite<GoapActionReference>();
|
||||
typesToRemove.Add(new KeyValuePair<Entity, ComponentType>(entity, componentType));
|
||||
|
||||
//remove processing or active tags
|
||||
for (var i = 0; i < actions.Length; i++)
|
||||
{
|
||||
if (EntityManager.HasComponent<GoapActiveAction>(actions[i].Entity))
|
||||
{
|
||||
PostUpdateCommands.RemoveComponent<GoapActiveAction>(actions[i].Entity);
|
||||
}
|
||||
if (EntityManager.HasComponent<GoapProcessingAction>(actions[i].Entity))
|
||||
{
|
||||
PostUpdateCommands.RemoveComponent<GoapProcessingAction>(actions[i].Entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/FSM/PerformActionStateSystem.cs.meta
Normal file
3
Assets/Scripts/AI/FSM/PerformActionStateSystem.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c8cc368d88ea43ffb12189f5b626c050
|
||||
timeCreated: 1532861738
|
||||
24
Assets/Scripts/AI/FSM/States.cs
Normal file
24
Assets/Scripts/AI/FSM/States.cs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
using Unity.Entities;
|
||||
|
||||
namespace AI.FSM
|
||||
{
|
||||
public struct FsmState : IComponentData
|
||||
{
|
||||
}
|
||||
|
||||
public struct MoveState : IComponentData
|
||||
{
|
||||
}
|
||||
|
||||
public struct IdleState : IComponentData
|
||||
{
|
||||
}
|
||||
|
||||
public struct IdleInitializedState : IComponentData
|
||||
{
|
||||
}
|
||||
|
||||
public struct PerformActionState : IComponentData
|
||||
{
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/FSM/States.cs.meta
Normal file
3
Assets/Scripts/AI/FSM/States.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9237a4be0aef4fa5b6b7bc50b118d303
|
||||
timeCreated: 1532858500
|
||||
59
Assets/Scripts/AI/FindTargetPlayerGoapAction.cs
Normal file
59
Assets/Scripts/AI/FindTargetPlayerGoapAction.cs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
using DefaultNamespace;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct FindTargetPlayerGoapAction : IComponentData
|
||||
{
|
||||
}
|
||||
|
||||
public class FindTargetPlayerGoapActionSystem : GoapActionSystem<FindTargetPlayerGoapAction>
|
||||
{
|
||||
protected override void OnInitialize(ref FindTargetPlayerGoapAction action, ref GoapSharedAction goapSharedAction)
|
||||
{
|
||||
goapSharedAction.Cost = 10;
|
||||
goapSharedAction.Effects.Add((GoapKeys.HasTarget, true));
|
||||
}
|
||||
|
||||
protected override void OnProcess(
|
||||
ref FindTargetPlayerGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
GoapAction goapAction,
|
||||
GoapActionActor actor,
|
||||
ref GoapActiveAction active)
|
||||
{
|
||||
EntityManager.SetComponentData(actor.Actor, new ActorTargetData { Target = goapAction.Target });
|
||||
active.Done = true;
|
||||
}
|
||||
|
||||
protected override bool OnValidate(
|
||||
ref FindTargetPlayerGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
ref GoapAction goapAction,
|
||||
GoapActionActor actor)
|
||||
{
|
||||
if (!EntityManager.HasComponents<Rigidbody2D, ActorTargetData>(actor.Actor))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var closestDist = float.MaxValue;
|
||||
var actorRigidBody = EntityManager.GetComponentObject<Rigidbody2D>(actor.Actor);
|
||||
var playerEntity = Entity.Null;
|
||||
|
||||
Entities.ForEach(
|
||||
(Entity entity, Rigidbody2D rigidbody, ref PlayerData playerData) =>
|
||||
{
|
||||
var d = Vector2.SqrMagnitude(actorRigidBody.position - rigidbody.position);
|
||||
if (d < closestDist)
|
||||
{
|
||||
closestDist = d;
|
||||
playerEntity = entity;
|
||||
}
|
||||
});
|
||||
|
||||
return playerEntity != Entity.Null;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/FindTargetPlayerGoapAction.cs.meta
Normal file
3
Assets/Scripts/AI/FindTargetPlayerGoapAction.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7a6760f22510409881c278b1c5c07284
|
||||
timeCreated: 1533316257
|
||||
9
Assets/Scripts/AI/GoapAction.cs
Normal file
9
Assets/Scripts/AI/GoapAction.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using Unity.Entities;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct GoapAction : IComponentData
|
||||
{
|
||||
public Entity Target;
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapAction.cs.meta
Normal file
3
Assets/Scripts/AI/GoapAction.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7b70258323e9451985a12955bfda4f63
|
||||
timeCreated: 1532986154
|
||||
9
Assets/Scripts/AI/GoapActionActor.cs
Normal file
9
Assets/Scripts/AI/GoapActionActor.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using Unity.Entities;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct GoapActionActor : IComponentData
|
||||
{
|
||||
public Entity Actor;
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapActionActor.cs.meta
Normal file
3
Assets/Scripts/AI/GoapActionActor.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ebd976f080014bb0970004151e44170d
|
||||
timeCreated: 1532986264
|
||||
21
Assets/Scripts/AI/GoapActionDeleteSystem.cs
Normal file
21
Assets/Scripts/AI/GoapActionDeleteSystem.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using DefaultNamespace;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
[UpdateInGroup(typeof(LateSimulationSystemGroup)), UpdateAfter(typeof(EntityDeathSystem))]
|
||||
public class GoapActionDeleteSystem : ComponentSystem
|
||||
{
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
Entities.ForEach(
|
||||
(Entity entity, ref GoapActionActor action) =>
|
||||
{
|
||||
if (!EntityManager.Exists(action.Actor))
|
||||
{
|
||||
PostUpdateCommands.DestroyEntity(entity);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapActionDeleteSystem.cs.meta
Normal file
3
Assets/Scripts/AI/GoapActionDeleteSystem.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: eead5bdbdf5740959e21183214b6419b
|
||||
timeCreated: 1532980250
|
||||
9
Assets/Scripts/AI/GoapActionEntry.cs
Normal file
9
Assets/Scripts/AI/GoapActionEntry.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using Unity.Entities;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct GoapActionEntry
|
||||
{
|
||||
public ComponentType ActionType;
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapActionEntry.cs.meta
Normal file
3
Assets/Scripts/AI/GoapActionEntry.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2a688d1d7e1b4bd79b6856f9bf061fb6
|
||||
timeCreated: 1532893857
|
||||
9
Assets/Scripts/AI/GoapActionReference.cs
Normal file
9
Assets/Scripts/AI/GoapActionReference.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using Unity.Entities;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct GoapActionReference : IBufferElementData
|
||||
{
|
||||
public Entity Entity;
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapActionReference.cs.meta
Normal file
3
Assets/Scripts/AI/GoapActionReference.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5df3d618587a4c0fa27d5f0bc5ad3673
|
||||
timeCreated: 1532895480
|
||||
132
Assets/Scripts/AI/GoapActionSystem.cs
Normal file
132
Assets/Scripts/AI/GoapActionSystem.cs
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
using System.Collections.Generic;
|
||||
using Unity.Entities;
|
||||
using Zenject;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public abstract class GoapActionInjectableSystem<T> : GoapActionSystem<T> where T : struct, IComponentData
|
||||
{
|
||||
private bool injected;
|
||||
|
||||
[Inject]
|
||||
private void OnInject()
|
||||
{
|
||||
injected = true;
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
if (!injected)
|
||||
{
|
||||
return;
|
||||
}
|
||||
base.OnUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class GoapActionSystem<T> : ComponentSystem where T : struct, IComponentData
|
||||
{
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
OnBeforeUpdate();
|
||||
|
||||
Entities.WithNone<GoapSharedAction>()
|
||||
.ForEach(
|
||||
(Entity entity, ref T action) =>
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
EntityManager.SetName(entity, action.GetType().Name);
|
||||
#endif
|
||||
OnInitializeInternal(entity, ref action);
|
||||
});
|
||||
|
||||
Entities.ForEach(
|
||||
(
|
||||
Entity entity,
|
||||
GoapSharedAction goapSharedAction,
|
||||
ref GoapActiveAction activeAction,
|
||||
ref GoapProcessingAction processing,
|
||||
ref T action,
|
||||
ref GoapAction goapAction,
|
||||
ref GoapActionActor actor) =>
|
||||
{
|
||||
OnProcessInternal(entity, goapSharedAction, ref activeAction, ref processing, ref action, ref goapAction, ref actor);
|
||||
});
|
||||
|
||||
Entities.ForEach(
|
||||
(
|
||||
Entity entity,
|
||||
GoapSharedAction goapSharedAction,
|
||||
ref GoapActionValidation validation,
|
||||
ref GoapAction goapAction,
|
||||
ref T action,
|
||||
ref GoapActionActor actor) =>
|
||||
{
|
||||
OnValidateInternal(entity, goapSharedAction, ref validation, ref goapAction, ref action, ref actor);
|
||||
});
|
||||
|
||||
OnAfterUpdate();
|
||||
}
|
||||
|
||||
private void OnInitializeInternal(Entity entity, ref T action)
|
||||
{
|
||||
var a = new GoapSharedAction { Effects = new HashSet<(GoapKeys, object)>(), Preconditions = new HashSet<(GoapKeys, object)>() };
|
||||
OnInitialize(ref action, ref a);
|
||||
|
||||
PostUpdateCommands.AddSharedComponent(entity, a);
|
||||
}
|
||||
|
||||
private void OnProcessInternal(
|
||||
Entity entity,
|
||||
GoapSharedAction goapSharedAction,
|
||||
ref GoapActiveAction active,
|
||||
ref GoapProcessingAction processing,
|
||||
ref T action,
|
||||
ref GoapAction goapAction,
|
||||
ref GoapActionActor actor)
|
||||
{
|
||||
if (EntityManager.Exists(actor.Actor))
|
||||
{
|
||||
OnProcess(ref action, ref goapSharedAction, goapAction, actor, ref active);
|
||||
}
|
||||
|
||||
PostUpdateCommands.SetSharedComponent(entity, goapSharedAction);
|
||||
}
|
||||
|
||||
private void OnValidateInternal(
|
||||
Entity entity,
|
||||
GoapSharedAction goapSharedAction,
|
||||
ref GoapActionValidation validation,
|
||||
ref GoapAction goapAction,
|
||||
ref T action,
|
||||
ref GoapActionActor actor)
|
||||
{
|
||||
if (validation.Validating)
|
||||
{
|
||||
validation.Valid = EntityManager.Exists(actor.Actor) && OnValidate(ref action, ref goapSharedAction, ref goapAction, actor);
|
||||
validation.Validating = false;
|
||||
}
|
||||
|
||||
PostUpdateCommands.SetSharedComponent(entity, goapSharedAction);
|
||||
}
|
||||
|
||||
protected abstract void OnInitialize(ref T action, ref GoapSharedAction goapSharedAction);
|
||||
|
||||
protected abstract void OnProcess(
|
||||
ref T action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
GoapAction goapAction,
|
||||
GoapActionActor actor,
|
||||
ref GoapActiveAction active);
|
||||
|
||||
protected abstract bool OnValidate(ref T action, ref GoapSharedAction goapSharedAction, ref GoapAction goapAction, GoapActionActor actor);
|
||||
|
||||
protected virtual void OnBeforeUpdate()
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void OnAfterUpdate()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapActionSystem.cs.meta
Normal file
3
Assets/Scripts/AI/GoapActionSystem.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c347eb66e6ae46839572d75aecbc62b9
|
||||
timeCreated: 1532972122
|
||||
11
Assets/Scripts/AI/GoapActionValidation.cs
Normal file
11
Assets/Scripts/AI/GoapActionValidation.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
using DefaultNamespace;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct GoapActionValidation : IComponentData
|
||||
{
|
||||
public bool1 Validating;
|
||||
public bool1 Valid;
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapActionValidation.cs.meta
Normal file
3
Assets/Scripts/AI/GoapActionValidation.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d04d5743e54f4b728cc08a7475cb4f0b
|
||||
timeCreated: 1532894851
|
||||
22
Assets/Scripts/AI/GoapActiveAction.cs
Normal file
22
Assets/Scripts/AI/GoapActiveAction.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using DefaultNamespace;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct GoapActiveAction : IComponentData
|
||||
{
|
||||
public bool1 InRange;
|
||||
public bool1 Done;
|
||||
public bool1 Fail;
|
||||
|
||||
public void MarkFailed()
|
||||
{
|
||||
Fail = true;
|
||||
}
|
||||
|
||||
public void MarkDone()
|
||||
{
|
||||
Done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapActiveAction.cs.meta
Normal file
3
Assets/Scripts/AI/GoapActiveAction.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c428527a44b14269a972e2a1a7b71f22
|
||||
timeCreated: 1532897400
|
||||
53
Assets/Scripts/AI/GoapAgentDataComponent.cs
Normal file
53
Assets/Scripts/AI/GoapAgentDataComponent.cs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct GoapAgentData : ISharedComponentData, IEquatable<GoapAgentData>
|
||||
{
|
||||
public List<Entity> Actions;
|
||||
public HashSet<(GoapKeys key, object value)> Goals;
|
||||
public HashSet<(GoapKeys key, object value)> States;
|
||||
|
||||
public bool Equals(GoapAgentData other)
|
||||
{
|
||||
return Equals(Actions, other.Actions) && Equals(Goals, other.Goals) && Equals(States, other.States);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
var hashCode = Actions != null ? Actions.GetHashCode() : 0;
|
||||
hashCode = (hashCode * 397) ^ (Goals != null ? Goals.GetHashCode() : 0);
|
||||
hashCode = (hashCode * 397) ^ (States != null ? States.GetHashCode() : 0);
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class GoapAgentDataComponent : MonoBehaviour, IConvertGameObjectToEntity
|
||||
{
|
||||
public string[] Types;
|
||||
|
||||
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
|
||||
{
|
||||
var value = new GoapAgentData
|
||||
{
|
||||
Goals = new HashSet<(GoapKeys, object)>(), States = new HashSet<(GoapKeys, object)>(), Actions = new List<Entity>()
|
||||
};
|
||||
foreach (var type in Types)
|
||||
{
|
||||
var e = dstManager.CreateEntity(
|
||||
ComponentType.FromTypeIndex(TypeManager.GetTypeIndex(Type.GetType(type, true))),
|
||||
ComponentType.ReadWrite<GoapAction>());
|
||||
dstManager.AddComponentData(e, new GoapActionActor { Actor = entity });
|
||||
value.Actions.Add(e);
|
||||
}
|
||||
|
||||
dstManager.AddSharedComponentData(entity, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapAgentDataComponent.cs.meta
Normal file
3
Assets/Scripts/AI/GoapAgentDataComponent.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c1e89ea26d1549da91c44ae3d4f71c54
|
||||
timeCreated: 1532858159
|
||||
30
Assets/Scripts/AI/GoapInitializationSystem.cs
Normal file
30
Assets/Scripts/AI/GoapInitializationSystem.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using AI.FSM;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
[UpdateInGroup(typeof(InitializationSystemGroup))]
|
||||
public class GoapInitializationSystem : ComponentSystem
|
||||
{
|
||||
private EntityQuery query;
|
||||
|
||||
protected override void OnCreate()
|
||||
{
|
||||
query = GetEntityQuery(ComponentType.Exclude<FsmState>(), ComponentType.ReadOnly<GoapAgentData>());
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
Entities.With(query)
|
||||
.ForEach(
|
||||
(Entity entity, GoapAgentData agent) =>
|
||||
{
|
||||
if (agent.Actions != null)
|
||||
{
|
||||
PostUpdateCommands.AddComponent(entity, new FsmState());
|
||||
PostUpdateCommands.AddComponent(entity, new IdleState());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapInitializationSystem.cs.meta
Normal file
3
Assets/Scripts/AI/GoapInitializationSystem.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ddf6596e115c492fb3a93e81af8066c6
|
||||
timeCreated: 1532867416
|
||||
4
Assets/Scripts/AI/GoapKeys.cs
Normal file
4
Assets/Scripts/AI/GoapKeys.cs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
namespace AI
|
||||
{
|
||||
public enum GoapKeys { Attacks, SeesTarget, HasTarget, HasAmmo }
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapKeys.cs.meta
Normal file
3
Assets/Scripts/AI/GoapKeys.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3ec34de789d14324a4382835bd4d5aa6
|
||||
timeCreated: 1533238619
|
||||
8
Assets/Scripts/AI/GoapProcessingAction.cs
Normal file
8
Assets/Scripts/AI/GoapProcessingAction.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
using Unity.Entities;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct GoapProcessingAction : IComponentData
|
||||
{
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapProcessingAction.cs.meta
Normal file
3
Assets/Scripts/AI/GoapProcessingAction.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4d18a8e45b574bc0a895aa9f553b3ff6
|
||||
timeCreated: 1532901302
|
||||
40
Assets/Scripts/AI/GoapSharedAction.cs
Normal file
40
Assets/Scripts/AI/GoapSharedAction.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using DefaultNamespace;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct GoapSharedAction : ISharedComponentData, IEquatable<GoapSharedAction>
|
||||
{
|
||||
public HashSet<(GoapKeys, object)> Preconditions;
|
||||
public HashSet<(GoapKeys, object)> Effects;
|
||||
public bool1 RequiresInRange;
|
||||
public float Cost;
|
||||
|
||||
public bool Equals(GoapSharedAction other)
|
||||
{
|
||||
return Equals(Preconditions, other.Preconditions) &&
|
||||
Equals(Effects, other.Effects) &&
|
||||
RequiresInRange.Equals(other.RequiresInRange) &&
|
||||
Cost.Equals(other.Cost);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is GoapSharedAction other && Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
var hashCode = Preconditions != null ? Preconditions.GetHashCode() : 0;
|
||||
hashCode = (hashCode * 397) ^ (Effects != null ? Effects.GetHashCode() : 0);
|
||||
hashCode = (hashCode * 397) ^ RequiresInRange.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ Cost.GetHashCode();
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/GoapSharedAction.cs.meta
Normal file
3
Assets/Scripts/AI/GoapSharedAction.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 96be20bf7b8d45988d20749ee7b46f0d
|
||||
timeCreated: 1532858748
|
||||
101
Assets/Scripts/AI/RangeAttackPlayerGoapAction.cs
Normal file
101
Assets/Scripts/AI/RangeAttackPlayerGoapAction.cs
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
using DefaultNamespace;
|
||||
using Events;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct RangeAttackPlayerGoapAction : IComponentData
|
||||
{
|
||||
}
|
||||
|
||||
[UpdateBefore(typeof(WeaponFiringSystem))]
|
||||
public class RangeAttackPlayerGoapActionSystem : GoapActionInjectableSystem<RangeAttackPlayerGoapAction>
|
||||
{
|
||||
protected override void OnInitialize(ref RangeAttackPlayerGoapAction action, ref GoapSharedAction goapSharedAction)
|
||||
{
|
||||
goapSharedAction.Cost = 10;
|
||||
goapSharedAction.Preconditions.Add((GoapKeys.SeesTarget, true));
|
||||
goapSharedAction.Preconditions.Add((GoapKeys.HasAmmo, true));
|
||||
goapSharedAction.Effects.Add(((int)GoapKeys.Attacks, true));
|
||||
}
|
||||
|
||||
protected override void OnProcess(
|
||||
ref RangeAttackPlayerGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
GoapAction goapAction,
|
||||
GoapActionActor actor,
|
||||
ref GoapActiveAction active)
|
||||
{
|
||||
if (!Validate(actor.Actor))
|
||||
{
|
||||
active.Fail = true;
|
||||
return;
|
||||
}
|
||||
|
||||
var weaponEntity = EntityManager.GetComponentData<ActorWeaponReferenceData>(actor.Actor).Weapon;
|
||||
var weaponData = EntityManager.GetComponentData<WeaponData>(weaponEntity);
|
||||
var npc = EntityManager.GetComponentData<ActorNpcData>(actor.Actor);
|
||||
|
||||
if (npc.AttackCooldown <= 0)
|
||||
{
|
||||
if (weaponData.FireTimer <= 0)
|
||||
{
|
||||
var actorData = EntityManager.GetComponentData<ActorData>(actor.Actor);
|
||||
var layerMask = EntityManager.HasComponent<ActorWeaponData>(actor.Actor)
|
||||
? (int)EntityManager.GetComponentData<ActorWeaponData>(actor.Actor).ProjectileMask
|
||||
: LayerMask.GetMask("Ground");
|
||||
PostUpdateCommands.PostEntityEvent(EntityManager, weaponEntity, new FireWeaponEvent { LayerMask = layerMask });
|
||||
if (EntityManager.TryGetComponentData<ActorAnimationData>(actor.Actor, out var animation))
|
||||
{
|
||||
animation.Triggers |= AnimationTriggerType.Attack;
|
||||
EntityManager.SetComponentData(actor.Actor, animation);
|
||||
}
|
||||
|
||||
active.Done = true;
|
||||
|
||||
EntityManager.SetComponentData(actor.Actor, actorData);
|
||||
EntityManager.SetComponentData(weaponEntity, weaponData);
|
||||
}
|
||||
else
|
||||
{
|
||||
active.Fail = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool OnValidate(
|
||||
ref RangeAttackPlayerGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
ref GoapAction goapAction,
|
||||
GoapActionActor actor)
|
||||
{
|
||||
if (!Validate(actor.Actor))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!EntityManager.Exists(EntityManager.GetComponentData<ActorWeaponReferenceData>(actor.Actor).Weapon) ||
|
||||
!EntityManager.HasComponent<WeaponData>(EntityManager.GetComponentData<ActorWeaponReferenceData>(actor.Actor).Weapon))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var WeaponData = GetComponentDataFromEntity<WeaponData>();
|
||||
var weaponData = WeaponData[actor.Actor];
|
||||
if (weaponData.FireTimer > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
goapSharedAction.Cost = 10f;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool Validate(Entity actor)
|
||||
{
|
||||
return EntityManager.HasComponents<ActorWeaponPropertiesData, ActorWeaponReferenceData, Rigidbody2D, ActorData, ActorNpcData>(actor);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/RangeAttackPlayerGoapAction.cs.meta
Normal file
3
Assets/Scripts/AI/RangeAttackPlayerGoapAction.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2095ab841e3b43068f58ac4db39ddd91
|
||||
timeCreated: 1533238406
|
||||
70
Assets/Scripts/AI/ReloadGoapAction.cs
Normal file
70
Assets/Scripts/AI/ReloadGoapAction.cs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
using DefaultNamespace;
|
||||
using Events;
|
||||
using System;
|
||||
using Unity.Entities;
|
||||
using Zenject;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct ReloadGoapAction : IComponentData
|
||||
{
|
||||
}
|
||||
|
||||
public class ReloadGoapActionSystem : GoapActionSystem<ReloadGoapAction>
|
||||
{
|
||||
[Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float ReloadAttackCooldown;
|
||||
}
|
||||
|
||||
[Inject] private readonly Settings settings;
|
||||
|
||||
protected override void OnInitialize(ref ReloadGoapAction action, ref GoapSharedAction goapSharedAction)
|
||||
{
|
||||
goapSharedAction.Effects.Add((GoapKeys.HasAmmo, true));
|
||||
goapSharedAction.Cost = 10;
|
||||
}
|
||||
|
||||
protected override void OnProcess(
|
||||
ref ReloadGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
GoapAction goapAction,
|
||||
GoapActionActor actor,
|
||||
ref GoapActiveAction active)
|
||||
{
|
||||
var weaponEntity = EntityManager.GetComponentData<ActorWeaponReferenceData>(actor.Actor).Weapon;
|
||||
if (!EntityManager.HasComponent<ReloadEvent>(weaponEntity))
|
||||
{
|
||||
active.MarkDone();
|
||||
}
|
||||
else
|
||||
{
|
||||
PostUpdateCommands.AddComponent(actor.Actor, new ReloadEvent());
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool OnValidate(
|
||||
ref ReloadGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
ref GoapAction goapAction,
|
||||
GoapActionActor actor)
|
||||
{
|
||||
if (!EntityManager.HasComponents<ActorWeaponPropertiesData, ActorWeaponReferenceData, ActorNpcData>(actor.Actor))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var weaponEntity = EntityManager.GetComponentData<ActorWeaponReferenceData>(actor.Actor).Weapon;
|
||||
if (!EntityManager.Exists(weaponEntity) || !EntityManager.HasComponent<WeaponData>(weaponEntity))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var weaponData = EntityManager.GetComponentData<WeaponData>(weaponEntity);
|
||||
if (weaponData.Ammo <= 0 && weaponData.ReloadTimer <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/ReloadGoapAction.cs.meta
Normal file
3
Assets/Scripts/AI/ReloadGoapAction.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: cb4d32816af9488c87f08debc843f58c
|
||||
timeCreated: 1533377513
|
||||
119
Assets/Scripts/AI/SearchTargetGoapAction.cs
Normal file
119
Assets/Scripts/AI/SearchTargetGoapAction.cs
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
using DefaultNamespace;
|
||||
using DefaultNamespace.Navigation;
|
||||
using System;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
using Zenject;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public struct SearchTargetGoapAction : IComponentData
|
||||
{
|
||||
}
|
||||
|
||||
[UpdateAfter(typeof(ActorGroundCheckSystem))]
|
||||
public class SearchTargetGoapActionSystem : GoapActionInjectableSystem<SearchTargetGoapAction>
|
||||
{
|
||||
[Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float AttackCooldown;
|
||||
public float RepathCooldown;
|
||||
}
|
||||
|
||||
[Inject] private Settings settings;
|
||||
|
||||
protected override void OnInitialize(ref SearchTargetGoapAction action, ref GoapSharedAction goapSharedAction)
|
||||
{
|
||||
goapSharedAction.Cost = 10;
|
||||
goapSharedAction.Effects.Add((GoapKeys.SeesTarget, true));
|
||||
goapSharedAction.Preconditions.Add((GoapKeys.HasTarget, true));
|
||||
}
|
||||
|
||||
protected override void OnProcess(
|
||||
ref SearchTargetGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
GoapAction goapAction,
|
||||
GoapActionActor actor,
|
||||
ref GoapActiveAction active)
|
||||
{
|
||||
var actorTarget = EntityManager.GetComponentData<ActorTargetData>(actor.Actor);
|
||||
if (!EntityManager.Exists(actorTarget.Target) || !EntityManager.HasComponent<Rigidbody2D>(actorTarget.Target))
|
||||
{
|
||||
active.Fail = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (EntityManager.HasComponent<ActorData>(actor.Actor))
|
||||
{
|
||||
var actorData = EntityManager.GetComponentData<ActorData>(actor.Actor);
|
||||
if (actorData.Grounded)
|
||||
{
|
||||
var target = EntityManager.GetComponentData<ActorTargetData>(actor.Actor);
|
||||
if (EntityManager.Exists(target.Target) &&
|
||||
EntityManager.TryGetComponentObject<Rigidbody2D>(target.Target, out var targetRigidBody))
|
||||
{
|
||||
var actorRigidBody = EntityManager.GetComponentObject<Rigidbody2D>(actor.Actor);
|
||||
|
||||
var actorCenter = actorRigidBody.position;
|
||||
var targetPosition = targetRigidBody.position;
|
||||
if (EntityManager.HasComponent<AimCenterData>(target.Target))
|
||||
{
|
||||
var aimCenter = EntityManager.GetComponentData<AimCenterData>(target.Target);
|
||||
actorCenter += aimCenter.Offset;
|
||||
targetPosition += aimCenter.Offset;
|
||||
}
|
||||
|
||||
var cast = Physics2D.Raycast(
|
||||
actorCenter,
|
||||
(targetPosition - actorCenter).normalized,
|
||||
float.MaxValue,
|
||||
LayerMask.GetMask("Player", "Ground"));
|
||||
var canSee = targetRigidBody == cast.rigidbody;
|
||||
|
||||
if (EntityManager.TryGetComponentData<NavigationAgentData>(actor.Actor, out var agent))
|
||||
{
|
||||
if (!canSee)
|
||||
{
|
||||
agent.destination = targetRigidBody.position;
|
||||
}
|
||||
else if (agent.Grounded)
|
||||
{
|
||||
agent.destination = null;
|
||||
}
|
||||
EntityManager.SetComponentData(actor.Actor, agent);
|
||||
}
|
||||
|
||||
if (targetRigidBody == cast.rigidbody)
|
||||
{
|
||||
//can see target
|
||||
var npc = EntityManager.GetComponentData<ActorNpcData>(actor.Actor);
|
||||
npc.AttackCooldown += settings.AttackCooldown;
|
||||
active.Done = true;
|
||||
actorData.Aim = cast.point;
|
||||
EntityManager.SetComponentData(actor.Actor, actorData);
|
||||
EntityManager.SetComponentData(actor.Actor, npc);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
active.Fail = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool OnValidate(
|
||||
ref SearchTargetGoapAction action,
|
||||
ref GoapSharedAction goapSharedAction,
|
||||
ref GoapAction goapAction,
|
||||
GoapActionActor actor)
|
||||
{
|
||||
if (!EntityManager.HasComponents<ActorTargetData, Rigidbody2D, ActorNpcData>(actor.Actor))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/AI/SearchTargetGoapAction.cs.meta
Normal file
3
Assets/Scripts/AI/SearchTargetGoapAction.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dbae26fdf33d47658f68d527d1912a7f
|
||||
timeCreated: 1533316096
|
||||
Loading…
Add table
Add a link
Reference in a new issue