Skip to content

Commit

Permalink
Fully implemented three player mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
dulsi committed Feb 7, 2016
1 parent e2251a5 commit 656f20d
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 22 deletions.
2 changes: 0 additions & 2 deletions src/joust/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#ifndef CONSTANTS_H_INCLUDED
#define CONSTANTS_H_INCLUDED

#define LIMIT_PLAYERS_2

#include <string>

const std::string APP_NAME = "Ostrich Riders";
Expand Down
28 changes: 14 additions & 14 deletions src/joust/LogicEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,26 +237,31 @@ void LogicEngine::startGame(int nPlayers)

// hud texts
float yText = 730.0f;
TextEntity* text1 = new TextEntity(&OstrichRiders::GetDefaultFont(), 24, 15.0f, yText);
text1->setText(L"Player 1");
scoreEntity[0] = new TextEntity(&OstrichRiders::GetDefaultFont(), 24, 245.0f, yText);
scoreEntity[0]->setText(0);
RotatingTextEntity *texts[2];
texts[0] = new RotatingTextEntity(&OstrichRiders::GetDefaultFont(), 24, 15.0f, yText);
texts[0]->setText(0, "Player 1");
scoreEntity[0] = new RotatingTextEntity(&OstrichRiders::GetDefaultFont(), 24, 245.0f, yText);
scoreEntity[0]->setText(0, 0);
scoreEntity[0]->setAlignment(TextEntity::ALIGN_RIGHT);

lifeEntity[0] = new LifeEntity(ImageManager::getImageManager()->getImage(invertedPlayers ? IMAGE_LIFE1 : IMAGE_LIFE0), 273.0f, yText + 3, &lives[0]);

if (nPlayers > 1)
{
TextEntity* text2 = new TextEntity(&OstrichRiders::GetDefaultFont(), 24, 925.0f, yText);
text2->setText(L"Player 2");
scoreEntity[1] = new TextEntity(&OstrichRiders::GetDefaultFont(), 24, 902.0f, yText);
scoreEntity[1]->setText(0);
texts[1] = new RotatingTextEntity(&OstrichRiders::GetDefaultFont(), 24, 925.0f, yText);
texts[1]->setText(0, "Player 2");
scoreEntity[1] = new RotatingTextEntity(&OstrichRiders::GetDefaultFont(), 24, 902.0f, yText);
scoreEntity[1]->setText(0, 0);
scoreEntity[1]->setAlignment(TextEntity::ALIGN_RIGHT);

lifeEntity[1] = new LifeEntity(ImageManager::getImageManager()->getImage(IMAGE_LIFE1), 690.0f, yText + 3, &lives[1]);
for (int i = 2; i < nPlayers; ++i)
{
lifeEntity[i % 2]->addLives(ImageManager::getImageManager()->getImage(IMAGE_LIFE0 + i), &lives[i]);
std::ostringstream intStream;
intStream << "Player " << (i + 1);
texts[i % 2]->setText(i / 2, intStream.str());
scoreEntity[i % 2]->setText(i / 2, 0);
}
}

Expand Down Expand Up @@ -843,8 +848,7 @@ void LogicEngine::update(float dt)
playerStatus[i] = PLAYER_STATUS_PLAYING;
}
}
if (i < 2)
scoreEntity[i]->setText(score[i]);
scoreEntity[i % 2]->setText(i / 2, score[i]);
}

if (arePlayersDead())
Expand Down Expand Up @@ -1031,9 +1035,7 @@ void LogicEngine::buildMenu()
MenuEntry* entryPlayers = new MenuEntry(L"Number of players", MenuEntry::typeChoice);
entryPlayers->addChoice(L"1");
entryPlayers->addChoice(L"2");
#ifndef LIMIT_PLAYERS_2
entryPlayers->addChoice(L"3");
#endif
mainMenu->addEntry(entryPlayers);

MenuEntry* entryMod = new MenuEntry(L"Mod", MenuEntry::typeChoice);
Expand All @@ -1059,10 +1061,8 @@ void LogicEngine::buildMenu()
optionMenu->addEntry(entryControls1);
MenuEntry* entryControls2 = new MenuEntry(L"Configure controls (P2)", MenuEntry::typeButton);
optionMenu->addEntry(entryControls2);
#ifndef LIMIT_PLAYERS_2
MenuEntry* entryControls3 = new MenuEntry(L"Configure controls (P3)", MenuEntry::typeButton);
optionMenu->addEntry(entryControls3);
#endif
MenuEntry* entryBack = new MenuEntry(L"Back", MenuEntry::typeButton);
optionMenu->addEntry(entryBack);

