Skip to content

Commit

Permalink
Fix vendor machines and add multiple options and stock (#1439)
Browse files Browse the repository at this point in the history
* Fix vendor machines and add multiple options and stock

* Fix logging

---------

Co-authored-by: Mechar418 <iamteapot42@gmail.com>
  • Loading branch information
David-Goru and iamteapot422 authored Jun 23, 2024
1 parent 1be4ebe commit 4ee9895
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ MonoBehaviour:
_componentIndexCache: 0
_addedNetworkObject: {fileID: 1871071288925904272}
_networkObjectCache: {fileID: 1871071288925904272}
_productToDispense: 5
_productsToDispense:
- Product: {fileID: 11400000, guid: edcff839a189118479387da05ec07d49, type: 2}
Stock: 5
_dispensingTransform: {fileID: 5160227759105755511}
--- !u!114 &-8220440043154804263
MonoBehaviour:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ GameObject:
- component: {fileID: 3665146264900623842}
- component: {fileID: 8086907785789700544}
m_Layer: 0
m_Name: VendorRobustSoftDrinks
m_Name: VendorRobustSoftdrinks
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
Expand Down Expand Up @@ -153,7 +153,21 @@ MonoBehaviour:
_componentIndexCache: 0
_addedNetworkObject: {fileID: 1871071288925904272}
_networkObjectCache: {fileID: 1871071288925904272}
_productToDispense: 3
_productsToDispense:
- Product: {fileID: 11400000, guid: dd302b5636b41bb48ba3a2cdc72cc64c, type: 2}
Stock: 10
- Product: {fileID: 11400000, guid: a49cca9d10e0a3146aae1906542ea50a, type: 2}
Stock: 10
- Product: {fileID: 11400000, guid: e450161ce63de9045b97061a364533b1, type: 2}
Stock: 10
- Product: {fileID: 11400000, guid: 5c76cb2799458404db16e87cc1606975, type: 2}
Stock: 10
- Product: {fileID: 11400000, guid: 6ccd0b8aea90f5a489d73c02ccab3a98, type: 2}
Stock: 10
- Product: {fileID: 11400000, guid: be837baab1848a14d944ab050bfebc15, type: 2}
Stock: 10
- Product: {fileID: 11400000, guid: f5c190c5bf049bc429ca90309c334105, type: 2}
Stock: 10
_dispensingTransform: {fileID: 5160227759105755511}
--- !u!114 &3665146264900623842
MonoBehaviour:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,15 @@ MonoBehaviour:
_componentIndexCache: 0
_addedNetworkObject: {fileID: 1871071288925904272}
_networkObjectCache: {fileID: 1871071288925904272}
_productToDispense: 11
_productsToDispense:
- Product: {fileID: 11400000, guid: ecb1cb0479842954a925e9013baf95f6, type: 2}
Stock: 5
- Product: {fileID: 11400000, guid: 3e3beac0c217b7c4492869d62e50a5b8, type: 2}
Stock: 5
- Product: {fileID: 11400000, guid: 2d3da1772e540e644a1b7cb548477c0c, type: 2}
Stock: 5
- Product: {fileID: 11400000, guid: 0628fd4af4e63fe4c884827fcb3ced1a, type: 2}
Stock: 5
_dispensingTransform: {fileID: 5160227759105755511}
--- !u!114 &-8922800647233213677
MonoBehaviour:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,15 @@ MonoBehaviour:
_componentIndexCache: 0
_addedNetworkObject: {fileID: 1871071288925904272}
_networkObjectCache: {fileID: 1871071288925904272}
_productToDispense: 2
_productsToDispense:
- Product: {fileID: 11400000, guid: 4d5800940a547b2419e83ec0ccd555d4, type: 2}
Stock: 5
- Product: {fileID: 11400000, guid: 4b6f5ee7c28256e4783f03f8d96872b7, type: 2}
Stock: 5
- Product: {fileID: 11400000, guid: 4c4f968211b927b4aa55e22fc7adcb2b, type: 2}
Stock: 5
- Product: {fileID: 11400000, guid: 58b1ed19ea55c074cb719ae4c4791cf1, type: 2}
Stock: 5
_dispensingTransform: {fileID: 5160227759105755511}
--- !u!114 &6841733904447030618
MonoBehaviour:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using SS3D.Interactions;
using SS3D.Interactions.Extensions;
using SS3D.Interactions.Interfaces;
using SS3D.Systems.Tile;
using UnityEngine;

namespace SS3D.Systems.Furniture
Expand All @@ -11,10 +12,14 @@ namespace SS3D.Systems.Furniture
/// </summary>
public class DispenseProductInteraction : Interaction
{
public string ProductName;
public int ProductStock;
public int ProductIndex;

/// <inheritdoc />
public override string GetName(InteractionEvent interactionEvent)
{
return "Dispense";
return $"Dispense {ProductName} (x{ProductStock})";
}

/// <inheritdoc />
Expand Down Expand Up @@ -44,7 +49,7 @@ public override bool Start(InteractionEvent interactionEvent, InteractionReferen

if (target is VendingMachine vendingMachine)
{
vendingMachine.DispenseProduct();
vendingMachine.DispenseProduct(ProductIndex);
}
return false;
}
Expand Down
60 changes: 46 additions & 14 deletions Assets/Scripts/SS3D/Systems/Furniture/VendingMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using SS3D.Data.Generated;
using SS3D.Interactions;
using SS3D.Interactions.Interfaces;
using SS3D.Logging;
using SS3D.Systems.Audio;
using SS3D.Systems.Inventory.Items;
using UnityEngine;

Expand All @@ -12,16 +14,14 @@ namespace SS3D.Systems.Furniture
/// Simple implementation of a vending machine. This has many todos.
///
/// TODO: Make proper UI for this.
/// TODO: Dispensing products should reduce their stock.
/// </summary>
public class VendingMachine : InteractionSource, IInteractionTarget
{
/// <summary>
/// The product to dispense.
/// TODO: Make a new struct for different products, and to support multiple products.
/// The products available to dispense and their stock.
/// </summary>
[SerializeField]
private GameObject _productToDispense;
private VendingMachineProductStock[] _productsToDispense;

/// <summary>
/// The transform representation of where the dispensed products should spawn at.
Expand All @@ -30,36 +30,68 @@ public class VendingMachine : InteractionSource, IInteractionTarget
private Transform _dispensingTransform;

/// <summary>
/// Requests the server to dispense a product.
/// Requests the server to dispense a specific product.
/// </summary>
[ServerRpc(RequireOwnership = false)]
public void CmdDispenseProduct()
public void CmdDispenseProduct(int productIndex)
{
DispenseProduct();
DispenseProduct(productIndex);
}

/// <summary>
/// Dispenses the vending machine product at the dispensing transform position with a random rotation.
/// Dispenses a specific vending machine product at the dispensing transform position with a random rotation.
/// If there's not enough stock, a sound is played and the product isn't dispensed.
/// </summary>
[Server]
public void DispenseProduct()
public void DispenseProduct(int productIndex)
{
if (productIndex >= _productsToDispense.Length)
{
Log.Error(this, $"Product with index {productIndex} not found in products to dispense in {gameObject.name}. "
+ $"Max possible index is {_productsToDispense.Length - 1}");
return;
}

if (productIndex < 0)
{
Log.Error(this, $"Invalid product index, value must be between 0 and {_productsToDispense.Length - 1}");
return;
}

VendingMachineProductStock productToDispenseStock = _productsToDispense[productIndex];
if (productToDispenseStock.Stock <= 0)
{
Subsystems.Get<AudioSystem>().PlayAudioSource(Audio.AudioType.Sfx, Sounds.BikeHorn, Position, NetworkObject,
false, 0.7f, 1, 1, 3);
return;
}

productToDispenseStock.Stock--;
Subsystems.Get<AudioSystem>().PlayAudioSource(Audio.AudioType.Sfx, Sounds.Can1, Position, NetworkObject,
false, 0.7f, 1, 1, 3);

ItemSystem itemSystem = Subsystems.Get<ItemSystem>();
Quaternion quaternion = Quaternion.Euler(new Vector3(Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360)));

itemSystem.SpawnItem(_productToDispense.name, _dispensingTransform.position, quaternion);
itemSystem.SpawnItem(productToDispenseStock.Product.name, _dispensingTransform.position, quaternion);
}

/// <inheritdoc />
public IInteraction[] CreateTargetInteractions(InteractionEvent interactionEvent)
{
return new IInteraction[]
IInteraction[] interactions = new IInteraction[_productsToDispense.Length];
for (int i = 0; i < _productsToDispense.Length; i++)
{
new DispenseProductInteraction
interactions[i] = new DispenseProductInteraction
{
ProductName = _productsToDispense[i].Product.NameString,
ProductIndex = i,
ProductStock = _productsToDispense[i].Stock,
Icon = InteractionIcons.Take
}
};
};
}

return interactions;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using SS3D.Systems.Tile;

namespace SS3D.Systems.Furniture
{
[System.Serializable]
public class VendingMachineProductStock
{
public ItemObjectSo Product;
public int Stock;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4ee9895

Please sign in to comment.