forked from TehCheat/HealthBars
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHealthBar.cs
More file actions
153 lines (133 loc) · 4.29 KB
/
HealthBar.cs
File metadata and controls
153 lines (133 loc) · 4.29 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using ExileCore.PoEMemory.Components;
using ExileCore.PoEMemory.MemoryObjects;
using ExileCore.Shared.Cache;
using ExileCore.Shared.Enums;
using SharpDX;
using Vector2 = System.Numerics.Vector2;
namespace HealthBars;
public class HealthBar
{
private static long _idCounter;
private readonly Stopwatch _dpsStopwatch = Stopwatch.StartNew();
private bool _isHostile;
private readonly CachedValue<float> _distanceCache;
public HealthBar(Entity entity, HealthBarsSettings settings)
{
Entity = entity;
AllSettings = settings;
_distanceCache = new TimeCache<float>(() => entity.DistancePlayer, 200);
Update();
}
public void CheckUpdate()
{
var entityIsHostile = Entity.IsHostile;
if (_isHostile != entityIsHostile)
{
_isHostile = entityIsHostile;
Update();
}
if (Settings.ShowDps)
{
DpsRefresh();
}
}
public bool Skip { get; set; } = false;
public Vector2 LastPosition { get; set; }
private HealthBarsSettings AllSettings { get; }
public long StableId { get; } = Interlocked.Increment(ref _idCounter);
public UnitSettings Settings => Type switch
{
CreatureType.Player when Entity.Equals(Entity.Player) => AllSettings.Self,
CreatureType.Player => AllSettings.Players,
CreatureType.Minion => AllSettings.Minions,
CreatureType.Normal => AllSettings.NormalEnemy,
CreatureType.Magic => AllSettings.MagicEnemy,
CreatureType.Rare => AllSettings.RareEnemy,
CreatureType.Unique => AllSettings.UniqueEnemy,
};
public RectangleF DisplayArea { get; set; }
public float Distance => _distanceCache.Value;
public Entity Entity { get; }
public CreatureType Type { get; private set; }
public Life Life => Entity.GetComponent<Life>();
public float HpPercent => Life.HPPercentage;
public float EsPercent => Life.ESPercentage;
public float EhpPercent => CurrentEhp / (float)MaxEhp;
public int CurrentEhp => Life.CurHP + Life.CurES;
public int MaxEhp => Life.MaxHP + Life.MaxES;
public readonly Queue<(DateTime Time, int Value)> EhpHistory = new Queue<(DateTime, int)>();
public Color Color
{
get
{
if (IsHidden(Entity))
return Color.LightGray;
if (HpPercent * 100 <= AllSettings.CullPercent)
return Settings.CullableColor;
return Settings.LifeColor;
}
}
private static bool IsHidden(Entity entity)
{
try
{
return entity.IsHidden;
}
catch
{
return false;
}
}
private void Update()
{
Type = GetEntityType();
}
private CreatureType GetEntityType()
{
if (Entity.HasComponent<Player>())
{
return CreatureType.Player;
}
if (Entity.HasComponent<Monster>())
{
var objectMagicProperties = Entity.GetComponent<ObjectMagicProperties>();
if (Entity.IsHostile)
{
return objectMagicProperties?.Rarity switch
{
MonsterRarity.White => CreatureType.Normal,
MonsterRarity.Magic => CreatureType.Magic,
MonsterRarity.Rare => CreatureType.Rare,
MonsterRarity.Unique => CreatureType.Unique,
_ => CreatureType.Minion
};
}
return CreatureType.Minion;
}
return CreatureType.Minion;
}
private void DpsRefresh()
{
if (_dpsStopwatch.ElapsedMilliseconds >= 200)
{
var hp = CurrentEhp;
if (hp == MaxEhp && EhpHistory.TryPeek(out var entry) && hp == entry.Value)
{
EhpHistory.Clear();
}
else
{
while (EhpHistory.TryPeek(out entry) &&
DateTime.UtcNow - entry.Time > TimeSpan.FromMilliseconds(AllSettings.DpsEstimateDuration))
{
EhpHistory.Dequeue();
}
}
EhpHistory.Enqueue((DateTime.UtcNow, hp));
}
}
}