diff --git a/MrBigsock/Assets/Code/PlayerController.cs b/MrBigsock/Assets/Code/PlayerController.cs index 7fcca51046af8da6f0d22f6e53aca3f351b52a9e..723e6cada0bcceb932495e66f3d2cc8974098c75 100644 --- a/MrBigsock/Assets/Code/PlayerController.cs +++ b/MrBigsock/Assets/Code/PlayerController.cs @@ -3,8 +3,11 @@ using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; + + using BigSock.UI; using BigSock.Item; +using BigSock.Service; namespace BigSock { @@ -103,7 +106,8 @@ namespace BigSock { //!! Code for testing the new item stuff. if(Input.GetKeyDown(KeyCode.Space)) { - TryPickUpItem(new ItemRunningShoes()); + var item = ItemService.SINGLETON.GetRandom(); // new ItemRunningShoes(); + TryPickUpItem(item); } } diff --git a/MrBigsock/Assets/Code/Services.meta b/MrBigsock/Assets/Code/Services.meta new file mode 100644 index 0000000000000000000000000000000000000000..97987a2fa59b5145745519a11feef91565c7ca79 --- /dev/null +++ b/MrBigsock/Assets/Code/Services.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c3d1c0c020522874bb0830077e109d3f +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MrBigsock/Assets/Code/Services/ItemService.cs b/MrBigsock/Assets/Code/Services/ItemService.cs new file mode 100644 index 0000000000000000000000000000000000000000..0317388eb055b956de63c35b3f620820939496b8 --- /dev/null +++ b/MrBigsock/Assets/Code/Services/ItemService.cs @@ -0,0 +1,72 @@ +using System.Collections; +using System; +using System.Linq; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.InputSystem; + +using BigSock.Item; + + +namespace BigSock.Service { + + /* + Service for handling items. + */ + public class ItemService { + /* + The instance to use. + */ + public static readonly ItemService SINGLETON = new ItemService(); + + + private Dictionary<ulong, IItem> _items = new Dictionary<ulong, IItem>(); + private List<IItem> _itemList = new List<IItem>(); + + + private System.Random _rnd = new System.Random(); + + + private ItemService() { + _loadItems(); + } + + /* + Load the items into the dictionary. + (Hard-coded for now, use reflection later) + */ + private void _loadItems() { + _items = new Dictionary<ulong, IItem>{ + { 101, new ItemRunningShoes() }, + { 201, new ItemFourEyes() }, + { 102, new ItemLunch() }, + { 103, new ItemCoffee() }, + }; + + _itemList = _items.Values.ToList(); + } + + /* + Creates a new instance of the object. + */ + //private T _new<T>(T obj) where T : IItem, new() { return new T(); } + private T _new<T>(T obj) where T : IItem { return obj; } + + /* + Get an instance of the item of the given id. + */ + public IItem Get(ulong id) { + if(_items.TryGetValue(id, out var res)) return _new(res); + return null; + } + + /* + Get a random item from the item pool. + */ + public IItem GetRandom() { + var num = _rnd.Next(_itemList.Count); + return _new(_itemList[num]); + } + + } +} \ No newline at end of file diff --git a/MrBigsock/Assets/Code/Services/ItemService.cs.meta b/MrBigsock/Assets/Code/Services/ItemService.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..b6453e1b1e506c98178add60761277751667716e --- /dev/null +++ b/MrBigsock/Assets/Code/Services/ItemService.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fe48543826f888744847f1c3ee05f2c8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: