-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWalker.cs
56 lines (47 loc) · 1.51 KB
/
Walker.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
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace Dungeon
{
public class Walker
{
private static readonly Dictionary<MoveDirection, Size> directionToOffset = new Dictionary<MoveDirection, Size>
{
{MoveDirection.Up, new Size(0, -1)},
{MoveDirection.Down, new Size(0, 1)},
{MoveDirection.Left, new Size(-1, 0)},
{MoveDirection.Right, new Size(1, 0)}
};
private static readonly Dictionary<Size, MoveDirection> offsetToDirection = new Dictionary<Size, MoveDirection>
{
{new Size(0, -1), MoveDirection.Up},
{new Size(0, 1), MoveDirection.Down},
{new Size(-1, 0), MoveDirection.Left},
{new Size(1, 0), MoveDirection.Right}
};
public static readonly IReadOnlyList<Size> PossibleDirections = offsetToDirection.Keys.ToList();
public Point Position { get; }
public Point? PointOfCollision { get; }
public Walker(Point position)
{
Position = position;
PointOfCollision = null;
}
private Walker(Point position, Point pointOfCollision)
{
Position = position;
PointOfCollision = pointOfCollision;
}
public Walker WalkInDirection(Map map, MoveDirection direction)
{
var newPoint = Position + directionToOffset[direction];
if (!map.InBounds(newPoint))
return new Walker(Position, Position);
return map.Dungeon[newPoint.X, newPoint.Y] == MapCell.Wall ? new Walker(newPoint, newPoint) : new Walker(newPoint);
}
public static MoveDirection ConvertOffsetToDirection(Size offset)
{
return offsetToDirection[offset];
}
}
}