Skip to content

Commit

Permalink
Predict vending machine BUI
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth committed Sep 22, 2024
1 parent 8ed779b commit 56d25ba
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 72 deletions.
20 changes: 4 additions & 16 deletions Content.Client/VendingMachines/VendingMachineBoundUserInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,17 @@ protected override void Open()
{
base.Open();

var vendingMachineSys = EntMan.System<VendingMachineSystem>();

_cachedInventory = vendingMachineSys.GetAllInventory(Owner);

_menu = this.CreateWindow<VendingMachineMenu>();
_menu.OpenCenteredLeft();
_menu.Title = EntMan.GetComponent<MetaDataComponent>(Owner).EntityName;

_menu.OnItemSelected += OnItemSelected;

_menu.Populate(_cachedInventory);

_menu.OpenCenteredLeft();
Refresh();
}

protected override void UpdateState(BoundUserInterfaceState state)
public void Refresh()
{
base.UpdateState(state);

if (state is not VendingMachineInterfaceState newState)
return;

_cachedInventory = newState.Inventory;
var system = EntMan.System<VendingMachineSystem>();
_cachedInventory = system.GetAllInventory(Owner);

_menu?.Populate(_cachedInventory);
}
Expand Down
10 changes: 10 additions & 0 deletions Content.Client/VendingMachines/VendingMachineSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ public sealed class VendingMachineSystem : SharedVendingMachineSystem
{
[Dependency] private readonly AnimationPlayerSystem _animationPlayer = default!;
[Dependency] private readonly SharedAppearanceSystem _appearanceSystem = default!;
[Dependency] private readonly SharedUserInterfaceSystem _uiSystem = default!;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<VendingMachineComponent, AppearanceChangeEvent>(OnAppearanceChange);
SubscribeLocalEvent<VendingMachineComponent, AnimationCompletedEvent>(OnAnimationCompleted);
SubscribeLocalEvent<VendingMachineComponent, AfterAutoHandleStateEvent>(OnVendingAfterState);
}

private void OnVendingAfterState(EntityUid uid, VendingMachineComponent component, ref AfterAutoHandleStateEvent args)
{
if (_uiSystem.TryGetOpenUi<VendingMachineBoundUserInterface>(uid, VendingMachineUiKey.Key, out var bui))
{
bui.Refresh();
}
}

private void OnAnimationCompleted(EntityUid uid, VendingMachineComponent component, AnimationCompletedEvent args)
Expand Down
30 changes: 4 additions & 26 deletions Content.Server/VendingMachines/VendingMachineSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ public sealed class VendingMachineSystem : SharedVendingMachineSystem
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly AccessReaderSystem _accessReader = default!;
[Dependency] private readonly AppearanceSystem _appearanceSystem = default!;
[Dependency] private readonly SharedActionsSystem _action = default!;
[Dependency] private readonly PricingSystem _pricing = default!;
[Dependency] private readonly ThrowingSystem _throwingSystem = default!;
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly SpeakOnUIClosedSystem _speakOnUIClosed = default!;

Expand All @@ -47,7 +45,6 @@ public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<VendingMachineComponent, MapInitEvent>(OnComponentMapInit);
SubscribeLocalEvent<VendingMachineComponent, PowerChangedEvent>(OnPowerChanged);
SubscribeLocalEvent<VendingMachineComponent, BreakageEventArgs>(OnBreak);
SubscribeLocalEvent<VendingMachineComponent, GotEmaggedEvent>(OnEmagged);
Expand All @@ -59,7 +56,6 @@ public override void Initialize()

Subs.BuiEvents<VendingMachineComponent>(VendingMachineUiKey.Key, subs =>
{
subs.Event<BoundUIOpenedEvent>(OnBoundUIOpened);
subs.Event<VendingMachineEjectMessage>(OnInventoryEjectMessage);
});

Expand All @@ -70,12 +66,6 @@ public override void Initialize()
SubscribeLocalEvent<VendingMachineRestockComponent, PriceCalculationEvent>(OnPriceCalculation);
}

private void OnComponentMapInit(EntityUid uid, VendingMachineComponent component, MapInitEvent args)
{
_action.AddAction(uid, ref component.ActionEntity, component.Action, uid);
Dirty(uid, component);
}

