A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

154 lines
5.5 KiB

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
// Disables "GraphicsDevice.DrawIndexedPrimitives(...) is obsolete." warning.
// MonoGame has obsoleted this function, but FNA needs it.
#pragma warning disable CS0618
namespace SemiColinGames {
public sealed class LinesOfSight : IDisposable {
// 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;
readonly bool[] coneEnabled = new bool[MAX_NPCS];
// 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][];
// 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 fill = Color.FromNonPremultiplied(new Vector4(1, 1, 1, 0.1f));
Color outline = Color.FromNonPremultiplied(new Vector4(1, 0, 0, 0.5f));
public LinesOfSight(GraphicsDevice graphics) {
for (int i = 0; i < MAX_NPCS; i++) {
coneFillVertices[i] = new VertexPositionColor[NUM_EDGE_VERTICES + 1];
coneOutlineVertices[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);
}
public void Update(NPC[] npcs, AABB[] collisionTargets) {
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;
float fov = npc.FieldOfView;
float visionRangeSq = visionRange * visionRange;
float fovStep = fov / (NUM_EDGE_VERTICES - 1);
coneFillVertices[index][0] =
new VertexPositionColor(new Vector3(npc.EyePosition, 0), fill);
coneOutlineVertices[index][0] =
new VertexPositionColor(new Vector3(npc.EyePosition, 0), outline);
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;
for (int j = 0; j < collisionTargets.Length; j++) {
AABB box = collisionTargets[j];
if (Math.Abs(box.Position.X - npc.Position.X) > visionRange + box.HalfSize.X) {
continue;
}
Vector2 delta = Vector2.Add(box.HalfSize, 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;
}
}
}
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);
}
}
public void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
// Draw the cones themselves.
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;
}
for (int npcIndex = 0; npcIndex < MAX_NPCS; npcIndex++) {
if (!coneEnabled[npcIndex]) {
continue;
}
vertexBuffer.SetData(coneFillVertices[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, 0, indices.Length / 3);
}
}
// 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;
}
vertexBuffer.SetData(coneOutlineVertices[npcIndex]);
indexBuffer.SetData(indices);
graphics.SetVertexBuffer(vertexBuffer);
graphics.Indices = indexBuffer;
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
pass.Apply();
graphics.DrawIndexedPrimitives(
PrimitiveType.LineStrip, 0, 0, NUM_EDGE_VERTICES + 1, 0, NUM_EDGE_VERTICES + 1);
}
}
}
}
}