riversong code showcase
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class StorageBuildingPanelSectionPresenter : IBuildingPanelSectionPresenter
|
||||
{
|
||||
private const int RequestedAmountLargeDelta = 5;
|
||||
|
||||
private readonly IProductCatalog _productCatalog;
|
||||
|
||||
public StorageBuildingPanelSectionPresenter(IProductCatalog productCatalog)
|
||||
{
|
||||
_productCatalog = productCatalog;
|
||||
}
|
||||
|
||||
public bool IsSectionRelevant(Building building)
|
||||
{
|
||||
return building.Definition.IsStorage;
|
||||
}
|
||||
|
||||
public void InitializeSection(BuildingPanelModel model, Building building)
|
||||
{
|
||||
model.StorageModel.Products.Clear();
|
||||
|
||||
for (var productHandle = 0; productHandle < _productCatalog.ProductTypeCount; productHandle++)
|
||||
{
|
||||
var productModel = new StoragePanelProductModel();
|
||||
|
||||
productModel.Initialize(
|
||||
productHandle,
|
||||
productHandle,
|
||||
_productCatalog.GetProduct(productHandle),
|
||||
() => ToggleAllowTaking(building, productModel),
|
||||
() => ToggleCanFulfillRequests(building, productModel),
|
||||
() => ChangeRequestedAmount(building, productModel, Keyboard.current.shiftKey.isPressed ? -RequestedAmountLargeDelta : -1),
|
||||
() => ChangeRequestedAmount(building, productModel, Keyboard.current.shiftKey.isPressed ? RequestedAmountLargeDelta : 1));
|
||||
|
||||
model.StorageModel.Products.Add(productModel);
|
||||
}
|
||||
|
||||
UpdateSection(model, building);
|
||||
|
||||
model.StorageModel.NotifyChanged();
|
||||
}
|
||||
|
||||
public void UpdateSection(BuildingPanelModel model, Building building)
|
||||
{
|
||||
ref var storage = ref building.GetStorageRW();
|
||||
ref var storagePolicy = ref building.GetProductStoragePolicyRW();
|
||||
|
||||
foreach (var productModel in model.StorageModel.Products)
|
||||
{
|
||||
var productHandle = productModel.ProductHandle;
|
||||
|
||||
productModel.AvailableNow = storage.AvailableNow(productHandle);
|
||||
productModel.InputBlocked = !storagePolicy.IsTakingAllowed(productHandle);
|
||||
productModel.CanFulfillRequests = storagePolicy.CanFulfillRequests(productHandle);
|
||||
productModel.RequestedAmount = storagePolicy.GetRequestedAmount(productHandle);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ToggleAllowTaking(Building building, StoragePanelProductModel productModel)
|
||||
{
|
||||
ref var storagePolicy = ref building.GetProductStoragePolicyRW();
|
||||
|
||||
var isTakingAllowed = storagePolicy.IsTakingAllowed(productModel.ProductHandle);
|
||||
|
||||
storagePolicy.SetAllowTaking(productModel.ProductHandle, !isTakingAllowed);
|
||||
productModel.InputBlocked = isTakingAllowed;
|
||||
}
|
||||
|
||||
private static void ToggleCanFulfillRequests(Building building, StoragePanelProductModel productModel)
|
||||
{
|
||||
ref var storagePolicy = ref building.GetProductStoragePolicyRW();
|
||||
|
||||
var canFulfillRequests = storagePolicy.CanFulfillRequests(productModel.ProductHandle);
|
||||
|
||||
storagePolicy.SetCanFulfillRequests(productModel.ProductHandle, !canFulfillRequests);
|
||||
productModel.CanFulfillRequests = !canFulfillRequests;
|
||||
}
|
||||
|
||||
private static void ChangeRequestedAmount(Building building, StoragePanelProductModel productModel, int delta)
|
||||
{
|
||||
ref var storage = ref building.GetStorageRW();
|
||||
ref var storagePolicy = ref building.GetProductStoragePolicyRW();
|
||||
|
||||
var requestedAmount = storagePolicy.GetRequestedAmount(productModel.ProductHandle) + delta;
|
||||
requestedAmount = Mathf.Clamp(requestedAmount, 0, storage.Capacity);
|
||||
|
||||
storagePolicy.SetRequestedAmount(productModel.ProductHandle, requestedAmount);
|
||||
productModel.RequestedAmount = requestedAmount;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user