|
using CyberSnake.ECS.AI.EnemyProperties; |
|
using Unity.Burst; |
|
using Unity.Collections; |
|
using Unity.Entities; |
|
using Unity.Jobs; |
|
|
|
namespace CyberSnake.ECS.AI.Armadillo.FSM |
|
{ |
|
// --------states------- |
|
public struct IdleState : IComponentData, IEnableableComponent {} |
|
public struct MoveTowardsPlayerState : IComponentData, IEnableableComponent {} |
|
public struct BeginAttackState : IComponentData, IEnableableComponent {} |
|
public struct ApplyDamageState : IComponentData, IEnableableComponent {} |
|
public struct EndAttackState : IComponentData, IEnableableComponent {} |
|
|
|
// ---------guards------ |
|
public struct AttackOnCooldownGuard : IComponentData{} |
|
public struct AttackPreparationDoneGuard : IComponentData{} |
|
public struct AttackFinishedGuard : IComponentData{} |
|
|
|
// --------main system----- |
|
public partial struct ArmadilloFSMSystem : ISystem |
|
{ |
|
public void OnCreate(ref SystemState state) |
|
{ |
|
state.RequireForUpdate<EndSimulationEntityCommandBufferSystem.Singleton>(); |
|
} |
|
|
|
public void OnUpdate(ref SystemState state) |
|
{ |
|
var lookupIdleState = SystemAPI.GetComponentLookup<IdleState>(); |
|
var lookupMoveTowardsPlayerState = SystemAPI.GetComponentLookup<MoveTowardsPlayerState>(); |
|
var lookupBeginAttackState = SystemAPI.GetComponentLookup<BeginAttackState>(); |
|
var lookupApplyDamageState = SystemAPI.GetComponentLookup<ApplyDamageState>(); |
|
var lookupEndAttackState = SystemAPI.GetComponentLookup<EndAttackState>(); |
|
|
|
var IdleState_to_MoveTowardsPlayerState = new IdleStateToMoveTowardsPlayerState() |
|
{ |
|
LookupIdleState = lookupIdleState, |
|
LookupMoveTowardsPlayerState = lookupMoveTowardsPlayerState |
|
}.ScheduleParallel(default(JobHandle)); |
|
var IdleState_to_BeginAttackState = new IdleStateToBeginAttackState() |
|
{ |
|
LookupIdleState = lookupIdleState, |
|
LookupBeginAttackState = lookupBeginAttackState |
|
}.ScheduleParallel(IdleState_to_MoveTowardsPlayerState); |
|
var MoveTowardsPlayerState_to_IdleState = new MoveTowardsPlayerStateToIdleState() |
|
{ |
|
LookupMoveTowardsPlayerState = lookupMoveTowardsPlayerState, |
|
LookupIdleState = lookupIdleState |
|
}.ScheduleParallel(IdleState_to_BeginAttackState); |
|
var MoveTowardsPlayerState_to_BeginAttackState = new MoveTowardsPlayerStateToBeginAttackState() |
|
{ |
|
LookupMoveTowardsPlayerState = lookupMoveTowardsPlayerState, |
|
LookupBeginAttackState = lookupBeginAttackState |
|
}.ScheduleParallel(MoveTowardsPlayerState_to_IdleState); |
|
var BeginAttackState_to_ApplyDamageState = new BeginAttackStateToApplyDamageState() |
|
{ |
|
LookupBeginAttackState = lookupBeginAttackState, |
|
LookupApplyDamageState = lookupApplyDamageState |
|
}.ScheduleParallel(MoveTowardsPlayerState_to_BeginAttackState); |
|
var ApplyDamageState_to_EndAttackState = new ApplyDamageStateToEndAttackState() |
|
{ |
|
LookupApplyDamageState = lookupApplyDamageState, |
|
LookupEndAttackState = lookupEndAttackState |
|
}.ScheduleParallel(BeginAttackState_to_ApplyDamageState); |
|
var EndAttackState_to_IdleState = new EndAttackStateToIdleState() |
|
{ |
|
LookupEndAttackState = lookupEndAttackState, |
|
LookupIdleState = lookupIdleState |
|
}.ScheduleParallel(ApplyDamageState_to_EndAttackState); |
|
var EndAttackState_to_MoveTowardsPlayerState = new EndAttackStateToMoveTowardsPlayerState() |
|
{ |
|
LookupEndAttackState = lookupEndAttackState, |
|
LookupMoveTowardsPlayerState = lookupMoveTowardsPlayerState |
|
}.ScheduleParallel(EndAttackState_to_IdleState); |
|
var EndAttackState_to_BeginAttackState = new EndAttackStateToBeginAttackState() |
|
{ |
|
LookupEndAttackState = lookupEndAttackState, |
|
LookupBeginAttackState = lookupBeginAttackState |
|
}.ScheduleParallel(EndAttackState_to_MoveTowardsPlayerState); |
|
|
|
EndAttackState_to_BeginAttackState.Complete(); |
|
} |
|
|
|
|
|
// -------jobs--------- |
|
[BurstCompile] |
|
[WithAll(typeof(IdleState))] |
|
[WithNone(typeof(TargetInAttackRange))] |
|
public partial struct IdleStateToMoveTowardsPlayerState : IJobEntity |
|
{ |
|
[NativeDisableParallelForRestriction] public ComponentLookup<IdleState> LookupIdleState; |
|
[NativeDisableParallelForRestriction] public ComponentLookup<MoveTowardsPlayerState> LookupMoveTowardsPlayerState; |
|
|
|
void Execute(Entity entity) |
|
{ |
|
UnityEngine.Debug.Log("new state: IdleStateToMoveTowardsPlayerState"); |
|
LookupIdleState.SetComponentEnabled(entity, false); |
|
LookupMoveTowardsPlayerState.SetComponentEnabled(entity, true); |
|
} |
|
} |
|
|
|
[BurstCompile] |
|
[WithAll(typeof(IdleState),typeof(TargetInAttackRange))] |
|
[WithNone(typeof(AttackOnCooldownGuard))] |
|
public partial struct IdleStateToBeginAttackState : IJobEntity |
|
{ |
|
[NativeDisableParallelForRestriction] public ComponentLookup<IdleState> LookupIdleState; |
|
[NativeDisableParallelForRestriction] public ComponentLookup<BeginAttackState> LookupBeginAttackState; |
|
|
|
void Execute(Entity entity) |
|
{ |
|
UnityEngine.Debug.Log("new state: IdleStateToBeginAttackState"); |
|
LookupIdleState.SetComponentEnabled(entity, false); |
|
LookupBeginAttackState.SetComponentEnabled(entity, true); |
|
} |
|
} |
|
|
|
[BurstCompile] |
|
[WithAll(typeof(MoveTowardsPlayerState),typeof(TargetInAttackRange),typeof(AttackOnCooldownGuard))] |
|
public partial struct MoveTowardsPlayerStateToIdleState : IJobEntity |
|
{ |
|
[NativeDisableParallelForRestriction] public ComponentLookup<MoveTowardsPlayerState> LookupMoveTowardsPlayerState; |
|
[NativeDisableParallelForRestriction] public ComponentLookup<IdleState> LookupIdleState; |
|
|
|
void Execute(Entity entity) |
|
{ |
|
UnityEngine.Debug.Log("new state: MoveTowardsPlayerStateToIdleState"); |
|
LookupMoveTowardsPlayerState.SetComponentEnabled(entity, false); |
|
LookupIdleState.SetComponentEnabled(entity, true); |
|
} |
|
} |
|
|
|
[BurstCompile] |
|
[WithAll(typeof(MoveTowardsPlayerState),typeof(TargetInAttackRange))] |
|
[WithNone(typeof(AttackOnCooldownGuard))] |
|
public partial struct MoveTowardsPlayerStateToBeginAttackState : IJobEntity |
|
{ |
|
[NativeDisableParallelForRestriction] public ComponentLookup<MoveTowardsPlayerState> LookupMoveTowardsPlayerState; |
|
[NativeDisableParallelForRestriction] public ComponentLookup<BeginAttackState> LookupBeginAttackState; |
|
|
|
void Execute(Entity entity) |
|
{ |
|
UnityEngine.Debug.Log("new state: MoveTowardsPlayerStateToBeginAttackState"); |
|
LookupMoveTowardsPlayerState.SetComponentEnabled(entity, false); |
|
LookupBeginAttackState.SetComponentEnabled(entity, true); |
|
} |
|
} |
|
|
|
[BurstCompile] |
|
[WithAll(typeof(BeginAttackState),typeof(AttackPreparationDoneGuard))] |
|
public partial struct BeginAttackStateToApplyDamageState : IJobEntity |
|
{ |
|
[NativeDisableParallelForRestriction] public ComponentLookup<BeginAttackState> LookupBeginAttackState; |
|
[NativeDisableParallelForRestriction] public ComponentLookup<ApplyDamageState> LookupApplyDamageState; |
|
|
|
void Execute(Entity entity) |
|
{ |
|
UnityEngine.Debug.Log("new state: BeginAttackStateToApplyDamageState"); |
|
LookupBeginAttackState.SetComponentEnabled(entity, false); |
|
LookupApplyDamageState.SetComponentEnabled(entity, true); |
|
} |
|
} |
|
|
|
[BurstCompile] |
|
[WithAll(typeof(ApplyDamageState))] |
|
public partial struct ApplyDamageStateToEndAttackState : IJobEntity |
|
{ |
|
[NativeDisableParallelForRestriction] public ComponentLookup<ApplyDamageState> LookupApplyDamageState; |
|
[NativeDisableParallelForRestriction] public ComponentLookup<EndAttackState> LookupEndAttackState; |
|
|
|
void Execute(Entity entity) |
|
{ |
|
UnityEngine.Debug.Log("new state: ApplyDamageStateToEndAttackState"); |
|
LookupApplyDamageState.SetComponentEnabled(entity, false); |
|
LookupEndAttackState.SetComponentEnabled(entity, true); |
|
} |
|
} |
|
|
|
[BurstCompile] |
|
[WithAll(typeof(EndAttackState),typeof(AttackFinishedGuard),typeof(TargetInAttackRange),typeof(AttackOnCooldownGuard))] |
|
public partial struct EndAttackStateToIdleState : IJobEntity |
|
{ |
|
[NativeDisableParallelForRestriction] public ComponentLookup<EndAttackState> LookupEndAttackState; |
|
[NativeDisableParallelForRestriction] public ComponentLookup<IdleState> LookupIdleState; |
|
|
|
void Execute(Entity entity) |
|
{ |
|
UnityEngine.Debug.Log("new state: EndAttackStateToIdleState"); |
|
LookupEndAttackState.SetComponentEnabled(entity, false); |
|
LookupIdleState.SetComponentEnabled(entity, true); |
|
} |
|
} |
|
|
|
[BurstCompile] |
|
[WithAll(typeof(EndAttackState),typeof(AttackFinishedGuard))] |
|
[WithNone(typeof(TargetInAttackRange))] |
|
public partial struct EndAttackStateToMoveTowardsPlayerState : IJobEntity |
|
{ |
|
[NativeDisableParallelForRestriction] public ComponentLookup<EndAttackState> LookupEndAttackState; |
|
[NativeDisableParallelForRestriction] public ComponentLookup<MoveTowardsPlayerState> LookupMoveTowardsPlayerState; |
|
|
|
void Execute(Entity entity) |
|
{ |
|
UnityEngine.Debug.Log("new state: EndAttackStateToMoveTowardsPlayerState"); |
|
LookupEndAttackState.SetComponentEnabled(entity, false); |
|
LookupMoveTowardsPlayerState.SetComponentEnabled(entity, true); |
|
} |
|
} |
|
|
|
[BurstCompile] |
|
[WithAll(typeof(EndAttackState),typeof(AttackFinishedGuard),typeof(TargetInAttackRange))] |
|
[WithNone(typeof(AttackOnCooldownGuard))] |
|
public partial struct EndAttackStateToBeginAttackState : IJobEntity |
|
{ |
|
[NativeDisableParallelForRestriction] public ComponentLookup<EndAttackState> LookupEndAttackState; |
|
[NativeDisableParallelForRestriction] public ComponentLookup<BeginAttackState> LookupBeginAttackState; |
|
|
|
void Execute(Entity entity) |
|
{ |
|
UnityEngine.Debug.Log("new state: EndAttackStateToBeginAttackState"); |
|
LookupEndAttackState.SetComponentEnabled(entity, false); |
|
LookupBeginAttackState.SetComponentEnabled(entity, true); |
|
} |
|
} |
|
|
|
|
|
// --------authroing util------- |
|
// add all states to the entity in disabled state except first one |
|
public static void AddStatesTo(IBaker baker, Entity entity) |
|
{ |
|
baker.AddComponent<IdleState>(entity); |
|
baker.SetComponentEnabled<IdleState>(entity, true); |
|
baker.AddComponent<MoveTowardsPlayerState>(entity); |
|
baker.SetComponentEnabled<MoveTowardsPlayerState>(entity, false); |
|
baker.AddComponent<BeginAttackState>(entity); |
|
baker.SetComponentEnabled<BeginAttackState>(entity, false); |
|
baker.AddComponent<ApplyDamageState>(entity); |
|
baker.SetComponentEnabled<ApplyDamageState>(entity, false); |
|
baker.AddComponent<EndAttackState>(entity); |
|
baker.SetComponentEnabled<EndAttackState>(entity, false); |
|
} |
|
} |
|
} |