-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathfind.cs
More file actions
132 lines (119 loc) · 3.75 KB
/
Pathfind.cs
File metadata and controls
132 lines (119 loc) · 3.75 KB
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
using System;
using Dungeon_Delvers.Managers;
using Dungeon_Delvers.Creatures;
using Microsoft.Xna.Framework;
namespace Dungeon_Delvers;
static class PathFind
{
public static Directions? BresenhamLine(int x0, int y0, int x1, int y1, int range, Tile[,] tiles)
{
if (Math.Sqrt(Math.Pow(x0 - x1, 2) + Math.Pow(y0 - y1, 2)) > range)
{
return null;
}
bool swap = Math.Abs(y1-y0) > Math.Abs(x1-x0);
bool oneZeroSwap = false;
if (swap)
{
(x0, y0) = (y0, x0);
(x1, y1) = (y1, x1);
}
if (x0 > x1)
{
oneZeroSwap = true;
(x0, x1) = (x1, x0);
(y0, y1) = (y1, y0);
}
int dx = x1 - x0;
int dy = Math.Abs(y1 - y0);
int D = dx / 2;
int y = y0;
int ySign;
if (y0 < y1) ySign = 1;
else ySign = -1;
// The first point to move to in order to move towards player
for (int x = x0; x <= x1; x++)
{
if (swap)
{
if (tiles[y, x].IsWall) return null;
}
else
{
if (tiles[x, y].IsWall) return null;
}
D -= dy;
if (D < 0)
{
y += ySign;
D += dx;
}
}
if (swap) // swapping back
{
(x0, y0) = (y0, x0);
(x1, y1) = (y1, x1);
}
if (oneZeroSwap)
{
(x0, x1) = (x1, x0);
(y0, y1) = (y1, y0);
}
// You might be tempted to claim that the naming of these variables is at best unhelpful.
// I would be inclined to agree
Directions direction;
bool XLessThan = y1 < y0;
bool XMoreThan = y1 > y0;
bool YLessThan = x1 < x0;
bool YMoreThan = x1 > x0;
if (XLessThan && YLessThan) direction = Directions.UpLeft;
else if (XMoreThan && YLessThan) direction = Directions.UpRight;
else if (XLessThan && YMoreThan) direction = Directions.DownLeft;
else if (XMoreThan && YMoreThan) direction = Directions.DownRight;
else if (XMoreThan) direction = Directions.Right;
else if (XLessThan) direction = Directions.Left;
else if (YMoreThan) direction = Directions.Down;
else direction = Directions.Up; // YLessThan
return direction;
}
public static Creature? CreatureInDir(Directions direction, Point position, Tile[,] tiles)
{
Point movement;
Tile tile;
switch (direction)
{
case (Directions.UpLeft):
movement = new Point(-1, -1);
break;
case (Directions.UpRight):
movement = new Point(1, -1);
break;
case (Directions.DownLeft):
movement = new Point(-1, 1);
break;
case (Directions.DownRight):
movement = new Point(1, 1);
break;
case (Directions.Up):
movement = new Point(0, -1);
break;
case (Directions.Down):
movement = new Point(0, 1);
break;
case (Directions.Left):
movement = new Point(-1, 0);
break;
default: // Directions.Right
movement = new Point(1, 0);
break;
}
do
{
position += movement;
tile = tiles[position.Y, position.X];
} while (!tile.IsWall && tile.Creature == null &&
position.X < tiles.GetLength(0)-1 && position.X > 0 &&
position.Y < tiles.GetLength(0)-1 && position.Y > 0);
return tile.Creature;
}
}