private void CorrectPosition(Entity entity1, Entity.MoveState moveState) { Vector2 position = entity1.GetPosition(); int coordinateX = (int)position.X; int coordinateY = (int)position.Y; switch (moveState) { case Entity.MoveState.MOVING_RIGHT: position.X = (coordinateX / 32) * 32; break; case Entity.MoveState.MOVING_LEFT: position.X = (coordinateX / 32) * 32 + 32; break; case Entity.MoveState.MOVING_UP: position.Y = (coordinateY / 32) * 32 + 32; break; case Entity.MoveState.MOVING_DOWN: position.Y = (coordinateY / 32) * 32; break; } entity1.SetPosition(position.X, position.Y); }
private Vector2 DetermineNextMove(Entity entity, Entity.MoveState nextMoveState) { Entity.MoveState currentDirection = entity.GetMoveState(); Vector2 position = entity.GetPosition(); int entityPosX = (int)(position.X / 32) * 32; int entityPosY = (int)(position.Y / 32) * 32; Entity targetEntity = null; switch (nextMoveState) { case Entity.MoveState.MOVING_DOWN: entityPosY += 32; if (currentDirection == Entity.MoveState.MOVING_RIGHT) { entityPosX += 32; } break; case Entity.MoveState.MOVING_LEFT: entityPosX -= 32; if (currentDirection == Entity.MoveState.MOVING_DOWN) { entityPosY += 32; } break; case Entity.MoveState.MOVING_RIGHT: entityPosX += 32; if (currentDirection == Entity.MoveState.MOVING_DOWN) { entityPosY += 32; } break; case Entity.MoveState.MOVING_UP: entityPosY -= 32; if (currentDirection == Entity.MoveState.MOVING_RIGHT) { entityPosX += 32; } break; } targetEntity = _entityManager.Get(entityPosX, entityPosY); if (targetEntity != null) { if (targetEntity.GetEntityType() != Entity.EntityType.FLOOR) { targetEntity.SetMoveState(currentDirection); return(DetermineNextMove(targetEntity, currentDirection)); } return(targetEntity.GetPosition()); } return(new Vector2(0, 0)); }
private Entity DetermineCollision(Entity entity, Entity.MoveState nextMoveState) { Vector2 position = entity.GetPosition(); Entity targetEntity = null; switch (nextMoveState) { case Entity.MoveState.MOVING_DOWN: targetEntity = _entityManager.Get(position.X, position.Y + 31); if (targetEntity != null && targetEntity.GetEntityType() != Entity.EntityType.FLOOR) { return(targetEntity); } targetEntity = _entityManager.Get(position.X + 31, position.Y + 31); if (targetEntity != null && targetEntity.GetEntityType() != Entity.EntityType.FLOOR) { return(targetEntity); } break; case Entity.MoveState.MOVING_LEFT: targetEntity = _entityManager.Get(position.X, position.Y); if (targetEntity != null && targetEntity.GetEntityType() != Entity.EntityType.FLOOR) { return(targetEntity); } targetEntity = _entityManager.Get(position.X, position.Y + 31); if (targetEntity != null && targetEntity.GetEntityType() != Entity.EntityType.FLOOR) { return(targetEntity); } break; case Entity.MoveState.MOVING_RIGHT: targetEntity = _entityManager.Get(position.X + 31, position.Y); if (targetEntity != null && targetEntity.GetEntityType() != Entity.EntityType.FLOOR) { return(targetEntity); } targetEntity = _entityManager.Get(position.X + 31, position.Y + 31); if (targetEntity != null && targetEntity.GetEntityType() != Entity.EntityType.FLOOR) { return(targetEntity); } break; case Entity.MoveState.MOVING_UP: targetEntity = _entityManager.Get(position.X, position.Y); if (targetEntity != null && targetEntity.GetEntityType() != Entity.EntityType.FLOOR) { return(targetEntity); } targetEntity = _entityManager.Get(position.X + 31, position.Y); if (targetEntity != null && targetEntity.GetEntityType() != Entity.EntityType.FLOOR) { return(targetEntity); } break; } return(null); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // TODO: Add your update logic here var kstate = Keyboard.GetState(); Entity player = _entityManager.Get(0); player.SetVelocity(400 * (float)gameTime.ElapsedGameTime.TotalSeconds); if (kstate.IsKeyDown(Keys.Escape)) { this.Exit(); } MouseState mouseState = Mouse.GetState(); if (mouseState.LeftButton == ButtonState.Pressed) { if (HasFocus()) { int mouseX = mouseState.X; int mouseY = mouseState.Y; System.Drawing.Rectangle bounds = GetWindowBounds(); if (mouseX >= 0 && mouseX < 1024 && mouseY > 0 && mouseY < 768) { int rowLength = _map.GetLength(0); int columnLength = _map.GetLength(1); int width = rowLength * 32 + OFFSET_X; int height = columnLength * 32 + OFFSET_Y; if (mouseState.X > OFFSET_X && mouseState.X < width && mouseState.Y > OFFSET_Y && mouseState.Y < height) { int row = (mouseState.X - OFFSET_X) / 32; int column = (mouseState.Y - OFFSET_Y) / 32; Entity pickedEntity = _entityManager.Get(mouseState.X, mouseState.Y); if (pickedEntity != null) { pickedEntity.Initialize("wall01", Entity.EntityType.STATIC); _map[row, column] = 1; } else { _entityManager.Get(mouseState.X, mouseState.Y); System.Console.Write("test"); } } } } } Entity.MoveState currentMoveState = player.GetMoveState(); bool directionChanged = false; bool turnAround = false; if (kstate.IsKeyDown(Keys.Up)) { if (currentMoveState == Entity.MoveState.MOVING_DOWN) { turnAround = true; _nextMoveState = Entity.MoveState.MOVING_UP; } else if (currentMoveState != Entity.MoveState.MOVING_UP) { directionChanged = true; _nextMoveState = Entity.MoveState.MOVING_UP; } } if (kstate.IsKeyDown(Keys.Down)) { if (currentMoveState == Entity.MoveState.MOVING_UP) { turnAround = true; _nextMoveState = Entity.MoveState.MOVING_DOWN; } else if (currentMoveState != Entity.MoveState.MOVING_DOWN) { directionChanged = true; _nextMoveState = Entity.MoveState.MOVING_DOWN; } } if (kstate.IsKeyDown(Keys.Left)) { if (currentMoveState == Entity.MoveState.MOVING_RIGHT) { turnAround = true; _nextMoveState = Entity.MoveState.MOVING_LEFT; } else if (currentMoveState != Entity.MoveState.MOVING_LEFT) { directionChanged = true; _nextMoveState = Entity.MoveState.MOVING_LEFT; } } if (kstate.IsKeyDown(Keys.Right)) { if (currentMoveState == Entity.MoveState.MOVING_LEFT) { turnAround = true; _nextMoveState = Entity.MoveState.MOVING_RIGHT; } else if (currentMoveState != Entity.MoveState.MOVING_RIGHT) { directionChanged = true; _nextMoveState = Entity.MoveState.MOVING_RIGHT; } } if (_nextMoveState != Entity.MoveState.IDLE) { // If stopped at a wall, or the player pressed to go in the opposite direction if (player.GetMoveState() == Entity.MoveState.IDLE || turnAround) { player.SetMoveState(_nextMoveState); _targetPosition.X = 0; _targetPosition.Y = 0; } else if (directionChanged) { Vector2 nextFreeSpace = DetermineNextMove(player, _nextMoveState); if (nextFreeSpace.X != 0 && nextFreeSpace.Y != 0) { //Player pressed to go in a direction that is currently blocked _targetPosition = nextFreeSpace; } else { _nextMoveState = Entity.MoveState.IDLE; _targetPosition.X = 0; _targetPosition.Y = 0; } } player.Update(); //Entity has reached target position, reset if (_targetPosition.X != 0 && CheckNextMove(player, _targetPosition)) { _targetPosition.X = 0; _targetPosition.Y = 0; CorrectPosition(player, player.GetMoveState()); player.SetMoveState(_nextMoveState); _nextMoveState = Entity.MoveState.IDLE; } Entity entity = DetermineCollision(player, player.GetMoveState()); if (entity != null) { //Entity has collided with something CorrectPosition(player, player.GetMoveState()); player.SetMoveState(Entity.MoveState.IDLE); } } else if (player.GetMoveState() != Entity.MoveState.IDLE) { player.Update(); Entity entity = DetermineCollision(player, player.GetMoveState()); if (entity != null) { CorrectPosition(player, player.GetMoveState()); player.SetMoveState(Entity.MoveState.IDLE); } } base.Update(gameTime); }