scores + config

This commit is contained in:
Thomas 2022-01-02 18:33:46 +01:00
parent ff258e6aeb
commit c6d3b022bc
No known key found for this signature in database
GPG Key ID: E538821A6CDFDAD7
7 changed files with 70 additions and 30 deletions

View File

@ -24,6 +24,17 @@ invaders:
speed: 0
distance: 10 # distance in pixels between invaders
typeA:
points: 100
color: green
typeB:
points: 250
color: blue
typeC:
points: 600
color: red
# Projectiles config
projectiles:
missiles:
@ -35,12 +46,7 @@ projectiles:
speed: 10
width: 10
points:
invader3: 100
invader2: 250
invader1: 600
grid:
- "AAAAAAAAAA"
- " BBB BBB "
- "CCCCCCCCCC"
- " AAAAAAAA"
- "B BBBBBBBB"
- " CCCCCCCC"

View File

@ -4,6 +4,7 @@
#include<string>
#include "utils.h"
#include "playerDef.h"
#include "invaderDef.h"
typedef string configKey;
@ -24,6 +25,8 @@ struct ConfigData {
unsigned invadersDistance;
unsigned invadersFireCooldown;
map<InvaderType, InvaderTypeDef> invadersDef;
unsigned missilesWidth;
unsigned missilesLength; // auto defined from width
unsigned missilesSpeed;

12
headers/invaderDef.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef GUARD_INVADERDEF_H
#define GUARD_INVADERDEF_H
#include "mingl/graphics/rgbacolor.h"
#include "utils.h"
struct InvaderTypeDef {
nsGraphics::RGBAcolor color;
unsigned points;
};
#endif

View File

@ -16,13 +16,13 @@ enum class WinValue{
using namespace std;
enum class Invader {
enum class InvaderType {
TYPEA,
TYPEB,
TYPEC,
NONE,
};
typedef vector<Invader> InvadersColumn;
typedef vector<InvaderType> InvadersColumn;
typedef vector<InvadersColumn> InvadersGrid;
typedef nsGraphics::Vec2D Position;
typedef unsigned playerID;

View File

@ -14,6 +14,10 @@ private:
int getInt(const configKey& key);
nsGraphics::RGBAcolor getColor(const configKey& key);
void getList(const configKey& key, vector<string>&);
void readGrid(const configKey& key);
void readPlayer(const configKey& baseKey, PlayerDef&);
void readInvaderType(const configKey& baseKey, InvaderTypeDef&);
};
void ConfigBuilder::dumpInternalValues(){
@ -98,8 +102,7 @@ void ConfigBuilder::parseFile(const string& fname) {
file.close();
}
void ConfigBuilder::readConfig() {
void ConfigBuilder::readGrid(const configKey& key){
vector<string> tmp;
getList("grid", tmp);
@ -116,19 +119,19 @@ void ConfigBuilder::readConfig() {
for(;i<s.size();++i){
switch(toupper(s[i])){
case 'A':{
collectedData.grid[i].push_back(Invader::TYPEA);
collectedData.grid[i].push_back(InvaderType::TYPEA);
break;
}
case 'B':{
collectedData.grid[i].push_back(Invader::TYPEB);
collectedData.grid[i].push_back(InvaderType::TYPEB);
break;
}
case 'C':{
collectedData.grid[i].push_back(Invader::TYPEC);
collectedData.grid[i].push_back(InvaderType::TYPEC);
break;
}
case ' ':{
collectedData.grid[i].push_back(Invader::NONE);
collectedData.grid[i].push_back(InvaderType::NONE);
break;
}
default:{
@ -137,10 +140,27 @@ void ConfigBuilder::readConfig() {
}
}
while(i<maxSize){
collectedData.grid[i].push_back(Invader::NONE);
collectedData.grid[i].push_back(InvaderType::NONE);
++i;
}
}
}
void ConfigBuilder::readPlayer(const configKey& baseKey, PlayerDef& pdef){
pdef.color = getColor(baseKey+".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){
invDef.points = getInt(baseKey+".points");
invDef.color = getColor(baseKey+".color");
}
void ConfigBuilder::readConfig() {
readGrid("grid");
// players
collectedData.playersWidth = getInt("players.width");
@ -151,15 +171,8 @@ void ConfigBuilder::readConfig() {
// the scalability behind the vector of players is only an illusion, because we force player count to be 1 or 2
// It was done so the 2+ players implementation could be easier in the future, if wanted
collectedData.playerDefs.resize(2);
collectedData.playerDefs[0].color = getColor("players.user1.color");
collectedData.playerDefs[0].keys.left = getChar("players.user1.keys.left");
collectedData.playerDefs[0].keys.right = getChar("players.user1.keys.right");
collectedData.playerDefs[0].keys.shoot = getChar("players.user1.keys.shoot");
collectedData.playerDefs[1].color = getColor("players.user2.color");
collectedData.playerDefs[1].keys.left = getChar("players.user2.keys.left");
collectedData.playerDefs[1].keys.right = getChar("players.user2.keys.right");
collectedData.playerDefs[1].keys.shoot = getChar("players.user2.keys.shoot");
readPlayer("players.user1", collectedData.playerDefs[0]);
readPlayer("players.user2", collectedData.playerDefs[1]);
// invaders
collectedData.invadersSize = getInt("invaders.size");
@ -167,6 +180,10 @@ void ConfigBuilder::readConfig() {
collectedData.invadersDistance = getInt("invaders.distance");
collectedData.invadersFireCooldown = getInt("invaders.fireCooldown");
readInvaderType("invaders.typeA", collectedData.invadersDef[InvaderType::TYPEA]);
readInvaderType("invaders.typeB", collectedData.invadersDef[InvaderType::TYPEB]);
readInvaderType("invaders.typeC", collectedData.invadersDef[InvaderType::TYPEC]);
// projectiles
collectedData.missilesWidth = getInt("projectiles.missiles.width");
collectedData.missilesLength = collectedData.missilesWidth*PROJ_LENGTH_FACTOR;

View File

@ -119,15 +119,15 @@ void Game::display() {
basePos.getY() + j * confData.invadersSize + j * confData.invadersDistance
);
switch(grid[i][j]){
case Invader::TYPEA:{
case InvaderType::TYPEA:{
pm.drawInvader1(vec, confData.invadersSize);
break;
}
case Invader::TYPEB:{
case InvaderType::TYPEB:{
pm.drawInvader2(vec, confData.invadersSize);
break;
}
case Invader::TYPEC:{
case InvaderType::TYPEC:{
pm.drawInvader3(vec, confData.invadersSize);
break;
}

View File

@ -188,7 +188,9 @@ bool Game::checkTorpedosAndInvaders() {
}
if(i==grid.size()) ++tor_ite;
else{
players[tor_ite->owner].score += 100;
// compute points associated with invaders
InvaderType invType = grid[i][grid[i].size()];
players[tor_ite->owner].score += confData.invadersDef[invType].points;
torpedos.erase(tor_ite);
grid[i].pop_back();
}