2020-02-12 18:39:51 +00:00
|
|
|
|
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-02-12 20:11:09 +00:00
|
|
|
|
|
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;
|
2020-02-19 21:36:38 +00:00
|
|
|
|
|
|
|
|
|
readonly VertexBuffer vertexBuffer;
|
|
|
|
|
readonly IndexBuffer indexBuffer;
|
2020-03-09 18:47:21 +00:00
|
|
|
|
readonly bool[] coneEnabled = new bool[MAX_NPCS];
|
2020-03-18 19:56:33 +00:00
|
|
|
|
// coneFillVertices[i][0] is the eye position; the rest are the edge vertices.
|
|
|
|
|
readonly VertexPositionColor[][] coneFillVertices = new VertexPositionColor[MAX_NPCS][];
|
|
|
|
|
readonly VertexPositionColor[][] coneOutlineVertices = new VertexPositionColor[MAX_NPCS][];
|
2020-02-12 20:54:28 +00:00
|
|
|
|
// The number of total triangles drawn is one less than the number of edge points.
|
2020-02-26 01:34:00 +00:00
|
|
|
|
readonly int[] indices = new int[(NUM_EDGE_VERTICES - 1) * 3];
|
2020-02-19 21:36:38 +00:00
|
|
|
|
|
2020-03-18 19:56:33 +00:00
|
|
|
|
Color fill = Color.FromNonPremultiplied(new Vector4(1, 1, 1, 0.1f));
|
|
|
|
|
Color outline = Color.FromNonPremultiplied(new Vector4(1, 0, 0, 0.5f));
|
2020-02-12 20:11:09 +00:00
|
|
|
|
|
|
|
|
|
public LinesOfSight(GraphicsDevice graphics) {
|
2020-03-09 18:47:21 +00:00
|
|
|
|
for (int i = 0; i < MAX_NPCS; i++) {
|
2020-03-18 19:56:33 +00:00
|
|
|
|
coneFillVertices[i] = new VertexPositionColor[NUM_EDGE_VERTICES + 1];
|
|
|
|
|
coneOutlineVertices[i] = new VertexPositionColor[NUM_EDGE_VERTICES + 1];
|
2020-03-09 18:47:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 20:11:09 +00:00
|
|
|
|
vertexBuffer = new VertexBuffer(
|
2020-02-26 01:34:00 +00:00
|
|
|
|
graphics, typeof(VertexPositionColor), NUM_EDGE_VERTICES * 3, BufferUsage.WriteOnly);
|
2020-02-12 20:54:28 +00:00
|
|
|
|
indexBuffer = new IndexBuffer(
|
|
|
|
|
graphics, typeof(int), indices.Length, BufferUsage.WriteOnly);
|
2020-02-12 20:11:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 21:30:34 +00:00
|
|
|
|
~LinesOfSight() {
|
|
|
|
|
Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose() {
|
|
|
|
|
vertexBuffer.Dispose();
|
|
|
|
|
indexBuffer.Dispose();
|
2020-02-19 22:18:00 +00:00
|
|
|
|
GC.SuppressFinalize(this);
|
2020-02-19 21:30:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
2020-02-12 20:11:09 +00:00
|
|
|
|
|
2020-02-12 18:39:51 +00:00
|
|
|
|
float visionRangeSq = visionRange * visionRange;
|
2020-02-26 01:34:00 +00:00
|
|
|
|
float fovStep = fov / (NUM_EDGE_VERTICES - 1);
|
2020-02-12 18:39:51 +00:00
|
|
|
|
|
2020-03-18 19:56:33 +00:00
|
|
|
|
coneFillVertices[index][0] =
|
|
|
|
|
new VertexPositionColor(new Vector3(npc.EyePosition, 0), fill);
|
|
|
|
|
coneOutlineVertices[index][0] =
|
|
|
|
|
new VertexPositionColor(new Vector3(npc.EyePosition, 0), outline);
|
2020-02-26 01:34:00 +00:00
|
|
|
|
for (int i = 0; i < NUM_EDGE_VERTICES; 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;
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < collisionTargets.Length; j++) {
|
|
|
|
|
AABB box = collisionTargets[j];
|
2020-03-11 15:01:34 +00:00
|
|
|
|
if (Math.Abs(box.Position.X - npc.Position.X) > visionRange + box.HalfSize.X) {
|
2020-02-12 18:39:51 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-03-11 15:01:34 +00:00
|
|
|
|
Vector2 delta = Vector2.Add(box.HalfSize, Vector2.Subtract(box.Position, eyePos));
|
2020-02-12 18:39:51 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 19:56:33 +00:00
|
|
|
|
coneFillVertices[index][i + 1] = new VertexPositionColor(
|
|
|
|
|
new Vector3((int) closestHit.X, (int) closestHit.Y, 0), fill);
|
|
|
|
|
coneOutlineVertices[index][i + 1] = new VertexPositionColor(
|
|
|
|
|
new Vector3((int) closestHit.X, (int) closestHit.Y, 0), outline);
|
2020-02-12 18:39:51 +00:00
|
|
|
|
}
|
2020-02-12 20:11:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 21:36:38 +00:00
|
|
|
|
public void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
|
2020-03-09 20:13:52 +00:00
|
|
|
|
// Draw the cones themselves.
|
2020-02-26 01:34:00 +00:00
|
|
|
|
for (int i = 0; i < NUM_EDGE_VERTICES - 1; i++) {
|
2020-02-12 20:54:28 +00:00
|
|
|
|
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-03-09 18:47:21 +00:00
|
|
|
|
for (int npcIndex = 0; npcIndex < MAX_NPCS; npcIndex++) {
|
|
|
|
|
if (!coneEnabled[npcIndex]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-03-18 19:56:33 +00:00
|
|
|
|
vertexBuffer.SetData(coneFillVertices[npcIndex]);
|
2020-03-09 18:47:21 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2020-02-12 18:39:51 +00:00
|
|
|
|
}
|
2020-03-09 20:13:52 +00:00
|
|
|
|
|
|
|
|
|
// Draw the outlines of the cones.
|
|
|
|
|
for (int i = 0; i <= NUM_EDGE_VERTICES; i++) {
|
|
|
|
|
indices[i] = i;
|
|
|
|
|
}
|
|
|
|
|
indices[NUM_EDGE_VERTICES + 1] = 0;
|
|
|
|
|
|
|
|
|
|
for (int npcIndex = 0; npcIndex < MAX_NPCS; npcIndex++) {
|
|
|
|
|
if (!coneEnabled[npcIndex]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-03-18 19:56:33 +00:00
|
|
|
|
vertexBuffer.SetData(coneOutlineVertices[npcIndex]);
|
2020-03-09 20:13:52 +00:00
|
|
|
|
indexBuffer.SetData(indices);
|
|
|
|
|
graphics.SetVertexBuffer(vertexBuffer);
|
|
|
|
|
graphics.Indices = indexBuffer;
|
|
|
|
|
|
|
|
|
|
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
|
|
|
|
|
pass.Apply();
|
2020-03-18 19:56:33 +00:00
|
|
|
|
graphics.DrawIndexedPrimitives(
|
|
|
|
|
PrimitiveType.LineStrip, 0, 0, NUM_EDGE_VERTICES + 1);
|
2020-03-09 20:13:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-12 18:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|