Files
Shopkeeper/Assets/Scripts/Items/TillSystem.cs
Caleb Sandford deQuincey e9c9bc4409 initial game submission
2025-06-11 13:04:35 +01:00

116 lines
3.0 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class TillSystem : MonoBehaviour
{
public static TillSystem Instance;
void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
public GameObject tillUIPanel;
private bool isTillOpen = false;
private float enteredPrice = 0f;
private CustomerAI currentCustomer;
private Queue<ShopItemData> itemsToScan = new Queue<ShopItemData>();
private float runningTotal = 0f;
private float customerTendered = 0f;
void Start()
{
tillUIPanel.SetActive(false);
LockCursor(true);
}
void Update()
{
}
public void OpenTill(CustomerAI customer)
{
isTillOpen = true;
tillUIPanel.SetActive(true);
LockCursor(false);
currentCustomer = customer;
itemsToScan.Clear();
foreach (var entry in customer.itemsToBuy)
{
for (int i = 0; i < entry.quantity; i++)
{
ShopItemData shopItem = entry.item;
itemsToScan.Enqueue(shopItem);
}
}
runningTotal = 0f;
// Update UI for new transaction
}
public void ScanNextItem()
{
if (itemsToScan.Count > 0)
{
var item = itemsToScan.Dequeue();
runningTotal += item.price;
// Update UI: show item and price
}
// If no items left, enable "Total" button
}
public void PressTotal()
{
// Show total on UI
// Simulate customer payment
customerTendered = currentCustomer.Pay(runningTotal);
// Show tendered amount on UI
}
public void HandOverChange(float changeGiven)
{
float correctChange = customerTendered - runningTotal;
bool correct = Mathf.Approximately(changeGiven, correctChange);
// Show green/red on UI
// Proceed to next customer
_ = currentCustomer.LeaveShop();
currentCustomer = null;
// Reset UI for next customer
}
public void CloseTill()
{
isTillOpen = false;
tillUIPanel.SetActive(false);
LockCursor(true); // Lock cursor when till is closed
}
void LockCursor(bool locked)
{
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
Cursor.visible = !locked;
}
public void EnterPrice(float price)
{
enteredPrice = price;
Debug.Log($"📌 Entered Price: £{enteredPrice:0.00}");
}
public void ProcessTransaction(float customerTendered)
{
if (customerTendered >= enteredPrice)
{
FinanceManager.Instance.AddRevenue(enteredPrice);
GeneralUIController.Instance.ShowMessage($"✅ Sale Complete! Change: £{customerTendered - enteredPrice:0.00}");
}
else
{
GeneralUIController.Instance.ShowMessage("🚨 Price too high! Customer walks away.");
}
}
public bool IsTillOpen => isTillOpen;
}