COOOOOOONST

This commit is contained in:
Thomas 2022-01-04 10:39:14 +01:00
parent 8ace7d1d01
commit 994a073ee5
No known key found for this signature in database
GPG Key ID: E538821A6CDFDAD7
13 changed files with 119 additions and 139 deletions

4
README
View File

@ -5,6 +5,10 @@ Nommage en anglais
Pas de fonctions de +100 lignes
Les guards sont de cette forme : GUARD_<filename>_H
Concernant les const :
Afin de limiter l'utilisation du mot const (pour garder une certaine lisibilité du code), les cas suivants n'ont pas besoin d'etre déclarés comme const :
- déclaration/défintion de fonctions : les types primitifs/valeurs d'enum passés par valeurs ne sont pas notés const
écran : constante 1280x720

View File

@ -40,9 +40,9 @@ private:
void handleScoreSaving();
// drawing methods
void display();
void displayGod();
void displayInvader(const Position& basePos, unsigned size, InvaderType type);
void display() const;
void displayGod() const;
void displayInvader(const Position& basePos, unsigned size, InvaderType type) const;
// managers
@ -56,7 +56,7 @@ private:
void moveTorpedos();
bool checkMissilesAndPlayers();
bool checkTorpedosAndInvaders();
bool invadersTouchPlayer();
bool invadersTouchPlayer() const;
// god things
void manageGod();
@ -67,8 +67,6 @@ public:
Game();
void managedGames();
WinValue playGame();
PlayMode initialMenuHandler();
bool deathMenuHandler();
bool reloadConfig();
};

View File

@ -9,6 +9,7 @@
#include "mingl/shape/circle.h"
#include "mingl/gui/sprite.h"
#include "utils.h"
#include "playMode.h"
using namespace std;
using namespace nsGraphics;
@ -21,28 +22,28 @@ public:
explicit PixelManager(MinGL&);
void drawInvaderA(const Position& baseVector, unsigned size, RGBAcolor& color);
void drawInvaderB(const Position& baseVector, unsigned size, RGBAcolor& color);
void drawInvaderC(const Position& baseVector, unsigned size, RGBAcolor& color);
void drawPlayer(const unsigned x, unsigned width, const nsGraphics::RGBAcolor& color);
void drawTorpedo(const Position& baseVector, unsigned width, const nsGraphics::RGBAcolor& color);
void drawMissile(const Position& baseVector, unsigned width, const nsGraphics::RGBAcolor& color);
void drawBackground();
void drawInvaderA(const Position& baseVector, unsigned size, const RGBAcolor& color) const;
void drawInvaderB(const Position& baseVector, unsigned size, const RGBAcolor& color) const;
void drawInvaderC(const Position& baseVector, unsigned size, const RGBAcolor& color) const;
void drawPlayer(unsigned x, unsigned width, const nsGraphics::RGBAcolor& color) const;
void drawTorpedo(const Position& baseVector, unsigned width, const nsGraphics::RGBAcolor& color) const;
void drawMissile(const Position& baseVector, unsigned width, const nsGraphics::RGBAcolor& color) const;
void drawBackground() const;
unsigned showInitialMenu();
unsigned showDeathMenu();
unsigned getScreenHeight();
unsigned getScreenWidth();
void startFrame();
void endFrame();
PlayMode showInitialMenu() const;
bool showDeathMenu() const;
unsigned getScreenHeight() const;
unsigned getScreenWidth() const;
void startFrame() const;
void endFrame() const;
void askPlayerNameMenu(playerID pID, string& name);
void askPlayerNameMenu(playerID pID, string& name) const;
// y will be negative sometimes, so not unsigned
void displayGodBench(int y);
void displayGodBench(int y) const;
void displayGodRightHand(const Position& pos);
void displayGodLeftHand(const Position& pos);
void displayGodRightHand(const Position& pos) const;
void displayGodLeftHand(const Position& pos) const;
};

