attach LinesOfSight to the first NPC

This commit is contained in:
Colin McMillen 2020-03-09 12:41:07 -04:00
parent 8720896e87
commit 4870964cc5
4 changed files with 36 additions and 44 deletions

View File

@ -33,16 +33,18 @@ namespace SemiColinGames {
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;
public void Update(NPC[] npcs, AABB[] collisionTargets) {
NPC npc = npcs[0];
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);
coneVertices[0] = new VertexPositionColor(new Vector3(player.EyePosition, 0), color);
coneVertices[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);
@ -52,7 +54,7 @@ namespace SemiColinGames {
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) {
if (Math.Abs(box.Position.X - npc.Position.X) > visionRange + halfTileSize.X) {
continue;
}
Vector2 delta = Vector2.Add(halfTileSize, Vector2.Subtract(box.Position, eyePos));

View File

@ -49,6 +49,7 @@ namespace SemiColinGames {
private const int spriteWidth = 96;
private const int spriteHeight = 81;
private const int spriteCenterYOffset = 2;
private readonly Vector2 eyeOffset = new Vector2(4, -3);
private FSM<NPC> fsm;
@ -64,6 +65,31 @@ namespace SemiColinGames {
public int Facing;
public Point Position;
public Vector2 EyePosition {
get {
return Vector2.Add(
Position.ToVector2(), new Vector2(eyeOffset.X * Facing, eyeOffset.Y));
}
}
public float VisionRange {
get {
return 150;
}
}
public float FieldOfView {
get {
return FMath.DegToRad(120);
}
}
public Vector2 VisionRay {
get {
return new Vector2(VisionRange * Facing, 0);
}
}
public void Update(float modelTime, World world) {
fsm.Update(this, modelTime, world);
}

View File

@ -24,8 +24,6 @@ namespace SemiColinGames {
// centered at that point and extending out by halfSize.X and halfSize.Y.
private Point position;
private Vector2 halfSize = new Vector2(11, 24);
private Vector2 eyeOffsetStanding = new Vector2(7, -14);
private Vector2 eyeOffsetWalking = new Vector2(15, -7);
private int jumps = 0;
private Pose pose = Pose.Jumping;
@ -176,40 +174,6 @@ namespace SemiColinGames {
}
}
public Vector2 EyePosition {
get {
bool walking = pose == Pose.Walking || pose == Pose.Jumping;
Vector2 eyeOffset = walking ? eyeOffsetWalking : eyeOffsetStanding;
return Vector2.Add(
Position.ToVector2(), new Vector2(eyeOffset.X * Facing, eyeOffset.Y));
}
}
public float VisionRange {
get {
return 150;
}
}
public float FieldOfView {
get {
return FMath.DegToRad(120);
}
}
public Vector2 VisionRay {
get {
Vector2 ray = new Vector2(VisionRange * Facing, 0);
if (pose == Pose.Stretching) {
ray = ray.Rotate(Facing * FMath.DegToRad(-30));
}
if (pose == Pose.Crouching) {
ray = ray.Rotate(Facing * FMath.DegToRad(30));
}
return ray;
}
}
// Returns the desired (dx, dy) for the player to move this frame.
Vector2 HandleInput(float modelTime, History<Input> input) {
Vector2 result = new Vector2() {

View File

@ -161,7 +161,7 @@ namespace SemiColinGames {
if (Player.Health <= 0) {
Reset();
}
LinesOfSight.Update(Player, CollisionTargets);
LinesOfSight.Update(npcs, CollisionTargets);
}
// Draws everything that's behind the player, from back to front.