LinesOfSight: support multiple NPCs

This commit is contained in:
Colin McMillen 2020-03-09 14:47:21 -04:00
parent 473256d105
commit 9504c2236c

View File

@ -5,18 +5,27 @@ using System;
namespace SemiColinGames { namespace SemiColinGames {
public sealed class LinesOfSight : IDisposable { public sealed class LinesOfSight : IDisposable {
const int NUM_EDGE_VERTICES = 60; // 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 VertexBuffer vertexBuffer;
readonly IndexBuffer indexBuffer; readonly IndexBuffer indexBuffer;
// coneVertices[0] is the eye position; the rest are the edge vertices. readonly bool[] coneEnabled = new bool[MAX_NPCS];
readonly VertexPositionColor[] coneVertices = new VertexPositionColor[NUM_EDGE_VERTICES + 1]; // 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. // 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]; readonly int[] indices = new int[(NUM_EDGE_VERTICES - 1) * 3];
Color color = Color.FromNonPremultiplied(new Vector4(0, 0, 1, 0.6f)); Color color = Color.FromNonPremultiplied(new Vector4(0, 0, 1, 0.6f));
public LinesOfSight(GraphicsDevice graphics) { public LinesOfSight(GraphicsDevice graphics) {
for (int i = 0; i < MAX_NPCS; i++) {
coneVertices[i] = new VertexPositionColor[NUM_EDGE_VERTICES + 1];
}
vertexBuffer = new VertexBuffer( vertexBuffer = new VertexBuffer(
graphics, typeof(VertexPositionColor), NUM_EDGE_VERTICES * 3, BufferUsage.WriteOnly); graphics, typeof(VertexPositionColor), NUM_EDGE_VERTICES * 3, BufferUsage.WriteOnly);
indexBuffer = new IndexBuffer( indexBuffer = new IndexBuffer(
@ -34,8 +43,16 @@ namespace SemiColinGames {
} }
public void Update(NPC[] npcs, AABB[] collisionTargets) { public void Update(NPC[] npcs, AABB[] collisionTargets) {
NPC npc = npcs[0]; for (int i = 0; i < MAX_NPCS; i++) {
coneEnabled[i] = false;
}
for (int i = 0; i < MAX_NPCS; i++) {
UpdateNpc(i, npcs[i], collisionTargets);
}
}
private void UpdateNpc(int index, NPC npc, AABB[] collisionTargets) {
coneEnabled[index] = true;
Vector2 eyePos = npc.EyePosition; Vector2 eyePos = npc.EyePosition;
float visionRange = npc.VisionRange; float visionRange = npc.VisionRange;
Vector2 ray = npc.VisionRay; Vector2 ray = npc.VisionRay;
@ -44,7 +61,7 @@ namespace SemiColinGames {
float visionRangeSq = visionRange * visionRange; float visionRangeSq = visionRange * visionRange;
float fovStep = fov / (NUM_EDGE_VERTICES - 1); float fovStep = fov / (NUM_EDGE_VERTICES - 1);
coneVertices[0] = new VertexPositionColor(new Vector3(npc.EyePosition, 0), color); coneVertices[index][0] = new VertexPositionColor(new Vector3(npc.EyePosition, 0), color);
for (int i = 0; i < NUM_EDGE_VERTICES; i++) { for (int i = 0; i < NUM_EDGE_VERTICES; i++) {
float angle = -fov / 2 + fovStep * i; float angle = -fov / 2 + fovStep * i;
Vector2 rotated = ray.Rotate(angle); Vector2 rotated = ray.Rotate(angle);
@ -73,26 +90,30 @@ namespace SemiColinGames {
float tint = 0.6f - hitTime / 2; float tint = 0.6f - hitTime / 2;
Color tinted = Color.FromNonPremultiplied(new Vector4(0, 0, 1, tint)); Color tinted = Color.FromNonPremultiplied(new Vector4(0, 0, 1, tint));
coneVertices[i + 1] = new VertexPositionColor(new Vector3(closestHit, 0), tinted); coneVertices[index][i + 1] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
} }
} }
public void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) { public void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
for (int i = 0; i < NUM_EDGE_VERTICES - 1; i++) { for (int i = 0; i < NUM_EDGE_VERTICES - 1; i++) {
indices[i * 3] = 0; indices[i * 3] = 0;
indices[i * 3 + 1] = i + 1; indices[i * 3 + 1] = i + 1;
indices[i * 3 + 2] = i + 2; indices[i * 3 + 2] = i + 2;
} }
vertexBuffer.SetData(coneVertices); for (int npcIndex = 0; npcIndex < MAX_NPCS; npcIndex++) {
indexBuffer.SetData(indices); if (!coneEnabled[npcIndex]) {
graphics.SetVertexBuffer(vertexBuffer); continue;
graphics.Indices = indexBuffer; }
vertexBuffer.SetData(coneVertices[npcIndex]);
indexBuffer.SetData(indices);
graphics.SetVertexBuffer(vertexBuffer);
graphics.Indices = indexBuffer;
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) { foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
pass.Apply(); pass.Apply();
graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3); graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3);
}
} }
} }
} }