improved console command logic

This commit is contained in:
devZoGok
2021-12-11 08:24:30 +02:00
parent 366df7c343
commit 7cbd1b3880
7 changed files with 200 additions and 118 deletions
+78 -64
View File
@@ -3,6 +3,8 @@
#include <cmath>
#include <vector.h>
#include <stateManager.h>
#include "consoleCommand.h"
#include "unitData.h"
#include "aircraftCarrier.h"
@@ -11,6 +13,8 @@
#include "submarine.h"
#include "demoJet.h"
#include "missileJet.h"
#include "inGameAppState.h"
#include "unitData.h"
using namespace std;
using namespace vb01;
@@ -19,79 +23,87 @@ using namespace vb01Gui;
namespace battleship{
using namespace unitData;
ConsoleCommand::ConsoleCommand(Listbox *l, vector<Player*> players, string name, vector<string> args) {
listbox = l;
this->players = players;
this->name = name;
arguments = args;
execute();
}
void ConsoleCommand::execute(string commandStr) {
vector<string> fullCommand;
parse(commandStr, fullCommand);
ConsoleCommand::~ConsoleCommand() {
}
void ConsoleCommand::execute() {
if (name == commandList[0])
addUnit();
else if (name == commandList[1])
debugUnits();
if (fullCommand[0] == "add-unit") {
validateAddUnitArguments(fullCommand);
executeAddUnit(atoi(fullCommand[1].c_str()), atoi(fullCommand[2].c_str()));
}
else
printConsoleMessage("No such command");
}
void ConsoleCommand::addUnit() {
int unitId = 0, playerId = 0;
Vector3 pos(0, 0, 0);
float angle = 0;
void ConsoleCommand::parse(string commandStr, vector<string> &fullCommand){
vector<int> spaceIds;
for (int i = 0; i < arguments.size(); i++) {
arguments[i].erase(0);
for (int i2 = 0; i2 < arguments[i].size(); i2++) {
if (i == 0)
playerId += (arguments[i][i2] - 48) * pow(10, arguments[i].size() - (i2 + 1));
else
unitId += (arguments[i][i2] - 48) * pow(10, arguments[i].size() - (i2 + 1));
}
for(int i = 0; i < commandStr.length(); i++)
if(commandStr[i] == ' ')
spaceIds.push_back(i);
fullCommand.push_back(commandStr.substr(0, spaceIds[0]));
for(int i = 0; i < spaceIds.size(); i++){
bool lastSpace = (i == spaceIds.size() - 1);
string argument = commandStr.substr(spaceIds[i] + 1, lastSpace ? string::npos : spaceIds[i + 1] - spaceIds[i] - 1);
fullCommand.push_back(argument);
}
}
void ConsoleCommand::validateAddUnitArguments(vector<string> &fullCommand){
//addUnit <playerId> <unitId> [<posX> <posY> <posZ> ]
if(fullCommand.size() != 3 || fullCommand.size() != 6)
return;
int playerId = atoi(fullCommand[1].c_str());
if(playerId != 0 && playerId != 1)
return;
int unitId = atoi(fullCommand[2].c_str());
if(!(0 <= unitId && unitId <= unitData::numberOfUnits))
return;
}
void ConsoleCommand::executeAddUnit(int playerId, int unitId) {
StateManager *stateManager = GameManager::getSingleton()->getStateManager();
InGameAppState *inGameState = (InGameAppState*)stateManager->getAppStateByType(AppStateType::IN_GAME_STATE);
vector<Player*> players = inGameState->getPlayers();
Vector3 pos(0, 0, 0);
Unit *u = nullptr;
switch (unitType[unitId]) {
case UNIT_TYPE::VESSEL:
u = new Vessel(players[playerId], pos, unitId);
break;
case UNIT_TYPE::DESTROYER:
u = new Destroyer(players[playerId], pos, unitId);
break;
case UNIT_TYPE::CRUISER:
u = new Cruiser(players[playerId], pos, unitId);
break;
case UNIT_TYPE::SUBMARINE:
u = new Submarine(players[playerId], pos, unitId);
break;
case UNIT_TYPE::AIRCRAFT_CARRIER:
u = new AircraftCarrier(players[playerId], pos, unitId);
break;
case UNIT_TYPE::MISSILE_JET:
u = new MissileJet(players[playerId], pos, unitId, false);
break;
case UNIT_TYPE::DEMO_JET:
u = new DemoJet(players[playerId], pos, unitId, false);
break;
default:
break;
}
if ((playerId == 0 || playerId == 1) && (unitId >= 0 && unitId <= numberOfUnits)) {
Unit *u = nullptr;
switch (unitType[unitId]) {
case UNIT_TYPE::VESSEL:
u = new Vessel(players[playerId],pos, unitId);
break;
case UNIT_TYPE::DESTROYER:
u = new Destroyer(players[playerId], pos, unitId);
break;
case UNIT_TYPE::CRUISER:
u = new Cruiser(players[playerId],pos, unitId);
break;
case UNIT_TYPE::SUBMARINE:
u = new Submarine(players[playerId], pos, unitId);
break;
case UNIT_TYPE::AIRCRAFT_CARRIER:
{
u = new AircraftCarrier(players[playerId],pos, unitId);
break;
}
case UNIT_TYPE::MISSILE_JET:
u = new MissileJet(players[playerId],pos, unitId, false);
break;
case UNIT_TYPE::DEMO_JET:
u = new DemoJet(players[playerId],pos, unitId, false);
break;
default:
break;
}
players[playerId]->addUnit(u);
} else if (playerId != 0 && playerId != 1)
printConsoleMessage("Invalid player id");
else if (unitId < 0 || unitId > numberOfUnits)
printConsoleMessage("Invalid unit id");
players[playerId]->addUnit(u);
}
/*
void ConsoleCommand::debugUnits() {
if (arguments.size() == 0)
for (Player *pl : players){
@@ -108,8 +120,10 @@ namespace battleship{
else
printConsoleMessage("Too many arguments");
}
*/
void ConsoleCommand::printConsoleMessage(string message) {
/*
int messageId = 0;
bool foundEmptySlot = false;
@@ -118,7 +132,7 @@ namespace battleship{
messageId = i;
foundEmptySlot = true;
}
/*
if (foundEmptySlot)
listbox->changeLine(messageId, message);
else