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

New items system.

- Items class hierarchy.
- Inventory class.
- CharacterStats class.
- Applies modifiers to character's stats.
parent 6bfe0aa8
No related branches found
No related tags found
1 merge request!20New items system.
Showing
with 468 additions and 7 deletions
...@@ -3,6 +3,7 @@ using System; ...@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
using BigSock.Item;
namespace BigSock { namespace BigSock {
// Common class for all characters. // Common class for all characters.
...@@ -11,7 +12,7 @@ namespace BigSock { ...@@ -11,7 +12,7 @@ namespace BigSock {
/* /*
Attack speed of the character. Attack speed of the character.
*/ */
public float AttackSpeed => baseAttackSpeed; public float AttackSpeed => Stats.AttackSpeed;
public float baseAttackSpeed = 1; public float baseAttackSpeed = 1;
/* /*
...@@ -22,20 +23,20 @@ namespace BigSock { ...@@ -22,20 +23,20 @@ namespace BigSock {
/* /*
Movement speed of the character. Movement speed of the character.
*/ */
public float MovementSpeed => baseMovementSpeed; public float MovementSpeed => Stats.MoveSpeed;
public float baseMovementSpeed = 1; public float baseMovementSpeed = 1;
/* /*
Damage of the character. Damage of the character.
*/ */
public float Damage => baseDamage; public float Damage => Stats.Damage;
public float baseDamage = 1; public float baseDamage = 1;
/* /*
Knockback force Knockback force
*/ */
public float KnockbackForce => knockbackForce; public float KnockbackForce => Stats.Knockback;
public float knockbackForce = 150; public float knockbackForce = 150;
...@@ -52,7 +53,7 @@ namespace BigSock { ...@@ -52,7 +53,7 @@ namespace BigSock {
/* /*
Maximum hit points of the character. Maximum hit points of the character.
*/ */
public float MaxHP => baseMaxHP; public float MaxHP => Stats.MaxHP;
public float baseMaxHP = 10; public float baseMaxHP = 10;
...@@ -81,8 +82,58 @@ namespace BigSock { ...@@ -81,8 +82,58 @@ namespace BigSock {
protected Rigidbody2D rb; protected Rigidbody2D rb;
void Start() { /*
rb = GetComponent<Rigidbody2D>(); The inventory of the character.
*/
public Inventory Inventory { get; protected set; } = new Inventory();
/*
The base stats of the character.
*/
public ICharacterStats BaseStats { get; protected set; } = new CharacterStats();
/*
The final stats of the character after applying modifiers.
*/
public ICharacterStats Stats { get; protected set; } = new CharacterStats();
protected virtual void Start() {
rb = GetComponent<Rigidbody2D>();
// Set the base stats.
BaseStats = new CharacterStats {
MaxHP = baseMaxHP,
Damage = baseDamage,
MoveSpeed = baseMovementSpeed,
Knockback = knockbackForce,
Range = 1,
AttackSpeed = baseAttackSpeed,
};
UpdateModifiers();
}
/*
Updates the modifiers to the character's stats.
*/
public void UpdateModifiers(ICharacterStats modifiers = null) {
modifiers ??= Inventory.Modifier;
Stats = BaseStats.Modify(modifiers);
}
/*
Try to pick up an item.
*/
public bool TryPickUpItem(IItem item) {
if(Inventory.AddItem(item)) {
UpdateModifiers();
print($"[Character.TryPickUpItem()] {item.Name} picked up. ({Inventory.Items.Count}/{Inventory.Cap})");
return true;
}
print($"[Character.TryPickUpItem()] {item.Name} NOT picked up. ({Inventory.Items.Count}/{Inventory.Cap})");
return false;
} }
/* /*
......
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock {
/*
class represents the stats of a character.
*/
public class CharacterStats : ICharacterStats {
/*
The hp of the character.
*/
public float MaxHP { get; set; }
/*
The damage of the character.
*/
public float Damage { get; set; }
/*
The movement speed of the character.
*/
public float MoveSpeed { get; set; }
/*
The knockback of the character.
*/
public float Knockback { get; set; }
/*
The range of the character.
*/
public float Range { get; set; }
/*
The attack speed of the character.
*/
public float AttackSpeed { get; set; }
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 697f21373556c9549b064efc46c9730b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock {
/*
Interface represents the stats of a character.
*/
public interface ICharacterStats {
/*
The hp of the character.
*/
float MaxHP { get; }
/*
The damage of the character.
*/
float Damage { get; }
/*
The movement speed of the character.
*/
float MoveSpeed { get; }
/*
The knockback of the character.
*/
float Knockback { get; }
/*
The range of the character.
*/
float Range { get; }
/*
The attack speed of the character.
*/
float AttackSpeed { get; }
}
/*
Holds extension methods for character stat objects.
*/
public static class CharacterStatsExtension {
/*
Identity object for character stats.
*/
public static readonly ICharacterStats IDENTITY = new CharacterStats{
MaxHP = 1,
Damage = 1,
MoveSpeed = 1,
Knockback = 1,
Range = 1,
AttackSpeed = 1,
};
/*
Adds the values of 2 character stats together.
*/
public static ICharacterStats Add(this ICharacterStats a, ICharacterStats b) {
return new CharacterStats{
MaxHP = a.MaxHP + b.MaxHP,
Damage = a.Damage + b.Damage,
MoveSpeed = a.MoveSpeed + b.MoveSpeed,
Knockback = a.Knockback + b.Knockback,
Range = a.Range + b.Range,
AttackSpeed = a.AttackSpeed + b.AttackSpeed,
};
}
/*
Multiplies the values of 2 character stats together.
*/
public static ICharacterStats Multiply(this ICharacterStats a, ICharacterStats b) {
return new CharacterStats{
MaxHP = a.MaxHP * b.MaxHP,
Damage = a.Damage * b.Damage,
MoveSpeed = a.MoveSpeed * b.MoveSpeed,
Knockback = a.Knockback * b.Knockback,
Range = a.Range * b.Range,
AttackSpeed = a.AttackSpeed * b.AttackSpeed,
};
}
/*
Modifies the first stat object by the second one.
Ex.: if the second is 20% hp, the result will be the first one, but with 20% more hp.
*/
public static ICharacterStats Modify(this ICharacterStats a, ICharacterStats b) {
return a.Multiply(b.Add(IDENTITY));
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: d67cdc4f8a1fd31439d31116ad4821b7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 023fe0779d2abeb44ad4470ad4e5642a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: fdc1d36c8d53fe54898d8cccff53b536
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock.Item {
/*
A class that represents an item that an effect when a condition is meet.
*/
public abstract class ConditionalItemBase : ItemBase {
/*
The type of trigger this item uses.
*/
public TriggerType Trigger { get; set; }
/*
The handler to activate when the condition is triggered.
*/
public Action<ICharEventParams> Handler { get; set; }
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 15064819886e52a4a85b803e26467c05
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock.Item {
/*
An interface representing a generic item.
*/
public interface IItem {
/*
The name of the item.
*/
string Name { get; }
/*
The description of the item.
*/
string Description { get; }
/*
The id of the item.
*/
ulong Id { get; }
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 0a4fa79a9560deb45976fa4bebd5d1ec
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock.Item {
/*
A class that represents the root of all items.
*/
public abstract class ItemBase : IItem {
/*
The name of the item.
*/
public string Name { get; protected set; }
/*
The description of the item.
*/
public string Description { get; protected set; }
/*
The id of the item.
*/
public ulong Id { get; protected set; }
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: e99afc192b85f3d43a7159cbbd68a39c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock.Item {
/*
A class that represents an item that provides passive stat boosts.
*/
public abstract class PassiveItemBase : ItemBase {
/*
The modifier of the item.
*/
public ICharacterStats Modifier { get; set; }
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 779b8b8f25258cf4983c1ac9d442cb8c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock.Item {
/*
The type of trigger that activates the item's effect.
*/
public enum TriggerType {
None,
OnKill,
OnAttack,
OnHit,
OnDamage,
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 39ba995ac8dbf00498eb995c98f71402
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 9b2c87c112ffd1a4dbacf0405214b30b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock {
/*
Class that represents the parameters of a generic character event.
*/
public class CharEventParams : ICharEventParams {
/*
The character that own's the event.
*/
public Character Source { get; set; }
/*
The character that triggered the event. (If any)
ex.: OnKill -> the killed enemy. OnDamage -> Enemy that dealt the damage.
*/
public Character Target { get; set; }
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 69465ff2019a5bb428771fa1c74e4f41
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