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

Created ItemService to handle items and item pools.

parent 8de7595a
No related branches found
No related tags found
1 merge request!23Created ItemService.
......@@ -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);
}
}
......
fileFormatVersion: 2
guid: c3d1c0c020522874bb0830077e109d3f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
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
fileFormatVersion: 2
guid: fe48543826f888744847f1c3ee05f2c8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
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