-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ping.cs
364 lines (331 loc) · 15.4 KB
/
Ping.cs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ReLogic.Graphics;
using Terraria;
using Terraria.Audio;
using Terraria.GameInput;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
namespace PingMod
{
public class Ping : ModProjectile
{
private const float RotationSpeed = 0.05f;
private bool _isLoaded;
private bool _isMoving;
private bool _isRemoving;
private Player Me => Main.player[Main.myPlayer];
private Player Owner => Main.player[projectile.owner];
private string PingLabel => Owner.name + "s ping";
private Texture2D Sprite => Main.projectileTexture[projectile.type];
public Ping()
{
PingMod.OnPostDrawInterface += DrawInterface;
PingMod.OnPostDrawFullscreenMap += DrawOnFullscreenMap;
}
public override void SetDefaults()
{
projectile.width = 10;
projectile.height = 10;
drawOffsetX = -10;
drawOriginOffsetY = -11;
projectile.hide = true;
}
public override void AI()
{
if (!Owner.active || !_isLoaded)
{
_isLoaded = true;
projectile.hide = true;
projectile.ai[0] = 1;
}
if (projectile.owner == Main.myPlayer)
{
UpdateLocal();
}
else
{
UpdateRemote();
}
projectile.rotation += RotationSpeed;
projectile.timeLeft = 100;
}
private void UpdateLocal()
{
if (Main.mouseLeft && Keyboard.GetState().IsKeyDown(Keys.LeftAlt) && !_isRemoving)
{
projectile.position = GetPingPosition();
projectile.hide = false;
_isMoving = true;
}
if (_isMoving && !Main.mouseLeft && !_isRemoving)
{
_isMoving = false;
projectile.ai[0] = 0;
projectile.netUpdate = true;
PlaySound();
Main.NewText("You pinged!");
}
if (!_isRemoving && Main.mouseRight && Keyboard.GetState().IsKeyDown(Keys.LeftAlt))
{
_isRemoving = true;
_isMoving = false;
projectile.hide = true;
projectile.ai[0] = 1;
projectile.netUpdate = true;
}
if (_isRemoving && !Main.mouseRight && !Main.mouseLeft)
{
_isRemoving = false;
_isMoving = false;
}
}
private void UpdateRemote()
{
projectile.hide = projectile.ai[0] == 1;
if (IsVisible() && projectile.position != projectile.oldPosition)
{
Main.NewText(Owner.name + " pinged!");
PlaySound();
}
}
private Vector2 GetPingPosition()
{
var mousePos = GetUnzoomedMousePos();
if (Main.mapFullscreen)
{
return GetFullscreenMapToWorldPos(mousePos);
}
if (InMinimap(mousePos))
{
return GetMinimapToWorldPos(mousePos);
}
return Main.MouseWorld;
}
private Vector2 GetUnzoomedMousePos()
{
PlayerInput.SetZoom_UI();
var mousePos = Main.MouseScreen;
PlayerInput.SetZoom_World();
return mousePos;
}
private bool InMinimap(Vector2 mousePos)
{
return Main.mapStyle == 1 &&
mousePos.X > Main.miniMapX &&
mousePos.X < Main.miniMapX + Main.miniMapWidth &&
mousePos.Y > Main.miniMapY &&
mousePos.Y < Main.miniMapY + Main.miniMapHeight;
}
private Vector2 GetMinimapToWorldPos(Vector2 mousePos)
{
var worldPosX = Me.position.X + (16 * (mousePos.X - Main.miniMapX) - 1920) / Main.mapMinimapScale;
var worldPosY = Me.position.Y + (16 * (mousePos.Y - Main.miniMapY) - 1920) / Main.mapMinimapScale;
return new Vector2(worldPosX, worldPosY);
}
private Vector2 GetFullscreenMapToWorldPos(Vector2 mousePos)
{
const float multiplier1 = 10;
const float multiplier2 = 16;
var mapScale = Main.mapFullscreenScale;
var mapPosX = Main.mapFullscreenPos.X * mapScale;
var mapPosY = Main.mapFullscreenPos.Y * mapScale;
var posOffsetX = -mapPosX + Main.screenWidth / 2 + multiplier1 * mapScale;
var posOffsetY = -mapPosY + Main.screenHeight / 2 + multiplier1 * mapScale;
var worldPosX = (int)((-posOffsetX + mousePos.X * Main.UIScale) / mapScale + multiplier1) * multiplier2;
var worldPosY = (int)((-posOffsetY + mousePos.Y * Main.UIScale) / mapScale + multiplier1) * multiplier2;
return new Vector2(worldPosX, worldPosY);
}
private void PlaySound()
{
Main.PlaySound(new LegacySoundStyle(SoundID.Unlock, 0), projectile.position);
}
private bool IsVisible()
{
return !projectile.hide && (projectile.owner == Main.myPlayer || Owner.team > 0 && Owner.team == Me.team);
}
public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor)
{
return false;
}
public override void PostDraw(SpriteBatch spriteBatch, Color lightColor)
{
DrawGlowMask();
}
private void DrawGlowMask()
{
if (Main.mapFullscreen || !IsVisible())
{
return;
}
var posX = projectile.position.X - Main.screenPosition.X + projectile.width * 0.5f + 1f;
var posY = projectile.position.Y - Main.screenPosition.Y + projectile.height - Sprite.Height * 0.5f + 11f;
Main.spriteBatch.Draw(Sprite, new Vector2(posX, posY),
new Rectangle(0, 0, Sprite.Width, Sprite.Height), Color.White,
projectile.rotation, Sprite.Size() * 0.5f, 1, SpriteEffects.None, 0f);
}
// Based on Main.DrawMap
private void DrawOnFullscreenMap()
{
if (!Main.mapFullscreen || !IsVisible())
{
return;
}
const float num6 = 10f;
const float num7 = 10f;
const byte b = byte.MaxValue;
var num108 = Main.UIScale;
var num16 = Main.mapFullscreenScale / num108;
var num20 = Main.mapFullscreenPos.X;
var num21 = Main.mapFullscreenPos.Y;
num20 *= num16;
num21 *= num16;
var num = -num20 + Main.screenWidth / 2;
var num2 = -num21 + Main.screenHeight / 2;
num += num6 * num16;
num2 += num7 * num16;
var num111 = (projectile.position.X + projectile.width / 2) / 16f * num16;
var num112 = (projectile.position.Y + projectile.gfxOffY + projectile.height / 2) / 16f * num16;
num111 += num;
num112 += num2;
num111 -= 10f * num16;
num112 -= 10f * num16;
Main.spriteBatch.Draw(Sprite, new Vector2(num111, num112),
new Rectangle(0, 0, Sprite.Width, Sprite.Height), new Color(b, b, b, b), projectile.rotation,
new Vector2(Sprite.Width / 2, Sprite.Height / 2), num108 / 2, SpriteEffects.None, 0f);
var num113 = num111 - Sprite.Width / 2 * num108;
var num114 = num112 - Sprite.Height / 2 * num108;
var num115 = num113 + Sprite.Width * num108;
var num116 = num114 + Sprite.Height * num108;
if (Main.mouseX >= num113 && Main.mouseX <= num115 && Main.mouseY >= num114 && Main.mouseY <= num116)
{
Main.instance.MouseText(PingLabel);
}
}
private void DrawInterface()
{
DrawOnMiniMap();
DrawDistanceMarker();
}
// Based on Main.DrawMap
private void DrawOnMiniMap()
{
if (Main.mapFullscreen || Main.mapStyle != 1 || !IsVisible())
{
return;
}
var num145 = (Main.screenPosition.X + PlayerInput.RealScreenWidth / 2) / 16f;
var num28 = (Main.screenPosition.Y + PlayerInput.RealScreenHeight / 2) / 16f;
var num16 = Main.mapMinimapScale;
var num14 = Main.miniMapWidth / num16;
var num15 = Main.miniMapHeight / num16;
var num12 = (int)num145 - num14 / 2f;
var num60 = ((projectile.position.X + projectile.width / 2) / 16f - num12) * num16;
var num13 = (int)num28 - num15 / 2f;
var num61 = ((projectile.position.Y + projectile.gfxOffY + projectile.height / 2) / 16f - num13) * num16;
var num = (float)Main.miniMapX;
var num2 = (float)Main.miniMapY;
var num3 = num;
var num4 = num2;
num60 += num3;
num61 += num4;
num61 -= 2f * num16 / 5f;
if (num60 > Main.miniMapX + 12 && num60 < Main.miniMapX + Main.miniMapWidth - 16 &&
num61 > Main.miniMapY + 10 && num61 < Main.miniMapY + Main.miniMapHeight - 14)
{
var num10 = -(num145 - (int)((Main.screenPosition.X + PlayerInput.RealScreenWidth / 2) / 16f)) * num16;
var num11 = -(num28 - (int)((Main.screenPosition.Y + PlayerInput.RealScreenHeight / 2) / 16f)) * num16;
var b = (byte)(255f * Main.mapMinimapAlpha);
var num57 = (num16 * 0.25f * 2f + 1f) / 3f;
Main.spriteBatch.Draw(Sprite, new Vector2(num60 + num10, num61 + num11),
new Rectangle(0, 0, Sprite.Width, Sprite.Height), new Color(b, b, b, b), projectile.rotation,
new Vector2(Sprite.Width / 2, Sprite.Height / 2), num57, SpriteEffects.None, 0f);
var num62 = num60 - Sprite.Width / 2 * num57;
var num63 = num61 - Sprite.Height / 2 * num57;
var num64 = num62 + Sprite.Width * num57;
var num65 = num63 + Sprite.Height * num57;
if (Main.mouseX >= num62 && Main.mouseX <= num64 && Main.mouseY >= num63 && Main.mouseY <= num65)
{
Main.instance.MouseText(PingLabel);
}
}
}
// Based on Main.DrawInterface_20_MultiplayerPlayerNames
private void DrawDistanceMarker()
{
if (Main.mapFullscreen || !IsVisible())
{
return;
}
PlayerInput.SetZoom_World();
var screenWidth = Main.screenWidth;
var screenHeight = Main.screenHeight;
var screenPosition = Main.screenPosition;
PlayerInput.SetZoom_UI();
var uIScale = Main.UIScale;
var pingLabelPos = Main.fontMouseText.MeasureString(PingLabel);
var pingLabelPosYNegative = 0f;
if (Owner.chatOverhead.timeLeft > 0)
{
pingLabelPosYNegative = -pingLabelPos.Y;
}
var screenCenter = new Vector2(screenWidth / 2 + screenPosition.X, screenHeight / 2 + screenPosition.Y);
var pingPos = projectile.position;
pingPos += (pingPos - screenCenter) * (Main.GameViewMatrix.Zoom - Vector2.One);
var distance2 = 0f;
var color = Owner.team < 1 ? Color.White : Main.teamColor[Owner.team];
var distanceX = pingPos.X + projectile.width / 2 - screenCenter.X;
var distanceY = pingPos.Y - pingLabelPos.Y - 2f + pingLabelPosYNegative - screenCenter.Y;
var distance = (float)Math.Sqrt(distanceX * distanceX + distanceY * distanceY);
var screenHeight2 = screenHeight;
if (screenHeight > screenWidth)
{
screenHeight2 = screenWidth;
}
screenHeight2 = screenHeight2 / 2 - 30;
if (screenHeight2 < 100)
{
screenHeight2 = 100;
}
if (distance < screenHeight2)
{
pingLabelPos.X = pingPos.X + projectile.width / 2 - pingLabelPos.X / 2f - screenPosition.X;
pingLabelPos.Y = pingPos.Y - pingLabelPos.Y - 2f + pingLabelPosYNegative - screenPosition.Y;
}
else
{
distance2 = distance;
distance = screenHeight2 / distance;
pingLabelPos.X = screenWidth / 2 + distanceX * distance - pingLabelPos.X / 2f;
pingLabelPos.Y = screenHeight / 2 + distanceY * distance;
}
if (Me.gravDir == -1f)
{
pingLabelPos.Y = screenHeight - pingLabelPos.Y;
}
pingLabelPos *= 1f / uIScale;
var pingLabelPos2 = Main.fontMouseText.MeasureString(PingLabel);
pingLabelPos += pingLabelPos2 * (1f - uIScale) / 4f;
if (distance2 > 0f)
{
var distanceTextValue = Language.GetTextValue("GameUI.PlayerDistance", (int)(distance2 / 16f * 2f));
var distanceTextPosition = Main.fontMouseText.MeasureString(distanceTextValue);
distanceTextPosition.X = pingLabelPos.X + pingLabelPos2.X / 2f - distanceTextPosition.X / 2f;
distanceTextPosition.Y = pingLabelPos.Y + pingLabelPos2.Y / 2f - distanceTextPosition.Y / 2f - 20f;
Main.spriteBatch.DrawString(Main.fontMouseText, distanceTextValue, new Vector2(distanceTextPosition.X - 2f, distanceTextPosition.Y), Color.Black, 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
Main.spriteBatch.DrawString(Main.fontMouseText, distanceTextValue, new Vector2(distanceTextPosition.X + 2f, distanceTextPosition.Y), Color.Black, 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
Main.spriteBatch.DrawString(Main.fontMouseText, distanceTextValue, new Vector2(distanceTextPosition.X, distanceTextPosition.Y - 2f), Color.Black, 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
Main.spriteBatch.DrawString(Main.fontMouseText, distanceTextValue, new Vector2(distanceTextPosition.X, distanceTextPosition.Y + 2f), Color.Black, 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
Main.spriteBatch.DrawString(Main.fontMouseText, distanceTextValue, distanceTextPosition, color, 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
}
Main.spriteBatch.DrawString(Main.fontMouseText, PingLabel, new Vector2(pingLabelPos.X - 2f, pingLabelPos.Y), Color.Black, 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
Main.spriteBatch.DrawString(Main.fontMouseText, PingLabel, new Vector2(pingLabelPos.X + 2f, pingLabelPos.Y), Color.Black, 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
Main.spriteBatch.DrawString(Main.fontMouseText, PingLabel, new Vector2(pingLabelPos.X, pingLabelPos.Y - 2f), Color.Black, 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
Main.spriteBatch.DrawString(Main.fontMouseText, PingLabel, new Vector2(pingLabelPos.X, pingLabelPos.Y + 2f), Color.Black, 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
Main.spriteBatch.DrawString(Main.fontMouseText, PingLabel, pingLabelPos, color, 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
}
}
}