Skip to content

Commit

Permalink
Added battle team support
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Nov 1, 2019
1 parent ce45d97 commit b19323e
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/game/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ set(MOD_GAME_SRC
states/substates/ingame/Pause.cpp
states/substates/ingame/PlayerSelect.cpp
states/substates/ingame/Statistics.cpp
states/substates/ingame/TeamSelect.cpp
states/substates/mainmenu/Base.cpp
states/substates/mainmenu/Options.cpp

Expand Down Expand Up @@ -131,6 +132,7 @@ set(MOD_GAME_H
states/substates/ingame/Pause.h
states/substates/ingame/PlayerSelect.h
states/substates/ingame/Statistics.h
states/substates/ingame/TeamSelect.h
states/substates/mainmenu/Base.h
states/substates/mainmenu/Options.h

Expand Down
14 changes: 13 additions & 1 deletion src/game/states/IngameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "substates/ingame/FadeInOut.h"
#include "substates/ingame/Gameplay.h"
#include "substates/ingame/PlayerSelect.h"
#include "substates/ingame/TeamSelect.h"
#include "system/Paths.h"
#include "system/Texture.h"
#include "system/util/MakeUnique.h"
Expand All @@ -23,6 +24,13 @@ bool isSinglePlayer(GameMode gamemode)
return sp_modes.count(gamemode);
}

namespace {
bool is_team_based(GameMode gamemode)
{
return gamemode == GameMode::MP_BATTLE;
}
} // namespace


IngameState::IngameState(AppContext& app, GameMode gamemode)
: gamemode(gamemode)
Expand All @@ -45,7 +53,11 @@ IngameState::IngameState(AppContext& app, GameMode gamemode)
}));
}
else {
states.emplace_back(std::make_unique<SubStates::Ingame::States::PlayerSelect>(app));
if (is_team_based(gamemode))
states.emplace_back(std::make_unique<SubStates::Ingame::States::TeamSelect>(app));
else
states.emplace_back(std::make_unique<SubStates::Ingame::States::PlayerSelect>(app));

states.emplace_back(std::make_unique<SubStates::Ingame::States::FadeIn>([this](){
states.pop_back();
}));
Expand Down
25 changes: 16 additions & 9 deletions src/game/states/substates/ingame/Gameplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace SubStates {
namespace Ingame {
namespace States {

Gameplay::Gameplay(AppContext& app, IngameState& parent, unsigned short starting_gravity_level)
Gameplay::Gameplay(AppContext& app, IngameState& parent, unsigned short starting_gravity_level, std::unordered_map<DeviceID, size_t>&& team_setup)
: player_devices(parent.device_order)
, theme_settings(app.theme().gameplay)
, music(app.audio().loadMusic(app.theme().random_game_music()))
Expand All @@ -50,12 +50,14 @@ Gameplay::Gameplay(AppContext& app, IngameState& parent, unsigned short starting
[&parent, &app](){
parent.states.emplace_back(std::make_unique<Statistics>(parent, app));
})
, player_team(std::move(team_setup))
{
TextPopup::text_color = app.theme().colors.popup;

assert(player_devices.size() > 0);
assert(player_devices.size() <= 4);
assert(starting_gravity_level < 15);
assert(player_team.size() == 0 || player_team.size() == player_devices.size());
parent.player_areas.clear();
parent.player_stats.clear();

Expand Down Expand Up @@ -218,7 +220,7 @@ void Gameplay::sendGarbageMaybe(IngameState& parent, DeviceID source_player,
// find target player
std::vector<DeviceID> possible_players;
for (const DeviceID possible_device : player_devices) {
if (player_status.at(possible_device) == PlayerStatus::PLAYING && possible_device != source_player)
if (player_status.at(possible_device) == PlayerStatus::PLAYING && player_team.at(possible_device) != player_team.at(source_player))
possible_players.push_back(possible_device);
}
assert(!possible_players.empty());
Expand Down Expand Up @@ -409,18 +411,23 @@ void Gameplay::registerObservers(IngameState& parent, AppContext& app)
parent.player_areas.at(device_id).startGameOver();

// find out who else is still playing
auto playing_players = playingPlayers();
std::vector<DeviceID> playing_players = playingPlayers();

// IF MARATHON
// wait until all players finish the game
// IF BATTLE
if (parent.gamemode == GameMode::MP_BATTLE) {
// if there's only one player left, s/he is the winner
if (playing_players.size() == 1) {
const DeviceID pdevid = playing_players.front();
player_status.at(pdevid) = PlayerStatus::FINISHED;
parent.player_areas.at(pdevid).startGameFinish();
sfx_onfinish->playOnce();
std::unordered_map<size_t, size_t> team_player_count;
for (DeviceID player : playing_players)
team_player_count[player_team.at(player)]++;

// if there's only one team left, they are the winner
if (team_player_count.size() == 1) {
for (DeviceID player : playing_players) {
player_status.at(player) = PlayerStatus::FINISHED;
parent.player_areas.at(player).startGameFinish();
sfx_onfinish->playOnce();
}
playing_players.clear();
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/game/states/substates/ingame/Gameplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ namespace States {

class Gameplay : public State {
public:
Gameplay(AppContext&, IngameState&, unsigned short starting_gravity_level = 0);
Gameplay(AppContext&, IngameState&,
unsigned short starting_gravity_level = 0,
std::unordered_map<DeviceID, size_t>&& team_setup = {});
virtual ~Gameplay();

void updateAnimationsOnly(IngameState&, AppContext&) final;
Expand Down Expand Up @@ -75,6 +77,7 @@ class Gameplay : public State {
FINISHED,
};
std::unordered_map<DeviceID, PlayerStatus> player_status;
std::unordered_map<DeviceID, size_t> player_team;

std::vector<DeviceID> playingPlayers();
void addNextPiece(IngameState&, DeviceID);
Expand Down
Loading

0 comments on commit b19323e

Please sign in to comment.