public bool IsHidden(Player p) { List<Obstacle> nearbyObstacles = tileController.GetNearbyObstacles(p.worldRectangle); foreach(Obstacle i in nearbyObstacles) { if(i.canOverlapWith && i.worldRectangle.Contains(p.collisionRectangle)) { return true; } } return false; }
public void CreatePlayer(Vector2 position) { //set thePlayer to a newly created player object thePlayer = new Player(); thePlayer.currentSpeed = 5; thePlayer.baseSpeed = 5; thePlayer.currentThirst = 10; thePlayer.maxThirst = 15; thePlayer.currentHunger = 10; thePlayer.maxHunger = 15; thePlayer.position = position; thePlayer.rectangleBounds = new Point(SPRITE_SIZE, SPRITE_SIZE); thePlayer.sprite = playerTexture; thePlayer.direction = Entities.Direction.Down; }
public void interacts(Player p, Obstacle o) { switch(o.tag) { case ObstacleType.Bush: // set player invisible p.isVisible = false; break; case ObstacleType.Fountain: p.currentThirst = p.maxThirst; // slurp.play(); break; case ObstacleType.Pond: // do nothing break; case ObstacleType.Tree: // do nothing break; } }
public bool UpdateNPC(NPC npc, Player player, List<Obstacle> visibleObstacles, GameTime gameTime) { if (npc.CanSeePlayer(player, visibleObstacles)) return true; switch (npc.tag) { case NPCType.PoliceA: { npc.MoveForward(gameTime, 1.0f); if(collisionController.IllegalMove(npc)) npc.MoveBackward(gameTime, 1.0f); npc.RotateRight(gameTime, 1.0f); } break; case NPCType.PoliceB: npc.stateTime += gameTime.ElapsedGameTime.Milliseconds; double angleToPlayer = Math.Atan2(-1*((double)(player.worldRectangle.Y + (player.worldRectangle.Height / 2)) - (npc.worldRectangle.Y + (npc.worldRectangle.Height / 2))), ((double)(player.worldRectangle.X + (player.worldRectangle.Width / 2)) - (npc.worldRectangle.X + (npc.worldRectangle.Width / 2)))); double facingAngle = Math.Atan2(-1*npc.vision.viewDirection.Y, npc.vision.viewDirection.X); double turnToAngle = facingAngle - angleToPlayer; while (turnToAngle < -1 * MathHelper.Pi) turnToAngle += MathHelper.TwoPi; while (turnToAngle > MathHelper.Pi) turnToAngle -= MathHelper.TwoPi; switch (npc.state) { case NPCState.PatrolRight: npc.MoveForward(gameTime, 1.0f); if (collisionController.IllegalMove(npc)) { npc.MoveBackward(gameTime, 1.0f); npc.state = NPCState.PatrolRightBackwards; npc.stateTime = 0; } if (Math.Abs(turnToAngle) < npc.vision.viewAngle && visibleObstacles.Count == 0 && player.isVisible && npc.vision.maxViewDistance * 1.5 > Math.Abs(((player.worldRectangle.Y + (player.worldRectangle.Height / 2)) - (npc.worldRectangle.Y + (npc.worldRectangle.Height / 2)))) + Math.Abs((player.worldRectangle.X + (player.worldRectangle.Width / 2)) - (npc.worldRectangle.X + (npc.worldRectangle.Width / 2)))) { npc.state = NPCState.Chase; npc.stateTime = 0; } else if (player.isVisible && npc.vision.maxViewDistance / 2.5 > Math.Abs(((player.worldRectangle.Y + (player.worldRectangle.Height / 2)) - (npc.worldRectangle.Y + (npc.worldRectangle.Height / 2)))) + Math.Abs((player.worldRectangle.X + (player.worldRectangle.Width / 2)) - (npc.worldRectangle.X + (npc.worldRectangle.Width / 2)))) { npc.state = NPCState.Investigate; npc.stateTime = 0; } else if (npc.stateTime > 3000) { npc.state = NPCState.PatrolLeft; npc.stateTime = 0; } else npc.RotateRight(gameTime, 1.0f); break; case NPCState.PatrolLeft: npc.MoveForward(gameTime, 1.0f); if (collisionController.IllegalMove(npc)) { npc.MoveBackward(gameTime, 1.0f); npc.state = NPCState.PatrolLeftBackwards; npc.stateTime = 0; } if (Math.Abs(turnToAngle) < npc.vision.viewAngle && visibleObstacles.Count == 0 && player.isVisible && npc.vision.maxViewDistance * 1.5 > Math.Abs(((player.worldRectangle.Y + (player.worldRectangle.Height / 2)) - (npc.worldRectangle.Y + (npc.worldRectangle.Height / 2)))) + Math.Abs((player.worldRectangle.X + (player.worldRectangle.Width / 2)) - (npc.worldRectangle.X + (npc.worldRectangle.Width / 2)))) { npc.state = NPCState.Chase; npc.stateTime = 0; } else if (player.isVisible && npc.vision.maxViewDistance / 2.5 > Math.Abs(((player.worldRectangle.Y + (player.worldRectangle.Height / 2)) - (npc.worldRectangle.Y + (npc.worldRectangle.Height / 2)))) + Math.Abs((player.worldRectangle.X + (player.worldRectangle.Width / 2)) - (npc.worldRectangle.X + (npc.worldRectangle.Width / 2)))) { npc.state = NPCState.Investigate; npc.stateTime = 0; } else if (npc.stateTime > 3000) { npc.state = NPCState.PatrolRight; npc.stateTime = 0; } else npc.RotateLeft(gameTime, 1.0f); break; case NPCState.PatrolLeftBackwards: npc.MoveBackward(gameTime, 1.0f); if (collisionController.IllegalMove(npc)) { npc.MoveForward(gameTime, 1.0f); npc.state = NPCState.PatrolLeft; npc.stateTime = 0; } if (Math.Abs(turnToAngle) < npc.vision.viewAngle && visibleObstacles.Count == 0 && player.isVisible && npc.vision.maxViewDistance * 1.5 > Math.Abs(((player.worldRectangle.Y + (player.worldRectangle.Height / 2)) - (npc.worldRectangle.Y + (npc.worldRectangle.Height / 2)))) + Math.Abs((player.worldRectangle.X + (player.worldRectangle.Width / 2)) - (npc.worldRectangle.X + (npc.worldRectangle.Width / 2)))) { npc.state = NPCState.Chase; npc.stateTime = 0; } else if (player.isVisible && npc.vision.maxViewDistance / 2.5 > Math.Abs(((player.worldRectangle.Y + (player.worldRectangle.Height / 2)) - (npc.worldRectangle.Y + (npc.worldRectangle.Height / 2)))) + Math.Abs((player.worldRectangle.X + (player.worldRectangle.Width / 2)) - (npc.worldRectangle.X + (npc.worldRectangle.Width / 2)))) { npc.state = NPCState.Investigate; npc.stateTime = 0; } else if (npc.stateTime > 250) { npc.state = NPCState.PatrolLeft; npc.stateTime = 0; } else npc.RotateLeft(gameTime, 2.5f); break; case NPCState.PatrolRightBackwards: npc.MoveBackward(gameTime, 1.0f); if (collisionController.IllegalMove(npc)) { npc.MoveForward(gameTime, 1.0f); npc.state = NPCState.PatrolRight; npc.stateTime = 0; } if (Math.Abs(turnToAngle) < npc.vision.viewAngle && visibleObstacles.Count == 0 && player.isVisible && npc.vision.maxViewDistance * 1.5 > Math.Abs(((player.worldRectangle.Y + (player.worldRectangle.Height / 2)) - (npc.worldRectangle.Y + (npc.worldRectangle.Height / 2)))) + Math.Abs((player.worldRectangle.X + (player.worldRectangle.Width / 2)) - (npc.worldRectangle.X + (npc.worldRectangle.Width / 2)))) { npc.state = NPCState.Chase; npc.stateTime = 0; } else if (player.isVisible && npc.vision.maxViewDistance / 2.5 > Math.Abs(((player.worldRectangle.Y + (player.worldRectangle.Height / 2)) - (npc.worldRectangle.Y + (npc.worldRectangle.Height / 2)))) + Math.Abs((player.worldRectangle.X + (player.worldRectangle.Width / 2)) - (npc.worldRectangle.X + (npc.worldRectangle.Width / 2)))) { npc.state = NPCState.Investigate; npc.stateTime = 0; } else if (npc.stateTime > 250) { npc.state = NPCState.PatrolRight; npc.stateTime = 0; } else npc.RotateRight(gameTime, 2.5f); break; case NPCState.Chase: if (!player.isVisible || npc.vision.maxViewDistance*2 < Math.Abs(((player.worldRectangle.Y + (player.worldRectangle.Height / 2)) - (npc.worldRectangle.Y + (npc.worldRectangle.Height / 2)))) + Math.Abs((player.worldRectangle.X + (player.worldRectangle.Width / 2)) - (npc.worldRectangle.X + (npc.worldRectangle.Width / 2)))) { npc.state = NPCState.PatrolLeft; //Randomize npc.stateTime = 0; break; } if (Math.Abs(turnToAngle) > .05) { if (turnToAngle > 0) npc.RotateLeft(gameTime, 2.0f); else if (turnToAngle < 0) npc.RotateRight(gameTime, 2.0f); } npc.MoveForward(gameTime, 2.0f); if (collisionController.IllegalMove(npc)) { npc.MoveBackward(gameTime, 2.0f); npc.MoveRight(gameTime, 2.0f); npc.state = NPCState.PatrolLeft; //Randomize npc.stateTime = 0; //if (turnToAngle > 0) //{ // npc.MoveUp(gameTime, 2.0f); // if (collisionController.IllegalMove(npc)) // { // npc.MoveDown(gameTime, 2.0f); // if (Math.Abs(turnToAngle) < MathHelper.PiOver2) // { // npc.MoveRight(gameTime, 2.0f); // if (collisionController.IllegalMove(npc)) // { // npc.MoveLeft(gameTime, 2.0f); // npc.state = NPCState.PatrolLeft; //Randomize // npc.stateTime = 0; // } // } // } //} //else if (turnToAngle < 0) //{ // npc.MoveDown(gameTime, 2.0f); // if (collisionController.IllegalMove(npc)) // { // npc.MoveUp(gameTime, 2.0f); // if (Math.Abs(turnToAngle) < MathHelper.PiOver2) // { // npc.MoveLeft(gameTime, 2.0f); // if (collisionController.IllegalMove(npc)) // { // npc.MoveRight(gameTime, 2.0f); // npc.state = NPCState.PatrolLeft; //Randomize // npc.stateTime = 0; // } // } // } //} } break; case NPCState.Investigate: if (!player.isVisible || npc.vision.maxViewDistance / 2 < Math.Abs(((player.worldRectangle.Y + (player.worldRectangle.Height / 2)) - (npc.worldRectangle.Y + (npc.worldRectangle.Height / 2)))) + Math.Abs((player.worldRectangle.X + (player.worldRectangle.Width / 2)) - (npc.worldRectangle.X + (npc.worldRectangle.Width / 2)))) { npc.state = NPCState.PatrolLeft; //Randomize npc.stateTime = 0; } else if (Math.Abs(turnToAngle) < npc.vision.viewAngle / 2 && visibleObstacles.Count == 0 && player.isVisible) { npc.state = NPCState.Chase; npc.stateTime = 0; } else if (turnToAngle > 0) npc.RotateLeft(gameTime, 3.0f); else if (turnToAngle < 0) npc.RotateRight(gameTime, 3.0f); break; } break; } return false; }
public bool Update(Player player, List<Obstacle> obstacles, GameTime gameTime) { foreach (NPC npc in this.npcs) { if (npc.OnScreen() || npc.VisionOnScreen()) { npc.UpdateSpriteData(gameTime); List<Obstacle> visibleObstacles = new List<Obstacle>(); foreach (Obstacle obs in obstacles) { if (!obs.canSeeThrough && npc.CanSee(obs.worldRectangle)) visibleObstacles.Add(obs); } //visibleObstacles = GetVisibleObstacles(npc, obstacles); foreach (Vision vision in npc.visions) { vision.Update(visibleObstacles); } if (UpdateNPC(npc, player, visibleObstacles, gameTime)) return true; } } return false; }
public override void Initialize() { Type = "TitleScreen"; oldState = Keyboard.GetState(); player = new Player(); police1 = new NPC(); police2 = new NPC(); player.position = new Vector2(200, HideOutGame.SCREEN_HEIGHT - 75); police1.position = new Vector2(50, HideOutGame.SCREEN_HEIGHT - 75); police2.position = new Vector2(100, HideOutGame.SCREEN_HEIGHT - 75); police1.rectangleBounds = new Point(NPCController.SPRITE_SIZE, NPCController.SPRITE_SIZE); police2.rectangleBounds = new Point(NPCController.SPRITE_SIZE, NPCController.SPRITE_SIZE); player.rectangleBounds = new Point(PlayerController.SPRITE_SIZE, PlayerController.SPRITE_SIZE); }
public bool IntersectsWithPlayer(Player player, List<Obstacle> obstacles) { double theta = GetTheta(); Vector2 p1 = new Vector2(parentLocation.X + parentLocation.Width / 2, parentLocation.Y + parentLocation.Height / 2); Vector2 p2 = new Vector2((float)((viewDistance - 10) * Math.Cos(theta + viewAngle)) + p1.X, (float)((viewDistance-10) * Math.Sin(theta + viewAngle)) + p1.Y); Rectangle rec = player.collisionRectangle; Vector2 v1 = new Vector2(rec.X, rec.Y); Vector2 v2 = new Vector2(rec.X + rec.Width, rec.Y); Vector2 v3 = new Vector2(rec.X + rec.Width, rec.Y + rec.Height); Vector2 v4 = new Vector2(rec.X, rec.Y + rec.Height); Vector2 top = Intersects(p1, p2, v1, v2); Vector2 right = Intersects(p1, p2, v2, v3); Vector2 bottom = Intersects(p1, p2, v3, v4); Vector2 left = Intersects(p1, p2, v4, v1); if (top.X >= 0 || right.X >= 0 || bottom.X >= 0 || left.X >= 0) { return true; } return false; }
public bool CanSeePlayer(Player player, List<Obstacle> visibleObstacles) { if (!player.isVisible || !CanSee(player.collisionRectangle)) return false; VertexPositionColor[] viewTriangle = GetFieldOfViewTriangle(); double theta = GetTheta(); double minFOVAngle = theta - viewAngle; double maxFOVAngle = theta + viewAngle; while (minFOVAngle < 0) minFOVAngle += MathHelper.TwoPi; while (minFOVAngle > MathHelper.TwoPi) minFOVAngle -= MathHelper.TwoPi; while (maxFOVAngle < 0) maxFOVAngle += MathHelper.TwoPi; while (maxFOVAngle > MathHelper.TwoPi) maxFOVAngle -= MathHelper.TwoPi; double topLeftAngle = Math.Tan((player.collisionRectangle.Y - viewTriangle[0].Position.Y) / (player.collisionRectangle.X - viewTriangle[0].Position.X)); double topRightAngle = Math.Tan((player.collisionRectangle.Y - viewTriangle[0].Position.Y) / ((player.collisionRectangle.X + player.collisionRectangle.Width) - viewTriangle[0].Position.X)); double bottomLeftAngle = Math.Tan(((player.collisionRectangle.Y + player.collisionRectangle.Height) - viewTriangle[0].Position.Y) / (player.collisionRectangle.X - viewTriangle[0].Position.X)); double bottomRightAngle = Math.Tan(((player.collisionRectangle.Y + player.collisionRectangle.Height) - viewTriangle[0].Position.Y) / ((player.collisionRectangle.X + player.collisionRectangle.Width) - viewTriangle[0].Position.X)); double minPlayerAngle = Math.Min(Math.Min(topLeftAngle, topRightAngle), Math.Min(bottomLeftAngle, bottomRightAngle)); double maxPlayerAngle = Math.Max(Math.Max(topLeftAngle, topRightAngle), Math.Max(bottomLeftAngle, bottomRightAngle)); if (minPlayerAngle < MathHelper.PiOver4 && maxPlayerAngle > MathHelper.TwoPi - MathHelper.PiOver4) { if (topLeftAngle > MathHelper.TwoPi - MathHelper.PiOver4) topLeftAngle -= MathHelper.TwoPi; if (topRightAngle > MathHelper.TwoPi - MathHelper.PiOver4) topRightAngle -= MathHelper.TwoPi; if (bottomLeftAngle > MathHelper.TwoPi - MathHelper.PiOver4) bottomLeftAngle -= MathHelper.TwoPi; if (bottomRightAngle > MathHelper.TwoPi - MathHelper.PiOver4) bottomRightAngle -= MathHelper.TwoPi; minPlayerAngle = Math.Min(Math.Min(topLeftAngle, topRightAngle), Math.Min(bottomLeftAngle, bottomRightAngle)); maxPlayerAngle = Math.Max(Math.Max(topLeftAngle, topRightAngle), Math.Max(bottomLeftAngle, bottomRightAngle)); } while (minPlayerAngle < 0) minPlayerAngle += MathHelper.TwoPi; while (minPlayerAngle > MathHelper.TwoPi) minPlayerAngle -= MathHelper.TwoPi; while (maxPlayerAngle < 0) maxPlayerAngle += MathHelper.TwoPi; while (maxPlayerAngle > MathHelper.TwoPi) maxPlayerAngle -= MathHelper.TwoPi; if (minPlayerAngle < minFOVAngle) minPlayerAngle = minFOVAngle; if (maxPlayerAngle > maxFOVAngle) maxPlayerAngle = maxFOVAngle; foreach (Obstacle obs in visibleObstacles) { topLeftAngle = Math.Tan((obs.worldRectangle.Y - viewTriangle[0].Position.Y) / (obs.worldRectangle.X - viewTriangle[0].Position.X)); topRightAngle = Math.Tan((obs.worldRectangle.Y - viewTriangle[0].Position.Y) / ((obs.worldRectangle.X + obs.worldRectangle.Width) - viewTriangle[0].Position.X)); bottomLeftAngle = Math.Tan(((obs.worldRectangle.Y + obs.worldRectangle.Height) - viewTriangle[0].Position.Y) / (obs.worldRectangle.X - viewTriangle[0].Position.X)); bottomRightAngle = Math.Tan(((obs.worldRectangle.Y + obs.worldRectangle.Height) - viewTriangle[0].Position.Y) / ((obs.worldRectangle.X + obs.worldRectangle.Width) - viewTriangle[0].Position.X)); double minObstacleAngle = Math.Min(Math.Min(topLeftAngle, topRightAngle), Math.Min(bottomLeftAngle, bottomRightAngle)); double maxObstacleAngle = Math.Max(Math.Max(topLeftAngle, topRightAngle), Math.Max(bottomLeftAngle, bottomRightAngle)); if (minObstacleAngle < MathHelper.PiOver4 && maxObstacleAngle > MathHelper.TwoPi - MathHelper.PiOver4) { if (topLeftAngle > MathHelper.TwoPi - MathHelper.PiOver4) topLeftAngle -= MathHelper.TwoPi; if (topRightAngle > MathHelper.TwoPi - MathHelper.PiOver4) topRightAngle -= MathHelper.TwoPi; if (bottomLeftAngle > MathHelper.TwoPi - MathHelper.PiOver4) bottomLeftAngle -= MathHelper.TwoPi; if (bottomRightAngle > MathHelper.TwoPi - MathHelper.PiOver4) bottomRightAngle -= MathHelper.TwoPi; minObstacleAngle = Math.Min(Math.Min(topLeftAngle, topRightAngle), Math.Min(bottomLeftAngle, bottomRightAngle)); maxObstacleAngle = Math.Max(Math.Max(topLeftAngle, topRightAngle), Math.Max(bottomLeftAngle, bottomRightAngle)); } while (minObstacleAngle < 0) minObstacleAngle += MathHelper.TwoPi; while (minObstacleAngle > MathHelper.TwoPi) minObstacleAngle -= MathHelper.TwoPi; while (maxObstacleAngle < 0) maxObstacleAngle += MathHelper.TwoPi; while (maxObstacleAngle > MathHelper.TwoPi) maxObstacleAngle -= MathHelper.TwoPi; if (minObstacleAngle < minFOVAngle) minObstacleAngle = minFOVAngle; if (maxObstacleAngle > maxFOVAngle) maxObstacleAngle = maxFOVAngle; //Check mindistance between player and obstacle; onward to false condition if obstacle is closer if (minObstacleAngle <= minPlayerAngle && maxObstacleAngle >= maxPlayerAngle) { double topLeftPlayerDistance = Distance(new Vector2(player.collisionRectangle.X, player.collisionRectangle.Y), new Vector2(viewTriangle[0].Position.X, viewTriangle[0].Position.Y)); double topRightPlayerDistance = Distance(new Vector2(player.collisionRectangle.X + player.collisionRectangle.Width, player.collisionRectangle.Y), new Vector2(viewTriangle[0].Position.X, viewTriangle[0].Position.Y)); double bottomLeftPlayerDistance = Distance(new Vector2(player.collisionRectangle.X, player.collisionRectangle.Y + player.collisionRectangle.Height), new Vector2(viewTriangle[0].Position.X, viewTriangle[0].Position.Y)); double bottomRightPlayerDistance = Distance(new Vector2(player.collisionRectangle.X + player.collisionRectangle.Width, player.collisionRectangle.Y + player.collisionRectangle.Height), new Vector2(viewTriangle[0].Position.X, viewTriangle[0].Position.Y)); double minPlayerDistance = Math.Min(Math.Min(topLeftPlayerDistance, topRightPlayerDistance), Math.Min(bottomLeftPlayerDistance, bottomRightPlayerDistance)); double topLeftObstacleDistance = Distance(new Vector2(obs.worldRectangle.X, obs.worldRectangle.Y), new Vector2(viewTriangle[0].Position.X, viewTriangle[0].Position.Y)); double topRightObstacleDistance = Distance(new Vector2(obs.worldRectangle.X + obs.worldRectangle.Width, obs.worldRectangle.Y), new Vector2(viewTriangle[0].Position.X, viewTriangle[0].Position.Y)); double bottomLeftObstacleDistance = Distance(new Vector2(obs.worldRectangle.X, obs.worldRectangle.Y + obs.worldRectangle.Height), new Vector2(viewTriangle[0].Position.X, viewTriangle[0].Position.Y)); double bottomRightObstacleDistance = Distance(new Vector2(obs.worldRectangle.X + obs.worldRectangle.Width, obs.worldRectangle.Y + obs.worldRectangle.Height), new Vector2(viewTriangle[0].Position.X, viewTriangle[0].Position.Y)); double minObstacleDistance = Math.Min(Math.Min(topLeftObstacleDistance, topRightObstacleDistance), Math.Min(bottomLeftObstacleDistance, bottomRightObstacleDistance)); if (minPlayerDistance > minObstacleDistance) return false; } } return true; }
public Obstacle NearFountain(Player p) { List<Obstacle> nearbyObstacles = tileController.GetNearbyObstacles(p.worldRectangle); foreach (Obstacle i in nearbyObstacles) { if(i.tag == ObstacleType.Fountain && i.worldRectangle.Intersects(p.worldRectangle)) { return i; } } return null; }
public bool CanSeePlayer(Player player, List<Obstacle> visibleObstacles) { if (!player.isVisible) return false; foreach(Vision v in this.visions) { if (v.IntersectsWithPlayer(player, visibleObstacles)) return true; } return false; }