Skip to content

Commit

Permalink
[ui] Introducing a basic scoreboard for #19
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Feb 26, 2020
1 parent 6be3dd8 commit b54e0e8
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/ui/scoreboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* \file scoreboard.cpp
* \brief UI element to show score in RoomView
* \author G. B.
* \version 0.1a
* \date 25/02/2020
*/
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is "Incompatible With Secondary Licenses",
* as defined by the Mozilla Public License, v. 2.0.
*/

#include "scoreboard.hpp"
using namespace std;
using namespace gcn;

/**
* Constructor
*/
ScoreBoard::ScoreBoard() : Widget() {
setWidth(100);
}

void ScoreBoard::draw(Graphics* graphics) {
unsigned int step = getWidth() / (scores.size() * 2);
int i = 0;

for (map<char, int>::iterator it = scores.begin(); it != scores.end(); it++) {
graphics->setColor(Color(0xffffff));
graphics->drawText(string(1, it->first), step * 2 * i, 0, Graphics::LEFT);
graphics->drawText(to_string(it->second), step * 2 * i + 1, 0, Graphics::LEFT);
i++;
}
}

/**
* \brief Initialize the object
* \param mode game mode
* \param nbTeamPlayers number of players for each team
* \param nbTeams number of teams (usually 2)
*/
void ScoreBoard::init(GameMode mode, const int nbTeamPlayers, const int nbTeams) {
int startScore = mode == GameMode::POINTS ? 0 : nbTeamPlayers + 1;

for (int i = 0; i < nbTeams; i++) {
scores['A' + i] = startScore;
}
}

/**
* \brief Edit the score of a team
* \param team char representing the team (A, B, C, D)
* \param delta delta to apply to the current score
*/
void ScoreBoard::addScore(const char team, const int delta) {
scores[team] += delta;
}

/**
* \brief Convenience function to register a death
* \param team char representing the team which suffered the death
*/
void ScoreBoard::removeLife(const char team) {
addScore(team, -1);
}

/**
* \brief Convenience function to get the current score for a team
* \param team char representing the team
* \return the current score for this team
*/
const int ScoreBoard::getScore(const char team) {
return scores[team];
}
39 changes: 39 additions & 0 deletions src/ui/scoreboard.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* \file scoreboard.hpp
* \brief UI element to show score in RoomView
* \author G. B.
* \version 0.1a
* \date 25/02/2020
*/
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is "Incompatible With Secondary Licenses",
* as defined by the Mozilla Public License, v. 2.0.
*/

#ifndef _H_SCOREBOARD_
#define _H_SCOREBOARD_

#include <string>
#include <map>
#include <guisan.hpp>
#include "../constants.hpp"

class ScoreBoard : public gcn::Widget {
public:
ScoreBoard();

void draw(gcn::Graphics* graphics) override;

void init(GameMode mode, const int nbTeamPlayers, const int nbTeams = 2);
void addScore(const char team, const int delta);
void removeLife(const char team);
const int getScore(const char team);

private:
std::map<char, int> scores;
};

#endif //! _H_SCOREBOARD_
3 changes: 3 additions & 0 deletions src/views/roomview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ gameMode(mode), currentMap(map) {
results.setActionEventId("results");
results.addActionListener(this);

scoreBoard.setHeight(15);

currentMap->load();

}
Expand Down Expand Up @@ -204,6 +206,7 @@ void RoomView::addWidgets() {
addWidget(&pb_motion, 70, pb_power.getY() - 20);

addWidget(&msgLog, 2, 2);
addWidget(&scoreBoard, getWidth() - scoreBoard.getWidth(), getHeight() - scoreBoard.getHeight());

addCenteredWidget(&results);
}
Expand Down
2 changes: 2 additions & 0 deletions src/views/roomview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "../ui/messagelog.hpp"
#include "../ui/chatballoon.hpp"
#include "../ui/resultsdialog.hpp"
#include "../ui/scoreboard.hpp"
#include "../common/commonplayer.hpp" // temporary, use clientplayer

class RoomView: public Context, public gcn::ActionListener {
Expand Down Expand Up @@ -69,6 +70,7 @@ class RoomView: public Context, public gcn::ActionListener {
gcn::ProgressBar pb_motion{ 0, MOTION_LIMIT, MOTION_LIMIT };
MessageLog msgLog;
ResultsDialog results;
ScoreBoard scoreBoard;

Uint16 turnCount = 0;
InteractionMode currentMode = InteractionMode::IDLE;
Expand Down

0 comments on commit b54e0e8

Please sign in to comment.