private void OnVendingPrice(EntityUid uid, VendingMachineComponent component, ref PriceCalculationEvent args)
{
var price = 0.0;
Expand All @@ -94,9 +84,9 @@ private void OnVendingPrice(EntityUid uid, VendingMachineComponent component, re
args.Price += price;
}

protected override void OnComponentInit(EntityUid uid, VendingMachineComponent component, ComponentInit args)
protected override void OnMapInit(EntityUid uid, VendingMachineComponent component, MapInitEvent args)
{
base.OnComponentInit(uid, component, args);
base.OnMapInit(uid, component, args);

if (HasComp<ApcPowerReceiverComponent>(uid))
{
Expand All @@ -110,18 +100,6 @@ private void OnActivatableUIOpenAttempt(EntityUid uid, VendingMachineComponent c
args.Cancel();
}

private void OnBoundUIOpened(EntityUid uid, VendingMachineComponent component, BoundUIOpenedEvent args)
{
UpdateVendingMachineInterfaceState(uid, component);
}

private void UpdateVendingMachineInterfaceState(EntityUid uid, VendingMachineComponent component)
{
var state = new VendingMachineInterfaceState(GetAllInventory(uid, component));

_userInterfaceSystem.SetUiState(uid, VendingMachineUiKey.Key, state);
}

private void OnInventoryEjectMessage(EntityUid uid, VendingMachineComponent component, VendingMachineEjectMessage args)
{
if (!this.IsPowered(uid, EntityManager))
Expand Down Expand Up @@ -297,7 +275,7 @@ public void TryEjectVendorItem(EntityUid uid, InventoryType type, string itemId,
_speakOnUIClosed.TrySetFlag((uid, speakComponent));

entry.Amount--;
UpdateVendingMachineInterfaceState(uid, vendComponent);
Dirty(uid, vendComponent);
TryUpdateVisualState(uid, vendComponent);
Audio.PlayPvs(vendComponent.SoundVend, uid);
}
Expand Down Expand Up @@ -493,7 +471,7 @@ public void TryRestockInventory(EntityUid uid, VendingMachineComponent? vendComp

RestockInventoryFromPrototype(uid, vendComponent);

UpdateVendingMachineInterfaceState(uid, vendComponent);
Dirty(uid, vendComponent);
TryUpdateVisualState(uid, vendComponent);
}

Expand Down
5 changes: 3 additions & 2 deletions Content.Shared/VendingMachines/SharedVendingMachineSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public abstract partial class SharedVendingMachineSystem : EntitySystem
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<VendingMachineComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<VendingMachineComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<VendingMachineRestockComponent, AfterInteractEvent>(OnAfterInteract);
}

protected virtual void OnComponentInit(EntityUid uid, VendingMachineComponent component, ComponentInit args)
protected virtual void OnMapInit(EntityUid uid, VendingMachineComponent component, MapInitEvent args)
{
RestockInventoryFromPrototype(uid, component, component.InitialStockQuality);
}
Expand All @@ -46,6 +46,7 @@ public void RestockInventoryFromPrototype(EntityUid uid,
AddInventoryFromPrototype(uid, packPrototype.StartingInventory, InventoryType.Regular, component, restockQuality);
AddInventoryFromPrototype(uid, packPrototype.EmaggedInventory, InventoryType.Emagged, component, restockQuality);
AddInventoryFromPrototype(uid, packPrototype.ContrabandInventory, InventoryType.Contraband, component, restockQuality);
Dirty(uid, component);
}

/// <summary>
Expand Down
23 changes: 6 additions & 17 deletions Content.Shared/VendingMachines/VendingMachineComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Content.Shared.VendingMachines
{
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
public sealed partial class VendingMachineComponent : Component
{
/// <summary>
Expand All @@ -21,24 +21,24 @@ public sealed partial class VendingMachineComponent : Component
/// Used by the server to determine how long the vending machine stays in the "Deny" state.
/// Used by the client to determine how long the deny animation should be played.
/// </summary>
[DataField("denyDelay")]
[DataField]
public float DenyDelay = 2.0f;

/// <summary>
/// Used by the server to determine how long the vending machine stays in the "Eject" state.
/// The selected item is dispensed afer this delay.
/// Used by the client to determine how long the deny animation should be played.
/// </summary>
[DataField("ejectDelay")]
[DataField]
public float EjectDelay = 1.2f;

[ViewVariables]
[DataField, AutoNetworkedField]
public Dictionary<string, VendingMachineInventoryEntry> Inventory = new();

[ViewVariables]
[DataField, AutoNetworkedField]
public Dictionary<string, VendingMachineInventoryEntry> EmaggedInventory = new();

[ViewVariables]
[DataField, AutoNetworkedField]
public Dictionary<string, VendingMachineInventoryEntry> ContrabandInventory = new();

public bool Contraband;
Expand Down Expand Up @@ -102,17 +102,6 @@ public sealed partial class VendingMachineComponent : Component
// Yoinked from: https://github.com/discordia-space/CEV-Eris/blob/35bbad6764b14e15c03a816e3e89aa1751660ba9/sound/machines/Custom_deny.ogg
public SoundSpecifier SoundDeny = new SoundPathSpecifier("/Audio/Machines/custom_deny.ogg");

/// <summary>
/// The action available to the player controlling the vending machine
/// </summary>
[DataField("action", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
[AutoNetworkedField]
public string? Action = "ActionVendingThrow";

[DataField("actionEntity")]
[AutoNetworkedField]
public EntityUid? ActionEntity;

public float NonLimitedEjectForce = 7.5f;

public float NonLimitedEjectRange = 5f;
Expand Down
11 changes: 0 additions & 11 deletions Content.Shared/VendingMachines/VendingMachineInterfaceState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@

namespace Content.Shared.VendingMachines
{
[NetSerializable, Serializable]
public sealed class VendingMachineInterfaceState : BoundUserInterfaceState
{
public List<VendingMachineInventoryEntry> Inventory;

public VendingMachineInterfaceState(List<VendingMachineInventoryEntry> inventory)
{
Inventory = inventory;
}
}

[Serializable, NetSerializable]
public sealed class VendingMachineEjectMessage : BoundUserInterfaceMessage
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
description: Just add capitalism!
abstract: true
components:
- type: ActionGrant
actions:
- ActionVendingThrow
- type: StationAiWhitelist
- type: AmbientOnPowered
- type: AmbientSound
Expand Down

0 comments on commit 56d25ba

Please sign in to comment.