forked from whztt07/UnityGameplayAbilitySystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericAbilitySystem.cs
More file actions
185 lines (161 loc) · 9.76 KB
/
Copy pathGenericAbilitySystem.cs
File metadata and controls
185 lines (161 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
public class GenericAbilitySystem : JobComponentSystem {
// void ApplyGameplayEffects(int index, EntityCommandBuffer.Concurrent Ecb, Entity Source, Entity Target, AttributesComponent attributesComponent)
BeginSimulationEntityCommandBufferSystem m_EntityCommandBufferSystem;
List<NativeArray<EGameplayEffect>> abilityCooldownEffects = new List<NativeArray<EGameplayEffect>>();
EntityQuery m_CooldownEffects;
EntityQuery m_ActiveAbilities;
List<IAbilityBehaviour> abilities = new List<IAbilityBehaviour>();
List<EntityQuery> m_AbilityBeginCastQuery = new List<EntityQuery>();
List<EntityQuery> m_UpdateCooldownsJobQuery = new List<EntityQuery>();
List<EntityQuery> m_UpdateAbilityAvailableQuery = new List<EntityQuery>();
List<EntityQuery> m_GenericCheckResourceForAbilityJobQuery = new List<EntityQuery>();
List<EntityQuery> m_GenericCheckAbilityGrantedJobQuery = new List<EntityQuery>();
protected override void OnCreate() {
base.OnCreate();
m_EntityCommandBufferSystem = World.GetOrCreateSystem<BeginSimulationEntityCommandBufferSystem>();
// Collect list of all types that implement IAbility and store reference to methods
var abilityTypes = World.Active.EntityManager.GetAssignableComponentTypes(typeof(IAbilityBehaviour)).ToArray();
// Iterate over all objects that implement the IAbility interface
for (var i = 0; i < abilityTypes.Length; i++) {
IAbilityBehaviour ability = (IAbilityBehaviour)Activator.CreateInstance(abilityTypes[i]);
abilities.Add(ability);
// Create the FunctionPointer hashmaps
var cooldownEffects = ability.CooldownEffects;
var nativeArray = new NativeArray<EGameplayEffect>(cooldownEffects, Allocator.Persistent);
var entityQueryDescContainer = ability.EntityQueries;
m_AbilityBeginCastQuery.Add(GetEntityQuery(entityQueryDescContainer.BeginAbilityCastJobQueryDesc));
m_UpdateCooldownsJobQuery.Add(GetEntityQuery(entityQueryDescContainer.UpdateCooldownsJobQueryDesc));
m_UpdateAbilityAvailableQuery.Add(GetEntityQuery(entityQueryDescContainer.CheckAbilityAvailableJobQueryDesc_UpdateAvailability));
m_GenericCheckResourceForAbilityJobQuery.Add(GetEntityQuery(entityQueryDescContainer.CheckAbilityAvailableJobQueryDesc_CheckResources));
m_GenericCheckAbilityGrantedJobQuery.Add(GetEntityQuery(entityQueryDescContainer.CheckAbilityGrantedJobQueryDesc));
abilityCooldownEffects.Add(nativeArray);
}
// Create EntityQueries for jobs
m_CooldownEffects = GetEntityQuery(new EntityQueryDesc
{
All = new[] { ComponentType.ReadOnly<CooldownEffectComponent>(), ComponentType.ReadOnly<GameplayEffectDurationComponent>(), ComponentType.ReadOnly<AttributeModificationComponent>() },
});
m_ActiveAbilities = GetEntityQuery(new EntityQueryDesc
{
All = new[] { ComponentType.ReadOnly<AbilityComponent>(), ComponentType.ReadOnly<AbilityStateComponent>(), ComponentType.ReadOnly<AbilitySourceTargetComponent>() },
});
}
[BurstCompile]
[RequireComponentTag(typeof(AttributeModificationComponent))]
public struct GatherCooldownEffectsJob : IJobForEach<CooldownEffectComponent, GameplayEffectDurationComponent> {
[WriteOnly] public NativeMultiHashMap<int, CooldownForCasterComponent>.ParallelWriter activeCooldownEffects;
public void Execute([ReadOnly] ref CooldownEffectComponent cdEffect, [ReadOnly] ref GameplayEffectDurationComponent geDuration) {
var caster = cdEffect.Caster;
var effect = geDuration.Effect;
var remaining = new CooldownForCasterComponent
{
Duration = geDuration.Duration,
TimeRemaining = geDuration.TimeRemaining,
Caster = caster
};
activeCooldownEffects.Add((int)effect, remaining);
}
}
public struct FilterCooldownEffectsForAbilityJob : IJob {
[ReadOnly] public NativeMultiHashMap<int, CooldownForCasterComponent> activeCooldownEffects;
[ReadOnly] public NativeArray<EGameplayEffect> validCooldownEffects;
public NativeHashMap<Entity, GrantedAbilityCooldownComponent> cooldownRemainingForAbility; // int refers to ability enum
public void Execute() {
// Get effects matching ability from main NMHP
for (var i = 0; i < validCooldownEffects.Length; i++) { // for each cooldown effect applicable to this ability
var effect = validCooldownEffects[i];
using (var cooldownEffectEnumerator = activeCooldownEffects.GetValuesForKey((int)effect)) {
while (cooldownEffectEnumerator.MoveNext()) {
var cooldownEffect = cooldownEffectEnumerator.Current;
if (!cooldownRemainingForAbility.TryGetValue(cooldownEffect.Caster, out var effectDuration)) {
cooldownRemainingForAbility.TryAdd(cooldownEffect.Caster, new GrantedAbilityCooldownComponent
{
Duration = cooldownEffect.Duration,
TimeRemaining = cooldownEffect.TimeRemaining
});
continue;
}
effectDuration.Duration = math.select(effectDuration.Duration, cooldownEffect.Duration, effectDuration.TimeRemaining < cooldownEffect.TimeRemaining);
effectDuration.TimeRemaining = math.select(effectDuration.TimeRemaining, cooldownEffect.TimeRemaining, effectDuration.TimeRemaining < cooldownEffect.TimeRemaining);
cooldownRemainingForAbility[cooldownEffect.Caster] = effectDuration;
}
}
}
}
}
[RequireComponentTag(typeof(AbilityComponent), typeof(AbilitySourceTargetComponent))]
[BurstCompile]
public struct UpdateAbilitiesStatusJob : IJobForEachWithEntity<AbilityStateComponent> {
public EntityCommandBuffer.Concurrent EntityCommandBuffer;
public void Execute(Entity entity, int index, ref AbilityStateComponent state) {
if (state.State == EAbilityState.TryActivate) {
state.State = EAbilityState.CheckCooldown;
}
if (state.State == EAbilityState.Failed) {
EntityCommandBuffer.DestroyEntity(index, entity);
}
if (state.State == EAbilityState.Completed) {
EntityCommandBuffer.DestroyEntity(index, entity);
}
}
}
[BurstCompile]
public struct CooldownGameplayEffects : IJobForEach<CooldownEffectComponent, GameplayEffectDurationComponent> {
public void Execute(ref CooldownEffectComponent cooldown, ref GameplayEffectDurationComponent duration) {
throw new NotImplementedException();
}
}
[BurstCompile]
protected override JobHandle OnUpdate(JobHandle inputDeps) {
var commandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
var m_CooldownEffectsCount = m_CooldownEffects.CalculateEntityCount();
var activeCooldownEffects = new NativeMultiHashMap<int, CooldownForCasterComponent>(m_CooldownEffectsCount, Allocator.TempJob);
ComponentDataFromEntity<AttributesComponent> attributeComponents = GetComponentDataFromEntity<AttributesComponent>();
var GCE = new GatherCooldownEffectsJob
{
activeCooldownEffects = activeCooldownEffects.AsParallelWriter()
}.Schedule(this, inputDeps);
var UAS = new UpdateAbilitiesStatusJob
{
EntityCommandBuffer = commandBuffer
}.Schedule(m_ActiveAbilities, inputDeps);
inputDeps = JobHandle.CombineDependencies(GCE, UAS);
// The rest of the functionality is defined by the individual abilities
for (var i = 0; i < abilities.Count; i++) {
// Get all cooldown effects applicable to this ability
var effectRemainingForAbility = new NativeHashMap<Entity, GrantedAbilityCooldownComponent>(m_CooldownEffectsCount, Allocator.TempJob);
var abilityAvailable = new NativeHashMap<Entity, bool>(m_CooldownEffectsCount, Allocator.TempJob);
abilityAvailable.Dispose(inputDeps);
inputDeps = new FilterCooldownEffectsForAbilityJob
{
activeCooldownEffects = activeCooldownEffects,
validCooldownEffects = abilityCooldownEffects[i],
cooldownRemainingForAbility = effectRemainingForAbility
}.Schedule(inputDeps);
var abilityJob = abilities[i].AbilityJobs;
var entityQueries = abilities[i].EntityQueries;
inputDeps = abilityJob.CheckAbilityAvailableJob(m_UpdateAbilityAvailableQuery[i], m_GenericCheckResourceForAbilityJobQuery[i], inputDeps, attributeComponents, effectRemainingForAbility);
inputDeps = abilityJob.UpdateCooldownsJob(m_UpdateCooldownsJobQuery[i], inputDeps, effectRemainingForAbility);
inputDeps = abilityJob.BeginAbilityCastJob(m_AbilityBeginCastQuery[i], inputDeps, commandBuffer, attributeComponents, Time.time);
effectRemainingForAbility.Dispose(inputDeps);
}
m_EntityCommandBufferSystem.AddJobHandleForProducer(inputDeps);
// Initialise cooldown data
activeCooldownEffects.Dispose(inputDeps);
return inputDeps;
}
protected override void OnDestroy() {
for (var i = 0; i < abilityCooldownEffects.Count; i++) {
abilityCooldownEffects[i].Dispose();
}
base.OnDestroy();
}
}