2020-02-12 18:39:51 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
|
|
|
|
class LinesOfSight {
|
2020-02-12 20:11:09 +00:00
|
|
|
|
|
2020-02-12 20:54:28 +00:00
|
|
|
|
const int numEdgeVertices = 60;
|
|
|
|
|
// coneVertices[0] is the eye position; the rest are the edge vertices.
|
|
|
|
|
VertexPositionColor[] coneVertices = new VertexPositionColor[numEdgeVertices + 1];
|
2020-02-12 20:11:09 +00:00
|
|
|
|
Color color = Color.FromNonPremultiplied(new Vector4(0, 0, 1, 0.6f));
|
2020-02-12 20:54:28 +00:00
|
|
|
|
// The number of total triangles drawn is one less than the number of edge points.
|
|
|
|
|
int[] indices = new int[(numEdgeVertices - 1) * 3];
|
2020-02-12 20:11:09 +00:00
|
|
|
|
VertexBuffer vertexBuffer;
|
2020-02-12 20:54:28 +00:00
|
|
|
|
IndexBuffer indexBuffer;
|
2020-02-12 20:11:09 +00:00
|
|
|
|
|
|
|
|
|
public LinesOfSight(GraphicsDevice graphics) {
|
|
|
|
|
vertexBuffer = new VertexBuffer(
|
2020-02-12 20:54:28 +00:00
|
|
|
|
graphics, typeof(VertexPositionColor), numEdgeVertices * 3, BufferUsage.WriteOnly);
|
|
|
|
|
indexBuffer = new IndexBuffer(
|
|
|
|
|
graphics, typeof(int), indices.Length, BufferUsage.WriteOnly);
|
2020-02-12 20:11:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(Player player, AABB[] collisionTargets) {
|
2020-02-12 18:39:51 +00:00
|
|
|
|
Vector2 eyePos = player.EyePosition;
|
2020-02-12 20:11:09 +00:00
|
|
|
|
|
2020-02-12 18:39:51 +00:00
|
|
|
|
float visionRange = 150;
|
|
|
|
|
float visionRangeSq = visionRange * visionRange;
|
|
|
|
|
float fov = FMath.DegToRad(120);
|
2020-02-12 20:54:28 +00:00
|
|
|
|
float fovStep = fov / (numEdgeVertices - 1);
|
2020-02-12 18:39:51 +00:00
|
|
|
|
|
|
|
|
|
Vector2 ray = new Vector2(visionRange * player.GetFacing, 0);
|
|
|
|
|
if (player.GetPose == Player.Pose.Stretching) {
|
|
|
|
|
ray = ray.Rotate(player.GetFacing * FMath.DegToRad(-30));
|
|
|
|
|
}
|
|
|
|
|
if (player.GetPose == Player.Pose.Crouching) {
|
|
|
|
|
ray = ray.Rotate(player.GetFacing * FMath.DegToRad(30));
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 20:54:28 +00:00
|
|
|
|
coneVertices[0] = new VertexPositionColor(new Vector3(player.EyePosition, 0), color);
|
|
|
|
|
for (int i = 0; i < numEdgeVertices; i++) {
|
2020-02-12 18:39:51 +00:00
|
|
|
|
float angle = -fov / 2 + fovStep * i;
|
|
|
|
|
Vector2 rotated = ray.Rotate(angle);
|
|
|
|
|
Vector2 closestHit = Vector2.Add(eyePos, rotated);
|
|
|
|
|
float hitTime = 1f;
|
|
|
|
|
|
|
|
|
|
Vector2 halfTileSize = new Vector2(World.TileSize / 2.0f, World.TileSize / 2.0f);
|
|
|
|
|
for (int j = 0; j < collisionTargets.Length; j++) {
|
|
|
|
|
AABB box = collisionTargets[j];
|
|
|
|
|
if (Math.Abs(box.Position.X - player.Position.X) > visionRange + halfTileSize.X) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Vector2 delta = Vector2.Add(halfTileSize, Vector2.Subtract(box.Position, eyePos));
|
|
|
|
|
if (delta.LengthSquared() > visionRangeSq) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Hit? maybeHit = box.IntersectSegment(eyePos, rotated);
|
|
|
|
|
if (maybeHit != null) {
|
|
|
|
|
Hit hit = maybeHit.Value;
|
|
|
|
|
if (hit.Time < hitTime) {
|
|
|
|
|
hitTime = hit.Time;
|
|
|
|
|
closestHit = hit.Position;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float tint = 0.6f - hitTime / 2;
|
|
|
|
|
Color tinted = Color.FromNonPremultiplied(new Vector4(0, 0, 1, tint));
|
2020-02-12 20:54:28 +00:00
|
|
|
|
coneVertices[i + 1] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
|
2020-02-12 18:39:51 +00:00
|
|
|
|
}
|
2020-02-12 20:11:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Draw(Player player, AABB[] collisionTargets, GraphicsDevice graphics,
|
|
|
|
|
BasicEffect lightingEffect) {
|
2020-02-12 18:39:51 +00:00
|
|
|
|
|
2020-02-12 20:54:28 +00:00
|
|
|
|
for (int i = 0; i < numEdgeVertices - 1; i++) {
|
|
|
|
|
indices[i * 3] = 0;
|
|
|
|
|
indices[i * 3 + 1] = i + 1;
|
|
|
|
|
indices[i * 3 + 2] = i + 2;
|
2020-02-12 18:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 20:54:28 +00:00
|
|
|
|
vertexBuffer.SetData(coneVertices);
|
|
|
|
|
indexBuffer.SetData(indices);
|
2020-02-12 18:39:51 +00:00
|
|
|
|
graphics.SetVertexBuffer(vertexBuffer);
|
2020-02-12 20:54:28 +00:00
|
|
|
|
graphics.Indices = indexBuffer;
|
2020-02-12 18:39:51 +00:00
|
|
|
|
|
|
|
|
|
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
|
|
|
|
|
pass.Apply();
|
2020-02-13 21:04:12 +00:00
|
|
|
|
graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3);
|
2020-02-12 18:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|