-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRaycasting.cs
96 lines (85 loc) · 3.72 KB
/
Raycasting.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
//using VectorLib;
namespace ROQWE
{
class Raycasting
{
public static bool Raycast(Vector source, Vector direction, Entity target)
{
if (target == null)
{
return false;
}
Vector TargetHighAngle;
Vector TargetLowAngle;
Vector SourceAngle = Vector.RadiansToVector(direction.Angle, 1);
Vector TopLeft = ((Vector)target.Position.XY - source) * Game.Scale + (new Vector(-Game.Scale, Game.Scale) / 2);
Vector TopRight = ((Vector)target.Position.XY - source) * Game.Scale + (new Vector(Game.Scale, Game.Scale) / 2);
Vector BottomRight = ((Vector)target.Position.XY - source) * Game.Scale + (new Vector(Game.Scale, -Game.Scale) / 2);
Vector BottomLeft = ((Vector)target.Position.XY - source) * Game.Scale + (new Vector(-Game.Scale, -Game.Scale) / 2);
double Angle1 = Math.Abs(Vector.CrossProduct(BottomRight - TopLeft, SourceAngle));
double Angle2 = Math.Abs(Vector.CrossProduct(TopRight - BottomLeft, SourceAngle));
if (Angle1 >= Angle2)
{
TargetHighAngle = TopRight;
TargetLowAngle = BottomLeft;
}
else
{
TargetHighAngle = TopLeft;
TargetLowAngle = BottomRight;
}
return (Vector.IsBetween(SourceAngle, TargetHighAngle, TargetLowAngle));
}
public static Entity Cast(Vector start, Vector direction)
{
/*Cube debug = new Cube((float)(start * Game.Scale + direction * Game.Scale).X, (float)(start * Game.Scale + direction * Game.Scale).Y, 20, 1, Color.Blue, 2)
{
Angle = direction.Angle
};//*/
List<Entity> RawTiles = new List<Entity>();
Vector Direction = direction;//(new Vector(1,1) * Vector.RotateVector(direction, Math.PI / 1)).Normalize();
for (Vector coords = start; (start -coords).Magnitude <= 50; coords += Direction)
{
for (int x = -1; x < 2 ; x++)
{
for (int y = -1; y < 2; y++)
{
for (int z = 0; z < 4; z++)
{
Entity tile = Game.Level[Game.Where].Find((coords + new Vector(x, y), z));
if (!" _".Contains(tile.Type)) RawTiles.Add(tile);
}
}
}
}
List<Entity> Tiles = RawTiles.Distinct().ToList();
foreach(Entity tile in Tiles)
{
//tile.Pic = new Quad(tile.X * Game.Scale, tile.Y * Game.Scale, Game.Scale, Game.Scale, Color.Black, -1);
//Game.DQD.Add(tile);
//Game.DQD.Add(tile);
if (!" _.@".Contains(tile.Type))
{
//tile.Pic = new Quad(tile.X * Game.Scale, tile.Y * Game.Scale, Game.Scale, Game.Scale, Color.Black,4);
//Game.DQD.Add(tile);
if (Raycast(start, Direction, tile))
{
return tile;
}
else
{
//debug.SetColor(Color.Red);
}
}
}
//Game.DQD.Add(new Entity(0, 0, 'D', Guid.NewGuid(), debug, 10));
return new Entity(new Vector(0), ' ');
}
}
}