예제 #1
0
    private void OnChargeChanged(EntityUid uid, PowerCellComponent component, ChargeChangedEvent args)
    {
        if (component.IsRigged)
        {
            Explode(uid);
            return;
        }

        if (!TryComp(uid, out BatteryComponent? battery))
        {
            return;
        }

        if (!TryComp(uid, out AppearanceComponent? appearance))
        {
            return;
        }

        var frac  = battery.CurrentCharge / battery.MaxCharge;
        var level = (byte)ContentHelpers.RoundToNearestLevels(frac, 1, PowerCellComponent.PowerCellVisualsLevels);

        appearance.SetData(PowerCellVisuals.ChargeLevel, level);

        // If this power cell is inside a cell-slot, inform that entity that the power has changed (for updating visuals n such).
        if (_containerSystem.TryGetContainingContainer(uid, out var container) &&
            TryComp(container.Owner, out PowerCellSlotComponent? slot) &&
            slot.CellSlot.Item == uid)
        {
            RaiseLocalEvent(container.Owner, new PowerCellChangedEvent(false), false);
        }
    }
예제 #2
0
        public override void OnChangeData(AppearanceComponent component)
        {
            base.OnChangeData(component);

            var sprite = component.Owner.GetComponent <ISpriteComponent>();

            if (component.TryGetData(PowerCellVisuals.ChargeLevel, out float fraction))
            {
                int level = ContentHelpers.RoundToNearestLevels(fraction, 1, 4) * 25;
                sprite.LayerSetState(Layers.Charge, $"{_prefix}_{level}");
            }
        }
        // Curently every single flashlight has the same number of levels for status and that's all it uses the charge for
        // Thus we'll just check if the level changes.
        private byte?GetLevel()
        {
            if (Cell == null)
            {
                return(null);
            }

            var currentCharge = Cell.CurrentCharge;

            if (MathHelper.CloseTo(currentCharge, 0) || Wattage > currentCharge)
            {
                return(0);
            }

            return((byte?)ContentHelpers.RoundToNearestLevels(currentCharge / Cell.MaxCharge * 255, 255, StatusLevels));
        }
예제 #4
0
            protected override void Update(FrameEventArgs args)
            {
                base.Update(args);

                _timer += args.DeltaSeconds;
                _timer %= TimerCycle;

                var charge = _parent.Charge ?? 0;

                int level;

                if (MathHelper.CloseTo(charge, 0))
                {
                    level = 0;
                }
                else
                {
                    level = ContentHelpers.RoundToNearestLevels(charge, 1.0, 6) + 1;
                }

                if (level == 0)
                {
                    _sections[0].PanelOverride = _styleBoxUnlit;
                }
                else if (level == 1)
                {
                    // Flash the last light.
                    _sections[0].PanelOverride = _timer > TimerCycle / 2 ? _styleBoxLit : _styleBoxUnlit;
                }
                else
                {
                    _sections[0].PanelOverride = _styleBoxLit;
                }

                _sections[1].PanelOverride = level >= 3 ? _styleBoxLit : _styleBoxUnlit;
                _sections[2].PanelOverride = level >= 4 ? _styleBoxLit : _styleBoxUnlit;
                _sections[3].PanelOverride = level >= 5 ? _styleBoxLit : _styleBoxUnlit;
                _sections[4].PanelOverride = level >= 6 ? _styleBoxLit : _styleBoxUnlit;
            }
예제 #5
0
    private int GetDamageLevel(EntityUid uid, ExaminableDamageComponent?component = null,
                               DamageableComponent?damageable = null, DestructibleComponent?destructible = null)
    {
        if (!Resolve(uid, ref component, ref damageable, ref destructible))
        {
            return(0);
        }

        if (component.MessagesProto == null)
        {
            return(0);
        }

        var maxLevels = component.MessagesProto.Messages.Length - 1;

        if (maxLevels <= 0)
        {
            return(0);
        }

        var trigger = (DamageTrigger?)destructible.Thresholds
                      .LastOrDefault(threshold => threshold.Trigger is DamageTrigger)?.Trigger;

        if (trigger == null)
        {
            return(0);
        }

        var damage          = damageable.TotalDamage;
        var damageThreshold = trigger.Damage;
        var fraction        = damageThreshold == 0 ? 0f : (float)damage / damageThreshold;

        var level = ContentHelpers.RoundToNearestLevels(fraction, 1, maxLevels);

        return(level);
    }
 private byte GetLevel(float fraction)
 {
     return((byte)ContentHelpers.RoundToNearestLevels(fraction, 1, SharedPowerCell.PowerCellVisualsLevels));
 }