diff --git a/Shared/LinesOfSight.cs b/Shared/LinesOfSight.cs index 3f5131f..3227514 100644 --- a/Shared/LinesOfSight.cs +++ b/Shared/LinesOfSight.cs @@ -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)); diff --git a/Shared/NPC.cs b/Shared/NPC.cs index 15a43c1..216eb46 100644 --- a/Shared/NPC.cs +++ b/Shared/NPC.cs @@ -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 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); } diff --git a/Shared/Player.cs b/Shared/Player.cs index c05e805..007f661 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -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) { Vector2 result = new Vector2() { diff --git a/Shared/World.cs b/Shared/World.cs index e73e9ea..8a5723b 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -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.