-
Notifications
You must be signed in to change notification settings - Fork 16
/
CharacterAnimationActions.cs
446 lines (388 loc) · 19.1 KB
/
CharacterAnimationActions.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
using AutomaticTypeMapper;
using EndlessClient.Audio;
using EndlessClient.ControlSets;
using EndlessClient.HUD;
using EndlessClient.HUD.Controls;
using EndlessClient.Rendering.Map;
using EOLib;
using EOLib.Config;
using EOLib.Domain.Character;
using EOLib.Domain.Extensions;
using EOLib.Domain.Map;
using EOLib.Domain.Notifiers;
using EOLib.Domain.Spells;
using EOLib.IO;
using EOLib.IO.Map;
using EOLib.IO.Repositories;
using EOLib.Localization;
using Optional;
using Optional.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
namespace EndlessClient.Rendering.Character
{
[AutoMappedType]
public class CharacterAnimationActions : ICharacterAnimationActions, IOtherCharacterAnimationNotifier, IEffectNotifier, IEmoteNotifier
{
private readonly IHudControlProvider _hudControlProvider;
private readonly ICharacterRepository _characterRepository;
private readonly ICurrentMapStateProvider _currentMapStateProvider;
private readonly ICharacterRendererProvider _characterRendererProvider;
private readonly ICurrentMapProvider _currentMapProvider;
private readonly ISpikeTrapActions _spikeTrapActions;
private readonly IPubFileProvider _pubFileProvider;
private readonly IStatusLabelSetter _statusLabelSetter;
private readonly ISfxPlayer _sfxPlayer;
private readonly Random _random;
public CharacterAnimationActions(IHudControlProvider hudControlProvider,
ICharacterRepository characterRepository,
ICurrentMapStateProvider currentMapStateProvider,
ICharacterRendererProvider characterRendererProvider,
ICurrentMapProvider currentMapProvider,
ISpikeTrapActions spikeTrapActions,
IPubFileProvider pubFileProvider,
IStatusLabelSetter statusLabelSetter,
ISfxPlayer sfxPlayer)
{
_hudControlProvider = hudControlProvider;
_characterRepository = characterRepository;
_currentMapStateProvider = currentMapStateProvider;
_characterRendererProvider = characterRendererProvider;
_currentMapProvider = currentMapProvider;
_spikeTrapActions = spikeTrapActions;
_pubFileProvider = pubFileProvider;
_statusLabelSetter = statusLabelSetter;
_sfxPlayer = sfxPlayer;
_random = new Random();
}
public void Face(EODirection direction)
{
if (!_hudControlProvider.IsInGame)
return;
CancelSpellPrep();
Animator.MainCharacterFace(direction);
}
public void StartWalking()
{
if (!_hudControlProvider.IsInGame)
return;
CancelSpellPrep();
Animator.StartMainCharacterWalkAnimation(Option.None<MapCoordinate>());
ShowWaterSplashiesIfNeeded(CharacterActionState.Walking, _characterRepository.MainCharacter.ID);
if (_characterRepository.MainCharacter.NoWall)
_sfxPlayer.PlaySfx(SoundEffectID.NoWallWalk);
else if (IsSteppingStone(_characterRepository.MainCharacter.RenderProperties))
_sfxPlayer.PlaySfx(SoundEffectID.JumpStone);
}
public void StartAttacking(int noteIndex = -1)
{
if (!_hudControlProvider.IsInGame)
return;
CancelSpellPrep();
if (noteIndex >= 0 || IsInstrumentWeapon(_characterRepository.MainCharacter.RenderProperties.WeaponGraphic))
Animator.Emote(_characterRepository.MainCharacter.ID, EOLib.Domain.Character.Emote.MusicNotes);
Animator.StartMainCharacterAttackAnimation();
ShowWaterSplashiesIfNeeded(CharacterActionState.Attacking, _characterRepository.MainCharacter.ID);
PlayWeaponSound(_characterRepository.MainCharacter, noteIndex);
}
public bool PrepareMainCharacterSpell(int spellId, ISpellTargetable spellTarget)
{
if (!_hudControlProvider.IsInGame)
return false;
var spellData = _pubFileProvider.ESFFile[spellId];
_characterRendererProvider.MainCharacterRenderer.MatchSome(r => r.ShoutSpellPrep(spellData.Shout));
return Animator.MainCharacterShoutSpellPrep(spellData, spellTarget);
}
public void Emote(Emote whichEmote)
{
if (!_hudControlProvider.IsInGame)
return;
Animator.Emote(_characterRepository.MainCharacter.ID, whichEmote);
}
public void StartOtherCharacterWalkAnimation(int characterID, byte destinationX, byte destinationY, EODirection direction)
{
if (!_hudControlProvider.IsInGame || !_currentMapStateProvider.Characters.ContainsKey(characterID))
return;
Animator.StartOtherCharacterWalkAnimation(characterID, destinationX, destinationY, direction);
ShowWaterSplashiesIfNeeded(CharacterActionState.Walking, characterID);
_spikeTrapActions.HideSpikeTrap(characterID);
_spikeTrapActions.ShowSpikeTrap(characterID);
if (IsSteppingStone(_currentMapStateProvider.Characters[characterID].RenderProperties))
_sfxPlayer.PlaySfx(SoundEffectID.JumpStone);
}
public void StartOtherCharacterAttackAnimation(int characterID, int noteIndex = -1)
{
if (!_hudControlProvider.IsInGame || !_currentMapStateProvider.Characters.ContainsKey(characterID))
return;
if (noteIndex >= 0 || IsInstrumentWeapon(_currentMapStateProvider.Characters[characterID].RenderProperties.WeaponGraphic))
Animator.Emote(characterID, EOLib.Domain.Character.Emote.MusicNotes);
Animator.StartOtherCharacterAttackAnimation(characterID);
ShowWaterSplashiesIfNeeded(CharacterActionState.Attacking, characterID);
PlayWeaponSound(_currentMapStateProvider.Characters[characterID], noteIndex);
}
public void NotifyWarpLeaveEffect(short characterId, WarpAnimation anim)
{
if (anim == WarpAnimation.Admin)
_characterRendererProvider.CharacterRenderers[characterId].ShowWarpLeave();
}
public void NotifyWarpEnterEffect(short characterId, WarpAnimation anim)
{
if (anim == WarpAnimation.Admin)
{
if (!_characterRendererProvider.CharacterRenderers.ContainsKey(characterId))
_characterRendererProvider.NeedsWarpArriveAnimation.Add(characterId);
else
_characterRendererProvider.CharacterRenderers[characterId].ShowWarpArrive();
}
}
public void NotifyPotionEffect(short playerId, int effectId)
{
if (playerId == _characterRepository.MainCharacter.ID)
_characterRendererProvider.MainCharacterRenderer.MatchSome(cr => cr.ShowPotionAnimation(effectId));
else if (_characterRendererProvider.CharacterRenderers.ContainsKey(playerId))
_characterRendererProvider.CharacterRenderers[playerId].ShowPotionAnimation(effectId);
}
public void NotifyStartSpellCast(short playerId, short spellId)
{
var shoutName = _pubFileProvider.ESFFile[spellId].Shout;
_characterRendererProvider.CharacterRenderers[playerId].ShoutSpellPrep(shoutName.ToLower());
}
public void NotifyTargetNpcSpellCast(short playerId)
{
// Main player starts its spell cast animation immediately when targeting NPC
// Other players need to wait for packet to be received and do it here
// this is some spaghetti...
if (_characterRendererProvider.CharacterRenderers.ContainsKey(playerId))
{
Animator.StartOtherCharacterSpellCast(playerId);
}
}
public void NotifySelfSpellCast(short playerId, short spellId, int spellHp, byte percentHealth)
{
var spellGraphic = _pubFileProvider.ESFFile[spellId].Graphic;
if (playerId == _characterRepository.MainCharacter.ID)
{
_characterRendererProvider.MainCharacterRenderer.MatchSome(cr =>
{
cr.ShoutSpellCast();
cr.ShowSpellAnimation(spellGraphic);
cr.ShowDamageCounter(spellHp, percentHealth, isHeal: true);
});
}
else
{
Animator.StartOtherCharacterSpellCast(playerId);
_characterRendererProvider.CharacterRenderers[playerId].ShoutSpellCast();
_characterRendererProvider.CharacterRenderers[playerId].ShowSpellAnimation(spellGraphic);
_characterRendererProvider.CharacterRenderers[playerId].ShowDamageCounter(spellHp, percentHealth, isHeal: true);
}
}
public void NotifyTargetOtherSpellCast(short sourcePlayerID, short targetPlayerID, short spellId, int recoveredHP, byte targetPercentHealth)
{
if (sourcePlayerID == _characterRepository.MainCharacter.ID)
{
_characterRendererProvider.MainCharacterRenderer.MatchSome(cr => cr.ShoutSpellCast());
}
else
{
Animator.StartOtherCharacterSpellCast(sourcePlayerID);
_characterRendererProvider.CharacterRenderers[sourcePlayerID].ShoutSpellCast();
}
var spellData = _pubFileProvider.ESFFile[spellId];
if (targetPlayerID == _characterRepository.MainCharacter.ID)
{
_characterRendererProvider.MainCharacterRenderer.MatchSome(cr =>
{
cr.ShowSpellAnimation(spellData.Graphic);
cr.ShowDamageCounter(recoveredHP, targetPercentHealth, isHeal: spellData.Type == EOLib.IO.SpellType.Heal);
});
}
else
{
_characterRendererProvider.CharacterRenderers[targetPlayerID].ShowSpellAnimation(spellData.Graphic);
_characterRendererProvider.CharacterRenderers[targetPlayerID].ShowDamageCounter(recoveredHP, targetPercentHealth, isHeal: spellData.Type == EOLib.IO.SpellType.Heal);
}
}
public void NotifyGroupSpellCast(short playerId, short spellId, short spellHp, List<GroupSpellTarget> spellTargets)
{
if (playerId == _characterRepository.MainCharacter.ID)
{
_characterRendererProvider.MainCharacterRenderer.MatchSome(cr => cr.ShoutSpellCast());
}
else if (_characterRendererProvider.CharacterRenderers.ContainsKey(playerId))
{
Animator.StartOtherCharacterSpellCast(playerId);
_characterRendererProvider.CharacterRenderers[playerId].ShoutSpellCast();
}
else
{
_currentMapStateProvider.UnknownPlayerIDs.Add(playerId);
}
var spellData = _pubFileProvider.ESFFile[spellId];
foreach (var target in spellTargets)
{
if (target.TargetId == _characterRepository.MainCharacter.ID)
{
_characterRendererProvider.MainCharacterRenderer.MatchSome(cr =>
{
cr.ShowSpellAnimation(spellData.Graphic);
cr.ShowDamageCounter(spellHp, target.PercentHealth, isHeal: true);
});
}
else if (_characterRendererProvider.CharacterRenderers.ContainsKey(target.TargetId))
{
_characterRendererProvider.CharacterRenderers[target.TargetId].ShowSpellAnimation(spellData.Graphic);
_characterRendererProvider.CharacterRenderers[target.TargetId].ShowDamageCounter(spellHp, target.PercentHealth, isHeal: true);
}
}
}
public void NotifyMapEffect(MapEffect effect, byte strength = 0)
{
switch (effect)
{
case MapEffect.Quake1:
case MapEffect.Quake2:
case MapEffect.Quake3:
case MapEffect.Quake4:
var mapRenderer = _hudControlProvider.GetComponent<IMapRenderer>(HudControlIdentifier.MapRenderer);
mapRenderer.StartEarthquake(strength);
_sfxPlayer.PlaySfx(SoundEffectID.Earthquake);
break;
case MapEffect.HPDrain:
_sfxPlayer.PlaySfx(SoundEffectID.MapEffectHPDrain);
break;
case MapEffect.TPDrain:
_sfxPlayer.PlaySfx(SoundEffectID.MapEffectTPDrain);
break;
case MapEffect.Spikes:
_sfxPlayer.PlaySfx(SoundEffectID.Spikes);
break;
}
}
public void NotifyEmote(short playerId, Emote emote)
{
try
{
Animator.Emote(playerId, emote);
}
catch (InvalidOperationException)
{
// if still transitioning to in-game state, the game will crash because the in-game control set is not completely set up yet
}
}
public void MakeMainPlayerDrunk()
{
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, EOResourceID.STATUS_LABEL_ITEM_USE_DRUNK);
}
public void NotifyEffectAtLocation(byte x, byte y, short effectId)
{
if (_hudControlProvider.IsInGame)
{
_hudControlProvider.GetComponent<IMapRenderer>(HudControlIdentifier.MapRenderer)
.RenderEffect(x, y, effectId);
}
}
private void ShowWaterSplashiesIfNeeded(CharacterActionState action, int characterID)
{
var character = characterID == _characterRepository.MainCharacter.ID
? Option.Some(_characterRepository.MainCharacter)
: _currentMapStateProvider.Characters.ContainsKey(characterID)
? Option.Some(_currentMapStateProvider.Characters[characterID])
: Option.None<EOLib.Domain.Character.Character>();
var characterRenderer = characterID == _characterRepository.MainCharacter.ID
? _characterRendererProvider.MainCharacterRenderer
: _characterRendererProvider.CharacterRenderers.ContainsKey(characterID)
? Option.Some(_characterRendererProvider.CharacterRenderers[characterID])
: Option.None<ICharacterRenderer>();
character.MatchSome(c =>
{
var rp = c.RenderProperties;
characterRenderer.MatchSome(cr =>
{
if (action == CharacterActionState.Attacking)
{
if (_currentMapProvider.CurrentMap.Tiles[rp.MapY, rp.MapX] == TileSpec.Water)
cr.ShowWaterSplashies();
}
else if (action == CharacterActionState.Walking)
{
if (_currentMapProvider.CurrentMap.Tiles[rp.GetDestinationY(), rp.GetDestinationX()] == TileSpec.Water)
cr.ShowWaterSplashies();
}
});
});
}
private void CancelSpellPrep()
{
Animator.MainCharacterCancelSpellPrep();
_characterRendererProvider.MainCharacterRenderer.MatchSome(r => r.StopShout());
}
private void PlayWeaponSound(EOLib.Domain.Character.Character character, int noteIndex = -1)
{
if (character.RenderProperties.WeaponGraphic == 0)
{
_sfxPlayer.PlaySfx(SoundEffectID.PunchAttack);
return;
}
_pubFileProvider.EIFFile.FirstOrNone(x => x.Type == ItemType.Weapon && x.DollGraphic == character.RenderProperties.WeaponGraphic)
.MatchSome(x =>
{
var instrumentIndex = Constants.InstrumentIDs.ToList().FindIndex(y => y == x.ID);
switch (instrumentIndex)
{
case 0:
{
if (noteIndex < 0 || noteIndex >= 36)
_sfxPlayer.PlaySfx(SoundEffectID.Harp1 + _random.Next(0, 3));
else
_sfxPlayer.PlayHarpNote(noteIndex);
}
break;
case 1:
{
if (noteIndex < 0 || noteIndex >= 36)
_sfxPlayer.PlaySfx(SoundEffectID.Guitar1 + _random.Next(0, 3));
else
_sfxPlayer.PlayGuitarNote(noteIndex);
}
break;
default:
switch (x.SubType)
{
case ItemSubType.Ranged:
if (x.ID == 365 && string.Equals(x.Name, "gun", System.StringComparison.OrdinalIgnoreCase))
_sfxPlayer.PlaySfx(SoundEffectID.Gun);
else
_sfxPlayer.PlaySfx(SoundEffectID.AttackBow);
break;
default:
_sfxPlayer.PlaySfx(SoundEffectID.MeleeWeaponAttack);
break;
}
break;
}
});
}
private bool IsInstrumentWeapon(int weaponGraphic)
{
return _pubFileProvider.EIFFile
.SingleOrNone(x => x.Type == ItemType.Weapon && x.DollGraphic == weaponGraphic)
.Match(some: x => Constants.InstrumentIDs.ToList().FindIndex(y => y == x.ID) >= 0, none: () => false);
}
private bool IsSteppingStone(CharacterRenderProperties renderProps)
{
return _currentMapProvider.CurrentMap.Tiles[renderProps.MapY, renderProps.MapX] == TileSpec.Jump
|| _currentMapProvider.CurrentMap.Tiles[renderProps.GetDestinationY(), renderProps.GetDestinationX()] == TileSpec.Jump;
}
private ICharacterAnimator Animator => _hudControlProvider.GetComponent<ICharacterAnimator>(HudControlIdentifier.CharacterAnimator);
}
public interface ICharacterAnimationActions
{
void Face(EODirection direction);
void StartWalking();
void StartAttacking(int noteIndex = -1);
bool PrepareMainCharacterSpell(int spellId, ISpellTargetable spellTarget);
void Emote(Emote whichEmote);
}
}