attach LinesOfSight to the first NPC
This commit is contained in:
parent
8720896e87
commit
4870964cc5
@ -33,16 +33,18 @@ namespace SemiColinGames {
|
|||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(Player player, AABB[] collisionTargets) {
|
public void Update(NPC[] npcs, AABB[] collisionTargets) {
|
||||||
Vector2 eyePos = player.EyePosition;
|
NPC npc = npcs[0];
|
||||||
float visionRange = player.VisionRange;
|
|
||||||
Vector2 ray = player.VisionRay;
|
Vector2 eyePos = npc.EyePosition;
|
||||||
float fov = player.FieldOfView;
|
float visionRange = npc.VisionRange;
|
||||||
|
Vector2 ray = npc.VisionRay;
|
||||||
|
float fov = npc.FieldOfView;
|
||||||
|
|
||||||
float visionRangeSq = visionRange * visionRange;
|
float visionRangeSq = visionRange * visionRange;
|
||||||
float fovStep = fov / (NUM_EDGE_VERTICES - 1);
|
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++) {
|
for (int i = 0; i < NUM_EDGE_VERTICES; i++) {
|
||||||
float angle = -fov / 2 + fovStep * i;
|
float angle = -fov / 2 + fovStep * i;
|
||||||
Vector2 rotated = ray.Rotate(angle);
|
Vector2 rotated = ray.Rotate(angle);
|
||||||
@ -52,7 +54,7 @@ namespace SemiColinGames {
|
|||||||
Vector2 halfTileSize = new Vector2(World.TileSize / 2.0f, World.TileSize / 2.0f);
|
Vector2 halfTileSize = new Vector2(World.TileSize / 2.0f, World.TileSize / 2.0f);
|
||||||
for (int j = 0; j < collisionTargets.Length; j++) {
|
for (int j = 0; j < collisionTargets.Length; j++) {
|
||||||
AABB box = collisionTargets[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;
|
continue;
|
||||||
}
|
}
|
||||||
Vector2 delta = Vector2.Add(halfTileSize, Vector2.Subtract(box.Position, eyePos));
|
Vector2 delta = Vector2.Add(halfTileSize, Vector2.Subtract(box.Position, eyePos));
|
||||||
|
@ -49,6 +49,7 @@ namespace SemiColinGames {
|
|||||||
private const int spriteWidth = 96;
|
private const int spriteWidth = 96;
|
||||||
private const int spriteHeight = 81;
|
private const int spriteHeight = 81;
|
||||||
private const int spriteCenterYOffset = 2;
|
private const int spriteCenterYOffset = 2;
|
||||||
|
private readonly Vector2 eyeOffset = new Vector2(4, -3);
|
||||||
|
|
||||||
private FSM<NPC> fsm;
|
private FSM<NPC> fsm;
|
||||||
|
|
||||||
@ -64,6 +65,31 @@ namespace SemiColinGames {
|
|||||||
public int Facing;
|
public int Facing;
|
||||||
public Point Position;
|
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) {
|
public void Update(float modelTime, World world) {
|
||||||
fsm.Update(this, modelTime, world);
|
fsm.Update(this, modelTime, world);
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,6 @@ namespace SemiColinGames {
|
|||||||
// centered at that point and extending out by halfSize.X and halfSize.Y.
|
// centered at that point and extending out by halfSize.X and halfSize.Y.
|
||||||
private Point position;
|
private Point position;
|
||||||
private Vector2 halfSize = new Vector2(11, 24);
|
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 int jumps = 0;
|
||||||
private Pose pose = Pose.Jumping;
|
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.
|
// Returns the desired (dx, dy) for the player to move this frame.
|
||||||
Vector2 HandleInput(float modelTime, History<Input> input) {
|
Vector2 HandleInput(float modelTime, History<Input> input) {
|
||||||
Vector2 result = new Vector2() {
|
Vector2 result = new Vector2() {
|
||||||
|
@ -161,7 +161,7 @@ namespace SemiColinGames {
|
|||||||
if (Player.Health <= 0) {
|
if (Player.Health <= 0) {
|
||||||
Reset();
|
Reset();
|
||||||
}
|
}
|
||||||
LinesOfSight.Update(Player, CollisionTargets);
|
LinesOfSight.Update(npcs, CollisionTargets);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draws everything that's behind the player, from back to front.
|
// Draws everything that's behind the player, from back to front.
|
||||||
|
Loading…
Reference in New Issue
Block a user