forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharStatus.cs
More file actions
165 lines (134 loc) · 4.59 KB
/
Copy pathCharStatus.cs
File metadata and controls
165 lines (134 loc) · 4.59 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
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Timers;
namespace L2dotNET.Models.Status
{
public class CharStatus
{
public L2Character Character { get; set; }
public List<L2Character> StatusListener { get; set; } = new List<L2Character>();
protected static readonly byte RegenFlagCp = 4;
private static readonly byte RegenFlagHp = 1;
private static readonly byte RegenFlagMp = 2;
public double CurrentHp { get => _currentHp; }
public double CurrentMp { get => _currentMp; }
private Timer _regTask;
protected byte _flagsRegenActive = 0;
private double _currentHp = 0;
private double _currentMp = 0;
public CharStatus(L2Character character)
{
Character = character;
}
public void AddStatusListener(L2Character character)
{
if (character == Character)
return;
StatusListener.Add(character);
}
public void RemoveStatusListener(L2Character character)
{
StatusListener.Remove(character);
}
public void ReduceHp(double value, L2Character attacker /*, bool awake, bool isDot, bool isHpConsumption*/)
{
if (Character.Dead)
return;
Console.WriteLine(attacker.ObjectId);
if (value > 0)
{
SetCurrentHp(Math.Max(_currentHp - value, 0), true);
}
if (_currentHp < 0.5)
{
Character.AbortAttack();
Character.DoDieAsync(attacker);
}
}
public void ReduceMp(double value)
{
_currentMp = Math.Max(_currentMp - value, 0);
}
public void SetCurrentMp(double newMp)
{
SetCurrentMp(newMp, true);
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void SetCurrentMp(double newMp, bool broadcastUpdate)
{
int maxMp = Character.MaxMp;
if (Character.Dead)
return;
if (newMp >= maxMp)
{
_currentMp = maxMp;
_flagsRegenActive &= RegenFlagMp;
if (_flagsRegenActive == 0)
StopHpMpRegeneration();
}
else
{
_currentMp = newMp;
_flagsRegenActive |= RegenFlagMp;
StartHpMpRegeneration();
}
if (broadcastUpdate)
Character.BroadcastStatusUpdateAsync();
}
public void SetCurrentHp(double newHp)
{
SetCurrentHp(newHp, true);
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void SetCurrentHp(double newHp, bool broadcastUpdate)
{
double maxHp = Character.MaxHp;
if (Character.Dead)
return;
if (newHp >= maxHp)
{
_currentHp = maxHp;
_flagsRegenActive &= RegenFlagHp;
if (_flagsRegenActive == 0)
StopHpMpRegeneration();
}
else
{
_currentHp = newHp;
_flagsRegenActive |= RegenFlagHp;
StartHpMpRegeneration();
}
if (broadcastUpdate)
Character.BroadcastStatusUpdateAsync();
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void StartHpMpRegeneration()
{
if (_regTask == null && !Character.Dead)
{
int period = 3000; //3 seconds for HP regen
_regTask = new Timer(period);
_regTask.Elapsed += RegenTask;
_regTask.Enabled = true;
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void StopHpMpRegeneration()
{
if (_regTask != null)
{
_regTask.Enabled = false;
_flagsRegenActive = 0;
}
}
private void RegenTask(object sender, ElapsedEventArgs e)
{
if (_currentHp < Character.MaxHp)
SetCurrentHp(_currentHp + (Character.MaxHp * 1.0 / 100), false); // we will calculate the actual modified when we do formulas
if (_currentMp < Character.MaxMp)
SetCurrentMp(_currentMp + (Character.MaxMp * 1.0 / 100), false); // we will calculate the actual modified when we do formulas
Character.BroadcastStatusUpdateAsync();
}
}
}