From 5ebfcbaeb9e72135dbc81fc085dde8bdb742607f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B3mez?= <50799541+David-Goru@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:09:14 +0200 Subject: [PATCH] Make vendor machines and Jukebox work with electricity (#1447) * Fix vendor machines and add multiple options and stock * Fix logging * Add electricity consumption to VendorMachines and Jukebox * Rename field * Tie UseMachineOnce to tick --------- Co-authored-by: Mechar418 --- .../Furniture/Generic/JukeboxBlueGreen.prefab | 43 +++++++++++++- .../Machines/Vendors/VendorHotDrinks.prefab | 41 ++++++++++++- .../Vendors/VendorRobustSoftdrinks.prefab | 41 ++++++++++++- .../Machines/Vendors/VendorSecTech.prefab | 41 ++++++++++++- .../Machines/Vendors/VendorYouTool.prefab | 41 ++++++++++++- Assets/Scripts/SS3D/Systems/Audio/Boombox.cs | 59 +++++++++++++++---- .../Systems/Audio/ChangeMusicInteraction.cs | 2 +- .../Electricity/MachinePowerConsumer.cs | 49 +++++++++++++++ .../Electricity/MachinePowerConsumer.cs.meta | 3 + .../SS3D/Systems/Furniture/VendingMachine.cs | 17 ++++++ 10 files changed, 318 insertions(+), 19 deletions(-) create mode 100644 Assets/Scripts/SS3D/Systems/Electricity/MachinePowerConsumer.cs create mode 100644 Assets/Scripts/SS3D/Systems/Electricity/MachinePowerConsumer.cs.meta diff --git a/Assets/Content/WorldObjects/Furniture/Generic/JukeboxBlueGreen.prefab b/Assets/Content/WorldObjects/Furniture/Generic/JukeboxBlueGreen.prefab index 296f0bdc67..f3fabbe1a3 100644 --- a/Assets/Content/WorldObjects/Furniture/Generic/JukeboxBlueGreen.prefab +++ b/Assets/Content/WorldObjects/Furniture/Generic/JukeboxBlueGreen.prefab @@ -14,6 +14,8 @@ GameObject: - component: {fileID: 4294524822034227355} - component: {fileID: 1326650896113229434} - component: {fileID: 8174704186285038031} + - component: {fileID: 8272970134100573020} + - component: {fileID: 5978761350964980520} m_Layer: 0 m_Name: JukeboxBlueGreen m_TagString: Untagged @@ -104,6 +106,8 @@ MonoBehaviour: k__BackingField: {fileID: 0} _networkBehaviours: - {fileID: 8174704186285038031} + - {fileID: 8272970134100573020} + - {fileID: 5978761350964980520} k__BackingField: {fileID: 0} k__BackingField: [] SerializedTransformProperties: @@ -115,7 +119,7 @@ MonoBehaviour: _initializeOrder: 0 _defaultDespawnType: 0 NetworkObserver: {fileID: 0} - k__BackingField: 123 + k__BackingField: 155 k__BackingField: 0 _scenePathHash: 0 k__BackingField: 0 @@ -149,11 +153,46 @@ MonoBehaviour: _componentIndexCache: 0 _addedNetworkObject: {fileID: 4294524822034227355} _networkObjectCache: {fileID: 4294524822034227355} + _powerConsumer: {fileID: 8272970134100573020} _songs: - {fileID: 8300000, guid: ec8b849764a7e9147b2a565f81a1ca42, type: 3} - {fileID: 8300000, guid: fd331e360db5fbb448e66367e41d1893, type: 3} - {fileID: 8300000, guid: 3f51862f0b88a5243a8cceb97147489d, type: 3} RadioOn: 0 CurrentMusic: 0 - InteractionIcon: {fileID: 0} + InteractionIcon: {fileID: 21300000, guid: d1f34343e08a14e48a856e409c355f32, type: 3} InteractionIconOn: {fileID: 0} +--- !u!114 &8272970134100573020 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6118765911134750290} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ea15f8a26dac4e4193bb75335cb9e9cd, type: 3} + m_Name: + m_EditorClassIdentifier: + _componentIndexCache: 1 + _addedNetworkObject: {fileID: 4294524822034227355} + _networkObjectCache: {fileID: 4294524822034227355} + _powerConsumptionIdle: 0.5 + _powerConsumptionInUse: 5 + isIdle: 1 + _powerStatus: 0 +--- !u!114 &5978761350964980520 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6118765911134750290} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1bbc87ddd49b01f4098910ffdf858004, type: 3} + m_Name: + m_EditorClassIdentifier: + _componentIndexCache: 2 + _addedNetworkObject: {fileID: 4294524822034227355} + _networkObjectCache: {fileID: 4294524822034227355} diff --git a/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorHotDrinks.prefab b/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorHotDrinks.prefab index 5726d488e7..11e0fe0178 100644 --- a/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorHotDrinks.prefab +++ b/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorHotDrinks.prefab @@ -16,6 +16,8 @@ GameObject: - component: {fileID: 8905492951320375787} - component: {fileID: -8220440043154804263} - component: {fileID: -7621784774190349983} + - component: {fileID: 8320483876716032414} + - component: {fileID: -6290800912275539549} m_Layer: 0 m_Name: VendorHotDrinks m_TagString: Untagged @@ -123,6 +125,8 @@ MonoBehaviour: k__BackingField: {fileID: 0} _networkBehaviours: - {fileID: 8905492951320375787} + - {fileID: 8320483876716032414} + - {fileID: -6290800912275539549} k__BackingField: {fileID: 0} k__BackingField: [] SerializedTransformProperties: @@ -134,7 +138,7 @@ MonoBehaviour: _initializeOrder: 0 _defaultDespawnType: 0 NetworkObserver: {fileID: 0} - k__BackingField: 2 + k__BackingField: 22 k__BackingField: 0 _scenePathHash: 0 k__BackingField: 0 @@ -154,6 +158,7 @@ MonoBehaviour: _componentIndexCache: 0 _addedNetworkObject: {fileID: 1871071288925904272} _networkObjectCache: {fileID: 1871071288925904272} + _powerConsumer: {fileID: 8320483876716032414} _productsToDispense: - Product: {fileID: 11400000, guid: edcff839a189118479387da05ec07d49, type: 2} Stock: 5 @@ -183,6 +188,40 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: key: {fileID: 11400000, guid: 7018e8af24d30114db9db31cd189ab88, type: 2} +--- !u!114 &8320483876716032414 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2658310835758749969} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ea15f8a26dac4e4193bb75335cb9e9cd, type: 3} + m_Name: + m_EditorClassIdentifier: + _componentIndexCache: 1 + _addedNetworkObject: {fileID: 1871071288925904272} + _networkObjectCache: {fileID: 1871071288925904272} + _powerConsumptionIdle: 0.5 + _powerConsumptionInUse: 10 + isIdle: 1 + _powerStatus: 0 +--- !u!114 &-6290800912275539549 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2658310835758749969} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1bbc87ddd49b01f4098910ffdf858004, type: 3} + m_Name: + m_EditorClassIdentifier: + _componentIndexCache: 2 + _addedNetworkObject: {fileID: 1871071288925904272} + _networkObjectCache: {fileID: 1871071288925904272} --- !u!1 &3671711954327816294 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorRobustSoftdrinks.prefab b/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorRobustSoftdrinks.prefab index 54765b2124..39e2e5a93c 100644 --- a/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorRobustSoftdrinks.prefab +++ b/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorRobustSoftdrinks.prefab @@ -16,6 +16,8 @@ GameObject: - component: {fileID: 8905492951320375787} - component: {fileID: 3665146264900623842} - component: {fileID: 8086907785789700544} + - component: {fileID: 2921440247678111929} + - component: {fileID: -119891910764691591} m_Layer: 0 m_Name: VendorRobustSoftdrinks m_TagString: Untagged @@ -122,6 +124,8 @@ MonoBehaviour: k__BackingField: {fileID: 0} _networkBehaviours: - {fileID: 8905492951320375787} + - {fileID: 2921440247678111929} + - {fileID: -119891910764691591} k__BackingField: {fileID: 0} k__BackingField: [] SerializedTransformProperties: @@ -133,7 +137,7 @@ MonoBehaviour: _initializeOrder: 0 _defaultDespawnType: 0 NetworkObserver: {fileID: 0} - k__BackingField: 2 + k__BackingField: 201 k__BackingField: 0 _scenePathHash: 0 k__BackingField: 0 @@ -153,6 +157,7 @@ MonoBehaviour: _componentIndexCache: 0 _addedNetworkObject: {fileID: 1871071288925904272} _networkObjectCache: {fileID: 1871071288925904272} + _powerConsumer: {fileID: 2921440247678111929} _productsToDispense: - Product: {fileID: 11400000, guid: dd302b5636b41bb48ba3a2cdc72cc64c, type: 2} Stock: 10 @@ -194,6 +199,40 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: key: {fileID: 11400000, guid: 47931fe5cd4465941b01da33ac091ef7, type: 2} +--- !u!114 &2921440247678111929 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2658310835758749969} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ea15f8a26dac4e4193bb75335cb9e9cd, type: 3} + m_Name: + m_EditorClassIdentifier: + _componentIndexCache: 1 + _addedNetworkObject: {fileID: 1871071288925904272} + _networkObjectCache: {fileID: 1871071288925904272} + _powerConsumptionIdle: 0.5 + _powerConsumptionInUse: 10 + isIdle: 1 + _powerStatus: 0 +--- !u!114 &-119891910764691591 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2658310835758749969} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1bbc87ddd49b01f4098910ffdf858004, type: 3} + m_Name: + m_EditorClassIdentifier: + _componentIndexCache: 2 + _addedNetworkObject: {fileID: 1871071288925904272} + _networkObjectCache: {fileID: 1871071288925904272} --- !u!1 &3671711954327816294 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorSecTech.prefab b/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorSecTech.prefab index 27135db23a..542cf14e0e 100644 --- a/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorSecTech.prefab +++ b/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorSecTech.prefab @@ -99,6 +99,8 @@ GameObject: - component: {fileID: 8905492951320375787} - component: {fileID: -8922800647233213677} - component: {fileID: -7835554894700288649} + - component: {fileID: 4707002213235673783} + - component: {fileID: -6580009485370809016} m_Layer: 0 m_Name: VendorSecTech m_TagString: Untagged @@ -207,6 +209,8 @@ MonoBehaviour: k__BackingField: {fileID: 0} _networkBehaviours: - {fileID: 8905492951320375787} + - {fileID: 4707002213235673783} + - {fileID: -6580009485370809016} k__BackingField: {fileID: 0} k__BackingField: [] SerializedTransformProperties: @@ -218,7 +222,7 @@ MonoBehaviour: _initializeOrder: 0 _defaultDespawnType: 0 NetworkObserver: {fileID: 0} - k__BackingField: 2 + k__BackingField: 48 k__BackingField: 0 _scenePathHash: 0 k__BackingField: 0 @@ -238,6 +242,7 @@ MonoBehaviour: _componentIndexCache: 0 _addedNetworkObject: {fileID: 1871071288925904272} _networkObjectCache: {fileID: 1871071288925904272} + _powerConsumer: {fileID: 4707002213235673783} _productsToDispense: - Product: {fileID: 11400000, guid: ecb1cb0479842954a925e9013baf95f6, type: 2} Stock: 5 @@ -273,6 +278,40 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: key: {fileID: 11400000, guid: 8519f267c36b37140af67c13d4231803, type: 2} +--- !u!114 &4707002213235673783 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2658310835758749969} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ea15f8a26dac4e4193bb75335cb9e9cd, type: 3} + m_Name: + m_EditorClassIdentifier: + _componentIndexCache: 1 + _addedNetworkObject: {fileID: 1871071288925904272} + _networkObjectCache: {fileID: 1871071288925904272} + _powerConsumptionIdle: 0.5 + _powerConsumptionInUse: 10 + isIdle: 1 + _powerStatus: 0 +--- !u!114 &-6580009485370809016 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2658310835758749969} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1bbc87ddd49b01f4098910ffdf858004, type: 3} + m_Name: + m_EditorClassIdentifier: + _componentIndexCache: 2 + _addedNetworkObject: {fileID: 1871071288925904272} + _networkObjectCache: {fileID: 1871071288925904272} --- !u!1 &3671711954327816294 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorYouTool.prefab b/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorYouTool.prefab index f7cb050ffb..fa4b07d519 100644 --- a/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorYouTool.prefab +++ b/Assets/Content/WorldObjects/Furniture/Machines/Vendors/VendorYouTool.prefab @@ -16,6 +16,8 @@ GameObject: - component: {fileID: 8905492951320375787} - component: {fileID: 6841733904447030618} - component: {fileID: -8380432832886518514} + - component: {fileID: 8149129049948001439} + - component: {fileID: 2156158514584011666} m_Layer: 0 m_Name: VendorYouTool m_TagString: Untagged @@ -122,6 +124,8 @@ MonoBehaviour: k__BackingField: {fileID: 0} _networkBehaviours: - {fileID: 8905492951320375787} + - {fileID: 8149129049948001439} + - {fileID: 2156158514584011666} k__BackingField: {fileID: 0} k__BackingField: [] SerializedTransformProperties: @@ -133,7 +137,7 @@ MonoBehaviour: _initializeOrder: 0 _defaultDespawnType: 0 NetworkObserver: {fileID: 0} - k__BackingField: 2 + k__BackingField: 54 k__BackingField: 0 _scenePathHash: 0 k__BackingField: 0 @@ -153,6 +157,7 @@ MonoBehaviour: _componentIndexCache: 0 _addedNetworkObject: {fileID: 1871071288925904272} _networkObjectCache: {fileID: 1871071288925904272} + _powerConsumer: {fileID: 8149129049948001439} _productsToDispense: - Product: {fileID: 11400000, guid: 4d5800940a547b2419e83ec0ccd555d4, type: 2} Stock: 5 @@ -188,6 +193,40 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: key: {fileID: 11400000, guid: 07ba3e874a1f2b348bad7bce50cec9dc, type: 2} +--- !u!114 &8149129049948001439 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2658310835758749969} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ea15f8a26dac4e4193bb75335cb9e9cd, type: 3} + m_Name: + m_EditorClassIdentifier: + _componentIndexCache: 1 + _addedNetworkObject: {fileID: 1871071288925904272} + _networkObjectCache: {fileID: 1871071288925904272} + _powerConsumptionIdle: 0.5 + _powerConsumptionInUse: 10 + isIdle: 1 + _powerStatus: 0 +--- !u!114 &2156158514584011666 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2658310835758749969} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1bbc87ddd49b01f4098910ffdf858004, type: 3} + m_Name: + m_EditorClassIdentifier: + _componentIndexCache: 2 + _addedNetworkObject: {fileID: 1871071288925904272} + _networkObjectCache: {fileID: 1871071288925904272} --- !u!1 &3671711954327816294 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/SS3D/Systems/Audio/Boombox.cs b/Assets/Scripts/SS3D/Systems/Audio/Boombox.cs index 1800039ab2..34127bf935 100644 --- a/Assets/Scripts/SS3D/Systems/Audio/Boombox.cs +++ b/Assets/Scripts/SS3D/Systems/Audio/Boombox.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using UnityEngine; using SS3D.Core; +using System.Electricity; using UnityEngine.Serialization; namespace SS3D.Systems.Audio @@ -13,12 +14,14 @@ namespace SS3D.Systems.Audio /// public class Boombox : InteractionTargetNetworkBehaviour, IToggleable { + [SerializeField] + private MachinePowerConsumer _powerConsumer; + [SerializeField] private List _songs; - // is it playing music [SyncVar] - public bool RadioOn; + public bool AudioOn; [SyncVar] public int CurrentMusic; @@ -27,23 +30,60 @@ public class Boombox : InteractionTargetNetworkBehaviour, IToggleable public Sprite InteractionIcon; public Sprite InteractionIconOn; + public bool GetState() + { + return AudioOn; + } + + protected override void OnEnabled() + { + base.OnEnabled(); + + _powerConsumer.OnPowerStatusUpdated += HandlePowerStatusUpdated; + } + public void Toggle() { - RadioOn = !RadioOn; - if (!RadioOn) + if (_powerConsumer.PowerStatus != PowerStatus.Powered) { - - Subsystems.Get().StopAudioSource(NetworkObject); + return; } - else + + AudioOn = !AudioOn; + _powerConsumer.isIdle = !AudioOn; + + if (AudioOn) { Subsystems.Get().PlayAudioSource(AudioType.Music, _songs[CurrentMusic], GameObject.transform.position, NetworkObject, false, 0.7f, 1, 1, 5); } + else + { + Subsystems.Get().StopAudioSource(NetworkObject); + } + } + + private void HandlePowerStatusUpdated(object sender, PowerStatus newStatus) + { + UpdateMusic(newStatus); + } + + private void UpdateMusic(PowerStatus powerStatus) + { + if (AudioOn && powerStatus != PowerStatus.Powered) + { + AudioOn = false; + Subsystems.Get().StopAudioSource(NetworkObject); + } } public void ChangeCurrentMusic() { + if (!AudioOn) + { + return; + } + Subsystems.Get().StopAudioSource(NetworkObject); Subsystems.Get().SetTimeAudioSource(NetworkObject, 0f); CurrentMusic = (CurrentMusic + 1) % (_songs.Count); @@ -51,11 +91,6 @@ public void ChangeCurrentMusic() false, 0.7f, 1, 1, 5); } - public bool GetState() - { - return RadioOn; - } - public override IInteraction[] CreateTargetInteractions(InteractionEvent interactionEvent) { List interactions = new List(2) diff --git a/Assets/Scripts/SS3D/Systems/Audio/ChangeMusicInteraction.cs b/Assets/Scripts/SS3D/Systems/Audio/ChangeMusicInteraction.cs index 6359c53ed2..e2c51a7f79 100644 --- a/Assets/Scripts/SS3D/Systems/Audio/ChangeMusicInteraction.cs +++ b/Assets/Scripts/SS3D/Systems/Audio/ChangeMusicInteraction.cs @@ -37,7 +37,7 @@ public override bool CanInteract(InteractionEvent interactionEvent) { return false; } - return boom.RadioOn; + return boom.AudioOn; } return false; diff --git a/Assets/Scripts/SS3D/Systems/Electricity/MachinePowerConsumer.cs b/Assets/Scripts/SS3D/Systems/Electricity/MachinePowerConsumer.cs new file mode 100644 index 0000000000..8fe52dc8fc --- /dev/null +++ b/Assets/Scripts/SS3D/Systems/Electricity/MachinePowerConsumer.cs @@ -0,0 +1,49 @@ +using FishNet.Object.Synchronizing; +using SS3D.Core; +using SS3D.Systems.Furniture; +using SS3D.Systems.Tile.Connections; +using UnityEngine; +namespace System.Electricity +{ + public class MachinePowerConsumer : BasicElectricDevice, IPowerConsumer + { + [SerializeField] + private float _powerConsumptionIdle = 1f; + [SerializeField] + private float _powerConsumptionInUse = 1f; + + public bool isIdle = true; + private bool _machineUsedOnce; + + [SyncVar(OnChange = nameof(SyncPowerStatus))] + private PowerStatus _powerStatus; + public float PowerNeeded + { + get + { + return isIdle ? _powerConsumptionIdle : _powerConsumptionInUse; + } + } + + public event EventHandler OnPowerStatusUpdated; + public PowerStatus PowerStatus { get => _powerStatus; set => _powerStatus = value; } + + private void SyncPowerStatus(PowerStatus oldValue, PowerStatus newValue, bool asServer) + { + OnPowerStatusUpdated?.Invoke(this, newValue); + } + + public void UseMachineOnce() + { + isIdle = false; + _machineUsedOnce = true; + Subsystems.Get().OnTick += HandleMachineWasUsed; + } + + private void HandleMachineWasUsed() + { + _machineUsedOnce = false; + Subsystems.Get().OnTick -= HandleMachineWasUsed; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/SS3D/Systems/Electricity/MachinePowerConsumer.cs.meta b/Assets/Scripts/SS3D/Systems/Electricity/MachinePowerConsumer.cs.meta new file mode 100644 index 0000000000..6a416dc924 --- /dev/null +++ b/Assets/Scripts/SS3D/Systems/Electricity/MachinePowerConsumer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ea15f8a26dac4e4193bb75335cb9e9cd +timeCreated: 1711310826 \ No newline at end of file diff --git a/Assets/Scripts/SS3D/Systems/Furniture/VendingMachine.cs b/Assets/Scripts/SS3D/Systems/Furniture/VendingMachine.cs index 08c8098428..73cf2c7781 100644 --- a/Assets/Scripts/SS3D/Systems/Furniture/VendingMachine.cs +++ b/Assets/Scripts/SS3D/Systems/Furniture/VendingMachine.cs @@ -6,7 +6,10 @@ using SS3D.Logging; using SS3D.Systems.Audio; using SS3D.Systems.Inventory.Items; +using System; +using System.Electricity; using UnityEngine; +using Random = UnityEngine.Random; namespace SS3D.Systems.Furniture { @@ -17,6 +20,9 @@ namespace SS3D.Systems.Furniture /// public class VendingMachine : InteractionSource, IInteractionTarget { + [SerializeField] + private MachinePowerConsumer _powerConsumer; + /// /// The products available to dispense and their stock. /// @@ -45,6 +51,11 @@ public void CmdDispenseProduct(int productIndex) [Server] public void DispenseProduct(int productIndex) { + if (_powerConsumer.PowerStatus == PowerStatus.Inactive) + { + return; + } + if (productIndex >= _productsToDispense.Length) { Log.Error(this, $"Product with index {productIndex} not found in products to dispense in {gameObject.name}. " @@ -66,6 +77,7 @@ public void DispenseProduct(int productIndex) return; } + _powerConsumer.UseMachineOnce(); productToDispenseStock.Stock--; Subsystems.Get().PlayAudioSource(Audio.AudioType.Sfx, Sounds.Can1, Position, NetworkObject, false, 0.7f, 1, 1, 3); @@ -79,6 +91,11 @@ public void DispenseProduct(int productIndex) /// public IInteraction[] CreateTargetInteractions(InteractionEvent interactionEvent) { + if (_powerConsumer.PowerStatus == PowerStatus.Inactive) + { + return Array.Empty(); + } + IInteraction[] interactions = new IInteraction[_productsToDispense.Length]; for (int i = 0; i < _productsToDispense.Length; i++) {