using DefaultNamespace; using Unity.Entities; using UnityEngine; namespace AI { public struct AttackTargetMeleeGoapAction : IComponentData { } public class AttackPlayerMeleeGoapActionSystem : GoapActionSystem { 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(actorEntity); var melee = EntityManager.GetComponentData(actorEntity); var meleeShared = EntityManager.GetSharedComponentData(actor.Actor); var target = EntityManager.GetComponentData(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(actor.Actor)) { return false; } var melee = EntityManager.GetComponentData(actor.Actor); if (melee.MeleeTimer > 0) { return false; } var meleeShared = EntityManager.GetSharedComponentData(actor.Actor); var actorRigidBody = EntityManager.GetComponentObject(actor.Actor); var target = EntityManager.GetComponentData(actor.Actor); if (EntityManager.TryGetComponentObject(target.Target, out var targetRigidBody)) { var d = Vector2.Distance(actorRigidBody.position, targetRigidBody.position); if (d < meleeShared.Range) { return true; } } return false; } } }