protected override JobHandle OnUpdate(JobHandle inputDeps)
            {
                var CooldownDurationComponents = GetComponentDataFromEntity <GameplayEffectDurationComponent>();
                var Cooldown1TagComponents     = GetComponentDataFromEntity <GlobalCooldownGameplayEffectComponent>();
                var Cooldown2TagComponents     = GetComponentDataFromEntity <Fire1CooldownGameplayEffectComponent>();
                var AbilityTagComponents       = GetComponentDataFromEntity <Fire1AbilityTag>();
                var AbilityCooldownComponents  = GetComponentDataFromEntity <AbilityCooldownComponent>();

                // Set the cooldown of the ability for each actor
                inputDeps = Entities.ForEach((Entity entity, int entityInQueryIndex, in DynamicBuffer <GameplayEffectBufferElement> gameplayEffectBuffer, in DynamicBuffer <GrantedAbilityBufferElement> grantedAbilityBuffer) => {
                    // The outer foreach loops through all actors
                    var abilityCooldownDuration = new GameplayEffectDurationComponent();
                    for (var i = 0; i < gameplayEffectBuffer.Length; i++)
                    {
                        // The inner loop is iterating through all active GE on a single actor
                        var gameplayEffectEntity = gameplayEffectBuffer[i].Value;
                        // Check if entity has any of the cooldown components
                        int cooldownDurationComponentThatHas = 0;
                        cooldownDurationComponentThatHas     = math.select(1, cooldownDurationComponentThatHas, Cooldown1TagComponents.HasComponent(gameplayEffectEntity));
                        cooldownDurationComponentThatHas     = math.select(2, cooldownDurationComponentThatHas, Cooldown2TagComponents.HasComponent(gameplayEffectEntity));
                        if (cooldownDurationComponentThatHas > 0)
                        {
                            if (!CooldownDurationComponents.HasComponent(gameplayEffectEntity))
                            {
                                continue;
                            }
                            var durationComponent = CooldownDurationComponents[gameplayEffectEntity];

                            // Cooldown selection logic:
                            // 1. Effect with the longest remaining duration is always the ability's cooldown remaining duration
                            // 2. If two effects have the same remaining duration, then the effect with the longest period is the ability's cooldown duration
                            bool thisEffectIsLongest = durationComponent.Value > abilityCooldownDuration.Value;
                            abilityCooldownDuration.Value.RemainingTime   = math.select(abilityCooldownDuration.Value.RemainingTime, durationComponent.Value.RemainingTime, thisEffectIsLongest);
                            abilityCooldownDuration.Value.NominalDuration = math.select(abilityCooldownDuration.Value.NominalDuration, durationComponent.Value.NominalDuration, thisEffectIsLongest);
                            abilityCooldownDuration.Value.WorldStartTime  = math.select(abilityCooldownDuration.Value.WorldStartTime, durationComponent.Value.WorldStartTime, thisEffectIsLongest);
                        }
                    }
                    for (var i = 0; i < grantedAbilityBuffer.Length; i++)
                    {
                        if (AbilityTagComponents.HasComponent(grantedAbilityBuffer[i]))
                        {
                            AbilityCooldownComponents[grantedAbilityBuffer[i]] = new AbilityCooldownComponent
                            {
                                Value = abilityCooldownDuration
                            };
                            break;
                        }
                    }
                })
Esempio n. 2
0
            protected override JobHandle OnUpdate(JobHandle inputDeps)
            {
                var Cooldown1DurationComponents = GetComponentDataFromEntity <GameplayEffectDurationComponent>();
                var Cooldown1TagComponents      = GetComponentDataFromEntity <GlobalCooldownGameplayEffectComponent>();
                var AbilityTagComponents        = GetComponentDataFromEntity <DefaultAttackAbilityTag>();
                var AbilityCooldownComponents   = GetComponentDataFromEntity <AbilityCooldownComponent>();

                // NativeHashMap<Entity, GameplayEffectDurationComponent> AbilityCooldownDurations = new NativeHashMap<Entity, GameplayEffectDurationComponent>(m_GEQuery.CalculateEntityCount(), Allocator.TempJob);
                inputDeps = Entities.ForEach((Entity entity, int entityInQueryIndex, in DynamicBuffer <GameplayEffectBufferElement> gameplayEffectBuffer, in DynamicBuffer <GrantedAbilityBufferElement> grantedAbilityBuffer) => {
                    // The outer foreach loops through all actors
                    var abilityCooldownDuration = new GameplayEffectDurationComponent();
                    for (var i = 0; i < gameplayEffectBuffer.Length; i++)
                    {
                        // The inner loop is iterating through all active GE on a single actor
                        var gameplayEffectEntity = gameplayEffectBuffer[i].Value;
                        // Check if entity has any of the cooldown components
                        if (Cooldown1DurationComponents.HasComponent(gameplayEffectEntity) && Cooldown1TagComponents.HasComponent(gameplayEffectEntity))
                        {
                            var durationComponent = Cooldown1DurationComponents[gameplayEffectEntity];

                            bool thisEffectIsLongest = durationComponent.Value > abilityCooldownDuration.Value;
                            abilityCooldownDuration.Value.RemainingTime   = math.select(abilityCooldownDuration.Value.RemainingTime, durationComponent.Value.RemainingTime, thisEffectIsLongest);
                            abilityCooldownDuration.Value.NominalDuration = math.select(abilityCooldownDuration.Value.NominalDuration, durationComponent.Value.NominalDuration, thisEffectIsLongest);
                            abilityCooldownDuration.Value.WorldStartTime  = math.select(abilityCooldownDuration.Value.WorldStartTime, durationComponent.Value.WorldStartTime, thisEffectIsLongest);
                        }
                    }
                    for (var i = 0; i < grantedAbilityBuffer.Length; i++)
                    {
                        if (AbilityTagComponents.HasComponent(grantedAbilityBuffer[i]))
                        {
                            AbilityCooldownComponents[grantedAbilityBuffer[i]] = new AbilityCooldownComponent
                            {
                                Value = abilityCooldownDuration
                            };
                            break;
                        }
                    }
                })