-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombat.cs
More file actions
199 lines (170 loc) · 6.79 KB
/
Combat.cs
File metadata and controls
199 lines (170 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using UnityEngine;
using Mirror;
using TMPro;
using UnityEngine.Events;
namespace uMMORPG
{
public enum DamageType { Normal, Block, Crit }
// inventory, attributes etc. can influence max health
public interface ICombatBonus
{
int GetDamageBonus();
int GetDefenseBonus();
float GetCriticalChanceBonus();
float GetBlockChanceBonus();
}
[Serializable] public class UnityEventIntDamageType : UnityEvent<int, DamageType> {}
[DisallowMultipleComponent]
public class Combat : NetworkBehaviour
{
[Header("Components")]
public Level level;
public Entity entity;
#pragma warning disable CS0109
public new Collider collider;
#pragma warning restore CS0109
[Header("Stats")]
[SyncVar] public bool invincible = false; // GMs, Npcs, ...
public LinearInt baseDamage = new LinearInt { baseValue = 1 };
public LinearInt baseDefense = new LinearInt { baseValue = 1 };
public LinearFloat baseBlockChance;
public LinearFloat baseCriticalChance;
[Header("Damage Popup")]
public GameObject damagePopupPrefab;
// events
[Header("Events")]
public UnityEventEntity onDamageDealtTo;
public UnityEventEntity onKilledEnemy;
public UnityEventEntityInt onServerReceivedDamage;
public UnityEventIntDamageType onClientReceivedDamage;
// cache components that give a bonus (attributes, inventory, etc.)
ICombatBonus[] _bonusComponents;
ICombatBonus[] bonusComponents =>
_bonusComponents ?? (_bonusComponents = GetComponents<ICombatBonus>());
// ---------------------------------------------------------------------
// Calculated stats (GC-safe)
// ---------------------------------------------------------------------
public int damage
{
get
{
int bonus = 0;
foreach (ICombatBonus bonusComponent in bonusComponents)
bonus += bonusComponent.GetDamageBonus();
return baseDamage.Get(level.current) + bonus;
}
}
public int defense
{
get
{
int bonus = 0;
foreach (ICombatBonus bonusComponent in bonusComponents)
bonus += bonusComponent.GetDefenseBonus();
return baseDefense.Get(level.current) + bonus;
}
}
public float blockChance
{
get
{
float bonus = 0;
foreach (ICombatBonus bonusComponent in bonusComponents)
bonus += bonusComponent.GetBlockChanceBonus();
return baseBlockChance.Get(level.current) + bonus;
}
}
public float criticalChance
{
get
{
float bonus = 0;
foreach (ICombatBonus bonusComponent in bonusComponents)
bonus += bonusComponent.GetCriticalChanceBonus();
return baseCriticalChance.Get(level.current) + bonus;
}
}
// ---------------------------------------------------------------------
// Combat
// ---------------------------------------------------------------------
[Server]
public virtual void DealDamageAt(Entity victim, int amount, float stunChance = 0, float stunTime = 0)
{
Combat victimCombat = victim.combat;
int damageDealt = 0;
DamageType damageType = DamageType.Normal;
if (!victimCombat.invincible)
{
if (UnityEngine.Random.value < victimCombat.blockChance)
{
damageType = DamageType.Block;
}
else
{
damageDealt = Mathf.Max(amount - victimCombat.defense, 1);
if (UnityEngine.Random.value < criticalChance)
{
damageDealt *= 2;
damageType = DamageType.Crit;
}
if (entity is Player player) player.combatSkills.HitEnemy(victim, damageDealt);
victim.health.current -= damageDealt;
victimCombat.onServerReceivedDamage.Invoke(entity, damageDealt);
if (UnityEngine.Random.value < stunChance)
{
double newStunEndTime = NetworkTime.time + stunTime;
victim.stunTimeEnd = Math.Max(newStunEndTime, victim.stunTimeEnd);
}
}
if (victim is Player targetPlayer) targetPlayer.combatSkills.ReceivedDamage(damageType);
onDamageDealtTo.Invoke(victim);
if (victim.health.current == 0)
onKilledEnemy.Invoke(victim);
}
victim.OnAggro(entity);
// ---- NETWORK CHANGE (CRITICAL) ----
// Send damage feedback ONLY to the victim, unreliable.
if (victim.connectionToClient != null)
{
victimCombat.TargetOnReceivedDamaged(
victim.connectionToClient,
damageDealt,
damageType
);
}
entity.lastCombatTime = NetworkTime.time;
victim.lastCombatTime = NetworkTime.time;
}
// ---------------------------------------------------------------------
// Client-side damage feedback (SELF ONLY)
// ---------------------------------------------------------------------
[TargetRpc(channel = Channels.Unreliable)]
public void TargetOnReceivedDamaged(
NetworkConnection target,
int amount,
DamageType damageType)
{
// Visual feedback is local-only
//ShowDamagePopup(amount, damageType);
// UI / addons hook
onClientReceivedDamage.Invoke(amount, damageType);
}
[Client]
void ShowDamagePopup(int amount, DamageType damageType)
{
if (damagePopupPrefab == null)
return;
Bounds bounds = collider.bounds;
Vector3 position = new Vector3(bounds.center.x, bounds.max.y, bounds.center.z);
GameObject popup = Instantiate(damagePopupPrefab, position, Quaternion.identity);
TextMeshPro text = popup.GetComponentInChildren<TextMeshPro>();
if (damageType == DamageType.Normal)
text.text = amount.ToString();
else if (damageType == DamageType.Block)
text.text = "<i>Block!</i>";
else if (damageType == DamageType.Crit)
text.text = amount + " Crit!";
}
}
}