private void RegenHealth(int i,
                                 HealthComponent.Component healthComponent,
                                 HealthRegenComponent.Component regenComponent,
                                 HealthRegenData regenData)
        {
            if (healthComponent.Health >= healthComponent.MaxHealth)
            {
                return;
            }

            regenData.NextRegenTimer -= Time.deltaTime;

            if (regenData.NextRegenTimer <= 0)
            {
                regenData.NextRegenTimer += regenComponent.RegenInterval;

                // Send command to regen entity.
                var commandSender       = toRegen.ModifyHealthCommandSenders[i];
                var modifyHealthRequest = new HealthComponent.ModifyHealth.Request(
                    toRegen.EntityId[i].EntityId,
                    new HealthModifier()
                {
                    Amount = regenComponent.RegenAmount
                });
                commandSender.RequestsToSend.Add(modifyHealthRequest);
            }

            toRegen.RegenData[i] = regenData;
        }
Esempio n. 2
0
        public void SendModifyHealthCommand(HealthComponent.ModifyHealth.Request request, Action <Improbable.Gdk.Health.HealthComponent.ModifyHealth.ReceivedResponse> callback = null)
        {
            int validCallbackEpoch = callbackEpoch;
            var requestId          = commandSender.SendCommand(request, entity);

            if (callback != null)
            {
                Action <Improbable.Gdk.Health.HealthComponent.ModifyHealth.ReceivedResponse> wrappedCallback = response =>
                {
                    if (!this.IsValid || validCallbackEpoch != this.callbackEpoch)
                    {
                        return;
                    }

                    callback(response);
                };
                callbackSystem.RegisterCommandResponseCallback(requestId, wrappedCallback);
            }
        }
        protected override void OnUpdate()
        {
            // Add the HealthRegenData if you don't currently have it.
            for (var i = 0; i < needData.Length; i++)
            {
                var healthRegenComponent = needData.HealthRegenComponents[i];

                var regenData = new HealthRegenData();

                if (healthRegenComponent.DamagedRecently)
                {
                    regenData.DamagedRecentlyTimer = healthRegenComponent.RegenCooldownTimer;
                    regenData.NextSpatialSyncTimer = healthRegenComponent.CooldownSyncInterval;
                }

                PostUpdateCommands.AddComponent(needData.Entities[i], regenData);
            }

            // When the HealthComponent takes a damaging event, reset the DamagedRecently timer.
            for (var i = 0; i < takingDamage.Length; i++)
            {
                var healthModifiedEvents = takingDamage.HealthModifiedEvents[i];
                var damagedRecently      = false;

                foreach (var modifiedEvent in takingDamage.HealthModifiedEvents[i].Events)
                {
                    var modifier = modifiedEvent.Modifier;
                    if (modifier.Amount < 0)
                    {
                        damagedRecently = true;
                        break;
                    }
                }
                if (!damagedRecently)
                {
                    continue;
                }

                var regenComponent = takingDamage.HealthRegenComponents[i];
                var regenData      = takingDamage.RegenData[i];

                regenComponent.DamagedRecently    = true;
                regenComponent.RegenCooldownTimer = regenComponent.RegenPauseTime;

                regenData.DamagedRecentlyTimer = regenComponent.RegenPauseTime;
                regenData.NextSpatialSyncTimer = regenComponent.CooldownSyncInterval;

                takingDamage.HealthRegenComponents[i] = regenComponent;
                takingDamage.RegenData[i]             = regenData;
            }

            // Count down the timers, and update the HealthComponent accordingly.
            for (var i = 0; i < toRegen.Length; i++)
            {
                var healthComponent = toRegen.HealthComponents[i];
                var regenComponent  = toRegen.HealthRegenComponents[i];

                var regenData = toRegen.RegenData[i];

                // Don't regen if dead.
                if (healthComponent.Health == 0)
                {
                    continue;
                }

                // If damaged recently, tick down the timer.
                if (regenComponent.DamagedRecently)
                {
                    regenData.DamagedRecentlyTimer -= Time.deltaTime;

                    if (regenData.DamagedRecentlyTimer <= 0)
                    {
                        regenData.DamagedRecentlyTimer    = 0;
                        regenComponent.DamagedRecently    = false;
                        regenComponent.RegenCooldownTimer = 0;
                        toRegen.HealthRegenComponents[i]  = regenComponent;
                    }
                    else
                    {
                        // Send a spatial update once every CooldownSyncInterval.
                        regenData.NextSpatialSyncTimer -= Time.deltaTime;
                        if (regenData.NextSpatialSyncTimer <= 0)
                        {
                            regenData.NextSpatialSyncTimer   += regenComponent.CooldownSyncInterval;
                            regenComponent.RegenCooldownTimer = regenData.DamagedRecentlyTimer;
                            toRegen.HealthRegenComponents[i]  = regenComponent;
                        }
                    }

                    toRegen.RegenData[i] = regenData;

                    return;
                }

                // If not damaged recently, and not already fully healed, regen.
                if (healthComponent.Health < healthComponent.MaxHealth)
                {
                    regenData.NextRegenTimer -= Time.deltaTime;
                    if (regenData.NextRegenTimer <= 0)
                    {
                        regenData.NextRegenTimer += regenComponent.RegenInterval;

                        // Send command to regen entity.
                        var commandSender       = toRegen.ModifyHealthCommandSenders[i];
                        var modifyHealthRequest = new HealthComponent.ModifyHealth.Request(
                            toRegen.EntityId[i].EntityId,
                            new HealthModifier()
                        {
                            Amount = regenComponent.RegenAmount
                        });
                        commandSender.RequestsToSend.Add(modifyHealthRequest);
                    }

                    toRegen.RegenData[i] = regenData;
                }
            }
        }
Esempio n. 4
0
        public void SendModifyHealthCommand(EntityId targetEntityId, global::Improbable.Gdk.Health.HealthModifier request, Action <Improbable.Gdk.Health.HealthComponent.ModifyHealth.ReceivedResponse> callback = null)
        {
            var commandRequest = new HealthComponent.ModifyHealth.Request(targetEntityId, request);

            SendModifyHealthCommand(commandRequest, callback);
        }