View File

@ -19,7 +19,7 @@ public:
vector<ScoreLink> scores;
void inputScore(const string& name, unsigned score);
void readFile();
void writeFile();
void writeFile() const;
};

View File

@ -27,7 +27,8 @@ enum class InvaderType {
};
class InvadersColumn : public vector<InvaderType>{
public:
size_t getOutterInvader();
// idk why CLion says this is not implemented, but it is
size_t getOutterInvader() const;
};
typedef vector<InvadersColumn> InvadersGrid;
typedef nsGraphics::Vec2D Position; // in case we need to ad dmore methods, we defined our own type

View File

@ -6,21 +6,21 @@ public:
ConfigData collectedData;
void parseFile(const string& fname);
void readConfig();
void dumpInternalValues();
void dumpInternalValues() const;
private:
map<string, string> internalValues;
string& getString(const configKey& key);
char getChar(const configKey& key);
int getInt(const configKey& key);
nsGraphics::RGBAcolor getColor(const configKey& key);
void getList(const configKey& key, vector<string>&);
const string& getString(const configKey& key) const;
char getChar(const configKey& key) const;
int getInt(const configKey& key) const;
void getColor(const configKey& key, nsGraphics::RGBAcolor&) const;
void getList(const configKey& key, vector<string>&) const;
void readGrid(const configKey& key);
void readGrid(const configKey& baseKey);
void readPlayer(const configKey& baseKey, PlayerDef&);
void readInvaderType(const configKey& baseKey, InvaderTypeDef&);
};
void ConfigBuilder::dumpInternalValues(){
void ConfigBuilder::dumpInternalValues() const {
for(const auto& ite : internalValues){
cerr << ite.first << " -> " << ite.second << endl;
}
@ -102,7 +102,7 @@ void ConfigBuilder::parseFile(const string& fname) {
file.close();
}
void ConfigBuilder::readGrid(const configKey& key){
void ConfigBuilder::readGrid(const configKey& baseKey) {
vector<string> tmp;
getList("grid", tmp);
@ -146,16 +146,16 @@ void ConfigBuilder::readGrid(const configKey& key){
}
}
void ConfigBuilder::readPlayer(const configKey& baseKey, PlayerDef& pdef){
pdef.color = getColor(baseKey+".color");
void ConfigBuilder::readPlayer(const configKey& baseKey, PlayerDef& pdef) {
getColor(baseKey+".color", pdef.color);
pdef.keys.left = getChar(baseKey+".keys.left");
pdef.keys.right = getChar(baseKey+".keys.right");
pdef.keys.shoot = getChar(baseKey+".keys.shoot");
}
void ConfigBuilder::readInvaderType(const configKey& baseKey, InvaderTypeDef& invDef){
void ConfigBuilder::readInvaderType(const configKey& baseKey, InvaderTypeDef& invDef) {
invDef.points = getInt(baseKey+".points");
invDef.color = getColor(baseKey+".color");
getColor(baseKey+".color", invDef.color);
}
void ConfigBuilder::readConfig() {
@ -188,23 +188,23 @@ void ConfigBuilder::readConfig() {
collectedData.missilesWidth = getInt("projectiles.missiles.width");
collectedData.missilesLength = collectedData.missilesWidth*PROJ_LENGTH_FACTOR;
collectedData.missilesSpeed = getInt("projectiles.missiles.speed");
collectedData.missilesColor = getColor("projectiles.missiles.color");
getColor("projectiles.missiles.color", collectedData.missilesColor);
collectedData.torpedosWidth = getInt("projectiles.torpedos.width");
collectedData.torpedosLength = collectedData.torpedosWidth*PROJ_LENGTH_FACTOR;
collectedData.torpedosSpeed = getInt("projectiles.torpedos.speed");
collectedData.torpedosColor = getColor("projectiles.torpedos.color");
getColor("projectiles.torpedos.color", collectedData.torpedosColor);
}
int ConfigBuilder::getInt(const configKey& key) {
int ConfigBuilder::getInt(const configKey& key) const {
return stoi(getString(key));
}
char ConfigBuilder::getChar(const configKey& key) {
char ConfigBuilder::getChar(const configKey& key) const {
return getString(key)[0];
}
string& ConfigBuilder::getString(const configKey& key) {
const string& ConfigBuilder::getString(const configKey& key) const {
if(internalValues.contains(key)){
return internalValues.at(key);
}else{
@ -212,7 +212,7 @@ string& ConfigBuilder::getString(const configKey& key) {
}
}
void ConfigBuilder::getList(const configKey& key, vector<string>& toPopulate) {
void ConfigBuilder::getList(const configKey& key, vector<string>& toPopulate) const {
size_t i=0;
string fullKey = key+".0";
if(!internalValues.contains(fullKey))throw runtime_error("Non-existent list key requested : "+key);
@ -224,25 +224,25 @@ void ConfigBuilder::getList(const configKey& key, vector<string>& toPopulate) {
}while(internalValues.contains(key+"."+to_string(i)));
}
nsGraphics::RGBAcolor ConfigBuilder::getColor(const configKey& key) {
void ConfigBuilder::getColor(const configKey& key, nsGraphics::RGBAcolor& color) const {
// switch do not work with strings, and I don't want to implement a constexpr hash function
string colorStr = getString(key);
if (colorStr == "black")return nsGraphics::KBlack;
else if (colorStr == "white")return nsGraphics::KWhite;
else if (colorStr == "red")return nsGraphics::KRed;
else if (colorStr == "lime")return nsGraphics::KLime;
else if (colorStr == "blue")return nsGraphics::KBlue;
else if (colorStr == "yellow")return nsGraphics::KYellow;
else if (colorStr == "cyan")return nsGraphics::KCyan;
else if (colorStr == "magenta")return nsGraphics::KMagenta;
else if (colorStr == "silver")return nsGraphics::KSilver;
else if (colorStr == "gray")return nsGraphics::KGray;
else if (colorStr == "maroon")return nsGraphics::KMaroon;
else if (colorStr == "olive")return nsGraphics::KOlive;
else if (colorStr == "green")return nsGraphics::KGreen;
else if (colorStr == "purple")return nsGraphics::KPurple;
else if (colorStr == "teal")return nsGraphics::KTeal;
else if (colorStr == "navy")return nsGraphics::KNavy;
if (colorStr == "black")color = nsGraphics::KBlack;
else if (colorStr == "white")color = nsGraphics::KWhite;
else if (colorStr == "red")color = nsGraphics::KRed;
else if (colorStr == "lime")color = nsGraphics::KLime;
else if (colorStr == "blue")color = nsGraphics::KBlue;
else if (colorStr == "yellow")color = nsGraphics::KYellow;
else if (colorStr == "cyan")color = nsGraphics::KCyan;
else if (colorStr == "magenta")color = nsGraphics::KMagenta;
else if (colorStr == "silver")color = nsGraphics::KSilver;
else if (colorStr == "gray")color = nsGraphics::KGray;
else if (colorStr == "maroon")color = nsGraphics::KMaroon;
else if (colorStr == "olive")color = nsGraphics::KOlive;
else if (colorStr == "green")color = nsGraphics::KGreen;
else if (colorStr == "purple")color = nsGraphics::KPurple;
else if (colorStr == "teal")color = nsGraphics::KTeal;
else if (colorStr == "navy")color = nsGraphics::KNavy;
else throw runtime_error("Invalid color string : "+colorStr);
}

View File

@ -4,7 +4,7 @@
/** Displays the screen once, and returns
*
*/
void Game::display() {
void Game::display() const {
pm.drawBackground();
for (unsigned i = 0; i < this->grid.size(); ++i){
for (unsigned j = 0; j < this->grid[i].size(); ++j){
@ -16,10 +16,10 @@ void Game::display() {
}
}
for(missile& miss : missiles){
for(const missile& miss : missiles){
pm.drawMissile(miss, confData.missilesWidth, confData.missilesColor);
}
for(torpedo& tor : torpedos){
for(const torpedo& tor : torpedos){
pm.drawTorpedo(tor, confData.torpedosWidth, confData.torpedosColor);
}
@ -30,9 +30,9 @@ void Game::display() {
displayGod();
}
void Game::displayInvader(const Position& pos, unsigned size, InvaderType type){
void Game::displayInvader(const Position& pos, unsigned size, InvaderType type) const {
if(type==InvaderType::NONE)return;
InvaderTypeDef invDef = confData.invadersDef[type];
InvaderTypeDef invDef = confData.invadersDef.at(type);
switch(type){
case InvaderType::TYPEA:{
pm.drawInvaderA(pos, size, invDef.color);

View File

@ -45,13 +45,13 @@ void Game::managedGames() {
while(playMode!=PlayMode::EXIT){
if(playMode==PlayMode::NONE){
playMode = initialMenuHandler();
playMode = pm.showInitialMenu();
}else{
playGame(); // will read the playMode
handleScoreSaving();
cout << "END OF GAME" << endl;
break; // TODO remove
if(!deathMenuHandler()) playMode = PlayMode::NONE; // back to the main menu
if(!pm.showDeathMenu()) playMode = PlayMode::NONE; // back to the main menu
}
}
@ -60,31 +60,6 @@ void Game::managedGames() {
// TODO maybe directly call theses from pm and do not use getters ?
/**
* @return the mode the use chose, we he clicks it
*/
PlayMode Game::initialMenuHandler(){
switch(pm.showInitialMenu()){
case 0:{
return PlayMode::SINGLE;
}
case 1:{
return PlayMode::TWO_LOCAL;
}
case 2:{
return PlayMode::TWO_TCPIP;
}
}
return PlayMode::EXIT;
}
/**
* @return if true, the user wants to play again, same mode, false if he wants to go back to the main menu
*/
bool Game::deathMenuHandler(){
return pm.showDeathMenu();
}
/**
* Plays the game, and returns once the game is finished
*

View File

@ -201,8 +201,8 @@ bool Game::checkTorpedosAndInvaders() {
return false;
}
bool Game::invadersTouchPlayer(){
for(InvadersColumn& line : grid){
bool Game::invadersTouchPlayer() const {
for(const InvadersColumn& line : grid){
if(basePos.getY() + line.size() * confData.invadersSize >= pm.getScreenHeight() - PLAYER_HEIGHT){
return true;
}

View File

@ -6,21 +6,37 @@ void Game::manageGod(){
return;
}
case GodState::LOAD:{
++god.counter;
if(god.counter==GOD_BENCH_SIZE){
god.counter = 0;
god.state = GodState::WAIT;
}else ++god.counter;
return;
}
case GodState::WAIT: {
if (god.counter == 1000) {
// init throw
god.counter = 0;
god.state = GodState::RETRIEVE1;
unsigned rx = rand() % grid.size();
unsigned ry = rand() % grid[0].size();
god.throwedInvPos.setX(rx);
god.throwedInvPos.setY(ry);
god.thrownInvType = grid[rx][ry];
god.thrownTransition.setX(GOD_HAND_SIZE);
god.thrownTransition.setY(rx);
} else ++god.counter;
return;
}
}
}
void applyBezier(Position& pos, const Position& point, double percent){
void applyBezier(Position& pos, const Position& point, const double percent) {
pos += (point-pos)*percent;
}
void Game::displayGod() {
void Game::displayGod() const {
switch (god.state) {
case GodState::NONE:
return;
@ -39,22 +55,6 @@ void Game::displayGod() {
Position rightHand(pm.getScreenWidth()-GOD_HAND_DISTANCE-GOD_HAND_SIZE, 0);
pm.displayGodLeftHand(leftHand);
pm.displayGodRightHand(rightHand);
++god.counter;
if(god.counter==1000){
// init throw
god.counter = 0;
god.state = GodState::RETRIEVE1;
unsigned rx = rand()%grid.size();
unsigned ry = rand()%grid[0].size();
god.throwedInvPos.setX(rx);
god.throwedInvPos.setY(ry);
god.thrownInvType = grid[rx][ry];
god.thrownTransition.setX(GOD_HAND_SIZE);
god.thrownTransition.setY(rx);
}
break;
}
case GodState::RETRIEVE1:{

View File

@ -1,3 +1,4 @@
#include <playMode.h>
#include "pixelManager.h"
#include "utils.h"
@ -6,7 +7,7 @@ PixelManager::PixelManager(MinGL& a) : window(a) {
window.initGraphic();
}
void PixelManager::drawInvaderA(const Position& baseVector, unsigned size, RGBAcolor& color){
void PixelManager::drawInvaderA(const Position& baseVector, unsigned size, const RGBAcolor& color) const {
float scale = size/(float)100;
window << nsShape::Circle(Position(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KGray);
window << nsShape::Triangle(Position(35*scale, 50*scale)+baseVector, Position(15*scale, 25*scale)+baseVector, Position(15*scale, 75*scale)+baseVector, nsGraphics::KBlack);
@ -16,7 +17,7 @@ void PixelManager::drawInvaderA(const Position& baseVector, unsigned size, RGBAc
window << nsShape::Rectangle(Position(35*scale, 65*scale)+baseVector, Position(65*scale, 72*scale)+baseVector, nsGraphics::KBlack);
}
void PixelManager::drawInvaderB(const Position& baseVector, unsigned size, RGBAcolor& color){
void PixelManager::drawInvaderB(const Position& baseVector, unsigned size, const RGBAcolor& color) const {
float scale = size/(float)100;
window << nsShape::Circle(Position(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KRed);
window << nsShape::Rectangle(Position(25*scale, 30*scale)+baseVector, Position(45*scale, 40*scale)+baseVector, nsGraphics::KBlack);
@ -24,7 +25,7 @@ void PixelManager::drawInvaderB(const Position& baseVector, unsigned size, RGBAc
window << nsShape::Rectangle(Position(35*scale, 65*scale)+baseVector, Position(65*scale, 72*scale)+baseVector, nsGraphics::KBlack);
}
void PixelManager::drawInvaderC(const Position& baseVector, unsigned size, RGBAcolor& color){
void PixelManager::drawInvaderC(const Position& baseVector, unsigned size, const RGBAcolor& color) const {
float scale = size/(float)100;
window << nsShape::Circle(Position(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KGreen);
window << nsShape::Circle(Position(35*scale, 35*scale)+baseVector, 10*scale, nsGraphics::KBlack);
@ -32,7 +33,7 @@ void PixelManager::drawInvaderC(const Position& baseVector, unsigned size, RGBAc
window << nsShape::Rectangle(Position(35*scale, 65*scale)+baseVector, Position(65*scale, 72*scale)+baseVector, nsGraphics::KBlack);
}
void PixelManager::drawPlayer(const unsigned x, unsigned width, const nsGraphics::RGBAcolor& color){
void PixelManager::drawPlayer(unsigned x, unsigned width, const nsGraphics::RGBAcolor& color) const {
width = width-10-10;
width = width/2;
window << nsShape::Triangle(Position(0+x, 720), Position(5+x, 720), Position(5+x, 720-PLAYER_HEIGHT/2), color);
@ -44,55 +45,55 @@ void PixelManager::drawPlayer(const unsigned x, unsigned width, const nsGraphics
window << nsShape::Triangle(Position(15+width+x,720-PLAYER_HEIGHT/2), Position(15+width*2+x,720-PLAYER_HEIGHT/2), Position(15+width+x,720-PLAYER_HEIGHT*0.9), color);
}
void PixelManager::askPlayerNameMenu(playerID pID, string& name){
void PixelManager::askPlayerNameMenu(playerID pID, string& name) const {
cout << "ask for player " << (pID+1) << endl;
name = "Thomas";
}
void PixelManager::drawMissile(const Position& baseVector, unsigned width, const nsGraphics::RGBAcolor& color){
void PixelManager::drawMissile(const Position& baseVector, unsigned width, const nsGraphics::RGBAcolor& color) const {
window << nsShape::Rectangle(baseVector, baseVector + Position(width, width * PROJ_LENGTH_FACTOR), color);
}
void PixelManager::drawTorpedo(const Position& baseVector, unsigned width, const nsGraphics::RGBAcolor& color){
void PixelManager::drawTorpedo(const Position& baseVector, unsigned width, const nsGraphics::RGBAcolor& color) const {
window << nsShape::Rectangle(baseVector, baseVector + Position(width, width * PROJ_LENGTH_FACTOR), color);
}
void PixelManager::drawBackground(){
void PixelManager::drawBackground() const {
background.draw(window);
}
unsigned PixelManager::showInitialMenu(){
return 1;
PlayMode PixelManager::showInitialMenu() const {
return PlayMode::SINGLE;
}
unsigned PixelManager::showDeathMenu() {
return 0;
bool PixelManager::showDeathMenu() const {
return true;
}
unsigned PixelManager::getScreenHeight() {
unsigned PixelManager::getScreenHeight() const {
return window.getWindowSize().getY();
}
unsigned PixelManager::getScreenWidth() {
unsigned PixelManager::getScreenWidth() const {
return window.getWindowSize().getX();
}
void PixelManager::startFrame() {
void PixelManager::startFrame() const {
window.clearScreen();
}
void PixelManager::endFrame() {
void PixelManager::endFrame() const {
window.finishFrame();
}
void PixelManager::displayGodBench(int y) {
void PixelManager::displayGodBench(int y) const {
}
void PixelManager::displayGodRightHand(const Position& pos) {
void PixelManager::displayGodRightHand(const Position& pos) const {
}
void PixelManager::displayGodLeftHand(const Position& pos) {
void PixelManager::displayGodLeftHand(const Position& pos) const {
}

View File

@ -10,7 +10,7 @@
#define SECRET_KEY "WeAreAGroupOf3"
void readWholeFile(ifstream& ifs, string& str){
void readWholeFile(const ifstream& ifs, string& str){
stringstream ss;
ss << ifs.rdbuf();
str.assign(ss.str());
@ -18,7 +18,7 @@ void readWholeFile(ifstream& ifs, string& str){
static std::hash<string> hasher;
bool verifyHash(size_t savedHash, string& content){
bool verifyHash(size_t savedHash, const string& content){
// non-cryptographic hash, but it is part of the std, and openssl is REALLY difficult
// to use in C++ while keeping semantic, because there are no wrappers...
size_t actualHash = hasher(content+SECRET_KEY);
@ -57,11 +57,11 @@ void ScoresManager::readFile() {
}
}
void ScoresManager::writeFile() {
void ScoresManager::writeFile() const {
ofstream ofs(SCORE_FILE);
string str; // this one must be counted in the hash too
for(ScoreLink& sc : scores){
for(const ScoreLink& sc : scores){
str.append(sc.name);
str.append(",");
str.append(to_string(sc.score));

View File

@ -8,7 +8,7 @@ bool areLinesColliding(unsigned start1, unsigned end1, unsigned start2, unsigned
// if it returns true, lines are colliding. I like truth tables
}
size_t InvadersColumn::getOutterInvader(){
size_t InvadersColumn::getOutterInvader() const {
size_t i=size();
while(i>0){
--i;