/// <summary> /// Causes the player to interact with the closest object within reach. /// </summary> /// <param name="currentRoom"></param> public void action(RoomLevelBase currentRoom) { RoomObjectBase obj = findClosestObject(currentRoom); //TODO: Finish player-object interaction //if(obj is RoomObjectButton) //((RoomObjectButton)obj). }
public void Update(KeyboardState keyboard, RoomLevelBase currentRoom) { //Save last position lastXPos = xPos; lastYPos = yPos; //Player movement / action if (keyboard.IsKeyDown(Config.keyLeft)) xPos -= Config.playerMoveSpeed; if (keyboard.IsKeyDown(Config.keyRight)) xPos += Config.playerMoveSpeed; if (keyboard.IsKeyDown(Config.keyUp)) yPos -= Config.playerMoveSpeed; if (keyboard.IsKeyDown(Config.keyDown)) yPos += Config.playerMoveSpeed; if (keyboard.IsKeyDown(Config.keyAction)) { if (!wasDoingActionLast) { currentRoom.doPlayerAction = true; wasDoingActionLast = true; } } else wasDoingActionLast = false; //Determines the texture of the player depending on movement if (xPos != lastXPos || yPos != lastYPos) { //If player has moved if (xPos < lastXPos) spriteDir = MoveDir.LEFT; else if (xPos > lastXPos) spriteDir = MoveDir.RIGHT; if (yPos < lastYPos) spriteDir = MoveDir.UP; else if (yPos > lastYPos) spriteDir = MoveDir.DOWN; sprite.setMoveDir(spriteDir); } else { //If player has not moved sprite.setMoveDir(spriteDir); sprite.setNotMoving(); } //Get Room objects Dictionary<String, RoomObjectBase> roomObjects = currentRoom.getRoomObjects(); //Get exit gate position and size RoomObjectGate exitGate = (RoomObjectGate) roomObjects[Names.Objects.GATE_EXIT]; Point exitGateTilePos = exitGate.getTilePosition(); exitGateTilePos.X += 1; //Gate size is manually adjusted to make more realistic collisions to get through it Point exitGateTileMaxPos = new Point(exitGateTilePos.X + exitGate.getTileSize().X - 3, exitGateTilePos.Y + exitGate.getTileSize().Y - 1); //Get background sprites TextureManager.DUNGEON_SPRITES[,] bgSprites = currentRoom.getBackgroundTexture(); //Get player tile position int halfTileSize = Reference.PIXELS_PER_GRID_SQUARE / 2; xTilePos = (int) Math.Floor((xPos + halfTileSize) / Reference.PIXELS_PER_GRID_SQUARE); yTilePos = (int) Math.Floor((yPos + halfTileSize) / Reference.PIXELS_PER_GRID_SQUARE); //Check tile in direction of movement for wall, unless the gate is there if (xPos < lastXPos) { //Moving Left if (xPos <= (xTilePos * Reference.PIXELS_PER_GRID_SQUARE)) { if (bgSprites[xTilePos - 1, yTilePos] == TextureManager.DUNGEON_SPRITES.WALL_L || bgSprites[xTilePos - 1, yTilePos] == TextureManager.DUNGEON_SPRITES.WALL_TL || bgSprites[xTilePos - 1, yTilePos] == TextureManager.DUNGEON_SPRITES.WALL_BL) //If there's a WALL_L, WALL_TL, or WALL_BL to the left of the player backToLastCoords('x'); //Check for object here if (checkForObject(roomObjects, MoveDir.LEFT)) backToLastCoords('x'); } } else if (xPos > lastXPos) { //Moving Right if (xPos >= (xTilePos * Reference.PIXELS_PER_GRID_SQUARE)) { if (bgSprites[xTilePos + 1, yTilePos] == TextureManager.DUNGEON_SPRITES.WALL_R || bgSprites[xTilePos + 1, yTilePos] == TextureManager.DUNGEON_SPRITES.WALL_TR || bgSprites[xTilePos + 1, yTilePos] == TextureManager.DUNGEON_SPRITES.WALL_BR) //If there's a WALL_R, WALL_TR, or WALL_BR to the right of the player backToLastCoords('x'); if (checkForObject(roomObjects, MoveDir.RIGHT)) backToLastCoords('x'); } } if (yPos < lastYPos) { //Moving Up if (yPos <= (yTilePos * Reference.PIXELS_PER_GRID_SQUARE)) { if (bgSprites[xTilePos, yTilePos - 2] == TextureManager.DUNGEON_SPRITES.WALL_T || bgSprites[xTilePos, yTilePos - 2] == TextureManager.DUNGEON_SPRITES.WALL_TL || bgSprites[xTilePos, yTilePos - 2] == TextureManager.DUNGEON_SPRITES.WALL_TR) { if (!((xTilePos >= exitGateTilePos.X && xTilePos <= exitGateTileMaxPos.X) && (yTilePos - 1 >= exitGateTilePos.Y && yTilePos - 1 <= exitGateTileMaxPos.Y))) { //If there's a WALL_T, WALL_TL, or WALL_TR above the player and there's no gate backToLastCoords('y'); Console.WriteLine("Stopping player at top!"); } } if (checkForObject(roomObjects, MoveDir.UP)) backToLastCoords('y'); } } else if (yPos > lastYPos) { //Moving Down if (yPos >= (yTilePos * Reference.PIXELS_PER_GRID_SQUARE)) { if (bgSprites[xTilePos, yTilePos + 1] == TextureManager.DUNGEON_SPRITES.WALL_B || bgSprites[xTilePos, yTilePos + 1] == TextureManager.DUNGEON_SPRITES.WALL_BL || bgSprites[xTilePos, yTilePos + 1] == TextureManager.DUNGEON_SPRITES.WALL_BR) //If there's a WALL_B, WALL_BL, or WALL_BR below the player backToLastCoords('y'); if (checkForObject(roomObjects, MoveDir.DOWN)) backToLastCoords('y'); } } }
/// <summary> /// Finds the closest object to the player within reach (Config.playerActionRange) which can be interacted with. /// Returns the object or null if none are found. /// </summary> /// <param name="currentRoom"></param> /// <returns></returns> public RoomObjectBase findClosestObject(RoomLevelBase currentRoom) { //Point playerCenter = new Point((int)xPos + (Reference.PIXELS_PER_GRID_SQUARE / 2), (int)yPos + (Reference.PIXELS_PER_GRID_SQUARE / 2)); Dictionary<String, RoomObjectBase> roomObjects = currentRoom.getRoomObjects(); RoomObjectBase closestObject = null; double distToClosest = double.MaxValue; foreach (RoomObjectBase obj in roomObjects.Values) { if (obj.canBeInteractedWith()) { Point objPos = obj.getAbsolutePosition(); if (closestObject == null) { closestObject = obj; distToClosest = getDist(new Point((int)xPos, (int)yPos), objPos); } else { //Check if obj is closer than current closest Point closestPos = closestObject.getTilePosition(); double thisDist = getDist(new Point((int)xPos, (int)yPos), objPos); if (thisDist < distToClosest) { closestObject = obj; distToClosest = thisDist; } } } } if (distToClosest < Reference.playerActionRange) return closestObject; else return null; }