sneak/Shared/LinesOfSight.cs

121 lines
4.1 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace SemiColinGames {
2020-03-09 16:19:19 +00:00
public sealed class LinesOfSight : IDisposable {
2020-03-09 18:47:21 +00:00
// Max number of NPCs whose vision cones will be shown at once.
const int MAX_NPCS = 4;
// Number of edge vertices per vision cone.
const int NUM_EDGE_VERTICES = 30;
readonly VertexBuffer vertexBuffer;
readonly IndexBuffer indexBuffer;
2020-03-09 18:47:21 +00:00
readonly bool[] coneEnabled = new bool[MAX_NPCS];
// coneVertices[i][0] is the eye position; the rest are the edge vertices.
readonly VertexPositionColor[][] coneVertices = new VertexPositionColor[MAX_NPCS][];
// The number of total triangles drawn is one less than the number of edge points.
readonly int[] indices = new int[(NUM_EDGE_VERTICES - 1) * 3];
Color color = Color.FromNonPremultiplied(new Vector4(0, 0, 1, 0.6f));
public LinesOfSight(GraphicsDevice graphics) {
2020-03-09 18:47:21 +00:00
for (int i = 0; i < MAX_NPCS; i++) {
coneVertices[i] = new VertexPositionColor[NUM_EDGE_VERTICES + 1];
}
vertexBuffer = new VertexBuffer(
graphics, typeof(VertexPositionColor), NUM_EDGE_VERTICES * 3, BufferUsage.WriteOnly);
indexBuffer = new IndexBuffer(
graphics, typeof(int), indices.Length, BufferUsage.WriteOnly);
}
~LinesOfSight() {
Dispose();
}
public void Dispose() {
vertexBuffer.Dispose();
indexBuffer.Dispose();
GC.SuppressFinalize(this);
}
2020-03-09 16:41:07 +00:00
public void Update(NPC[] npcs, AABB[] collisionTargets) {
2020-03-09 18:47:21 +00:00
for (int i = 0; i < MAX_NPCS; i++) {
coneEnabled[i] = false;
}
for (int i = 0; i < MAX_NPCS; i++) {
UpdateNpc(i, npcs[i], collisionTargets);
}
}
2020-03-09 16:41:07 +00:00
2020-03-09 18:47:21 +00:00
private void UpdateNpc(int index, NPC npc, AABB[] collisionTargets) {
coneEnabled[index] = true;
2020-03-09 16:41:07 +00:00
Vector2 eyePos = npc.EyePosition;
float visionRange = npc.VisionRange;
Vector2 ray = npc.VisionRay;
float fov = npc.FieldOfView;
float visionRangeSq = visionRange * visionRange;
float fovStep = fov / (NUM_EDGE_VERTICES - 1);
2020-03-09 18:47:21 +00:00
coneVertices[index][0] = new VertexPositionColor(new Vector3(npc.EyePosition, 0), color);
for (int i = 0; i < NUM_EDGE_VERTICES; 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];
2020-03-09 16:41:07 +00:00
if (Math.Abs(box.Position.X - npc.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-03-09 18:47:21 +00:00
coneVertices[index][i + 1] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
}
}
public void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
for (int i = 0; i < NUM_EDGE_VERTICES - 1; i++) {
indices[i * 3] = 0;
indices[i * 3 + 1] = i + 1;
indices[i * 3 + 2] = i + 2;
}
2020-03-09 18:47:21 +00:00
for (int npcIndex = 0; npcIndex < MAX_NPCS; npcIndex++) {
if (!coneEnabled[npcIndex]) {
continue;
}
vertexBuffer.SetData(coneVertices[npcIndex]);
indexBuffer.SetData(indices);
graphics.SetVertexBuffer(vertexBuffer);
graphics.Indices = indexBuffer;
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
pass.Apply();
graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3);
}
}
}
}
}