LinesOfSight: support multiple NPCs
This commit is contained in:
parent
473256d105
commit
9504c2236c
@ -5,18 +5,27 @@ using System;
|
||||
namespace SemiColinGames {
|
||||
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 IndexBuffer indexBuffer;
|
||||
// coneVertices[0] is the eye position; the rest are the edge vertices.
|
||||
readonly VertexPositionColor[] coneVertices = new VertexPositionColor[NUM_EDGE_VERTICES + 1];
|
||||
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) {
|
||||
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(
|
||||
@ -34,8 +43,16 @@ namespace SemiColinGames {
|
||||
}
|
||||
|
||||
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;
|
||||
float visionRange = npc.VisionRange;
|
||||
Vector2 ray = npc.VisionRay;
|
||||
@ -44,7 +61,7 @@ namespace SemiColinGames {
|
||||
float visionRangeSq = visionRange * visionRange;
|
||||
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++) {
|
||||
float angle = -fov / 2 + fovStep * i;
|
||||
Vector2 rotated = ray.Rotate(angle);
|
||||
@ -73,26 +90,30 @@ namespace SemiColinGames {
|
||||
|
||||
float tint = 0.6f - hitTime / 2;
|
||||
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) {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
vertexBuffer.SetData(coneVertices);
|
||||
indexBuffer.SetData(indices);
|
||||
graphics.SetVertexBuffer(vertexBuffer);
|
||||
graphics.Indices = indexBuffer;
|
||||
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);
|
||||
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
|
||||
pass.Apply();
|
||||
graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user