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
|
|
|
|
|
|
|
|
|
const int numConePoints = 60;
|
|
|
|
|
VertexPositionColor[] conePoints = new VertexPositionColor[numConePoints];
|
|
|
|
|
Color color = Color.FromNonPremultiplied(new Vector4(0, 0, 1, 0.6f));
|
|
|
|
|
VertexPositionColor[] vertices = new VertexPositionColor[numConePoints * 3];
|
|
|
|
|
VertexBuffer vertexBuffer;
|
|
|
|
|
|
|
|
|
|
public LinesOfSight(GraphicsDevice graphics) {
|
|
|
|
|
vertexBuffer = new VertexBuffer(
|
|
|
|
|
graphics, typeof(VertexPositionColor), numConePoints * 3, BufferUsage.WriteOnly);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(Player player, AABB[] collisionTargets) {
|
2020-02-12 18:39:51 +00:00
|
|
|
|
// TODO: DrawIndexedPrimitives
|
|
|
|
|
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);
|
|
|
|
|
float fovStep = fov / (numConePoints - 1);
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < conePoints.Length; i++) {
|
|
|
|
|
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));
|
|
|
|
|
conePoints[i] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
|
|
|
|
|
}
|
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:11:09 +00:00
|
|
|
|
VertexPositionColor eyeVertex = new VertexPositionColor(
|
|
|
|
|
new Vector3(player.EyePosition, 0), color);
|
2020-02-12 18:39:51 +00:00
|
|
|
|
for (int i = 0; i < numConePoints - 1; i++) {
|
|
|
|
|
vertices[i * 3] = eyeVertex;
|
|
|
|
|
vertices[i * 3 + 1] = conePoints[i];
|
|
|
|
|
vertices[i * 3 + 2] = conePoints[i + 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vertexBuffer.SetData<VertexPositionColor>(vertices);
|
|
|
|
|
|
|
|
|
|
graphics.SetVertexBuffer(vertexBuffer);
|
|
|
|
|
|
|
|
|
|
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
|
|
|
|
|
pass.Apply();
|
|
|
|
|
graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, vertices.Length / 3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|