-
Notifications
You must be signed in to change notification settings - Fork 16
/
CharacterRenderer.cs
485 lines (391 loc) · 17.7 KB
/
CharacterRenderer.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
using System;
using System.Linq;
using EndlessClient.Controllers;
using EndlessClient.GameExecution;
using EndlessClient.Rendering.CharacterProperties;
using EndlessClient.Rendering.Chat;
using EndlessClient.Rendering.Effects;
using EndlessClient.Rendering.Factories;
using EndlessClient.Rendering.Sprites;
using EndlessClient.UIControls;
using EOLib;
using EOLib.Domain.Character;
using EOLib.Domain.Extensions;
using EOLib.Domain.Map;
using EOLib.Graphics;
using EOLib.IO.Map;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Optional;
using XNAControls;
namespace EndlessClient.Rendering.Character
{
public class CharacterRenderer : DrawableGameComponent, ICharacterRenderer
{
private readonly IMapInteractionController _mapInteractionController;
private readonly IRenderTargetFactory _renderTargetFactory;
private readonly IHealthBarRendererFactory _healthBarRendererFactory;
private readonly IChatBubbleFactory _chatBubbleFactory;
private readonly ICharacterProvider _characterProvider;
private readonly IRenderOffsetCalculator _renderOffsetCalculator;
private readonly ICharacterPropertyRendererBuilder _characterPropertyRendererBuilder;
private readonly ICharacterTextures _characterTextures;
private readonly ICharacterSpriteCalculator _characterSpriteCalculator;
private readonly IGameStateProvider _gameStateProvider;
private readonly ICurrentMapProvider _currentMapProvider;
private readonly IEffectRenderer _effectRenderer;
private ICharacter _character;
private bool _textureUpdateRequired, _positionIsRelative = true;
private MouseState _previousMouseState;
private MouseState _currentMouseState;
private SpriteBatch _sb;
private RenderTarget2D _charRenderTarget;
private Texture2D _outline;
private BlinkingLabel _nameLabel;
private string _shoutName = string.Empty;
private DateTime? _spellCastTime;
private IHealthBarRenderer _healthBarRenderer;
private Lazy<IChatBubble> _chatBubble;
public ICharacter Character
{
get { return _character; }
set
{
if (_character == value) return;
_textureUpdateRequired = _character.RenderProperties.GetHashCode() != value.RenderProperties.GetHashCode();
_character = value;
}
}
public bool Transparent { get; set; }
public Rectangle DrawArea { get; private set; }
public Rectangle MapProjectedDrawArea => DrawArea;
private int? _topPixel;
public int TopPixel => _topPixel.HasValue ? _topPixel.Value : 0;
public int TopPixelWithOffset => TopPixel + DrawArea.Y;
public Rectangle EffectTargetArea
=> DrawArea.WithPosition(new Vector2(DrawArea.X, DrawArea.Y - 8));
public CharacterRenderer(INativeGraphicsManager nativeGraphicsmanager,
Game game,
IMapInteractionController mapInteractionController,
IRenderTargetFactory renderTargetFactory,
IHealthBarRendererFactory healthBarRendererFactory,
IChatBubbleFactory chatBubbleFactory,
ICharacterProvider characterProvider,
IRenderOffsetCalculator renderOffsetCalculator,
ICharacterPropertyRendererBuilder characterPropertyRendererBuilder,
ICharacterTextures characterTextures,
ICharacterSpriteCalculator characterSpriteCalculator,
ICharacter character,
IGameStateProvider gameStateProvider,
ICurrentMapProvider currentMapProvider)
: base(game)
{
_mapInteractionController = mapInteractionController;
_renderTargetFactory = renderTargetFactory;
_healthBarRendererFactory = healthBarRendererFactory;
_chatBubbleFactory = chatBubbleFactory;
_characterProvider = characterProvider;
_renderOffsetCalculator = renderOffsetCalculator;
_characterPropertyRendererBuilder = characterPropertyRendererBuilder;
_characterTextures = characterTextures;
_characterSpriteCalculator = characterSpriteCalculator;
_character = character;
_gameStateProvider = gameStateProvider;
_currentMapProvider = currentMapProvider;
_effectRenderer = new EffectRenderer(nativeGraphicsmanager, this);
_chatBubble = new Lazy<IChatBubble>(() => _chatBubbleFactory.CreateChatBubble(this));
}
#region Game Component
public override void Initialize()
{
_charRenderTarget = _renderTargetFactory.CreateRenderTarget();
_sb = new SpriteBatch(Game.GraphicsDevice);
_nameLabel = new BlinkingLabel(Constants.FontSize08pt5)
{
Visible = _gameStateProvider.CurrentState == GameStates.PlayingTheGame,
TextWidth = 89,
TextAlign = LabelAlignment.MiddleCenter,
ForeColor = Color.White,
AutoSize = true,
Text = _character?.Name ?? string.Empty,
DrawOrder = 30,
KeepInClientWindowBounds = false,
};
_nameLabel.Initialize();
if (!_nameLabel.Game.Components.Contains(_nameLabel))
_nameLabel.Game.Components.Add(_nameLabel);
_nameLabel.DrawPosition = GetNameLabelPosition();
_previousMouseState = _currentMouseState = Mouse.GetState();
_healthBarRenderer = _healthBarRendererFactory.CreateHealthBarRenderer(this);
base.Initialize();
}
protected override void LoadContent()
{
_characterTextures.Refresh(_character.RenderProperties);
_outline = new Texture2D(GraphicsDevice, 1, 1);
_outline.SetData(new[] { Color.White });
base.LoadContent();
}
public override void Update(GameTime gameTime)
{
_topPixel = _topPixel ?? FigureOutTopPixel(_characterSpriteCalculator, _character.RenderProperties);
// Effects can be rendered when character is not visible (leaving map)
_effectRenderer.Update();
if (!Visible)
return;
_currentMouseState = Mouse.GetState();
if (_textureUpdateRequired)
{
_characterTextures.Refresh(_character.RenderProperties);
DrawToRenderTarget();
_textureUpdateRequired = false;
}
if (_positionIsRelative)
SetGridCoordinatePosition();
if (_gameStateProvider.CurrentState == GameStates.PlayingTheGame)
{
UpdateNameLabel(gameTime);
if (DrawArea.ContainsPoint(_currentMouseState.X, _currentMouseState.Y) &&
_currentMouseState.RightButton == ButtonState.Released &&
_previousMouseState.RightButton == ButtonState.Pressed)
{
_mapInteractionController.RightClick(new MapCellState { Character = Option.Some(Character) });
}
}
_healthBarRenderer.Update(gameTime);
_previousMouseState = _currentMouseState;
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
if (!Visible || _sb.IsDisposed)
return;
//todo: check if this is the renderer for the main player
// if hidden, draw if: they are not active character and active character is admin
_sb.Begin();
DrawToSpriteBatch(_sb);
if (_sb.IsDisposed)
return;
_sb.End();
//todo: draw effect over character
base.Draw(gameTime);
}
#endregion
#region ICharacterRenderer
public void SetAbsoluteScreenPosition(int xPosition, int yPosition)
{
SetScreenCoordinates(xPosition, yPosition);
_positionIsRelative = false;
}
public void SetToCenterScreenPosition()
{
const int x = 314; // 618 / 2.0
var skinRect = _characterTextures.Skin.SourceRectangle;
var y = (298 - skinRect.Height)/2 - skinRect.Height/4;
SetAbsoluteScreenPosition(x, y);
}
public void DrawToSpriteBatch(SpriteBatch spriteBatch)
{
_effectRenderer.DrawBehindTarget(spriteBatch);
if (Visible)
spriteBatch.Draw(_charRenderTarget, new Vector2(0, GetSteppingStoneOffset(Character.RenderProperties)), GetAlphaColor());
_effectRenderer.DrawInFrontOfTarget(spriteBatch);
_healthBarRenderer.DrawToSpriteBatch(spriteBatch);
}
#endregion
#region Texture Loading Helpers
private static int FigureOutTopPixel(ICharacterSpriteCalculator spriteCalculator, ICharacterRenderProperties renderProperties)
{
var spriteForSkin = spriteCalculator.GetSkinTexture(renderProperties);
var skinData = spriteForSkin.GetSourceTextureData<Color>();
int i = 0;
while (i < skinData.Length && skinData[i].A == 0) i++;
return i == skinData.Length - 1 ? 0 : i/spriteForSkin.SourceRectangle.Height;
}
#endregion
#region Update/Drawing Helpers
private void DrawToRenderTarget()
{
GraphicsDevice.SetRenderTarget(_charRenderTarget);
GraphicsDevice.Clear(ClearOptions.Target, Color.Transparent, 0, 0);
_sb.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
var characterPropertyRenderers = _characterPropertyRendererBuilder
.BuildList(_characterTextures, _character.RenderProperties)
.Where(x => x.CanRender);
foreach (var renderer in characterPropertyRenderers)
renderer.Render(_sb, DrawArea);
if (_gameStateProvider.CurrentState == GameStates.None)
{
_sb.Draw(_outline, DrawArea.WithSize(DrawArea.Width, 1), Color.Black);
_sb.Draw(_outline, DrawArea.WithPosition(new Vector2(DrawArea.X + DrawArea.Width, DrawArea.Y)).WithSize(1, DrawArea.Height), Color.Black);
_sb.Draw(_outline, DrawArea.WithPosition(new Vector2(DrawArea.X, DrawArea.Y + DrawArea.Height)).WithSize(DrawArea.Width, 1), Color.Black);
_sb.Draw(_outline, DrawArea.WithSize(1, DrawArea.Height), Color.Black);
_sb.Draw(_outline, DrawArea, Color.FromNonPremultiplied(255, 0, 0, 64));
_sb.Draw(_outline, MapProjectedDrawArea, Color.FromNonPremultiplied(0, 255, 0, 64));
}
_sb.End();
GraphicsDevice.SetRenderTarget(null);
}
private Color GetAlphaColor()
{
// don't render the transparent character layer if hidden/dead, otherwise the additive blending
// will render it with full alpha
if (_character.RenderProperties.IsHidden || _character.RenderProperties.IsDead)
return Transparent ? Color.Transparent : Color.FromNonPremultiplied(255, 255, 255, 128);
return Transparent
? Color.FromNonPremultiplied(255, 255, 255, 128)
: Color.White;
}
private void SetGridCoordinatePosition()
{
//todo: the constants here should be dynamically configurable to support window resizing
var screenX = _renderOffsetCalculator.CalculateOffsetX(_character.RenderProperties) + 312 - GetMainCharacterOffsetX();
var screenY = _renderOffsetCalculator.CalculateOffsetY(_character.RenderProperties) + 106 - GetMainCharacterOffsetY();
SetScreenCoordinates(screenX, screenY);
}
private void SetScreenCoordinates(int xPosition, int yPosition)
{
// size of standing still skin texture
DrawArea = new Rectangle(xPosition, yPosition, 18, 58);
_textureUpdateRequired = true;
}
private int GetMainCharacterOffsetX()
{
return _renderOffsetCalculator.CalculateOffsetX(_characterProvider.MainCharacter.RenderProperties);
}
private int GetMainCharacterOffsetY()
{
return _renderOffsetCalculator.CalculateOffsetY(_characterProvider.MainCharacter.RenderProperties);
}
private void UpdateNameLabel(GameTime gameTime)
{
if (_healthBarRenderer.Visible)
{
_nameLabel.Visible = false;
}
else if (DrawArea.Contains(_currentMouseState.Position))
{
_nameLabel.Visible = true;
_nameLabel.BlinkRate = null;
_nameLabel.Text = _character.Name;
}
else if (_shoutName != string.Empty && _nameLabel.Text != _shoutName)
{
_nameLabel.Visible = true;
_nameLabel.BlinkRate = 250;
_nameLabel.Text = _shoutName;
}
else if (_shoutName == string.Empty)
{
_nameLabel.Visible = false;
}
if (_spellCastTime.HasValue && (DateTime.Now - _spellCastTime.Value).TotalMilliseconds >= 600)
{
_nameLabel.Visible = false;
_nameLabel.Text = _character.Name;
_nameLabel.ForeColor = Color.White;
_nameLabel.BlinkRate = null;
_shoutName = string.Empty;
_spellCastTime = null;
}
_nameLabel.DrawPosition = GetNameLabelPosition();
}
private Vector2 GetNameLabelPosition()
{
return new Vector2(DrawArea.X - Math.Abs(DrawArea.Width - _nameLabel.ActualWidth) / 2,
TopPixelWithOffset - 8 - _nameLabel.ActualHeight);
}
private bool GetIsSteppingStone(ICharacterRenderProperties renderProps)
{
if (_gameStateProvider.CurrentState != GameStates.PlayingTheGame)
return false;
return _currentMapProvider.CurrentMap.Tiles[renderProps.MapY, renderProps.MapX] == TileSpec.Jump
|| (renderProps.IsActing(CharacterActionState.Walking) && _currentMapProvider.CurrentMap.Tiles[renderProps.GetDestinationY(), renderProps.GetDestinationX()] == TileSpec.Jump);
}
private int GetSteppingStoneOffset(ICharacterRenderProperties renderProps)
{
var isSteppingStone = GetIsSteppingStone(renderProps);
if (isSteppingStone && renderProps.IsActing(CharacterActionState.Walking))
{
switch(renderProps.ActualWalkFrame)
{
case 1: return -8;
case 2: return -16;
case 3: return -16;
case 4: return -8;
}
}
return 0;
}
#endregion
#region Effects
public bool EffectIsPlaying()
{
return _effectRenderer.State == EffectState.Playing;
}
public void ShowWaterSplashies()
{
if (_effectRenderer.EffectType == EffectType.WaterSplashies &&
_effectRenderer.State == EffectState.Playing)
_effectRenderer.Restart();
_effectRenderer.PlayEffect(EffectType.WaterSplashies, 0);
}
public void ShowWarpArrive()
{
_effectRenderer.PlayEffect(EffectType.WarpDestination, 0);
}
public void ShowWarpLeave()
{
_effectRenderer.PlayEffect(EffectType.WarpOriginal, 0);
}
public void ShowPotionAnimation(int potionId)
{
_effectRenderer.PlayEffect(EffectType.Potion, potionId);
}
public void ShowSpellAnimation(int spellId)
{
_effectRenderer.PlayEffect(EffectType.Spell, spellId);
}
#endregion
// Called when the spell cast begins
public void ShoutSpellPrep(string spellName)
{
_shoutName = spellName;
}
// Called when the spell prep time ends and the player actually casts the spell
public void ShoutSpellCast()
{
_nameLabel.BlinkRate = null;
_nameLabel.ForeColor = Color.FromNonPremultiplied(0xf5, 0xc8, 0x9c, 0xff); // todo: make constant for this
_spellCastTime = DateTime.Now;
}
// Called when the shout (spell prep time) should be cancelled without casting
public void StopShoutingSpell()
{
_shoutName = string.Empty;
}
public void ShowDamageCounter(int damage, int percentHealth, bool isHeal)
{
if (isHeal)
_healthBarRenderer.SetHealth(damage, percentHealth);
else
_healthBarRenderer.SetDamage(damage.SomeWhen(d => d > 0), percentHealth);
}
public void ShowChatBubble(string message, bool isGroupChat)
{
_chatBubble.Value.SetMessage(message, isGroupChat);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_outline?.Dispose();
_nameLabel.Dispose();
_sb.Dispose();
_charRenderTarget.Dispose();
}
base.Dispose(disposing);
}
}
}