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.
 
 
 

97 lines
3.4 KiB

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace SemiColinGames {
class LinesOfSight : IDisposable {
const int numEdgeVertices = 60;
readonly VertexBuffer vertexBuffer;
readonly IndexBuffer indexBuffer;
// coneVertices[0] is the eye position; the rest are the edge vertices.
readonly VertexPositionColor[] coneVertices = new VertexPositionColor[numEdgeVertices + 1];
// The number of total triangles drawn is one less than the number of edge points.
readonly int[] indices = new int[(numEdgeVertices - 1) * 3];
Color color = Color.FromNonPremultiplied(new Vector4(0, 0, 1, 0.6f));
public LinesOfSight(GraphicsDevice graphics) {
vertexBuffer = new VertexBuffer(
graphics, typeof(VertexPositionColor), numEdgeVertices * 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(Player player, AABB[] collisionTargets) {
Vector2 eyePos = player.EyePosition;
float visionRange = player.VisionRange;
Vector2 ray = player.VisionRay;
float fov = player.FieldOfView;
float visionRangeSq = visionRange * visionRange;
float fovStep = fov / (numEdgeVertices - 1);
coneVertices[0] = new VertexPositionColor(new Vector3(player.EyePosition, 0), color);
for (int i = 0; i < numEdgeVertices; 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];
if (Math.Abs(box.Position.X - player.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));
coneVertices[i + 1] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
}
}
public void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
for (int i = 0; i < numEdgeVertices - 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;
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
pass.Apply();
graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3);
}
}
}
}