Fixed attacks

This commit is contained in:
Maximilian Giller 2023-07-10 23:05:14 +02:00
parent ac435f8e4f
commit 95562a9da7
7 changed files with 105 additions and 36 deletions

View file

@ -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)

View file

@ -3,7 +3,8 @@
enum GameAction
{
CONFIRM
CONFIRM,
RUN
};
#endif //HOLESOME_GAME_ACTION_HPP

View file

@ -74,7 +74,7 @@ void MapPlayer::updateCollidingWithPlayers()
}
std::shared_ptr<MapPlayer> mapPlayerA = MapSimulation::getInstance()->getMapPlayerByBody(bodyA);
std::shared_ptr<MapPlayer> mapPlayerB = MapSimulation::getInstance()->getMapPlayerByBody(bodyA);
std::shared_ptr<MapPlayer> mapPlayerB = MapSimulation::getInstance()->getMapPlayerByBody(bodyB);
if (mapPlayerA == nullptr || mapPlayerB == nullptr)
{

View file

@ -125,6 +125,17 @@ 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);
@ -150,16 +161,19 @@ void Player::handlePlayerCollisions()
float numAttackingPlayers = 0;
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;
}

View file

@ -18,6 +18,9 @@ const std::map<sf::Keyboard::Key, ButtonConfig> KEY_CONFIGS = {
{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)},
@ -26,26 +29,37 @@ const std::map<sf::Keyboard::Key, ButtonConfig> KEY_CONFIGS = {
{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::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<int, ButtonConfig> GAMEPAD_BUTTON_CONFIGS = {
{GamepadButton::EAST, ButtonConfig(InputDeviceGroup::GAMEPAD, {GameAction::CONFIRM})},
{GamepadButton::SOUTH, 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<GameAction, GameActionConfig> 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<InputDeviceGroup, std::string> 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<InputDeviceGroup, std::string> 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

View file

@ -112,6 +112,37 @@ void JoinScreen::addPlayerAtIndex(std::shared_ptr<Player> &player, int index)
topLeft.y + skinSprite->getSize().y + 20);
playerList.push_back(deviceGroupText);
// Show button to run
auto runText = std::make_shared<sf::Text>();
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<sf::Text>();
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<sf::Text>();
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> &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> &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<sf::Text>();
@ -143,6 +174,6 @@ void JoinScreen::addPlayerAtIndex(std::shared_ptr<Player> &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);
}

View file

@ -58,7 +58,7 @@ void WinnerScreen::createWinnerTribute(const std::shared_ptr<Player> &winner)
{
auto highestPoints = std::make_shared<sf::Text>();
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,