-
Notifications
You must be signed in to change notification settings - Fork 16
/
NPCRenderer.cs
338 lines (269 loc) · 12.4 KB
/
NPCRenderer.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
using EndlessClient.Controllers;
using EndlessClient.GameExecution;
using EndlessClient.HUD.Spells;
using EndlessClient.Input;
using EndlessClient.Rendering.Chat;
using EndlessClient.Rendering.Effects;
using EndlessClient.Rendering.Factories;
using EndlessClient.Rendering.Sprites;
using EOLib;
using EOLib.Domain.Extensions;
using EOLib.Domain.NPC;
using EOLib.Domain.Spells;
using EOLib.Graphics;
using EOLib.IO.Repositories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Optional;
using System;
using System.Linq;
using System.Windows.Markup;
using XNAControls;
namespace EndlessClient.Rendering.NPC
{
public class NPCRenderer : DrawableGameComponent, INPCRenderer
{
private static readonly object _rt_locker_ = new object();
private readonly IClientWindowSizeProvider _clientWindowSizeProvider;
private readonly IENFFileProvider _enfFileProvider;
private readonly INPCSpriteSheet _npcSpriteSheet;
private readonly IGridDrawCoordinateCalculator _gridDrawCoordinateCalculator;
private readonly IHealthBarRendererFactory _healthBarRendererFactory;
private readonly IChatBubbleFactory _chatBubbleFactory;
private readonly IRenderTargetFactory _renderTargetFactory;
private readonly IUserInputProvider _userInputProvider;
private readonly IEffectRenderer _effectRenderer;
private readonly IHealthBarRenderer _healthBarRenderer;
private RenderTarget2D _npcRenderTarget;
private SpriteBatch _spriteBatch;
private DateTime _lastStandingAnimation;
private int _fadeAwayAlpha;
private bool _isDying, _isBlankSprite;
private XNALabel _nameLabel;
private IChatBubble _chatBubble;
public int NameLabelY { get; private set; }
public int HorizontalCenter { get; private set; }
public bool IsAlive => !_isDying && !IsDead;
public Rectangle DrawArea { get; private set; }
public EOLib.Domain.NPC.NPC NPC { get; set; }
public ISpellTargetable SpellTarget => NPC;
public bool IsDead { get; private set; }
public Rectangle EffectTargetArea { get; private set; }
public NPCRenderer(IEndlessGameProvider endlessGameProvider,
IClientWindowSizeProvider clientWindowSizeProvider,
IENFFileProvider enfFileProvider,
INPCSpriteSheet npcSpriteSheet,
IGridDrawCoordinateCalculator gridDrawCoordinateCalculator,
IHealthBarRendererFactory healthBarRendererFactory,
IChatBubbleFactory chatBubbleFactory,
IRenderTargetFactory renderTargetFactory,
IUserInputProvider userInputProvider,
IEffectRendererFactory effectRendererFactory,
EOLib.Domain.NPC.NPC initialNPC)
: base((Game)endlessGameProvider.Game)
{
NPC = initialNPC;
_clientWindowSizeProvider = clientWindowSizeProvider;
_enfFileProvider = enfFileProvider;
_npcSpriteSheet = npcSpriteSheet;
_gridDrawCoordinateCalculator = gridDrawCoordinateCalculator;
_healthBarRendererFactory = healthBarRendererFactory;
_chatBubbleFactory = chatBubbleFactory;
_renderTargetFactory = renderTargetFactory;
_userInputProvider = userInputProvider;
_effectRenderer = effectRendererFactory.Create();
DrawArea = GetStandingFrameRectangle();
_lastStandingAnimation = DateTime.Now;
_fadeAwayAlpha = 255;
_clientWindowSizeProvider.GameWindowSizeChanged += (_, _) =>
{
lock (_rt_locker_)
{
_npcRenderTarget.Dispose();
_npcRenderTarget = _renderTargetFactory.CreateRenderTarget();
}
};
_healthBarRenderer = _healthBarRendererFactory.CreateHealthBarRenderer(this);
}
public override void Initialize()
{
UpdateDrawAreas();
_nameLabel = new XNALabel(Constants.FontSize08pt5)
{
Visible = false,
TextWidth = 89,
TextAlign = LabelAlignment.MiddleCenter,
ForeColor = Color.White,
AutoSize = true,
Text = _enfFileProvider.ENFFile[NPC.ID].Name,
DrawOrder = 30,
KeepInClientWindowBounds = false,
};
_nameLabel.Initialize();
if (!_nameLabel.Game.Components.Contains(_nameLabel))
_nameLabel.Game.Components.Add(_nameLabel);
_nameLabel.DrawPosition = GetNameLabelPosition();
lock (_rt_locker_)
_npcRenderTarget = _renderTargetFactory.CreateRenderTarget();
_spriteBatch = new SpriteBatch(Game.GraphicsDevice);
var frameTexture = _npcSpriteSheet.GetNPCTexture(_enfFileProvider.ENFFile[NPC.ID].Graphic, NPC.Frame, NPC.Direction);
var data = new Color[frameTexture.Width * frameTexture.Height];
frameTexture.GetData(data);
_isBlankSprite = data.All(x => x.A == 0);
base.Initialize();
}
public override void Update(GameTime gameTime)
{
if (!Visible) return;
UpdateDrawAreas();
UpdateStandingFrameAnimation();
UpdateDeadState();
DrawToRenderTarget();
var currentMousePosition = _userInputProvider.CurrentMouseState.Position;
if (DrawArea.Contains(currentMousePosition))
{
var chatBubbleIsVisible = _chatBubble != null && _chatBubble.Visible;
_nameLabel.Visible = !_healthBarRenderer.Visible && !chatBubbleIsVisible && !_isDying && IsClickablePixel(currentMousePosition);
_nameLabel.DrawPosition = GetNameLabelPosition();
}
else
{
_nameLabel.Visible = false;
}
_effectRenderer.Update();
_healthBarRenderer.Update(gameTime);
base.Update(gameTime);
}
public bool IsClickablePixel(Point currentMousePosition)
{
var currentFrame = _npcSpriteSheet.GetNPCTexture(_enfFileProvider.ENFFile[NPC.ID].Graphic, NPC.Frame, NPC.Direction);
var colorData = new Color[] { Color.FromNonPremultiplied(0, 0, 0, 255) };
if (currentFrame != null && !_isBlankSprite)
{
if (_npcRenderTarget.Bounds.Contains(currentMousePosition))
_npcRenderTarget.GetData(0, new Rectangle(currentMousePosition.X, currentMousePosition.Y, 1, 1), colorData, 0, 1);
}
return _isBlankSprite || colorData[0].A > 0;
}
public void DrawToSpriteBatch(SpriteBatch spriteBatch)
{
if (!Visible) return;
_effectRenderer.DrawBehindTarget(spriteBatch);
if (_npcRenderTarget != null)
spriteBatch.Draw(_npcRenderTarget, Vector2.Zero, Color.White);
_effectRenderer.DrawInFrontOfTarget(spriteBatch);
_healthBarRenderer.DrawToSpriteBatch(spriteBatch);
}
public void StartDying()
{
_isDying = true;
}
public void ShowDamageCounter(int damage, int percentHealth, bool isHeal)
{
var optionalDamage = damage.SomeWhen(d => d > 0);
_healthBarRenderer.SetDamage(optionalDamage, percentHealth, () => _chatBubble?.Show());
_chatBubble?.Hide();
}
public void ShowChatBubble(string message, bool isGroupChat)
{
if (_chatBubble == null)
_chatBubble = _chatBubbleFactory.CreateChatBubble(this);
_chatBubble.SetMessage(message, isGroupChat: false);
}
#region Effects
public bool EffectIsPlaying()
{
return _effectRenderer.State == EffectState.Playing;
}
public void PlayEffect(int graphic)
{
_effectRenderer.PlayEffect(graphic, this);
}
#endregion
private Rectangle GetStandingFrameRectangle()
{
var data = _enfFileProvider.ENFFile[NPC.ID];
var baseFrame = _npcSpriteSheet.GetNPCTexture(data.Graphic, NPCFrame.Standing, EODirection.Down);
return new Rectangle(0, 0, baseFrame.Width, baseFrame.Height);
}
private void UpdateDrawAreas()
{
var data = _enfFileProvider.ENFFile[NPC.ID];
var frameTexture = _npcSpriteSheet.GetNPCTexture(data.Graphic, NPC.Frame, NPC.Direction);
var metaData = _npcSpriteSheet.GetNPCMetadata(data.Graphic);
var isUpOrRight = NPC.IsFacing(EODirection.Up, EODirection.Right) ? -1 : 1;
var isDownOrRight = NPC.IsFacing(EODirection.Down, EODirection.Right) ? -1 : 1;
int metaDataOffsetX, metaDataOffsetY;
if (NPC.Frame == NPCFrame.Attack2)
{
metaDataOffsetX = metaData.AttackOffsetX * isUpOrRight + (metaData.OffsetX * isUpOrRight);
metaDataOffsetY = metaData.AttackOffsetY * isDownOrRight - metaData.OffsetY;
}
else
{
metaDataOffsetX = metaData.OffsetX * isUpOrRight;
metaDataOffsetY = -metaData.OffsetY;
}
var renderCoordinates = _gridDrawCoordinateCalculator.CalculateDrawCoordinates(NPC) +
new Vector2(metaDataOffsetX - frameTexture.Width / 2, metaDataOffsetY - (frameTexture.Height - 23));
DrawArea = frameTexture.Bounds.WithPosition(renderCoordinates);
var horizontalOffset = _npcSpriteSheet.GetNPCMetadata(data.Graphic).OffsetX * (NPC.IsFacing(EODirection.Down, EODirection.Left) ? -1 : 1);
HorizontalCenter = DrawArea.X + (DrawArea.Width / 2) + horizontalOffset;
NameLabelY = DrawArea.Y - (int)(_nameLabel?.ActualHeight + 4 ?? 0);
EffectTargetArea = DrawArea.WithSize(DrawArea.Width + horizontalOffset * 2, DrawArea.Height);
}
private void UpdateStandingFrameAnimation()
{
var now = DateTime.Now;
var data = _enfFileProvider.ENFFile[NPC.ID];
var metaData = _npcSpriteSheet.GetNPCMetadata(data.Graphic);
if (!metaData.HasStandingFrameAnimation
|| !NPC.IsActing(NPCActionState.Standing)
|| (now - _lastStandingAnimation).TotalMilliseconds < 250)
return;
_lastStandingAnimation = now;
NPC = NPC.WithFrame(NPC.Frame == NPCFrame.Standing ? NPCFrame.StandingFrame1 : NPCFrame.Standing);
}
private void UpdateDeadState()
{
if (!_isDying) return;
if (_fadeAwayAlpha >= 3)
_fadeAwayAlpha -= 3;
IsDead = _fadeAwayAlpha <= 0 && !EffectIsPlaying();
}
private void DrawToRenderTarget()
{
if (_npcRenderTarget == null)
return;
var data = _enfFileProvider.ENFFile[NPC.ID];
var texture = _npcSpriteSheet.GetNPCTexture(data.Graphic, NPC.Frame, NPC.Direction);
var color = Color.FromNonPremultiplied(255, 255, 255, _fadeAwayAlpha);
var effects = NPC.IsFacing(EODirection.Left, EODirection.Down) ? SpriteEffects.None : SpriteEffects.FlipHorizontally;
lock (_rt_locker_)
{
GraphicsDevice.SetRenderTarget(_npcRenderTarget);
GraphicsDevice.Clear(ClearOptions.Target, Color.Transparent, 1, 0);
_spriteBatch.Begin();
_spriteBatch.Draw(texture, DrawArea, null, color, 0, Vector2.Zero, effects, 1);
_spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
}
}
private Vector2 GetNameLabelPosition()
{
return new Vector2(HorizontalCenter - (_nameLabel.ActualWidth / 2f), NameLabelY);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_nameLabel.Dispose();
_chatBubble?.Dispose();
_spriteBatch?.Dispose();
lock (_rt_locker_)
_npcRenderTarget?.Dispose();
}
base.Dispose(disposing);
}
}
}