-
Notifications
You must be signed in to change notification settings - Fork 16
/
OldNPCRenderer.cs
422 lines (352 loc) · 15 KB
/
OldNPCRenderer.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using System;
using System.Linq;
using EndlessClient.Dialogs.Old;
using EndlessClient.Old;
using EndlessClient.Rendering.Sprites;
using EOLib;
using EOLib.Domain.NPC;
using EOLib.IO;
using EOLib.Net.API;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAControls;
using XNAControls.Old;
using XNALabel = XNAControls.Old.XNALabel;
namespace EndlessClient.Rendering
{
//Map NPCs are coupled with draw/update operations
//Note: this is like character, in that it isn't actually drawn or updated by the framework
// Map Renderer handles all draw/update operations
public class OldNPCRenderer : DrawableGameComponent
{
/* Default NPC speed table for eoserv, corresponding to the speed stored in the NPCRenderer spawn */
/* Not sure if this is ever transferred to the client at all, but it is a config value... */
//private static readonly int[] SPEED_TABLE = {900, 600, 1300, 1900, 3700, 7500, 15000, 0};
public OldNPC NPC { get; }
/// <summary>
/// The actual target draw rectangle of the NPC
/// </summary>
public Rectangle DrawArea { get; private set; }
/// <summary>
/// Represents the current map location (scaled to one human character, not NPC size).
/// <para>Since most NPCs are graphically larger/smaller than one "slot" based on character size,</para>
/// <para> this helps determine what the normal size would be if it were a character sprite.</para>
/// </summary>
public Rectangle MapProjectedDrawArea { get; private set; }
/// <summary>
/// The Y coordinate of the first non-transparent pixel on the NPC's default sprite
/// </summary>
public int TopPixel { get; private set; }
/// <summary>
/// The current state of the NPC; determines which sprite to draw
/// </summary>
public NPCFrame Frame { get; private set; }
//drawing related members
private readonly NPCSpriteSheet _npcSheet;
private SpriteBatch _sb;
private Rectangle _npcTextureFrameRectangle;
private bool hasStandFrame1;
private int _fadeAwayAlpha;
private int DrawOffsetX => NPC.X * 32 - NPC.Y * 32 + walkingAdjustedX;
private int DrawOffsetY => NPC.X * 16 + NPC.Y * 16 + walkingAdjustedY;
//"child" controls added to this NPC
private readonly DamageCounter m_damageCounter;
private XNALabel _mouseoverName;
//update related members
private int walkingAdjustedX, walkingAdjustedY;
private DateTime _actionStartTime;
private DateTime _lastAnimUpdateTime;
private MouseState _prevMouseState, _currMouseState;
public OldNPCRenderer(OldNPC npc)
: base(EOGame.Instance)
{
NPC = npc;
_fadeAwayAlpha = 255;
_actionStartTime = DateTime.Now;
_lastAnimUpdateTime = DateTime.Now;
_npcSheet = new NPCSpriteSheet(((EOGame)Game).GFXManager);
m_damageCounter = new DamageCounter(this);
CreateMouseoverName();
}
#region Game Component Overrides
public override void Initialize()
{
base.Initialize(); //required to use the GraphicsDevice property
InitializeTopPixel();
InitializeStandingFrame1();
_sb = new SpriteBatch(GraphicsDevice);
Frame = NPCFrame.Standing;
var baseFrame = _npcSheet.GetNPCTexture(NPC.Data.Graphic, Frame, NPC.Direction);
_npcTextureFrameRectangle = new Rectangle(0, 0, baseFrame.Width, baseFrame.Height);
UpdateDrawArea();
}
public override void Update(GameTime gameTime)
{
if (!Visible) return;
UpdateDrawArea();
UpdateStandingFrameIfNeeded();
UpdateWalkFrameIfNeeded();
UpdateAttackFrameIfNeeded();
if (Game.IsActive)
{
_currMouseState = Mouse.GetState();
UpdateMouseoverName();
HandleLeftClick();
_prevMouseState = _currMouseState;
}
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
if (!Visible) return;
DrawToSpriteBatch(_sb);
base.Draw(gameTime);
}
public void DrawToSpriteBatch(SpriteBatch batch, bool started = false)
{
if (!started)
batch.Begin();
SpriteEffects effects = NPC.Direction == EODirection.Left || NPC.Direction == EODirection.Down
? SpriteEffects.None
: SpriteEffects.FlipHorizontally;
Color col = NPC.Dying ? Color.FromNonPremultiplied(255, 255, 255, _fadeAwayAlpha -= 3) : Color.White;
batch.Draw(_npcSheet.GetNPCTexture(NPC.Data.Graphic, Frame, NPC.Direction),
DrawArea,
null,
col,
0f,
Vector2.Zero,
effects,
1f);
if (NPC.Dying && _fadeAwayAlpha <= 0)
NPC.EndDying();
if (!started)
batch.End();
}
#endregion
#region Public Interface
public void Walk(byte x, byte y, EODirection dir)
{
if (NPC.Walking) return;
_actionStartTime = DateTime.Now;
NPC.BeginWalking(dir, x, y);
}
public void Attack(EODirection dir)
{
if (NPC.Attacking) return;
_actionStartTime = DateTime.Now;
NPC.BeginAttacking(dir);
}
public void HideChatBubble()
{
}
public void TakeDamageFrom(OldCharacter opponent, int damage, int pctHealth)
{
m_damageCounter.SetValue(damage, pctHealth); //NPCs don't know heal spells
NPC.SetOpponent(opponent);
}
public void Kill()
{
NPC.BeginDying();
if (_mouseoverName != null)
{
_mouseoverName.Close();
_mouseoverName = null;
}
}
#endregion
#region Helpers
private void InitializeTopPixel()
{
int tries;
for (tries = 0; tries < 3; ++tries)
{
try
{
//get the first non-transparent pixel to determine offsets for name labels and damage counters
var frameTexture = _npcSheet.GetNPCTexture(NPC.Data.Graphic, NPCFrame.Standing, NPC.Direction);
var frameTextureData = new Color[frameTexture.Width * frameTexture.Height];
frameTexture.GetData(frameTextureData);
if (frameTextureData.All(x => x.A == 0))
TopPixel = 0;
else
{
var firstVisiblePixelIndex = frameTextureData.Select((color, index) => new { color, index })
.Where(x => x.color.R != 0)
.Select(x => x.index)
.First();
TopPixel = firstVisiblePixelIndex/frameTexture.Height;
}
} //this block throws errors sometimes..no idea why. It usually doesn't fail 3 times.
catch (InvalidOperationException) { continue; }
break;
}
if (tries >= 3)
throw new InvalidOperationException("Something weird happened initializing this NPC.");
}
private void InitializeStandingFrame1()
{
//attempt to get standing frame 1. It will have non-black pixels if it exists.
Texture2D frameTexture = _npcSheet.GetNPCTexture(NPC.Data.Graphic, NPCFrame.StandingFrame1, NPC.Direction);
Color[] textureData = new Color[frameTexture.Width * frameTexture.Height];
frameTexture.GetData(textureData);
hasStandFrame1 = textureData.Any(_c => _c.R != 0 || _c.G != 0 || _c.B != 0);
}
private void CreateMouseoverName()
{
_mouseoverName = new XNALabel(new Rectangle(1, 1, 1, 1), Constants.FontSize09)
{
Visible = false,
Text = NPC.Data.Name,
ForeColor = Color.White,
AutoSize = false,
DrawOrder = (int)ControlDrawLayer.BaseLayer + 3
};
_mouseoverName.DrawLocation = new Vector2(
DrawArea.X + (DrawArea.Width - _mouseoverName.ActualWidth) / 2f,
DrawArea.Y + TopPixel - _mouseoverName.ActualHeight - 4);
_mouseoverName.ResizeBasedOnText();
}
private void UpdateDrawArea()
{
DrawArea = new Rectangle(
DrawOffsetX + 320 - OldWorld.Instance.MainPlayer.ActiveCharacter.OffsetX - (int)(_npcTextureFrameRectangle.Width / 6.4 * 3.2),
DrawOffsetY + 168 - OldWorld.Instance.MainPlayer.ActiveCharacter.OffsetY - _npcTextureFrameRectangle.Height,
_npcTextureFrameRectangle.Width, _npcTextureFrameRectangle.Height);
var oneGridSize = new Vector2(OldWorld.Instance.ActiveCharacterRenderer.DrawArea.Width,
OldWorld.Instance.ActiveCharacterRenderer.DrawArea.Height);
MapProjectedDrawArea = new Rectangle(
DrawArea.X + (int) (Math.Abs(oneGridSize.X - DrawArea.Width)/2),
DrawArea.Bottom - (int) oneGridSize.Y,
(int)oneGridSize.X,
(int)oneGridSize.Y);
}
private void UpdateStandingFrameIfNeeded()
{
if (NPC.Walking || NPC.Attacking || !hasStandFrame1)
return;
//switch the standing animation for NPCs every 500ms, if they're standing still
if ((DateTime.Now - _lastAnimUpdateTime).TotalMilliseconds > 250)
{
if (Frame == NPCFrame.Standing)
{
Frame = NPCFrame.StandingFrame1;
}
else if (Frame == NPCFrame.StandingFrame1)
{
Frame = NPCFrame.Standing;
}
_lastAnimUpdateTime = DateTime.Now;
}
}
private void UpdateMouseoverName()
{
if (_mouseoverName == null) return;
_mouseoverName.Visible = DrawArea.ContainsPoint(_currMouseState.X, _currMouseState.Y);
_mouseoverName.DrawLocation = new Vector2(
DrawArea.X + (DrawArea.Width - _mouseoverName.ActualWidth) / 2f,
DrawArea.Y + TopPixel - _mouseoverName.ActualHeight - 4);
}
private void HandleLeftClick()
{
bool mouseClicked = _currMouseState.LeftButton == ButtonState.Released &&
_prevMouseState.LeftButton == ButtonState.Pressed;
if (mouseClicked && DrawArea.ContainsPoint(_currMouseState.X, _currMouseState.Y))
{
if (OldWorld.Instance.MainPlayer.ActiveCharacter.NeedsSpellTarget)
{
var data = OldWorld.Instance.ESF[OldWorld.Instance.MainPlayer.ActiveCharacter.SelectedSpell];
if (data.TargetRestrict != SpellTargetRestrict.Friendly)
{
OldWorld.Instance.ActiveCharacterRenderer.SetSpellTarget(this);
}
else
{
//todo status label message "you cannot attack this NPC"
OldWorld.Instance.MainPlayer.ActiveCharacter.SelectSpell(-1);
}
return; //don't process regular click on NPC while targeting a spell
}
PacketAPI api = ((EOGame)Game).API;
switch (NPC.Data.Type)
{
case NPCType.Inn: break;
case NPCType.Bank: BankAccountDialog.Show(api, NPC.Index); break;
case NPCType.Barber: break;
case NPCType.Guild: break;
case NPCType.Priest: break;
case NPCType.Law: break;
case NPCType.Skills: SkillmasterDialog.Show(api, NPC.Index); break;
case NPCType.Quest: QuestDialog.Show(api, NPC.Index, NPC.Data.VendorID, NPC.Data.Name); break;
}
}
}
private void UpdateWalkFrameIfNeeded()
{
if (!NPC.Walking || (DateTime.Now - _actionStartTime).TotalMilliseconds < 100)
return;
_actionStartTime = DateTime.Now;
switch (NPC.Direction)
{
case EODirection.Down: walkingAdjustedX += -8; walkingAdjustedY += 4; break;
case EODirection.Left: walkingAdjustedX += -8; walkingAdjustedY += -4; break;
case EODirection.Up: walkingAdjustedX += 8; walkingAdjustedY += -4; break;
case EODirection.Right: walkingAdjustedX += 8; walkingAdjustedY += 4; break;
}
switch (Frame)
{
case NPCFrame.Standing:
case NPCFrame.StandingFrame1:
Frame = NPCFrame.WalkFrame1;
break;
case NPCFrame.WalkFrame1:
Frame = NPCFrame.WalkFrame2;
break;
case NPCFrame.WalkFrame2:
Frame = NPCFrame.WalkFrame3;
break;
case NPCFrame.WalkFrame3:
Frame = NPCFrame.WalkFrame4;
break;
case NPCFrame.WalkFrame4:
Frame = NPCFrame.Standing;
NPC.EndWalking();
walkingAdjustedX = walkingAdjustedY = 0;
break;
}
}
private void UpdateAttackFrameIfNeeded()
{
if (!NPC.Attacking || (DateTime.Now - _actionStartTime).TotalMilliseconds < 100)
return;
_actionStartTime = DateTime.Now;
switch (Frame)
{
case NPCFrame.Standing:
case NPCFrame.StandingFrame1:
Frame = NPCFrame.Attack1;
break;
case NPCFrame.Attack1:
Frame = NPCFrame.Attack2;
break;
case NPCFrame.Attack2:
Frame = NPCFrame.Standing;
NPC.EndAttacking();
break;
}
}
#endregion
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (_sb != null)
_sb.Dispose();
if (_mouseoverName != null)
_mouseoverName.Close();
}
base.Dispose(disposing);
}
}
}