-
Notifications
You must be signed in to change notification settings - Fork 16
/
NPCAnimator.cs
146 lines (117 loc) · 5.19 KB
/
NPCAnimator.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
using EndlessClient.GameExecution;
using EOLib;
using EOLib.Domain.Extensions;
using EOLib.Domain.Map;
using EOLib.Domain.NPC;
using Microsoft.Xna.Framework;
using Optional.Collections;
using System.Collections.Generic;
using System.Linq;
namespace EndlessClient.Rendering.NPC
{
public class NPCAnimator : GameComponent, INPCAnimator
{
private const int TICKS_PER_ACTION_FRAME = 8; // 8 x10ms ticks per action frame
private readonly List<RenderFrameActionTime> _npcStartWalkingTimes;
private readonly List<RenderFrameActionTime> _npcStartAttackingTimes;
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly IFixedTimeStepRepository _fixedTimeStepRepository;
public NPCAnimator(IEndlessGameProvider gameProvider,
ICurrentMapStateRepository currentMapStateRepository,
IFixedTimeStepRepository fixedTimeStepRepository)
: base((Game)gameProvider.Game)
{
_currentMapStateRepository = currentMapStateRepository;
_fixedTimeStepRepository = fixedTimeStepRepository;
_npcStartWalkingTimes = new List<RenderFrameActionTime>();
_npcStartAttackingTimes = new List<RenderFrameActionTime>();
}
public override void Update(GameTime gameTime)
{
AnimateNPCWalking();
AnimateNPCAttacking();
base.Update(gameTime);
}
public void StartWalkAnimation(int npcIndex)
{
if (_npcStartWalkingTimes.Any(x => x.UniqueID == npcIndex))
return;
var startWalkingTimeAndID = new RenderFrameActionTime(npcIndex, _fixedTimeStepRepository.TickCount);
_npcStartWalkingTimes.Add(startWalkingTimeAndID);
}
public void StartAttackAnimation(int npcIndex)
{
if (_npcStartAttackingTimes.Any(x => x.UniqueID == npcIndex))
return;
var startAttackingTimeAndID = new RenderFrameActionTime(npcIndex, _fixedTimeStepRepository.TickCount);
_npcStartAttackingTimes.Add(startAttackingTimeAndID);
}
public void StopAllAnimations()
{
_npcStartWalkingTimes.Clear();
}
private void AnimateNPCWalking()
{
var npcsDoneWalking = new List<RenderFrameActionTime>();
foreach (var pair in _npcStartWalkingTimes)
{
if ((_fixedTimeStepRepository.TickCount - pair.ActionTick) >= TICKS_PER_ACTION_FRAME)
{
var npc = _currentMapStateRepository.NPCs.SingleOrNone(x => x.Index == pair.UniqueID);
npc.Match(
some: n =>
{
var nextFrameNPC = AnimateOneWalkFrame(n);
pair.UpdateActionStartTime(_fixedTimeStepRepository.TickCount);
if (nextFrameNPC.Frame == NPCFrame.Standing)
npcsDoneWalking.Add(pair);
_currentMapStateRepository.NPCs.Remove(n);
_currentMapStateRepository.NPCs.Add(nextFrameNPC);
},
none: () => npcsDoneWalking.Add(pair));
}
}
_npcStartWalkingTimes.RemoveAll(npcsDoneWalking.Contains);
}
private void AnimateNPCAttacking()
{
var npcsDoneAttacking = new List<RenderFrameActionTime>();
foreach (var pair in _npcStartAttackingTimes)
{
if ((_fixedTimeStepRepository.TickCount - pair.ActionTick) >= TICKS_PER_ACTION_FRAME)
{
var npc = _currentMapStateRepository.NPCs.SingleOrNone(x => x.Index == pair.UniqueID);
npc.Match(
some: n =>
{
var nextFrameNPC = n.WithNextAttackFrame();
pair.UpdateActionStartTime(_fixedTimeStepRepository.TickCount);
if (nextFrameNPC.Frame == NPCFrame.Standing)
npcsDoneAttacking.Add(pair);
_currentMapStateRepository.NPCs.Remove(n);
_currentMapStateRepository.NPCs.Add(nextFrameNPC);
},
none: () => npcsDoneAttacking.Add(pair));
}
}
_npcStartAttackingTimes.RemoveAll(npcsDoneAttacking.Contains);
}
private static EOLib.Domain.NPC.NPC AnimateOneWalkFrame(EOLib.Domain.NPC.NPC npc)
{
var nextFrameNPC = npc.WithNextWalkFrame();
if (nextFrameNPC.IsActing(NPCActionState.Standing))
{
nextFrameNPC = nextFrameNPC
.WithX(nextFrameNPC.GetDestinationX())
.WithY(nextFrameNPC.GetDestinationY());
}
return nextFrameNPC;
}
}
public interface INPCAnimator : IGameComponent
{
void StartWalkAnimation(int npcIndex);
void StartAttackAnimation(int npcIndex);
void StopAllAnimations();
}
}