Browse Source

Improve self-managed observers, remove when inactive

Devin Braune 5 years ago
parent
commit
02e0ead3c2
  1. 2
      Assets/Scripts/Shop/Model/BuyModel.cs
  2. 14
      Assets/Scripts/Shop/View/ShopView.cs
  3. 4
      Assets/Scripts/Shop/View/ShopViewGrid.cs
  4. 8
      Assets/Scripts/Shop/View/ViewItemContainer.cs

2
Assets/Scripts/Shop/Model/BuyModel.cs

@ -20,7 +20,7 @@ public class BuyModel : ShopModel
{ {
inventory.RemoveItemByIndex(selectedItemIndex); // Before removing the item from the model's actual inventory inventory.RemoveItemByIndex(selectedItemIndex); // Before removing the item from the model's actual inventory
OnRemove(GetSelectedItem()); // If there's a view subscribed, this will probably remove the item from it OnRemove(GetSelectedItem()); // If there's a view subscribed, this will probably remove the item from it
SelectItemByIndex(selectedItemIndex > inventory.GetItemCount() ? --selectedItemIndex : selectedItemIndex); SelectItemByIndex(selectedItemIndex >= inventory.GetItemCount() ? --selectedItemIndex : selectedItemIndex);
} }
} }

14
Assets/Scripts/Shop/View/ShopView.cs

@ -31,16 +31,26 @@ public abstract class ShopView : MonoBehaviour, IShopModelObserver<Item>
protected ShopModel other; // Other model in MVC pattern (our own inventory) protected ShopModel other; // Other model in MVC pattern (our own inventory)
private ShopController shopController; //Controller in MVC pattern private ShopController shopController; //Controller in MVC pattern
protected virtual void Start() // Set up the view's necessary stuff before the view is enabled
protected virtual void Awake()
{ {
model = new BuyModel(2f, 16, 500); //Right now use magic values to set up the shop model = new BuyModel(2f, 16, 500); //Right now use magic values to set up the shop
shopController = gameObject.AddComponent<MouseController>().Initialize(model);//Set the default controller to be the mouse controller shopController = gameObject.AddComponent<MouseController>().Initialize(model);//Set the default controller to be the mouse controller
SetupItemIconView(); //Setup the grid view's properties SetupItemIconView(); //Setup the grid view's properties
PopulateItemIconView(); //Display items
InitializeButtons(); //Connect the buttons to the controller InitializeButtons(); //Connect the buttons to the controller
//model.Subscribe(this); //model.Subscribe(this);
} }
private void OnEnable()
{
PopulateItemIconView(); //Display items
}
private void OnDisable()
{
ClearIconView();
}
//------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------
// SetupItemIconView() // SetupItemIconView()
//------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------

4
Assets/Scripts/Shop/View/ShopViewGrid.cs

@ -15,12 +15,12 @@ public class ShopViewGrid : ShopView
//views. //views.
// Start is called before the first frame update // Start is called before the first frame update
protected override void Start() protected override void Awake()
{ {
viewConfig = Resources.Load<ViewConfig>("ViewConfig");//Load the ViewConfig scriptable object from the Resources folder viewConfig = Resources.Load<ViewConfig>("ViewConfig");//Load the ViewConfig scriptable object from the Resources folder
Debug.Assert(viewConfig != null); Debug.Assert(viewConfig != null);
Debug.Assert(_gridLayoutGroup != null); Debug.Assert(_gridLayoutGroup != null);
base.Start(); base.Awake();
print("ShopView Grid Initialised"); print("ShopView Grid Initialised");
} }

8
Assets/Scripts/Shop/View/ViewItemContainer.cs

@ -82,7 +82,7 @@ public class ViewItemContainer : MonoBehaviour, IItemContainer, IShopModelObserv
public void OnRemoved(Item item) public void OnRemoved(Item item)
{ {
if (item != Item) return; // Well we only want this if the item removed from the model is actually ours! if (item != Item) return; // Well we only want this if the item removed from the model is actually ours!
_unsubscriber.Dispose(); // Careful! If we don't make this thing aware that it manages itself, and we forget to manage it, we have a memory leak IsSelected = false; // Kind of pointless, but won't hurt
Destroy(gameObject); Destroy(gameObject);
} }
@ -90,4 +90,10 @@ public class ViewItemContainer : MonoBehaviour, IItemContainer, IShopModelObserv
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
// The reason we do this in OnDestroy() is so we don't have a memory leak when this gets removed externally somehow
private void OnDestroy()
{
_unsubscriber?.Dispose(); // Careful! If we don't make this thing aware that it manages itself, and we forget to manage it, we have a memory leak
}
} }

Loading…
Cancel
Save