Skip to content

Commit cb3c6e7

Browse files
authored
Stamina Implemented (#30)
* Changes Made + Persistent Inventory Items Saving + Bag Refactored from SO to Serializable Class + Custom Json Serializer for SOs using Addressable + Small Utils Edits and Bug Fixes * Changes Made + Lighting regenerated for game scene + Small modifications and added burst project settings files * Refactored Functions in to states (just renames) * Updated Unity version to v2022 LTS * checkpoint * checkpoint, refactored StateStatus enum to a bool IsEnabled * added refactored and tested new state transitions * pooling abstraction implemented, moved SerializableVector3.cs to Data package to fix core dependency compile error * implemented spawner to hit effect on weapons * Changes made - Added serialized dictionary as an attribute to Generic Dictionary - More improvements to Spawner * checkpoint * finished #16 * small DeSpawn fix for Spawner.cs * EventBus implemented * EventBus.cs implemented/refactored into Project * namespace and class renames * revert from merge * GameState converted from enums to struct/class * checkpoint * just renames * stamina/endurance implemented * Endurance Immutable on Turret prefab
1 parent 2ff6218 commit cb3c6e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+773
-722
lines changed

Packages/com.ekaka.character/Runtime/Damage/DamageController.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System.Collections.Generic;
22
using Character.Main;
3-
using Damage.Main;
43
using UnityEngine;
5-
using UnityEngine.Serialization;
64

75
namespace Character.Damage
86
{

Packages/com.ekaka.damage/Runtime/Main/DamageData.cs renamed to Packages/com.ekaka.character/Runtime/Damage/DamageData.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Sensors.Main;
55
using UnityEngine;
66

7-
namespace Damage.Main
7+
namespace Character.Damage
88
{
99
//!!!INDEXES MUST BE UNIQUE
1010
//!!!NEVER CHANGE THE INDEX NUMBERS, CAUSES A SHUFFLE ISSUE ON CUSTOM EDITOR
@@ -55,9 +55,12 @@ public DamageData(Dictionary<DamageType, float> hits, Damager damager, IDamageab
5555

5656
foreach (var hitPair in Hits)
5757
{
58-
Resistance resistance = Damageable.Resistance[hitPair.Key];
59-
60-
if (!resistance.Invulnerable) DamageDealt += hitPair.Value - (resistance.Value * hitPair.Value);
58+
DamageDealt += hitPair.Value;
59+
60+
if (Damageable.Resistance.TryGetValue(hitPair.Key, out Resistance resistance) && !resistance.Invulnerable)
61+
{
62+
DamageDealt -= resistance.Value * hitPair.Value;
63+
}
6164
}
6265

6366
MaxDamageType = Hits.Aggregate((h, l) => h.Value > l.Value ? h : l).Key;

Packages/com.ekaka.damage/Runtime/Main/Damager.cs renamed to Packages/com.ekaka.character/Runtime/Damage/Damager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Damage.Main
1+
namespace Character.Damage
22
{
33
public class Damager
44
{

Packages/com.ekaka.damage/Runtime/Main/DeathHandler.cs renamed to Packages/com.ekaka.character/Runtime/Damage/DeathHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using UnityEngine;
22

3-
namespace Damage.Main
3+
namespace Character.Damage
44
{
55
public abstract class DeathHandler : MonoBehaviour
66
{

Packages/com.ekaka.character/Runtime/Damage/DefaultDeathHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Damage.Main;
21

32
namespace Character.Damage
43
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using Character.Main;
3+
using Core.Game;
4+
using UnityEngine;
5+
6+
namespace Character.Damage
7+
{
8+
[Serializable]
9+
public class Endurance : Stat
10+
{
11+
[field: SerializeField, Tooltip("How many units of stamina is recovered per second.")]
12+
public float RecoveryRate { get; private set; } = 5f;
13+
14+
public IDamageable Damageable { get; private set; }
15+
16+
public void Initialize(IDamageable damageable)
17+
{
18+
CurrentValue = FullValue;
19+
20+
Damageable = damageable;
21+
}
22+
23+
public void RecoverStamina(float normalizedRate)
24+
{
25+
GainValue(RecoveryRate * normalizedRate * Time.deltaTime, out _, true);
26+
}
27+
28+
public void DrainStamina(float normalizedRate)
29+
{
30+
LoseValue(RecoveryRate * normalizedRate * Time.deltaTime, out _, true);
31+
}
32+
}
33+
}

Packages/com.ekaka.character/Runtime/Damage/Endurance.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.ekaka.damage/Runtime/Main/IDamageable.cs renamed to Packages/com.ekaka.character/Runtime/Damage/IDamageable.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using Core.Game;
51
using Core.Common;
62
using Sensors.Main;
7-
using UnityEngine;
83

9-
namespace Damage.Main
4+
namespace Character.Damage
105
{
116
public interface IDamageable : ITargetable
127
{
138
public Vitality Vitality { get; }
149

10+
public Endurance Endurance { get; }
11+
1512
public Damager Damager { get; }
1613

17-
public float CurrentHealth => Vitality.CurrentHealth;
14+
public float CurrentHealth => Vitality.CurrentValue;
15+
16+
public float CurrentStamina => Endurance.CurrentValue;
17+
18+
public float NormalizedHealth => CurrentHealth / Vitality.FullValue;
1819

19-
public float NormalizedHealth => CurrentHealth / Vitality.FullHealth;
20+
public float NormalizedStamina => CurrentStamina / Endurance.FullValue;
2021

2122
public bool IsDead => CurrentHealth <= 0;
2223

@@ -26,11 +27,13 @@ public interface IDamageable : ITargetable
2627
public float LoadCurrentHealth();
2728
}
2829

29-
public static class DamagableWrapper
30+
public static class DamageableWrapper
3031
{
31-
public static void InitializeDamagable(this IDamageable damageable)
32+
public static void InitializeDamageable(this IDamageable damageable)
3233
{
3334
damageable.Vitality.Initialize(damageable);
35+
36+
damageable.Endurance.Initialize(damageable);
3437
}
3538

3639
public static void TakeDamage(this IDamageable damageable, DamageData damageReceived)
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System;
2+
using System.Linq;
3+
using Character.Main;
4+
using Core.Common;
5+
using UnityEngine;
6+
7+
namespace Character.Damage
8+
{
9+
[Serializable]
10+
public struct Resistance
11+
{
12+
[field: SerializeField] public bool Invulnerable { get; private set; }
13+
14+
[field: ShowIf(nameof(Invulnerable), false)]
15+
[field: Range(0, 1)]
16+
[field: Tooltip("0 being will take all the damage sent 1 being invulnerable to damage")]
17+
[field: SerializeField] public float Value { get; private set; }
18+
}
19+
20+
[Serializable]
21+
public class Vitality : Stat
22+
{
23+
#region DamageTaken
24+
25+
public delegate void DamageTaken(DamageData damage);
26+
27+
public event DamageTaken OnDamageTaken;
28+
29+
private void InvokeDamageTaken(DamageData damage)
30+
{
31+
OnDamageTaken?.Invoke(damage);
32+
}
33+
34+
#endregion
35+
36+
#region Death
37+
38+
public delegate void DeathDelegate(DamageData damage);
39+
40+
public event DeathDelegate OnDeath;
41+
42+
private void InvokeDeath(DamageData damage)
43+
{
44+
OnDeath?.Invoke(damage);
45+
46+
if (!DeathOverrides.TryGetValue(damage.MaxDamageType, out DeathHandler death))
47+
{
48+
death = Death;
49+
}
50+
51+
death.Apply(damage);
52+
}
53+
54+
[field: SerializeField] public DeathHandler Death { get; private set; }
55+
56+
[field: SerializeField, SerializedDictionary,
57+
Tooltip(
58+
"If the highest killing blow hit damage type is not found in this dictionary, default death will be applied instead.")]
59+
public GenericDictionary<DamageType, DeathHandler> DeathOverrides { get; private set; }
60+
61+
#endregion
62+
63+
[field: SerializeField, SerializedDictionary,
64+
Tooltip(
65+
"Resistance to specific Damage type, 0 - 1, 1 being invulnerable to damage.")]
66+
public GenericDictionary<DamageType, Resistance> Resistance { get; private set; }
67+
68+
public IDamageable Damageable { get; private set; }
69+
70+
public void Initialize(IDamageable damageable)
71+
{
72+
Damageable = damageable;
73+
74+
//load health from damageable
75+
CurrentValue = Damageable.LoadCurrentHealth();
76+
77+
//if current health is 0, it means it's a new Game or Damageable wasn't initialized before
78+
if (CurrentValue <= 0)
79+
{
80+
CurrentValue = FullValue;
81+
}
82+
}
83+
84+
public void TakeDamage(DamageData damageReceived)
85+
{
86+
Debug.Log($"{damageReceived.DamageDealt} damage dealt to {Damageable.Obj.name}");
87+
88+
if (LoseValue(damageReceived.DamageDealt, out float damageTaken))
89+
{
90+
Debug.Log($"{damageTaken} damage taken by {Damageable.Obj.name}");
91+
92+
InvokeDamageTaken(damageReceived);
93+
94+
damageReceived.Damager.InvokeDamageDealt(damageReceived);
95+
96+
if (IsDepleted)
97+
{
98+
Debug.Log($"{Damageable.Obj.name} Dead...");
99+
100+
InvokeDeath(damageReceived);
101+
102+
damageReceived.Damager.InvokeKillingBlow(damageReceived);
103+
}
104+
}
105+
}
106+
107+
public void GainHealth(float healthReceived)
108+
{
109+
Debug.Log($"{healthReceived} health received to {Damageable.Obj.name}");
110+
111+
if (GainValue(healthReceived, out float healthGained))
112+
{
113+
Debug.Log($"{healthGained} health gained by {Damageable.Obj.name}");
114+
}
115+
}
116+
}
117+
}

Packages/com.ekaka.character/Runtime/Main/Actor.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Linq;
42
using Core.Game;
53
using Core.Common;
6-
using Damage.Main;
4+
using Character.Damage;
75
using Sensors.Main;
86
using UnityEngine;
97

@@ -88,15 +86,17 @@ public CharacterController CharacterController
8886

8987
#endregion
9088

91-
#region Damagable
89+
#region Damageable
9290

9391
[field: SerializeField] public Vitality Vitality { get; private set; }
92+
93+
[field: SerializeField] public Endurance Endurance { get; private set; }
9494

9595
public Damager Damager { get; private set; } = new Damager();
9696

9797
public virtual float LoadCurrentHealth()
9898
{
99-
return Vitality.FullHealth;
99+
return Vitality.FullValue;
100100
}
101101

102102
#endregion
@@ -125,21 +125,9 @@ protected virtual void Initialize()
125125
controller.InvokeReady();
126126
}
127127

128-
#region Targetable
129-
130-
ITargetable targetable = this;
131-
132-
targetable.InitializeTargetable();
133-
134-
#endregion
135-
136-
#region Damagable
137-
138-
IDamageable damageable = this;
139-
140-
damageable.InitializeDamagable();
128+
this.InitializeTargetable();
141129

142-
#endregion
130+
this.InitializeDamageable();
143131

144132
InvokeReady();
145133
}

0 commit comments

Comments
 (0)