Extend VContainer DI: eliminate manual composition and duplicate references
- Register InventoryModel, ShopModel as container-managed singletons - Register GameController, ShopController, InventoryController via RegisterComponent - Replace [SerializeField] with [Inject] for service dependencies in controllers - Move maxActiveModifierSlots config to GameLifetimeScope (composition root) - Remove manual model creation and Initialize() calls from GameController - Add ToggleVisibility() to ShopController/InventoryController, removing GetComponentInChildren - Move event subscriptions from Awake to Start for safe VContainer injection order - Transfer game startup orchestration to GameController.Start() Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
using YachtDice.Economy;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
|
||||
@@ -6,27 +7,30 @@ namespace YachtDice.Shop
|
||||
{
|
||||
public class ShopController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private ModifierCatalogSO catalog;
|
||||
[SerializeField] private ShopView shopView;
|
||||
[SerializeField] private CurrencyBank currencyBank;
|
||||
|
||||
private ModifierCatalogSO catalog;
|
||||
private CurrencyBank currencyBank;
|
||||
private ShopModel model;
|
||||
|
||||
public ModifierCatalogSO Catalog => catalog;
|
||||
|
||||
public void Initialize(ShopModel shopModel)
|
||||
[Inject]
|
||||
public void Construct(ModifierCatalogSO catalog, CurrencyBank currencyBank, ShopModel model)
|
||||
{
|
||||
model = shopModel;
|
||||
this.catalog = catalog;
|
||||
this.currencyBank = currencyBank;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
shopView.OnBuyClicked += HandleBuyClicked;
|
||||
|
||||
if (currencyBank != null)
|
||||
currencyBank.OnBalanceChanged += HandleCurrencyChanged;
|
||||
|
||||
currencyBank.OnBalanceChanged += HandleCurrencyChanged;
|
||||
model.OnItemPurchased += HandleItemPurchased;
|
||||
|
||||
shopView.Populate(catalog.All, model);
|
||||
shopView.UpdateCurrencyDisplay(currencyBank != null ? currencyBank.Balance : 0);
|
||||
shopView.UpdateCurrencyDisplay(currencyBank.Balance);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
@@ -41,6 +45,16 @@ namespace YachtDice.Shop
|
||||
model.OnItemPurchased -= HandleItemPurchased;
|
||||
}
|
||||
|
||||
public void ToggleVisibility()
|
||||
{
|
||||
if (shopView == null) return;
|
||||
|
||||
if (shopView.IsVisible)
|
||||
shopView.Hide();
|
||||
else
|
||||
shopView.Show();
|
||||
}
|
||||
|
||||
private void HandleBuyClicked(ModifierDefinitionSO def)
|
||||
{
|
||||
model.TryPurchase(def);
|
||||
|
||||
Reference in New Issue
Block a user