-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreature.cs
260 lines (191 loc) · 6.61 KB
/
Creature.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
using System.Threading;
using LiteNetLib;
using LiteNetLib.Utils;
using System;
using System.Threading.Tasks;
namespace MmorpgServer
{
public class Creature : Entity, Updatable, Integrable
{
Vector2 Velocity;
Int32 Ticks;
bool canWalk = true;
bool canAttack = true;
bool isMoving = false;
TimeSpan canStopAt;
TimeSpan? stopAt;
protected double MovementDirection;
public Creature()
{
CollisionShape = new Circle(Vector2.Zero, 0.5);
Solid = true;
}
public delegate void MoveHandler(Creature creature, in Vector2 from, in Vector2 to);
public delegate void StartedAttackingHandler(Creature creature);
public delegate void StoppedAttackingHandler(Creature creature);
public delegate void StartedMovingHandler(Creature creature);
public delegate void StoppedMovingHandler(Creature creature);
public delegate void RotationChangedHandler(Creature creature, in double from, in double to);
public delegate void GetHitHandler(Creature creature);
public event PropertyChangedHandlerFast<Creature, Vector2> Moved;
public event PropertyChangedHandler<Creature, bool> IsMovingChanged;
public event RotationChangedHandler RotationChanged;
public event GetHitHandler GetHit;
public event StartedAttackingHandler StartedAttacking;
public event StoppedMovingHandler StoppedAttacking;
public override void SendEnterPacket(NetPeer peer)
{
NetDataWriter writer = new NetDataWriter();
writer.Put((byte)1);
writer.Put((Int32)Id);
writer.Put((float)CollisionShape.Rotation);
writer.Put((float)CollisionShape.Position.X);
writer.Put((float)CollisionShape.Position.Y);
peer.Send(writer, DeliveryMethod.ReliableOrdered);
}
public override bool HideOnFog
{
get
{
return true;
}
}
public double Radius
{
get
{
return 0.5;
}
}
public Double MovementSpeed;
public override void Initialize()
{
base.Initialize();
World.Updateables.Add(this);
}
public override void Destroy()
{
World.Updateables.Remove(this);
base.Destroy();
}
public void Update(Double delta)
{
if (stopAt != null && World.CurrentTime >= stopAt)
{
stopAt = null;
MovementSpeed = 0;
}
if (canWalk)
{
this.Velocity = new Vector2(Math.Cos(MovementDirection) * MovementSpeed * delta, Math.Sin(MovementDirection) * MovementSpeed * delta);
if (this.Velocity.SqrLength > 0)
{
World.Integrables.Enqueue(this);
this.Ticks = 0;
}
else
{
if (isMoving)
{
isMoving = false;
IsMovingChanged?.Invoke(this, true, false);
}
}
}
else
{
if (isMoving)
{
isMoving = false;
IsMovingChanged?.Invoke(this, true, false);
}
}
}
public void Integrate()
{
++this.Ticks;
if (this.Ticks > 10)
return;
var from = CollisionShape.Position;
CollisionShape.Position = from + Velocity;
CollisionResult result;
World.ResolveCollision(this, out result);
if (result.Penetration > 0)
{
CollisionShape.Position += (result.Normal * (result.Penetration + 0.01));
double velocityLength = Velocity.Length;
double remainingLength = velocityLength - result.Penetration;
double dot = (Velocity.X * result.Normal.Y + Velocity.Y * result.Normal.X) * remainingLength;
Velocity.X = dot * result.Normal.Y;
Velocity.Y = dot * result.Normal.X;
World.Integrables.Enqueue(this);
}
if (from != Position)
{
OnPositionChanged(from, Position);
if (!isMoving)
{
isMoving = true;
IsMovingChanged?.Invoke(this, false, true);
}
Moved?.Invoke(this, from, Position);
}
}
public async Task Attack()
{
if (canAttack)
{
canAttack = false;
try
{
Task canAttackDelay = World.Delay(TimeSpan.FromSeconds(1));
StartedAttacking?.Invoke(this);
await canAttackDelay;
StoppedAttacking?.Invoke(this);
}
finally
{
canAttack = true;
}
}
}
public void SetRotation(double angle)
{
if (angle != CollisionShape.Rotation)
{
double from = CollisionShape.Rotation;
CollisionShape.Rotation = angle;
RotationChanged?.Invoke(this, from, angle);
}
}
public override void AddEvents(PlayerController pc)
{
Moved += pc.CreatureMoved;
StartedAttacking += pc.CreatureStartedAttacking;
StoppedAttacking += pc.CreatureStoppedAttacking;
IsMovingChanged += pc.CreatureIsMovingChanged;
RotationChanged += pc.CreatureRotationChanged;
base.AddEvents(pc);
}
public override void RemoveEvents(PlayerController pc)
{
Moved -= pc.CreatureMoved;
StartedAttacking -= pc.CreatureStartedAttacking;
StoppedAttacking -= pc.CreatureStoppedAttacking;
IsMovingChanged += pc.CreatureIsMovingChanged;
RotationChanged -= pc.CreatureRotationChanged;
base.RemoveEvents(pc);
}
public void Move(double direction)
{
MovementSpeed = 6;
MovementDirection = direction;
canStopAt = World.CurrentTime + TimeSpan.FromSeconds(0.1);
stopAt = null;
}
public void Stop()
{
stopAt = canStopAt;
}
}
}