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;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.InputSystem;
......@@ -13,20 +15,36 @@ namespace BigSock.Service {
/*
Service for handling items.
*/
public class ItemService {
public partial class ItemService {
/*
The instance to use.
*/
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 List<IItem> _itemList = new List<IItem>();
private System.Random _rnd = new System.Random();
private ItemService() {
_loadItems();
}
......@@ -34,39 +52,32 @@ namespace BigSock.Service {
/*
Load the items into the dictionary.
(Hard-coded for now, use reflection later)
Reflection code: https://stackoverflow.com/a/6944605
*/
private void _loadItems() {
_items = new Dictionary<ulong, IItem>{
{ 101, new ItemRunningShoes() },
{ 201, new ItemFourEyes() },
{ 102, new ItemLunch() },
{ 103, new ItemCoffee() },
};
_itemList = _items.Values.ToList();
}
// Get the classs that inherit the item base class.
var types = Assembly
.GetAssembly(typeof(ItemBase))
.GetTypes()
.Where(myType => myType.IsClass
&& !myType.IsAbstract
&& myType.IsSubclassOf(typeof(ItemBase)));
/*
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; }
// Create list of instances.
_itemList = types
.Select(t => (IItem) Activator.CreateInstance(t, new object[0]))
.ToList();
/*
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;
// Map to a dictionary by their ids.
_items = _itemList
.ToDictionary(t => t.Id);
}
/*
Get a random item from the item pool.
Creates a new instance of the object.
*/
public IItem GetRandom() {
var num = _rnd.Next(_itemList.Count);
return _new(_itemList[num]);
}
private IItem _new(IItem obj)
=> (IItem) Activator.CreateInstance(obj.GetType(), new object[0]);
}
}
\ 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