Skip to content
Snippets Groups Projects
Commit b6dce7d7 authored by Robin Halseth Sandvik's avatar Robin Halseth Sandvik
Browse files

Updated ItemService to use reflection.

- Now creates a new instance of the item instead of returning the same one.
- Grabs all implementations of the item class dynamically at start.
parent dd645dba
No related branches found
No related tags found
1 merge request!23Created ItemService.
...@@ -2,6 +2,8 @@ using System.Collections; ...@@ -2,6 +2,8 @@ using System.Collections;
using System; using System;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
...@@ -13,20 +15,36 @@ namespace BigSock.Service { ...@@ -13,20 +15,36 @@ namespace BigSock.Service {
/* /*
Service for handling items. Service for handling items.
*/ */
public class ItemService { public partial class ItemService {
/* /*
The instance to use. The instance to use.
*/ */
public static readonly ItemService SINGLETON = new ItemService(); public static readonly ItemService SINGLETON = new ItemService();
/*
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]);
}
}
public partial class ItemService {
private Dictionary<ulong, IItem> _items = new Dictionary<ulong, IItem>(); private Dictionary<ulong, IItem> _items = new Dictionary<ulong, IItem>();
private List<IItem> _itemList = new List<IItem>(); private List<IItem> _itemList = new List<IItem>();
private System.Random _rnd = new System.Random(); private System.Random _rnd = new System.Random();
private ItemService() { private ItemService() {
_loadItems(); _loadItems();
} }
...@@ -34,39 +52,32 @@ namespace BigSock.Service { ...@@ -34,39 +52,32 @@ namespace BigSock.Service {
/* /*
Load the items into the dictionary. Load the items into the dictionary.
(Hard-coded for now, use reflection later) (Hard-coded for now, use reflection later)
Reflection code: https://stackoverflow.com/a/6944605
*/ */
private void _loadItems() { private void _loadItems() {
_items = new Dictionary<ulong, IItem>{ // Get the classs that inherit the item base class.
{ 101, new ItemRunningShoes() }, var types = Assembly
{ 201, new ItemFourEyes() }, .GetAssembly(typeof(ItemBase))
{ 102, new ItemLunch() }, .GetTypes()
{ 103, new ItemCoffee() }, .Where(myType => myType.IsClass
}; && !myType.IsAbstract
&& myType.IsSubclassOf(typeof(ItemBase)));
_itemList = _items.Values.ToList();
}
/* // Create list of instances.
Creates a new instance of the object. _itemList = types
*/ .Select(t => (IItem) Activator.CreateInstance(t, new object[0]))
//private T _new<T>(T obj) where T : IItem, new() { return new T(); } .ToList();
private T _new<T>(T obj) where T : IItem { return obj; }
/* // Map to a dictionary by their ids.
Get an instance of the item of the given id. _items = _itemList
*/ .ToDictionary(t => t.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. Creates a new instance of the object.
*/ */
public IItem GetRandom() { private IItem _new(IItem obj)
var num = _rnd.Next(_itemList.Count); => (IItem) Activator.CreateInstance(obj.GetType(), new object[0]);
return _new(_itemList[num]);
}
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment