private void AddInventoryFromPrototype(EntityUid uid, Dictionary <string, uint>?entries,
                                               InventoryType type,
                                               VendingMachineComponent?component = null)
        {
            if (!Resolve(uid, ref component) || entries == null)
            {
                return;
            }

            var inventory = new List <VendingMachineInventoryEntry>();

            foreach (var(id, amount) in entries)
            {
                if (_prototypeManager.HasIndex <EntityPrototype>(id))
                {
                    inventory.Add(new VendingMachineInventoryEntry(type, id, amount));
                }
            }

            switch (type)
            {
            case InventoryType.Regular:
                component.Inventory.AddRange(inventory);
                break;

            case InventoryType.Emagged:
                component.EmaggedInventory.AddRange(inventory);
                break;

            case InventoryType.Contraband:
                component.ContrabandInventory.AddRange(inventory);
                break;
            }
        }
        public void InitializeFromPrototype(EntityUid uid, VendingMachineComponent?vendComponent = null)
        {
            if (!Resolve(uid, ref vendComponent))
            {
                return;
            }

            if (string.IsNullOrEmpty(vendComponent.PackPrototypeId))
            {
                return;
            }

            if (!_prototypeManager.TryIndex(vendComponent.PackPrototypeId, out VendingMachineInventoryPrototype? packPrototype))
            {
                return;
            }

            MetaData(uid).EntityName        = packPrototype.Name;
            vendComponent.AnimationDuration = TimeSpan.FromSeconds(packPrototype.AnimationDuration);
            vendComponent.SpriteName        = packPrototype.SpriteName;
            if (!string.IsNullOrEmpty(vendComponent.SpriteName))
            {
                if (TryComp <SpriteComponent>(vendComponent.Owner, out var spriteComp))
                {
                    const string vendingMachineRSIPath = "Structures/Machines/VendingMachines/{0}.rsi";
                    spriteComp.BaseRSIPath = string.Format(vendingMachineRSIPath, vendComponent.SpriteName);
                }
            }

            AddInventoryFromPrototype(uid, packPrototype.StartingInventory, InventoryType.Regular, vendComponent);
            AddInventoryFromPrototype(uid, packPrototype.EmaggedInventory, InventoryType.Emagged, vendComponent);
            AddInventoryFromPrototype(uid, packPrototype.ContrabandInventory, InventoryType.Contraband, vendComponent);
        }
Пример #3
0
        public void TryEjectVendorItem(EntityUid uid, string itemId, bool throwItem, VendingMachineComponent?vendComponent = null)
        {
            if (!Resolve(uid, ref vendComponent))
            {
                return;
            }

            if (vendComponent.Ejecting || vendComponent.Broken || !IsPowered(uid, vendComponent))
            {
                return;
            }

            var entry = vendComponent.Inventory.Find(x => x.ID == itemId);

            if (entry == null)
            {
                _popupSystem.PopupEntity(Loc.GetString("vending-machine-component-try-eject-invalid-item"), uid, Filter.Pvs(uid));
                Deny(uid, vendComponent);
                return;
            }

            if (entry.Amount <= 0)
            {
                _popupSystem.PopupEntity(Loc.GetString("vending-machine-component-try-eject-out-of-stock"), uid, Filter.Pvs(uid));
                Deny(uid, vendComponent);
                return;
            }

            if (entry.ID == null)
            {
                return;
            }

            if (!TryComp <TransformComponent>(vendComponent.Owner, out var transformComp))
            {
                return;
            }

            // Start Ejecting, and prevent users from ordering while anim playing
            vendComponent.Ejecting = true;
            entry.Amount--;
            vendComponent.UserInterface?.SendMessage(new VendingMachineInventoryMessage(vendComponent.Inventory));
            TryUpdateVisualState(uid, VendingMachineVisualState.Eject, vendComponent);
            vendComponent.Owner.SpawnTimer(vendComponent.AnimationDuration, () =>
            {
                vendComponent.Ejecting = false;
                TryUpdateVisualState(uid, VendingMachineVisualState.Normal, vendComponent);
                var ent = EntityManager.SpawnEntity(entry.ID, transformComp.Coordinates);
                if (throwItem)
                {
                    float range       = vendComponent.NonLimitedEjectRange;
                    Vector2 direction = new Vector2(_random.NextFloat(-range, range), _random.NextFloat(-range, range));
                    _throwingSystem.TryThrow(ent, direction, vendComponent.NonLimitedEjectForce);
                }
            });
            SoundSystem.Play(Filter.Pvs(vendComponent.Owner), vendComponent.SoundVend.GetSound(), vendComponent.Owner, AudioParams.Default.WithVolume(-2f));
        }
Пример #4
0
        public bool IsPowered(EntityUid uid, VendingMachineComponent?vendComponent = null)
        {
            if (!Resolve(uid, ref vendComponent))
            {
                return(false);
            }

            if (!TryComp <ApcPowerReceiverComponent>(vendComponent.Owner, out var receiver))
            {
                return(false);
            }
            return(receiver.Powered);
        }
        public void Deny(EntityUid uid, VendingMachineComponent?vendComponent = null)
        {
            if (!Resolve(uid, ref vendComponent))
            {
                return;
            }

            SoundSystem.Play(vendComponent.SoundDeny.GetSound(), Filter.Pvs(vendComponent.Owner), vendComponent.Owner, AudioParams.Default.WithVolume(-2f));
            // Play the Deny animation
            TryUpdateVisualState(uid, VendingMachineVisualState.Deny, vendComponent);
            //TODO: This duration should be a distinct value specific to the deny animation
            vendComponent.Owner.SpawnTimer(vendComponent.AnimationDuration, () =>
            {
                TryUpdateVisualState(uid, VendingMachineVisualState.Normal, vendComponent);
            });
        }
Пример #6
0
        public void EjectRandom(EntityUid uid, bool throwItem, VendingMachineComponent?vendComponent = null)
        {
            if (!Resolve(uid, ref vendComponent))
            {
                return;
            }

            var availableItems = vendComponent.Inventory.Where(x => x.Amount > 0).ToList();

            if (availableItems.Count <= 0)
            {
                return;
            }

            TryEjectVendorItem(uid, _random.Pick(availableItems).ID, throwItem, vendComponent);
        }
        public bool IsAuthorized(EntityUid uid, EntityUid?sender, VendingMachineComponent?vendComponent = null)
        {
            if (!Resolve(uid, ref vendComponent) || sender == null)
            {
                return(false);
            }

            if (TryComp <AccessReaderComponent?>(vendComponent.Owner, out var accessReader))
            {
                if (!_accessReader.IsAllowed(sender.Value, accessReader) && !vendComponent.Emagged)
                {
                    _popupSystem.PopupEntity(Loc.GetString("vending-machine-component-try-eject-access-denied"), uid, Filter.Pvs(uid));
                    Deny(uid, vendComponent);
                    return(false);
                }
            }
            return(true);
        }
Пример #8
0
        public void InitializeFromPrototype(EntityUid uid, VendingMachineComponent?vendComponent = null)
        {
            if (!Resolve(uid, ref vendComponent))
            {
                return;
            }

            if (string.IsNullOrEmpty(vendComponent.PackPrototypeId))
            {
                return;
            }

            if (!_prototypeManager.TryIndex(vendComponent.PackPrototypeId, out VendingMachineInventoryPrototype? packPrototype))
            {
                return;
            }

            MetaData(uid).EntityName        = packPrototype.Name;
            vendComponent.AnimationDuration = TimeSpan.FromSeconds(packPrototype.AnimationDuration);
            vendComponent.SpriteName        = packPrototype.SpriteName;
            if (!string.IsNullOrEmpty(vendComponent.SpriteName))
            {
                if (TryComp <SpriteComponent>(vendComponent.Owner, out var spriteComp))
                {
                    const string vendingMachineRSIPath = "Structures/Machines/VendingMachines/{0}.rsi";
                    spriteComp.BaseRSIPath = string.Format(vendingMachineRSIPath, vendComponent.SpriteName);
                }
            }
            var inventory = new List <VendingMachineInventoryEntry>();

            foreach (var(id, amount) in packPrototype.StartingInventory)
            {
                if (!_prototypeManager.TryIndex(id, out EntityPrototype? prototype))
                {
                    continue;
                }
                inventory.Add(new VendingMachineInventoryEntry(id, amount));
            }
            vendComponent.Inventory = inventory;
        }
        public void TryUpdateVisualState(EntityUid uid, VendingMachineVisualState?state = VendingMachineVisualState.Normal, VendingMachineComponent?vendComponent = null)
        {
            if (!Resolve(uid, ref vendComponent))
            {
                return;
            }

            var finalState = state == null ? VendingMachineVisualState.Normal : state;

            if (vendComponent.Broken)
            {
                finalState = VendingMachineVisualState.Broken;
            }
            else if (vendComponent.Ejecting)
            {
                finalState = VendingMachineVisualState.Eject;
            }
            else if (!this.IsPowered(uid, EntityManager))
            {
                finalState = VendingMachineVisualState.Off;
            }

            if (TryComp <AppearanceComponent>(vendComponent.Owner, out var appearance))
            {
                appearance.SetData(VendingMachineVisuals.VisualState, finalState);
            }
        }