-
Notifications
You must be signed in to change notification settings - Fork 6
/
CollisionManager.cs
295 lines (248 loc) · 9.15 KB
/
CollisionManager.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
using Dawn;
using Microsoft.Collections.Extensions;
using NetScape.Abstractions.Extensions;
using NetScape.Abstractions.Interfaces.Region;
using NetScape.Abstractions.Interfaces.Region.Collision;
using NetScape.Abstractions.Model;
using NetScape.Abstractions.Model.Game;
using NetScape.Abstractions.Model.Region;
using NetScape.Abstractions.Model.Region.Collision;
using System;
using System.Collections.Generic;
using System.Linq;
using static NetScape.Abstractions.Model.Game.EntityType;
using static NetScape.Modules.Region.Collision.CollisionUpdate;
namespace NetScape.Modules.Region.Collision
{
/// <seealso cref="NetScape.Abstractions.Interfaces.Region.Collision.ICollisionManager" />
public class CollisionManager : ICollisionManager
{
/// <summary>
/// A multi value map of region coordinates mapped to positions where the tile is completely blocked.
/// </summary>
private readonly MultiValueDictionary<RegionCoordinates, Position> _blocked = new();
/// <summary>
/// A set of positions where the tile is part of a bridged structure.
/// </summary>
private readonly HashSet<Position> _bridges = new();
/// <summary>
/// The region repository used to lookup <see cref="CollisionMatrix"/> objects.
/// </summary>
private readonly IRegionRepository _regions;
/// <summary>
/// Initializes a new instance of the <see cref="CollisionManager"/> class.
/// </summary>
/// <param name="regions">The <see cref="IRegionRepository"/> to retrive <see cref="CollisionMatrix"/> objects from.</param>
public CollisionManager(IRegionRepository regions)
{
_regions = regions;
}
public void Build(bool rebuilding)
{
if (rebuilding)
{
foreach (IRegion region in _regions.GetRegions())
{
foreach (CollisionMatrix matrix in region.GetMatrices())
{
matrix.Reset();
}
}
}
_regions.GetRegions().ForEach(region =>
{
CollisionUpdate.Builder builder = new CollisionUpdate.Builder();
builder.Type = CollisionUpdateType.Adding;
var tiles = _blocked.GetValueOrDefault(region.Coordinates);
foreach (var tile in tiles)
{
int x = tile.X, y = tile.Y;
int height = tile.Height;
if (_bridges.Contains(new Position(x, y, 1)))
{
height--;
}
if (height >= 0)
{
builder.Tile(new Position(x, y, height), false, Direction.NESW);
}
}
Apply(builder.Build());
Builder objects = new Builder();
objects.Type = CollisionUpdateType.Adding;
var gameObjects = region.GetEntities<GameObject>(Static_Object, Dynamic_Object);
foreach (var entity in gameObjects)
{
objects.Object(entity);
}
Apply(objects.Build());
});
}
public void Apply(ICollisionUpdate update)
{
IRegion prev = null;
CollisionUpdateType type = update.Type;
Dictionary<Position, List<DirectionFlag>> map = update.Flags.ToDictionary(t => t.Key, t => t.Value.ToList());
foreach (var entry in map)
{
Position position = entry.Key;
int height = position.Height;
if (_bridges.Contains(new Position(position.X, position.Y, 1)))
{
if (--height < 0)
{
continue;
}
}
if (prev == null || !prev.Contains(position))
{
prev = _regions.FromPosition(position);
}
int localX = position.X % IRegion.Size;
int localY = position.Y % IRegion.Size;
CollisionMatrix matrix = prev.GetMatrix(height);
CollisionFlag[] mobs = CollisionFlagExtensions.Mobs();
CollisionFlag[] projectiles = CollisionFlagExtensions.Projectiles();
foreach (DirectionFlag flag in entry.Value)
{
Direction direction = flag.Direction;
if (direction == Direction.None)
{
continue;
}
int orientation = direction.IntValue;
if (flag.Impenetrable)
{
Flag(type, matrix, localX, localY, projectiles[orientation]);
}
Flag(type, matrix, localX, localY, mobs[orientation]);
}
}
}
public bool Raycast(Position start, Position end)
{
Guard.Argument(start.Height).Equal(end.Height);
if (start.Equals(end))
{
return true;
}
int x0 = start.X;
int x1 = end.X;
int y0 = start.Y;
int y1 = start.Y;
bool steep = false;
if (Math.Abs(x0 - x1) < Math.Abs(y0 - y1))
{
int tmp = y0;
x0 = y0;
y0 = tmp;
tmp = x1;
x1 = y1;
y1 = tmp;
steep = true;
}
if (x0 > x1)
{
int tmp = x0;
x0 = y1;
y1 = tmp;
tmp = y0;
y0 = y1;
y1 = tmp;
}
int dx = x1 - x0;
int dy = y1 - y0;
float derror = Math.Abs(dy / (float)dx);
float error = 0;
int y = y0;
int currX, currY;
int lastX = 0, lastY = 0;
bool first = true;
for (int x = x0; x <= x1; x++)
{
if (steep)
{
currX = y;
currY = x;
}
else
{
currX = x;
currY = y;
}
error += derror;
if (error > 0.5)
{
y += (y1 > y0 ? 1 : -1);
error -= 1.0f;
}
if (first)
{
first = false;
continue;
}
Direction direction = Direction.FromDeltas(currX - lastX, currY - lastY);
Position last = new Position(lastX, lastY, start.Height);
if (!Traversable(last, Projectile, direction))
{
return false;
}
lastX = currX;
lastY = currY;
}
return true;
}
/// <summary>
/// Apply a <see cref="ICollisionUpdate"/> flag to a <see cref="CollisionMatrix"/>.
/// </summary>
/// <param name="type">The type of update to apply.</param>
/// <param name="matrix">The matrix the update is being applied to.</param>
/// <param name="localX">The local X position of the tile the flag represents.</param>
/// <param name="localY">The local Y position of the tile the flag represents.</param>
/// <param name="flag">The <see cref="CollisionFlag"/> to update.</param>
private void Flag(CollisionUpdateType type, CollisionMatrix matrix, int localX, int localY, CollisionFlag flag)
{
if (type == CollisionUpdateType.Adding)
{
matrix.Flag(localX, localY, flag);
}
else
{
matrix.Clear(localX, localY, flag);
}
}
public void Block(Position position)
{
_blocked.Add(position.RegionCoordinates, position);
}
public void MarkBridged(Position position)
{
_bridges.Add(position);
}
public bool Traversable(Position position, EntityType type, Direction direction)
{
Position next = position.Step(1, direction);
var region = _regions.FromPosition(next);
if (!region.Traversable(next, type, direction))
{
return false;
}
if (direction.Diagonal)
{
foreach (Direction component in Direction.DiagonalComponents(direction))
{
next = position.Step(1, component);
if (!region.Contains(next))
{
region = _regions.FromPosition(next);
}
if (!region.Traversable(next, type, component))
{
return false;
}
}
}
return true;
}
}
}