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

Merge branch 'master' into 'main'

Implemented initial version of conditional items.

See merge request !22
parents 75078db9 5b0c576e
No related branches found
No related tags found
2 merge requests!29Updating master,!22Implemented initial version of conditional items.
Showing
with 449 additions and 31 deletions
......@@ -7,7 +7,7 @@ using BigSock.Item;
namespace BigSock {
// Common class for all characters.
public class Character : Entity {
public partial class Character : Entity {
/*
Attack speed of the character.
......@@ -85,7 +85,7 @@ namespace BigSock {
/*
The inventory of the character.
*/
public Inventory Inventory { get; protected set; } = new Inventory();
public Inventory Inventory { get; protected set; }
/*
The base stats of the character.
......@@ -102,6 +102,8 @@ namespace BigSock {
protected virtual void Start() {
rb = GetComponent<Rigidbody2D>();
Inventory = new Inventory(this);
// Set the base stats.
BaseStats = new CharacterStats {
MaxHP = baseMaxHP,
......@@ -123,18 +125,7 @@ namespace BigSock {
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;
}
/*
Add Kcockback.
......@@ -163,7 +154,7 @@ namespace BigSock {
/*
Adds damage to the player if they don't have IFrames.
*/
public virtual bool TakeDamage(IAttackStats attack) {
public virtual bool TakeDamage(AttackStats attack) {
// Check if player has IFrames
if(NextTimeCanTakeDamage > DateTime.Now)
return false;
......@@ -171,10 +162,12 @@ namespace BigSock {
// Start new IFrames
NextTimeCanTakeDamage = DateTime.Now + IFrameDuration;
// Trigger the event for taking damage.
OnTakeDamage?.Invoke(this, attack?.Actor, attack);
// Add damage
HP -= attack.Damage;
OnDamage(attack);
AfterDamage(attack);
TryKill();
......@@ -189,7 +182,7 @@ namespace BigSock {
if(Alive && HP <= 0) {
Alive = false;
OnDeath();
AfterDeath();
return true;
}
......@@ -200,18 +193,188 @@ namespace BigSock {
/*
Method for what to do when the character takes damage.
*/
protected virtual void OnDamage(IAttackStats attack) {
print($"[Character.TakeDamage()] {HP} - {attack.Damage}");
protected virtual void AfterDamage(IAttackStats attack) {
print($"[Character.AfterDamage()] {HP} - {attack.Damage}");
KnockBack(attack);
}
/*
Method for what to do when the character dies.
*/
protected virtual void OnDeath() {
print($"[Character.TryKill()] start. | {HP}, {Alive}");
protected virtual void AfterDeath() {
print($"[Character.AfterDeath()] start. | {HP}, {Alive}");
Destroy(gameObject);
}
}
/*
Items
*/
public partial class Character {
/*
Adds a listener for a conditional item to this character's event.
*/
public void AddItemListener(ConditionalItemBase item) {
switch(item.Trigger) {
case TriggerType.Fire: OnFire += ((OnFireItemBase) item).Handler; break;
case TriggerType.Hit: OnHit += ((OnHitItemBase) item).Handler; break;
case TriggerType.Kill: OnKill += ((OnKillItemBase) item).Handler; break;
case TriggerType.TakeDamage: OnTakeDamage += ((OnTakeDamageItemBase) item).Handler; break;
case TriggerType.Heal: OnHeal += ((OnHealItemBase) item).Handler; break;
case TriggerType.Death: OnDeath += ((OnDeathItemBase) item).Handler; break;
default: print($"[Character.AddItemListener()] No handler for {item.Trigger}!"); break;
}
}
/*
Remove a listener for a conditional item from this character's event.
*/
public void RemoveItemListener(ConditionalItemBase item) {
switch(item.Trigger) {
case TriggerType.Fire: OnFire -= ((OnFireItemBase) item).Handler; break;
case TriggerType.Hit: OnHit -= ((OnHitItemBase) item).Handler; break;
case TriggerType.Kill: OnKill -= ((OnKillItemBase) item).Handler; break;
case TriggerType.TakeDamage: OnTakeDamage -= ((OnTakeDamageItemBase) item).Handler; break;
case TriggerType.Heal: OnHeal -= ((OnHealItemBase) item).Handler; break;
case TriggerType.Death: OnDeath -= ((OnDeathItemBase) item).Handler; break;
default: print($"[Character.RemoveItemListener()] No handler for {item.Trigger}!"); break;
}
}
/*
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;
}
/*
Try to drop an item.
*/
public bool TryDropItem(IItem item) {
if(Inventory.RemoveItem(item)) {
UpdateModifiers();
print($"[Character.TryDropItem()] {item.Name} dropped. ({Inventory.Items.Count}/{Inventory.Cap})");
return true;
}
print($"[Character.TryDropItem()] {item.Name} NOT dropped. ({Inventory.Items.Count}/{Inventory.Cap})");
return false;
}
}
/*
Events
*/
public partial class Character {
/*
Trigers the OnHit event.
*/
public void TargetHit(Character target, AttackStats attack) {
OnHit?.Invoke(this, target, attack);
}
/*
Triggers when character uses an attack.
Params: actor, attack stats.
*/
public event Action<Character, AttackStats> OnFire;
/*
Triggers when character hits an enemy.
Params: actor, target, attack stats.
*/
public event Action<Character, Character, AttackStats> OnHit;
/*
Triggers when character kills an enemy.
Params: actor, target, attack stats.
*/
public event Action<Character, Character, AttackStats> OnKill;
/*
Triggers when character takes damage.
Params: actor, target, attack stats.
(Target here is the character that dealt damage to this one)
*/
public event Action<Character, Character, AttackStats> OnTakeDamage;
/*
Triggers when character heals back health.
Params: actor, amount.
(Add heal source later on)
*/
public event Action<Character, int> OnHeal;
/*
Triggers when character has taken fatal damage.
(This is before the character is killed, so can be used to prevent death)
Params: actor, target, attack stats.
(Target here is the character that dealt damage to this one)
*/
public event Action<Character, Character, AttackStats> OnDeath;
/*
Triggers when character picked up an item.
Params: actor, item.
*/
public event Action<Character, IItem> OnPickedUpItem;
/*
Triggers when character gains xp.
Params: actor, amount.
*/
public event Action<Character, int> OnGainedXP;
/*
Triggers when character levels up.
Params: actor.
*/
public event Action<Character> OnLevelUp;
/*
Triggers when character enters a new room.
Params: actor.
(Add room object later)
*/
public event Action<Character> OnEnterNewRoom;
/*
Triggers when character clears a room.
Params: actor.
(Add room object later)
*/
public event Action<Character> OnClearedRoom;
/*
Triggers when character enters a new level.
Params: actor.
(Add level object later)
*/
public event Action<Character> OnEnterNewStage;
}
}
......@@ -36,5 +36,10 @@ namespace BigSock {
*/
public Vector2 Source { get; set; }
/*
The character that activated the attack.
*/
public Character Actor { get; set; }
}
}
\ No newline at end of file
......@@ -36,5 +36,9 @@ namespace BigSock {
*/
Vector2 Source { get; }
/*
The character that activated the attack.
*/
Character Actor { get; }
}
}
\ No newline at end of file
......@@ -72,6 +72,21 @@ namespace BigSock {
AttackSpeed = a.AttackSpeed + b.AttackSpeed,
};
}
/*
Removes the values of one stat object from the other.
Effectively a - b.
*/
public static ICharacterStats Remove(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.
......
fileFormatVersion: 2
guid: 0e31f4d0057891f41801425a6b916ad2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock.Item {
/*
Base class for all items that trigger when the user recieves fatal damage.
*/
public abstract class OnDeathItemBase : ConditionalItemBase {
/*
The type of trigger this item uses.
*/
public override TriggerType Trigger => TriggerType.Death;
/*
The handler to activate when the condition is triggered.
*/
public abstract void Handler(Character source, Character target, AttackStats attack);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 12406fa4fcf374142ad545946396cbde
guid: 84e953071eafa1d44a67e96cea7eaf66
MonoImporter:
externalObjects: {}
serializedVersion: 2
......
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock.Item {
/*
Base class for all items that trigger when the user attacks.
*/
public abstract class OnFireItemBase : ConditionalItemBase {
/*
The type of trigger this item uses.
*/
public override TriggerType Trigger => TriggerType.Fire;
/*
The handler to activate when the condition is triggered.
*/
public abstract void Handler(Character source, AttackStats attack);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 69b7a27e8309b684a8ade9d41920034d
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 {
/*
Base class for all items that trigger when the user heals.
*/
public abstract class OnHealItemBase : ConditionalItemBase {
/*
The type of trigger this item uses.
*/
public override TriggerType Trigger => TriggerType.Heal;
/*
The handler to activate when the condition is triggered.
*/
public abstract void Handler(Character source, int amount);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 54316703ea17ea64c9babffa1b4de872
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 {
/*
Base class for all items that trigger when the user hits an enemy.
*/
public abstract class OnHitItemBase : ConditionalItemBase {
/*
The type of trigger this item uses.
*/
public override TriggerType Trigger => TriggerType.Hit;
/*
The handler to activate when the condition is triggered.
*/
public abstract void Handler(Character source, Character target, AttackStats attack);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: d00b673cdbafae9489329e3fcfd19d4a
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 {
/*
Base class for all items that trigger when the user kills an enemy.
*/
public abstract class OnKillItemBase : ConditionalItemBase {
/*
The type of trigger this item uses.
*/
public override TriggerType Trigger => TriggerType.Kill;
/*
The handler to activate when the condition is triggered.
*/
public abstract void Handler(Character source, Character target, AttackStats attack);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 09173f95f2f51ec49b2b48f7efc13aac
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 {
/*
Base class for all items that trigger when the user takes damage.
*/
public abstract class OnTakeDamageItemBase : ConditionalItemBase {
/*
The type of trigger this item uses.
*/
public override TriggerType Trigger => TriggerType.TakeDamage;
/*
The handler to activate when the condition is triggered.
*/
public abstract void Handler(Character source, Character target, AttackStats attack);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: e6a3ceaa2b8339e4ba9e7d3ecff2b5ce
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -11,15 +11,18 @@ namespace BigSock.Item {
A class that represents an item that an effect when a condition is meet.
*/
public abstract class ConditionalItemBase : ItemBase {
public static readonly System.Random RND = new System.Random();
/*
The type of trigger this item uses.
*/
public TriggerType Trigger { get; set; }
public abstract TriggerType Trigger { get; }
/*
The handler to activate when the condition is triggered.
*/
public Action<ICharEventParams> Handler { get; set; }
//public Action<ICharEventParams> Handler { get; set; }
}
}
\ No newline at end of file
......@@ -14,17 +14,17 @@ namespace BigSock.Item {
/*
The name of the item.
*/
public string Name { get; protected set; }
public abstract string Name { get; }
/*
The description of the item.
*/
public string Description { get; protected set; }
public abstract string Description { get; }
/*
The id of the item.
*/
public ulong Id { get; protected set; }
public abstract ulong Id { get; }
}
}
\ No newline at end of file
......@@ -12,9 +12,24 @@ namespace BigSock.Item {
*/
public enum TriggerType {
None,
OnKill,
OnAttack,
OnHit,
OnDamage,
// Target
Fire,
Hit,
Kill,
// Actor
TakeDamage,
Heal,
Death,
PickedUpItem,
GainedXP,
LevelUp,
// Stage
EnterNewRoom,
ClearedRoom,
EnterNewStage,
}
}
\ 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