From 95562a9da7d11e9ef4368aac6868dafd7705b209 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Mon, 10 Jul 2023 23:05:14 +0200 Subject: [PATCH] Fixed attacks --- src/config.h | 6 ++- src/game/input/game_action.hpp | 3 +- src/game/physics/map/map_player.cpp | 2 +- src/game/player/player.cpp | 29 ++++++++++---- src/input_config.h | 62 +++++++++++++++++++---------- src/screens/join_screen.cpp | 37 +++++++++++++++-- src/screens/winner_screen.cpp | 2 +- 7 files changed, 105 insertions(+), 36 deletions(-) diff --git a/src/config.h b/src/config.h index 805eed0..97f2687 100644 --- a/src/config.h +++ b/src/config.h @@ -16,9 +16,11 @@ #define PLAYER_RADIUS_PER_LEVEL 0.25f #define DEFAULT_MAX_PLAYER_NUMBER 4 #define MIN_PLAYER_COUNT 2 -#define REDUCED_POINT_DELTA_FOR_ATTACK 20 +#define POINT_DELTA_FOR_ATTACK 20 #define ATTACK_PER_SECOND 1.f -#define PLAYER_ALIVE_THRESHOLD -10 +#define PLAYER_ALIVE_THRESHOLD (-10) +#define PLAYER_RUN_SPEED (DEFAULT_PLAYER_SPEED * 2.f) +#define PLAYER_RUN_SPEED_COST_PER_SECOND 10.f // World #define WORLD_GRAVITY b2Vec2(0.f, -9.8f) diff --git a/src/game/input/game_action.hpp b/src/game/input/game_action.hpp index 7896455..296d89f 100644 --- a/src/game/input/game_action.hpp +++ b/src/game/input/game_action.hpp @@ -3,7 +3,8 @@ enum GameAction { - CONFIRM + CONFIRM, + RUN }; #endif //HOLESOME_GAME_ACTION_HPP diff --git a/src/game/physics/map/map_player.cpp b/src/game/physics/map/map_player.cpp index 68d4145..cad7050 100644 --- a/src/game/physics/map/map_player.cpp +++ b/src/game/physics/map/map_player.cpp @@ -74,7 +74,7 @@ void MapPlayer::updateCollidingWithPlayers() } std::shared_ptr mapPlayerA = MapSimulation::getInstance()->getMapPlayerByBody(bodyA); - std::shared_ptr mapPlayerB = MapSimulation::getInstance()->getMapPlayerByBody(bodyA); + std::shared_ptr mapPlayerB = MapSimulation::getInstance()->getMapPlayerByBody(bodyB); if (mapPlayerA == nullptr || mapPlayerB == nullptr) { diff --git a/src/game/player/player.cpp b/src/game/player/player.cpp index 5778096..7143d0f 100644 --- a/src/game/player/player.cpp +++ b/src/game/player/player.cpp @@ -125,12 +125,23 @@ void Player::setPassiveMode(bool newPassiveMode) void Player::performInteractiveUpdates() { + if (input->isPerformingAction(GameAction::RUN)) + { + speed = PLAYER_RUN_SPEED; + + // Subtract cost + setPoints(points - PLAYER_RUN_SPEED_COST_PER_SECOND * FRAME_TIME.asSeconds()); + } else + { + speed = DEFAULT_PLAYER_SPEED; + } + auto moveDirection = input->direction.asIsometricVector(); auto moveDelta = moveDirection * speed * FRAME_TIME.asSeconds(); coordinates->move(moveDelta); } -void Player::setCollidingPlayers(const std::vector& collidingPlayerIds) +void Player::setCollidingPlayers(const std::vector &collidingPlayerIds) { collidingPlayers.clear(); for (auto collidingPlayerId: collidingPlayerIds) @@ -148,18 +159,21 @@ void Player::handlePlayerCollisions() // Count number of players that are bigger than this player by the required margin float numAttackingPlayers = 0; - for (const auto& collidingPlayer: collidingPlayers) + for (const auto &collidingPlayer: collidingPlayers) { - if (collidingPlayer->getPoints() >= points + REDUCED_POINT_DELTA_FOR_ATTACK) + if (collidingPlayer->getPoints() - getPoints() >= POINT_DELTA_FOR_ATTACK) { numAttackingPlayers++; } } // Reduce points if necessary - float damage = numAttackingPlayers * ATTACK_PER_SECOND * FRAME_TIME.asSeconds() * (getPoints() + 1 + abs(PLAYER_ALIVE_THRESHOLD)); - if (damage > 0) { - LOG(INFO) << "Player " << playerId << " lost " << damage << " points due to collisions. Total: " << points - damage; + float damage = numAttackingPlayers * ATTACK_PER_SECOND * FRAME_TIME.asSeconds() * + (points + 1.f + fabs(PLAYER_ALIVE_THRESHOLD)); + if (numAttackingPlayers > 0) + { + LOG(INFO) << "Player " << playerId << " is getting attacked by " << numAttackingPlayers << "players and lost " + << damage << " points."; } setPoints(points - damage); @@ -187,7 +201,8 @@ void Player::setAlive(bool newAlive) void Player::draw(sf::RenderWindow *window) { - if (isAlive) { + if (isAlive) + { GameObject::draw(window); return; } diff --git a/src/input_config.h b/src/input_config.h index 38a6986..135e3a4 100644 --- a/src/input_config.h +++ b/src/input_config.h @@ -14,38 +14,52 @@ // Keys const std::map KEY_CONFIGS = { - {sf::Keyboard::Up, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, HardDirection::UP)}, - {sf::Keyboard::Down, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, HardDirection::DOWN)}, - {sf::Keyboard::Left, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, HardDirection::LEFT)}, - {sf::Keyboard::Right, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, HardDirection::RIGHT)}, - {sf::Keyboard::Space, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, {GameAction::CONFIRM})}, + {sf::Keyboard::Up, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, HardDirection::UP)}, + {sf::Keyboard::Down, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, HardDirection::DOWN)}, + {sf::Keyboard::Left, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, HardDirection::LEFT)}, + {sf::Keyboard::Right, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, HardDirection::RIGHT)}, + {sf::Keyboard::RShift, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, {GameAction::RUN})}, + {sf::Keyboard::RControl, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, {GameAction::RUN})}, + {sf::Keyboard::Numpad0, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, {GameAction::RUN})}, + {sf::Keyboard::Space, ButtonConfig(InputDeviceGroup::KEYBOARD_ARROWS, {GameAction::CONFIRM})}, - {sf::Keyboard::W, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, HardDirection::UP)}, - {sf::Keyboard::S, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, HardDirection::DOWN)}, - {sf::Keyboard::A, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, HardDirection::LEFT)}, - {sf::Keyboard::D, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, HardDirection::RIGHT)}, - {sf::Keyboard::Q, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, {GameAction::CONFIRM})}, - {sf::Keyboard::E, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, {GameAction::CONFIRM})}, + {sf::Keyboard::W, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, HardDirection::UP)}, + {sf::Keyboard::S, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, HardDirection::DOWN)}, + {sf::Keyboard::A, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, HardDirection::LEFT)}, + {sf::Keyboard::D, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, HardDirection::RIGHT)}, + {sf::Keyboard::Q, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, {GameAction::CONFIRM})}, + {sf::Keyboard::E, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, {GameAction::CONFIRM})}, + {sf::Keyboard::LShift, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, {GameAction::RUN})}, + {sf::Keyboard::LControl, ButtonConfig(InputDeviceGroup::KEYBOARD_WASD, {GameAction::RUN})}, - {sf::Keyboard::I, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, HardDirection::UP)}, - {sf::Keyboard::K, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, HardDirection::DOWN)}, - {sf::Keyboard::J, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, HardDirection::LEFT)}, - {sf::Keyboard::L, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, HardDirection::RIGHT)}, - {sf::Keyboard::U, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, {GameAction::CONFIRM})}, - {sf::Keyboard::O, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, {GameAction::CONFIRM})} + {sf::Keyboard::I, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, HardDirection::UP)}, + {sf::Keyboard::K, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, HardDirection::DOWN)}, + {sf::Keyboard::J, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, HardDirection::LEFT)}, + {sf::Keyboard::L, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, HardDirection::RIGHT)}, + {sf::Keyboard::U, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, {GameAction::CONFIRM})}, + {sf::Keyboard::O, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, {GameAction::CONFIRM})}, + {sf::Keyboard::RAlt, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, {GameAction::RUN})}, + {sf::Keyboard::B, ButtonConfig(InputDeviceGroup::KEYBOARD_IJKL, {GameAction::RUN})} }; // Gamepad buttons const std::map GAMEPAD_BUTTON_CONFIGS = { - {GamepadButton::EAST, ButtonConfig(InputDeviceGroup::GAMEPAD, {GameAction::CONFIRM})}, - {GamepadButton::SOUTH, ButtonConfig(InputDeviceGroup::GAMEPAD, {GameAction::CONFIRM})} + {GamepadButton::EAST, ButtonConfig(InputDeviceGroup::GAMEPAD, {GameAction::CONFIRM})}, + {GamepadButton::SOUTH, ButtonConfig(InputDeviceGroup::GAMEPAD, {GameAction::CONFIRM})}, + {GamepadButton::RIGHT_SHOULDER, ButtonConfig(InputDeviceGroup::GAMEPAD, {GameAction::RUN})}, + {GamepadButton::LEFT_SHOULDER, ButtonConfig(InputDeviceGroup::GAMEPAD, {GameAction::RUN})}, + {GamepadButton::RIGHT_TRIGGER, ButtonConfig(InputDeviceGroup::GAMEPAD, {GameAction::RUN})}, + {GamepadButton::LEFT_TRIGGER, ButtonConfig(InputDeviceGroup::GAMEPAD, {GameAction::RUN})}, + {GamepadButton::NORTH, ButtonConfig(InputDeviceGroup::GAMEPAD, {GameAction::RUN})}, + {GamepadButton::WEST, ButtonConfig(InputDeviceGroup::GAMEPAD, {GameAction::RUN})} }; // Actions const std::map GAME_ACTION_CONFIGS = { - {GameAction::CONFIRM, GameActionConfig(InteractionMode::PRESS)} + {GameAction::CONFIRM, GameActionConfig(InteractionMode::PRESS)}, + {GameAction::RUN, GameActionConfig(InteractionMode::HOLD)} }; @@ -59,7 +73,13 @@ const std::map DEVICE_GROUP_CONFIRM = { {InputDeviceGroup::KEYBOARD_WASD, "Q or E"}, {InputDeviceGroup::KEYBOARD_IJKL, "U or O"}, {InputDeviceGroup::KEYBOARD_ARROWS, "Space"}, - {InputDeviceGroup::GAMEPAD, "A or B"} + {InputDeviceGroup::GAMEPAD, "A"} +}; +const std::map DEVICE_GROUP_RUN = { + {InputDeviceGroup::KEYBOARD_WASD, "L-Shift or -Ctrl"}, + {InputDeviceGroup::KEYBOARD_IJKL, "B or R-Alt"}, + {InputDeviceGroup::KEYBOARD_ARROWS, "R-Shift, -Ctrl or Pad-0"}, + {InputDeviceGroup::GAMEPAD, "Trigger or Shoulder"} }; #endif //HOLESOME_INPUT_CONFIG_H diff --git a/src/screens/join_screen.cpp b/src/screens/join_screen.cpp index 20cf629..d2540a1 100644 --- a/src/screens/join_screen.cpp +++ b/src/screens/join_screen.cpp @@ -112,6 +112,37 @@ void JoinScreen::addPlayerAtIndex(std::shared_ptr &player, int index) topLeft.y + skinSprite->getSize().y + 20); playerList.push_back(deviceGroupText); + // Show button to run + auto runText = std::make_shared(); + runText->setString("Press"); + runText->setFont(*FontManager::getInstance()->getDefaultFont()); + runText->setCharacterSize(15); + runText->setFillColor(sf::Color::White); + runText->setPosition(topLeft.x + contentWidth / 2.f * 0.9f - runText->getGlobalBounds().width / 2.f, + topLeft.y + skinSprite->getSize().y + 60); + playerList.push_back(runText); + + std::string buttonToRun = DEVICE_GROUP_RUN.at(deviceGroup); + auto runButton = std::make_shared(); + runButton->setString(buttonToRun); + runButton->setFont(*FontManager::getInstance()->getDefaultFont()); + + runButton->setCharacterSize(15); + runButton->setFillColor(sf::Color::White); + runButton->setPosition(topLeft.x + contentWidth / 2.f * 0.9f - runButton->getGlobalBounds().width / 2.f, + topLeft.y + skinSprite->getSize().y + 80); + playerList.push_back(runButton); + + auto confirmText = std::make_shared(); + confirmText->setString("to run"); + confirmText->setFont(*FontManager::getInstance()->getDefaultFont()); + confirmText->setCharacterSize(15); + confirmText->setFillColor(sf::Color::White); + confirmText->setPosition(topLeft.x + contentWidth / 2.f * 0.9f - confirmText->getGlobalBounds().width / 2.f, + topLeft.y + skinSprite->getSize().y + 100); + playerList.push_back(confirmText); + + if (lastPlayerCount < MIN_PLAYER_COUNT) { return; @@ -124,7 +155,7 @@ void JoinScreen::addPlayerAtIndex(std::shared_ptr &player, int index) pressText->setCharacterSize(24); pressText->setFillColor(sf::Color::Yellow); pressText->setPosition(topLeft.x + contentWidth / 2.f * 0.9f - pressText->getGlobalBounds().width / 2.f, - topLeft.y + skinSprite->getSize().y + 100); + topLeft.y + skinSprite->getSize().y + 170); playerList.push_back(pressText); std::string buttonToStart = DEVICE_GROUP_CONFIRM.at(deviceGroup); @@ -134,7 +165,7 @@ void JoinScreen::addPlayerAtIndex(std::shared_ptr &player, int index) buttonText->setCharacterSize(24); buttonText->setFillColor(sf::Color::Yellow); buttonText->setPosition(topLeft.x + contentWidth / 2.f * 0.9f - buttonText->getGlobalBounds().width / 2.f, - topLeft.y + skinSprite->getSize().y + 130); + topLeft.y + skinSprite->getSize().y + 200); playerList.push_back(buttonText); auto toStartText = std::make_shared(); @@ -143,6 +174,6 @@ void JoinScreen::addPlayerAtIndex(std::shared_ptr &player, int index) toStartText->setCharacterSize(24); toStartText->setFillColor(sf::Color::Yellow); toStartText->setPosition(topLeft.x + contentWidth / 2.f * 0.9f - toStartText->getGlobalBounds().width / 2.f, - topLeft.y + skinSprite->getSize().y + 160); + topLeft.y + skinSprite->getSize().y + 230); playerList.push_back(toStartText); } diff --git a/src/screens/winner_screen.cpp b/src/screens/winner_screen.cpp index 59669c1..e59d60f 100644 --- a/src/screens/winner_screen.cpp +++ b/src/screens/winner_screen.cpp @@ -58,7 +58,7 @@ void WinnerScreen::createWinnerTribute(const std::shared_ptr &winner) { auto highestPoints = std::make_shared(); highestPoints->setFont(*FontManager::getInstance()->getDefaultFont()); - highestPoints->setString("Highest Points: " + std::to_string(winner->getPoints())); + highestPoints->setString("Highest Points: " + std::to_string(winner->getMaxPoints())); highestPoints->setCharacterSize(20); highestPoints->setFillColor(sf::Color::Yellow); highestPoints->setPosition(REFERENCE_SIZE.x / 2 - highestPoints->getGlobalBounds().width / 2,