shit commit bc Djalim needs to work
This commit is contained in:
parent
5a3f51fa27
commit
11318bc09c
@ -1,7 +1,7 @@
|
|||||||
cmake_minimum_required(VERSION 3.18)
|
cmake_minimum_required(VERSION 3.18)
|
||||||
project(Space)
|
project(Space)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
file(GLOB_RECURSE SRC src/*)
|
file(GLOB_RECURSE SRC src/*)
|
||||||
file(GLOB_RECURSE LIB_HEADERS lib_headers/*)
|
file(GLOB_RECURSE LIB_HEADERS lib_headers/*)
|
||||||
|
29
config.yaml
29
config.yaml
@ -1,29 +0,0 @@
|
|||||||
# Players config
|
|
||||||
players:
|
|
||||||
width: 250
|
|
||||||
startPositionX: 0
|
|
||||||
speed: 5
|
|
||||||
user1_keys:
|
|
||||||
moveLeft: '4'
|
|
||||||
moveRight: '6'
|
|
||||||
shoot: '5'
|
|
||||||
user2_keys:
|
|
||||||
moveLeft: 'k'
|
|
||||||
moveRight: 'l'
|
|
||||||
shoot: 'm'
|
|
||||||
|
|
||||||
|
|
||||||
# Enemies config
|
|
||||||
enemies:
|
|
||||||
startPositionX: 0
|
|
||||||
startPositionY: 0
|
|
||||||
speed: 2
|
|
||||||
size: 4
|
|
||||||
distance: 10 # distance pixels between invaders
|
|
||||||
|
|
||||||
missiles:
|
|
||||||
speed: 2
|
|
||||||
width: 10
|
|
||||||
torpedos:
|
|
||||||
speed: 2
|
|
||||||
width: 10
|
|
33
config.yml
Normal file
33
config.yml
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
# Players config
|
||||||
|
players:
|
||||||
|
width: 250
|
||||||
|
startXPosition: 0
|
||||||
|
speed: 1
|
||||||
|
user1:
|
||||||
|
color: red
|
||||||
|
keys:
|
||||||
|
left: 4
|
||||||
|
right: 6
|
||||||
|
shoot: 5
|
||||||
|
user2:
|
||||||
|
color: blue
|
||||||
|
keys:
|
||||||
|
left: q
|
||||||
|
right: d
|
||||||
|
shoot: s
|
||||||
|
|
||||||
|
|
||||||
|
# Enemies config
|
||||||
|
invaders:
|
||||||
|
size: 4
|
||||||
|
speed: 2
|
||||||
|
distance: 10 # distance in pixels between invaders
|
||||||
|
|
||||||
|
# Projectiles config
|
||||||
|
projectiles:
|
||||||
|
missiles:
|
||||||
|
speed: 2
|
||||||
|
width: 10
|
||||||
|
torpedos:
|
||||||
|
speed: 2
|
||||||
|
width: 10
|
@ -2,6 +2,8 @@
|
|||||||
#define GUARD_CONFIG_H
|
#define GUARD_CONFIG_H
|
||||||
|
|
||||||
#include<vector>
|
#include<vector>
|
||||||
|
#include<map>
|
||||||
|
#include<string>
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "player_def.h"
|
#include "player_def.h"
|
||||||
|
|
||||||
@ -13,26 +15,32 @@ class Config{
|
|||||||
public:
|
public:
|
||||||
aliensGrid grid;
|
aliensGrid grid;
|
||||||
|
|
||||||
unsigned alien_size;
|
unsigned invadersSize;
|
||||||
unsigned distance;
|
unsigned invadersDistance;
|
||||||
|
|
||||||
unsigned missile_width;
|
unsigned missilesWidth;
|
||||||
unsigned missile_length; // auto defined from width
|
unsigned missilesLength; // auto defined from width
|
||||||
unsigned torpedo_width;
|
unsigned torpedosWidth;
|
||||||
unsigned torpedo_length; // auto defined from width
|
unsigned torpedosLength; // auto defined from width
|
||||||
unsigned player_width;
|
unsigned playersWidth;
|
||||||
|
|
||||||
unsigned alien_speed;
|
unsigned invadersSpeed;
|
||||||
unsigned player_speed;
|
unsigned playersSpeed;
|
||||||
|
|
||||||
PlayerDef p1Def;
|
PlayerDef p1Def;
|
||||||
PlayerDef p2Def;
|
PlayerDef p2Def;
|
||||||
unsigned startXPosition;
|
unsigned startXPosition;
|
||||||
|
|
||||||
|
bool reloadConfig();
|
||||||
|
string* getString(const configKey& key);
|
||||||
|
char getChar(const configKey& key);
|
||||||
|
int getInt(const configKey& key);
|
||||||
|
nsGraphics::RGBAcolor getColor(const configKey& key);
|
||||||
|
|
||||||
|
private:
|
||||||
|
map<string, string> internalValues;
|
||||||
|
bool parseConfig();
|
||||||
bool loadConfig();
|
bool loadConfig();
|
||||||
string getString(configKey& key);
|
|
||||||
int getInt(configKey& key);
|
|
||||||
nsGraphics::RGBAcolor getColor(configKey& key);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
112
src/config.cpp
112
src/config.cpp
@ -1,38 +1,120 @@
|
|||||||
|
#include <fstream>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return false if there was an error
|
||||||
|
*/
|
||||||
|
bool Config::reloadConfig() {
|
||||||
|
internalValues.clear();
|
||||||
|
if(!parseConfig())return false;
|
||||||
|
if(!loadConfig()) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void trimSpaces(string& str){
|
||||||
|
str.erase(0, str.find_first_not_of(' '));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Config::parseConfig() {
|
||||||
|
ifstream file("config.yml");
|
||||||
|
if(!file.is_open())throw runtime_error("Error while opening config.yml. Check file location ?");
|
||||||
|
|
||||||
|
vector<string> keyParts;
|
||||||
|
while (!file.eof()) {
|
||||||
|
string line;
|
||||||
|
getline(file, line);
|
||||||
|
auto match = line.find('#');
|
||||||
|
if (match != string::npos)line.erase(match);
|
||||||
|
if (line.empty())continue;
|
||||||
|
|
||||||
|
unsigned currentIndent = 0;
|
||||||
|
while (line[currentIndent] == ' ')++currentIndent;
|
||||||
|
|
||||||
|
|
||||||
|
match = line.find(':');
|
||||||
|
if (match == string::npos)throw runtime_error("Invalid line : " + line);
|
||||||
|
string key = line.substr(0, match);
|
||||||
|
string value = line.substr(match + 1);
|
||||||
|
trimSpaces(key);
|
||||||
|
trimSpaces(value);
|
||||||
|
if (value.empty()) {
|
||||||
|
keyParts.resize(currentIndent);
|
||||||
|
keyParts.push_back(key);
|
||||||
|
} else {
|
||||||
|
string fullKey;
|
||||||
|
for (unsigned i = 0; i < currentIndent; ++i) {
|
||||||
|
fullKey.append(keyParts[i]);
|
||||||
|
fullKey.append(".");
|
||||||
|
}
|
||||||
|
fullKey.append(key);
|
||||||
|
internalValues[fullKey] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool Config::loadConfig() {
|
bool Config::loadConfig() {
|
||||||
|
// TODO PUT THAT IN CONFIG ??
|
||||||
#define S 48
|
#define S 48
|
||||||
grid.resize(S);
|
grid.resize(S);
|
||||||
for(unsigned i=0;i<S;++i){
|
for(unsigned i=0;i<S;++i){
|
||||||
grid[i].resize(3);
|
grid[i].resize(3);
|
||||||
for(unsigned j=0;j<3;++j)grid[i][j] = j;
|
for(unsigned j=0;j<3;++j)grid[i][j] = j;
|
||||||
}
|
}
|
||||||
alien_size = 20;
|
|
||||||
distance = 5;
|
|
||||||
alien_speed = 1;
|
|
||||||
player_speed = 2;
|
|
||||||
|
|
||||||
missile_width = 10;
|
// players
|
||||||
missile_length = 15;
|
playersWidth = getInt("players.width");
|
||||||
torpedo_width = 10;
|
startXPosition = getInt("players.startXPosition");
|
||||||
torpedo_length = 15;
|
playersSpeed = getInt("players.speed");
|
||||||
|
|
||||||
player_width = 1000;
|
p1Def.color = getColor("players.user1.color");
|
||||||
startXPosition = 0;
|
p1Def.keys.left = getChar("players.user1.keys.left");
|
||||||
p1Def.color = nsGraphics::KTeal;
|
p1Def.keys.right = getChar("players.user1.keys.right");
|
||||||
|
p1Def.keys.shoot = getChar("players.user1.keys.shoot");
|
||||||
|
|
||||||
|
p2Def.color = getColor("players.user2.color");
|
||||||
|
p2Def.keys.left = getChar("players.user2.keys.left");
|
||||||
|
p2Def.keys.right = getChar("players.user2.keys.right");
|
||||||
|
p2Def.keys.shoot = getChar("players.user2.keys.shoot");
|
||||||
|
|
||||||
|
// invaders
|
||||||
|
invadersSize = getInt("invaders.size");
|
||||||
|
invadersSpeed = getInt("invaders.speed");
|
||||||
|
invadersDistance = getInt("invaders.distance");
|
||||||
|
|
||||||
|
// projectiles
|
||||||
|
missilesWidth = getInt("projectiles.missiles.width");
|
||||||
|
missilesLength = missilesWidth*PROJ_LENGTH_FACTOR;
|
||||||
|
|
||||||
|
torpedosWidth = getInt("projectiles.torpedos.width");
|
||||||
|
torpedosLength = torpedosWidth*PROJ_LENGTH_FACTOR;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Config::getInt(configKey& key) {
|
int Config::getInt(const configKey& key) {
|
||||||
|
return stoi(*getString(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
string Config::getString(configKey& key) {
|
char Config::getChar(const configKey& key) {
|
||||||
|
return getString(key)->at(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
nsGraphics::RGBAcolor Config::getColor(configKey& key) {
|
string* Config::getString(const configKey& key) {
|
||||||
|
if(internalValues.contains(key)){
|
||||||
|
return &internalValues.at(key);
|
||||||
|
}else{
|
||||||
|
cerr << "Non-existent key requested : " << key << endl;
|
||||||
|
return nullptr; // invalid reference returned,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nsGraphics::RGBAcolor Config::getColor(const configKey& key) {
|
||||||
// switch do not work with strings, and I don't want to implement a constexpr hash function
|
// switch do not work with strings, and I don't want to implement a constexpr hash function
|
||||||
string colorStr = getString(key);
|
string colorStr = *getString(key);
|
||||||
if (colorStr == "black")return nsGraphics::KBlack;
|
if (colorStr == "black")return nsGraphics::KBlack;
|
||||||
else if (colorStr == "white")return nsGraphics::KWhite;
|
else if (colorStr == "white")return nsGraphics::KWhite;
|
||||||
else if (colorStr == "red")return nsGraphics::KRed;
|
else if (colorStr == "red")return nsGraphics::KRed;
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
|
|
||||||
Game::Game() : WININIT, pm(window) {
|
Game::Game() : WININIT, pm(window) {
|
||||||
conf.loadConfig();
|
conf.reloadConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::managedGames() {
|
void Game::managedGames() {
|
||||||
@ -52,7 +52,7 @@ bool Game::deathMenuHandler(){
|
|||||||
|
|
||||||
bool Game::invadersTouchPlayer(){
|
bool Game::invadersTouchPlayer(){
|
||||||
for(aliensLine& line : grid){
|
for(aliensLine& line : grid){
|
||||||
if(basePos.getY()+line.size()*conf.alien_size>=pm.getScreenHeight()-PLAYER_HEIGHT){
|
if(basePos.getY()+line.size()*conf.invadersSize >= pm.getScreenHeight() - PLAYER_HEIGHT){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -73,7 +73,7 @@ WinValue Game::playGame(){ // returns when game is finished
|
|||||||
|
|
||||||
if(playMode!=SINGLE){
|
if(playMode!=SINGLE){
|
||||||
// mirror the start X position for the other
|
// mirror the start X position for the other
|
||||||
p2.x = pm.getScreenWidth()-conf.startXPosition-conf.player_width;
|
p2.x = pm.getScreenWidth()-conf.startXPosition-conf.playersWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
basePos = position(0,0);
|
basePos = position(0,0);
|
||||||
@ -112,26 +112,25 @@ void Game::display() {
|
|||||||
for (unsigned i = 0; i < this->grid.size(); ++i){
|
for (unsigned i = 0; i < this->grid.size(); ++i){
|
||||||
for (unsigned j = 0; j < this->grid[i].size(); ++j){
|
for (unsigned j = 0; j < this->grid[i].size(); ++j){
|
||||||
nsGraphics::Vec2D vec =nsGraphics::Vec2D(
|
nsGraphics::Vec2D vec =nsGraphics::Vec2D(
|
||||||
basePos.getX()+i*conf.alien_size+i*conf.distance,
|
basePos.getX() +i*conf.invadersSize + i * conf.invadersDistance,
|
||||||
basePos.getY()+j*conf.alien_size+j*conf.distance
|
basePos.getY() +j*conf.invadersSize + j * conf.invadersDistance
|
||||||
);
|
);
|
||||||
switch(grid[i][j]){
|
switch(grid[i][j]){
|
||||||
case 0:{
|
case 0:{
|
||||||
pm.dessinerInvader1(vec, conf.alien_size);
|
pm.dessinerInvader1(vec, conf.invadersSize);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 1:{
|
case 1:{
|
||||||
pm.dessinerInvader2(vec, conf.alien_size);
|
pm.dessinerInvader2(vec, conf.invadersSize);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 2:{
|
case 2:{
|
||||||
pm.dessinerInvader3(vec, conf.alien_size);
|
pm.dessinerInvader3(vec, conf.invadersSize);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cout << p1.x << endl;
|
pm.dessinerJoueur(position(p1.x, 0), conf.playersWidth, conf.p1Def.color);
|
||||||
pm.dessinerJoueur(position(p1.x, 0), conf.player_width, conf.p1Def.color);
|
if(playMode!=SINGLE)pm.dessinerJoueur(position(p2.x, 0), conf.playersWidth, conf.p2Def.color);
|
||||||
if(playMode!=SINGLE)pm.dessinerJoueur(position(p2.x, 0), conf.player_width, conf.p2Def.color);
|
|
||||||
}
|
}
|
@ -8,15 +8,15 @@
|
|||||||
|
|
||||||
void Game::managePlayerMoves(PlayerDef& pdef, unsigned& playerX){
|
void Game::managePlayerMoves(PlayerDef& pdef, unsigned& playerX){
|
||||||
if (ISPRESSED(pdef.keys.left)){
|
if (ISPRESSED(pdef.keys.left)){
|
||||||
if(playerX < conf.player_speed) playerX = 0;
|
if(playerX < conf.playersSpeed) playerX = 0;
|
||||||
else playerX = playerX-conf.player_speed;
|
else playerX = playerX-conf.playersSpeed;
|
||||||
}
|
}
|
||||||
if (ISPRESSED(pdef.keys.right)){
|
if (ISPRESSED(pdef.keys.right)){
|
||||||
if(playerX+conf.player_speed>=pm.getScreenWidth()) playerX = pm.getScreenWidth()-1;
|
if(playerX+conf.playersSpeed >= pm.getScreenWidth()) playerX = pm.getScreenWidth() - 1;
|
||||||
else playerX = playerX+conf.player_speed;
|
else playerX = playerX+conf.playersSpeed;
|
||||||
}
|
}
|
||||||
if(ISPRESSED(pdef.keys.shoot)){
|
if(ISPRESSED(pdef.keys.shoot)){
|
||||||
torpedos.emplace_back(playerX+conf.player_width/2, pm.getScreenHeight()-PLAYER_HEIGHT);
|
torpedos.emplace_back(playerX+ conf.playersWidth / 2, pm.getScreenHeight() - PLAYER_HEIGHT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,23 +34,23 @@ void Game::managePlayers(){
|
|||||||
bool Game::manageInvaders(){
|
bool Game::manageInvaders(){
|
||||||
if(direction){ // go to the right
|
if(direction){ // go to the right
|
||||||
int end = basePos.getX(); // start position
|
int end = basePos.getX(); // start position
|
||||||
end+= grid.size()*conf.alien_size; // add the aliens
|
end+= grid.size()*conf.invadersSize; // add the aliens
|
||||||
end+= (grid.size()-1)*conf.distance; // add the distance between aliens
|
end+= (grid.size()-1)*conf.invadersDistance; // add the invadersDistance between aliens
|
||||||
|
|
||||||
// you got the end position of the alien crowd !
|
// you got the end position of the alien crowd !
|
||||||
|
|
||||||
if(end+conf.alien_speed<pm.getScreenWidth()){
|
if(end+conf.invadersSpeed < pm.getScreenWidth()){
|
||||||
basePos.setX(basePos.getX()+conf.alien_speed);
|
basePos.setX(basePos.getX()+conf.invadersSpeed);
|
||||||
}else{
|
}else{
|
||||||
basePos.setY(basePos.getY()+conf.alien_size);
|
basePos.setY(basePos.getY()+conf.invadersSize);
|
||||||
direction = !direction;
|
direction = !direction;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
if(basePos.getX()>=conf.alien_speed){
|
if(basePos.getX()>=conf.invadersSpeed){
|
||||||
basePos.setX(basePos.getX()-conf.alien_speed);
|
basePos.setX(basePos.getX()-conf.invadersSpeed);
|
||||||
}else{
|
}else{
|
||||||
basePos.setY(basePos.getY()+conf.alien_size);
|
basePos.setY(basePos.getY()+conf.invadersSize);
|
||||||
direction = !direction;
|
direction = !direction;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -73,12 +73,12 @@ void Game::remCollidingProjectiles(){
|
|||||||
while(tor != torpedos.end()){
|
while(tor != torpedos.end()){
|
||||||
|
|
||||||
// missiles can't be right under torpedos, so that must means they are colliding in Y
|
// missiles can't be right under torpedos, so that must means they are colliding in Y
|
||||||
if(miss->getY()+conf.missile_length<tor->getY()){
|
if(miss->getY()+conf.missilesLength < tor->getY()){
|
||||||
|
|
||||||
}
|
}
|
||||||
if(lineCollideCheck( // now check if they collide in X
|
if(lineCollideCheck( // now check if they collide in X
|
||||||
miss->getX(), miss->getX()+conf.missile_width,
|
miss->getX(), miss->getX()+conf.missilesWidth,
|
||||||
tor->getX(), tor->getX()+conf.torpedo_width)){
|
tor->getX(), tor->getX()+conf.torpedosWidth)){
|
||||||
missiles.erase(miss);
|
missiles.erase(miss);
|
||||||
torpedos.erase(tor);
|
torpedos.erase(tor);
|
||||||
wasColling = true;
|
wasColling = true;
|
||||||
@ -119,8 +119,8 @@ bool Game::checkMissilesAndPlayers() {
|
|||||||
for(missile& miss : missiles){
|
for(missile& miss : missiles){
|
||||||
if(miss.getY()<=PLAYER_HEIGHT){ // colliding on Y
|
if(miss.getY()<=PLAYER_HEIGHT){ // colliding on Y
|
||||||
if(lineCollideCheck( // now check collision on X
|
if(lineCollideCheck( // now check collision on X
|
||||||
miss.getX(), miss.getX()+conf.missile_width,
|
miss.getX(), miss.getX()+conf.missilesWidth,
|
||||||
p1.x, p1.x+conf.player_width)){
|
p1.x, p1.x+conf.playersWidth)){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user