-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevel.cs
239 lines (207 loc) · 8.3 KB
/
Level.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAHelpers;
using Microsoft.Xna.Framework.Content;
namespace _2DVisbility
{
class Level
{
private bool[,] _cellsAreInvisible;
private bool[,] _cellsAreOpaque;
private bool[,] _cellsAreDiscovered;
private Texture2D _invisibleTexture;
private Texture2D _visibleTexture;
private Texture2D _opaqueTexture;
private Vector2 _levelSize;
private Game1 _game;
public Player Player { get; set; }
public Vector2 PlayerOrientation { get; set; }
public bool HasDrawn { get; set; }
public Level(Game1 game)
{
Texture2D levelInfo = game.Content.Load<Texture2D>("Level");
Color[] levelData = new Color[levelInfo.Width * levelInfo.Height];
levelInfo.GetData<Color>(levelData);
PlayerOrientation = new Vector2(-5);
_game = game;
_levelSize = new Vector2(levelInfo.Width, levelInfo.Height);
_cellsAreInvisible = new bool[(int)_levelSize.X, (int)_levelSize.Y];
_cellsAreOpaque = new bool[(int)_levelSize.X, (int)_levelSize.Y];
_cellsAreDiscovered = new bool[(int)_levelSize.X, (int)_levelSize.Y];
for (int x = 0; x < levelInfo.Width; x++)
{
for (int y = 0; y < levelInfo.Height; y++)
{
if (levelData[x + y * levelInfo.Width] == Color.White)
{
_cellsAreOpaque[x, y] = true;
}
}
}
_invisibleTexture = game.Content.Load<Texture2D>("Invisible");
_visibleTexture = game.Content.Load<Texture2D>("Visible");
_opaqueTexture = game.Content.Load<Texture2D>("Opaque");
}
public void Update()
{
ResetAllCells();
List<Vector2> validCells = GetAllValidCells(Player.Position);
foreach (Vector2 cell in validCells)
{
List<Vector2> intersectingCells = GetCellsIntersectingWithLine(Player.Position, cell);
List<Vector2> solidIntersections = intersectingCells.Where((testCell) => _cellsAreOpaque[(int)testCell.X, (int)testCell.Y]).ToList();
Vector2 targetLine = cell - Player.Position;
targetLine.Normalize();
Vector2 normalizedOrientation = PlayerOrientation;
normalizedOrientation.Normalize();
float playerRadians = (float)Math.Atan2(normalizedOrientation.Y, normalizedOrientation.X);
float coneLeftRadians = playerRadians - (Player.ConeWidth / 2);
float coneRightRadians = playerRadians + (Player.ConeWidth / 2);
float targetRadians = (float)Math.Atan2(targetLine.Y, targetLine.X);
bool isInLoS = PlayerOrientation != new Vector2(-5) &&
MathFunctions.Wrap(targetRadians - playerRadians, -(float)Math.PI, (float)Math.PI) < Player.ConeWidth / 2 &&
MathFunctions.Wrap(targetRadians - playerRadians, -(float)Math.PI, (float)Math.PI) > -(Player.ConeWidth / 2);
if (!HasDrawn)
{
Vector2 coneLeftVector = new Vector2(
(float)Math.Cos(coneLeftRadians),
(float)Math.Sin(coneLeftRadians)) * Player.VisionRadius;
Vector2 coneRightVector = new Vector2(
(float)Math.Cos(coneRightRadians),
(float)Math.Sin(coneRightRadians)) * Player.VisionRadius;
_game.DebugLineDrawer.QueueLine(new Line2D
{
Start = (Player.Position * Game1.TileSize),
End = (Player.Position + coneLeftVector) * Game1.TileSize,
Color = Color.Yellow,
});
_game.DebugLineDrawer.QueueLine(new Line2D
{
Start = (Player.Position * Game1.TileSize),
End = (Player.Position + coneRightVector) * Game1.TileSize,
Color = Color.Yellow,
});
HasDrawn = true;
}
if (solidIntersections.Count > 0 &&
!(solidIntersections.Count == 1 && solidIntersections[0] == cell))
{
_cellsAreInvisible[(int)cell.X, (int)cell.Y] = true;
}
else if (isInLoS)
{
_cellsAreInvisible[(int)cell.X, (int)cell.Y] = false;
_cellsAreDiscovered[(int)cell.X, (int)cell.Y] = true;
}
}
//perform visibility calcs here
}
private void ResetAllCells()
{
for (int x = 0; x < _levelSize.X; x++)
{
for (int y = 0; y < _levelSize.Y; y++)
{
_cellsAreInvisible[x, y] = true;
}
}
}
private List<Vector2> GetAllValidCells(Vector2 basePosition)
{
List<Vector2> retList = new List<Vector2>();
for (int x = 0; x < _levelSize.X; x++)
{
for (int y = 0; y < _levelSize.Y; y++)
{
if ((new Vector2(x + 0.5f, y + 0.5f) - basePosition).Length() < Player.VisionRadius)
{
retList.Add(new Vector2(x, y));
}
}
}
return retList;
}
//Bresenham's line algorithm
private List<Vector2> GetCellsIntersectingWithLine(Vector2 start, Vector2 end)
{
List<Vector2> retList = new List<Vector2>();
bool lineIsSteep = Math.Abs(end.Y - start.Y) > Math.Abs(end.X - start.X);
if (lineIsSteep) //swap x and y to make line not steep
{
Vector2 temp = start;
temp.X = start.Y;
temp.Y = start.X;
start = temp;
temp = end;
temp.X = end.Y;
temp.Y = end.X;
end = temp;
}
//swap start and end
if (start.X > end.X)
{
Vector2 temp = end;
end = start;
start = temp;
}
int deltaX = (int)(end.X - start.X);
int deltaY = (int)Math.Abs(end.Y - start.Y);
int error = deltaX / 2;
int yStep = start.Y < end.Y ? 1 : -1;
int y = (int)start.Y;
for (int x = (int)start.X; x < end.X; x++)
{
if (lineIsSteep)
{
retList.Add(new Vector2(y, x));
}
else
{
retList.Add(new Vector2(x, y));
}
error -= deltaY;
if (error < 0)
{
y += yStep;
error += deltaX;
}
}
return retList;
}
public void Draw(SpriteBatch batch)
{
for (int x = 0; x < _levelSize.X; x++)
{
for (int y = 0; y < _levelSize.Y; y++)
{
Texture2D drawTex;
if (_cellsAreInvisible[x, y])
{
drawTex = _invisibleTexture;
}
else
{
drawTex = _visibleTexture;
}
if (_cellsAreOpaque[x, y])
{
drawTex = _opaqueTexture;
}
if (_cellsAreDiscovered[x, y])
{
batch.Draw(drawTex, new Vector2(x * Game1.TileSize, y * Game1.TileSize), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
}
}
}
}
public bool GetCellIsOpaque(Vector2 position)
{
return _cellsAreOpaque[(int)position.X, (int)position.Y];
}
}
}