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

Merge branch 'master' into 'main'

Created ItemService.

See merge request !23
parents bb8cf539 b6dce7d7
No related branches found
No related tags found
2 merge requests!29Updating master,!23Created ItemService.
......@@ -25,7 +25,7 @@ namespace BigSock.Item {
/*
The max number of items the inventory can hold.
*/
public int Cap { get; set; } = 3;
public int Cap { get; set; } = 10;
/*
......
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock.Item {
/*
A passive item that increases user's max hp by 25%.
*/
public class ItemCoffee : PassiveItemBase {
public override ulong Id => 103;
public override string Name => "Caffeinated Coffee";
public override string Description => "Increases attack speed by 25%";
public ItemCoffee() {
Modifier = new CharacterStats{
AttackSpeed = 0.25f,
};
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: d8951e56770b7214ead328a100d9441e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -13,11 +13,26 @@ namespace BigSock.Item {
public class ItemFourEyes : OnHitItemBase {
public override ulong Id => 201;
public override string Name => "Four Eyes";
public override string Description => "30% chance to deal double dammage.";
public override string Description => "30% chance to deal double dammage. Has a 2 second cooldown.";
public static readonly double CHANCE = 0.3;
public static readonly TimeSpan COOLDOWN = new TimeSpan(0, 0, 0, 2, 0);
/*
Stores the next time the character can recieve damage.
*/
public DateTime NextTimeCanTrigger { get; private set; } = DateTime.Now;
public ItemFourEyes() { }
public override void Handler(Character source, Character target, AttackStats attack) {
// Check if the cooldown has happened yet.
if(NextTimeCanTrigger > DateTime.Now) return;
// Start new trigger time.
NextTimeCanTrigger = DateTime.Now + COOLDOWN;
// Check if it triggers.
var roll = RND.NextDouble();
if(roll >= CHANCE) {
attack.Damage *= 2;
......
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock.Item {
/*
A passive item that increases user's max hp by 20%.
*/
public class ItemLunch : PassiveItemBase {
public override ulong Id => 102;
public override string Name => "Lunch";
public override string Description => "Increases hp by 20%";
public ItemLunch() {
Modifier = new CharacterStats{
MaxHP = 0.2f,
};
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: e4644e42ad4bf244093930558ebb86d0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -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 {
......@@ -46,7 +49,8 @@ namespace BigSock {
hpBar.SetHealth(Convert.ToInt32(HP));
//!! DEBUG: Add item to player at start to test if it works.
TryPickUpItem(new ItemFourEyes());
TryPickUpItem(ItemService.SINGLETON.Get(201));
TryPickUpItem(ItemService.SINGLETON.Get(201));
}
......@@ -103,7 +107,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 System.Reflection;
using UnityEngine;
using UnityEngine.InputSystem;
using BigSock.Item;
namespace BigSock.Service {
/*
Service for handling items.
*/
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();
}
/*
Load the items into the dictionary.
(Hard-coded for now, use reflection later)
Reflection code: https://stackoverflow.com/a/6944605
*/
private void _loadItems() {
// 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)));
// Create list of instances.
_itemList = types
.Select(t => (IItem) Activator.CreateInstance(t, new object[0]))
.ToList();
// Map to a dictionary by their ids.
_items = _itemList
.ToDictionary(t => t.Id);
}
/*
Creates a new instance of the object.
*/
private IItem _new(IItem obj)
=> (IItem) Activator.CreateInstance(obj.GetType(), new object[0]);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: fe48543826f888744847f1c3ee05f2c8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -53,7 +53,7 @@ namespace BigSock {
void OnCollisionEnter2D(Collision2D collision)
{
print($"[AttackMovement.OnCollisionEnter2D()] {collision.transform.position}");
//print($"[AttackMovement.OnCollisionEnter2D()] {collision.transform.position}");
var target = collision.gameObject.GetComponent<Character>();
if(target != null) {
......
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