internal void UpdateVisuals(PowercellData data)
        {
            var charge = data.GetCharge() < 1 ? 0f : data.GetCapacity();

            float percent = charge / data.GetCapacity();

            QuickLogger.Debug($"Percent: {percent}");

            if (_batteryPercent != null)
            {
                _batteryPercent.text = ((data.GetCharge() < 0f) ? Language.main.Get("ChargerSlotEmpty") : $"{Mathf.CeilToInt(percent * 100)}%");
            }

            if (_batteryFill != null)
            {
                if (data.GetCharge() >= 0f)
                {
                    Color value = (percent >= 0.5f) ? Color.Lerp(this.colorHalf, this.colorFull, 2f * percent - 1f) : Color.Lerp(this.colorEmpty, this.colorHalf, 2f * percent);
                    _batteryFill.color      = value;
                    _batteryFill.fillAmount = percent;
                }
                else
                {
                    _batteryFill.color      = colorEmpty;
                    _batteryFill.fillAmount = 0f;
                }
            }
        }
Exemplo n.º 2
0
        private void Update()
        {
            if (this.NotAllowToOperate)
            {
                return;
            }

            _energyToConsume = EnergyConsumptionPerSecond * DayNightCycle.main.deltaTime;
            var  batteryChargePull = DayNightCycle.main.deltaTime * chargeSpeed * _powercellData.GetCapacity();
            bool requiresEnergy    = GameModeUtils.RequiresPower();
            bool hasPowerToConsume = !requiresEnergy || (this.AvailablePower >= _energyToConsume);

            if (hasPowerToConsume && !_prevPowerState)
            {
                OnPowerResume?.Invoke();
                _prevPowerState = true;
            }
            else if (!hasPowerToConsume && _prevPowerState)
            {
                OnPowerOutage?.Invoke();
                _prevPowerState = false;
            }

            if (!hasPowerToConsume)
            {
                return;
            }


            if (requiresEnergy)
            {
                if (_connectedRelay.GetPower() <= _energyToConsume)
                {
                    _powercellData.RemoveCharge(_energyToConsume);
                    return;
                }

                if (!GetHasBreakerTripped())
                {
                    _connectedRelay.ConsumeEnergy(_energyToConsume, out float amountConsumed);
                }


                if (!_powercellData.IsFull())
                {
                    if (_connectedRelay.GetPower() >= batteryChargePull)
                    {
                        _chargeTimer -= Time.deltaTime;

                        if (_chargeTimer < 0)
                        {
                            _connectedRelay.ConsumeEnergy(batteryChargePull, out float amountPConsumed);
                            _powercellData.AddCharge(amountPConsumed);
                            QuickLogger.Debug($"Charging Battery: {amountPConsumed} units", true);
                            _chargeTimer = 5f;
                        }
                    }
                }
                //QuickLogger.Debug($"Power Consumed: {amountConsumed}");

                _mono.DisplayManager.UpdateVisuals(_powercellData);
            }
        }