Expand Down
8 changes: 2 additions & 6 deletions src/joust/LogicEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "EggEntity.h"
#include "JoustTileMapEntity.h"
#include "LifeEntity.h"
#include "RotatingTextEntity.h"
#include "SelectorEntity.h"
//#include "../sfmlGame/Model/GameMap.h"
#include "JoustGameMap.h"
Expand All @@ -48,13 +49,8 @@ const int MENU_MAIN_EXIT = 4;
const int MENU_OPTION_FULLSCREEN = 0;
const int MENU_OPTION_CONTROLS1 = 1;
const int MENU_OPTION_CONTROLS2 = 2;
#ifdef LIMIT_PLAYERS_2
const int MENU_OPTION_CONTROLS3 = 4;
const int MENU_OPTION_BACK = 3;
#else
const int MENU_OPTION_CONTROLS3 = 3;
const int MENU_OPTION_BACK = 4;
#endif

const int MENU_CONTROL_LEFT = 0;
const int MENU_CONTROL_RIGHT = 1;
Expand Down Expand Up @@ -150,7 +146,7 @@ class LogicEngine
int playerStatus[NPLAYERS_MAX]; // player's status
PlayerInput* playerInput[NPLAYERS_MAX]; // input set of the player
float playerDelay[NPLAYERS_MAX]; // delay for the player
TextEntity* scoreEntity[NPLAYERS_MAX];
RotatingTextEntity* scoreEntity[NPLAYERS_MAX];
LifeEntity* lifeEntity[NPLAYERS_MAX];
bool isSurvivor[NPLAYERS_MAX];
float killDelay[NPLAYERS_MAX];
Expand Down
60 changes: 60 additions & 0 deletions src/joust/RotatingTextEntity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/** This file is part of sfmlGame.
*
* FreeTumble is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTumble is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FreeTumble. If not, see <http://www.gnu.org/licenses/>.
*/

#include <sstream>
#include <iostream>

#include "RotatingTextEntity.h"

RotatingTextEntity::RotatingTextEntity(const sf::Font* font, int size, float x, float y) : TextEntity(font, size, x, y), current(0), currentTime(0.0)
{
}

void RotatingTextEntity::render(sf::RenderWindow* app)
{
string->setString(texts[current]);
TextEntity::render(app);
}

void RotatingTextEntity::animate(float delay)
{
if (texts.size() > 1)
{
currentTime += delay;
if (currentTime >= ROTATING_DELAY)
{
current = ++current % texts.size();
currentTime = currentTime - (((int)currentTime / ROTATING_DELAY) * ROTATING_DELAY);
}
}
GameEntity::animate(delay);
}

void RotatingTextEntity::setText(size_t index, std::string text)
{
while (index >= texts.size())
texts.push_back("");
texts[index] = text;
}

void RotatingTextEntity::setText(size_t index, int intText)
{
while (index >= texts.size())
texts.push_back("");
std::ostringstream intStream;
intStream << intText;
texts[index] = intStream.str();
}
40 changes: 40 additions & 0 deletions src/joust/RotatingTextEntity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/** This file is part of sfmlGame.
*
* FreeTumble is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTumble is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FreeTumble. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef ROTATINGTEXTENTITY_H_INCLUDED
#define ROTATINGTEXTENTITY_H_INCLUDED

#include "Constants.h"
#include "../sfmlGame/Entity/TextEntity.h"

class RotatingTextEntity : public TextEntity
{
public:
RotatingTextEntity(const sf::Font* font, int size, float x = 0.0f, float y = 0.0f);

virtual void render(sf::RenderWindow* app);
virtual void animate(float delay);

virtual void setText(size_t index, std::string text);
virtual void setText(size_t index, int intText);

protected:
std::vector<std::string> texts;
int current;
float currentTime;
};

#endif // TEXTENTITY_H_INCLUDED

0 comments on commit 656f20d

Please sign in to comment.