forked from rogeraabbccdd/CS2-DamageText
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDamageText.cs
More file actions
162 lines (141 loc) · 5.03 KB
/
DamageText.cs
File metadata and controls
162 lines (141 loc) · 5.03 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
using System.Drawing;
using System.Text.Json.Serialization;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.Core.Attributes.Registration;
namespace DamageText;
public class DamageTextConfig : BasePluginConfig
{
[JsonPropertyName("ConfigVersion")]
public override int Version { get; set; } = 2;
[JsonPropertyName("NormalSize")]
public int NormalSize { get; set; } = 80;
[JsonPropertyName("NormalHeadshotSize")]
public int NormalHeadshotSize { get; set; } = 90;
[JsonPropertyName("KillSize")]
public int KillSize { get; set; } = 120;
[JsonPropertyName("KillHeadshotSize")]
public int KillHeadshotSize { get; set; } = 120;
[JsonPropertyName("NormalColor")]
public string NormalColor { get; set; } = "#ffffff";
[JsonPropertyName("NormalHeadshotColor")]
public string NormalHeadshotColor { get; set; } = "#ffff00";
[JsonPropertyName("KillColor")]
public string KillColor { get; set; } = "#ff0000";
[JsonPropertyName("KillHeadshotColor")]
public string KillHeadshotColor { get; set; } = "#ff0000";
[JsonPropertyName("TextDisplayDuration")]
public float TextDisplayDuration { get; set; } = 0.5f;
}
public class DamageTextPlugin : BasePlugin, IPluginConfig<DamageTextConfig>
{
public override string ModuleName => "Damage Text";
public override string ModuleVersion => "1.1.0";
public override string ModuleAuthor => "Kento";
public override string ModuleDescription => "Show damage text like RPG games :D";
public DamageTextConfig Config { get; set; } = new();
private Random random = new Random();
public override void Load(bool hotReload)
{
}
public override void Unload(bool hotReload)
{
}
public void OnConfigParsed(DamageTextConfig config)
{
Config = config;
}
[GameEventHandler]
public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
{
CCSPlayerController? victim = @event.Userid;
int damage = @event.DmgHealth;
CCSPlayerController? attacker = @event.Attacker;
bool headshot = @event.Hitgroup == 1;
bool kill = @event.Health <= 0;
if (
victim == null || victim.PlayerPawn.Value == null ||
victim.PlayerPawn.Value.AbsOrigin == null || victim.PlayerPawn.Value.AbsRotation == null
) return HookResult.Continue;
Vector position = new Vector
{
X = victim.PlayerPawn.Value.AbsOrigin.X + (float)GetRandomDouble(10, 15, false),
Y = victim.PlayerPawn.Value.AbsOrigin.Y + (float)GetRandomDouble(10, 15, false),
Z = victim.PlayerPawn.Value.AbsOrigin.Z + (float)GetRandomDouble(20, 80)
};
QAngle angle = new QAngle
{
X = victim.PlayerPawn.Value.AbsRotation.X + 0.0f,
Z = victim.PlayerPawn.Value.AbsRotation.Z + 90.0f,
Y = (attacker == null || attacker.PlayerPawn.Value == null) ?
(float)GetRandomDouble(0, 360) :
attacker.PlayerPawn.Value.EyeAngles.Y - 90f
};
int size;
string color;
if (kill)
{
if (headshot)
{
color = Config.KillHeadshotColor;
size = Config.KillHeadshotSize;
}
else
{
color = Config.KillColor;
size = Config.KillSize;
}
}
else
{
if (headshot)
{
color = Config.NormalHeadshotColor;
size = Config.NormalHeadshotSize;
}
else
{
color = Config.NormalColor;
size = Config.NormalSize;
}
}
ShowDamageText(
damage.ToString(),
color,
size,
position,
angle
);
return HookResult.Continue;
}
public double GetRandomDouble(double min, double max, bool positive = true)
{
if (positive || random.Next(0, 2) == 0)
{
// 從 min 到 max 的範圍選擇
return min + random.NextDouble() * (max - min);
}
else
{
// 從 -min 到 -max 的範圍選擇
return -min + random.NextDouble() * (-max - -min);
}
}
private void ShowDamageText (string text, string color, int size, Vector position, QAngle angle)
{
var entity = Utilities.CreateEntityByName<CPointWorldText>("point_worldtext");
if (entity == null) return;
entity.DispatchSpawn();
entity.MessageText = text;
entity.Enabled = true;
entity.Color = ColorTranslator.FromHtml(color);
entity.FontSize = size;
entity.Fullbright = true;
entity.WorldUnitsPerPx = 0.1f;
entity.DepthOffset = 0.0f;
entity.Teleport(position, angle, new Vector(0,0,0));
entity.DispatchSpawn();
AddTimer(Config.TextDisplayDuration, entity.Remove);
}
}