Skip to content

Commit

Permalink
Merge pull request #10 from inFullMobile/develop
Browse files Browse the repository at this point in the history
Update 0.4 undo support
  • Loading branch information
noxytrux authored May 9, 2018
2 parents 69b0aab + 9d724bc commit 8031986
Show file tree
Hide file tree
Showing 24 changed files with 619 additions and 222 deletions.
25 changes: 22 additions & 3 deletions CORE/AI/AIPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ void AIPlayer::moveCurrentObjectToPoint(GameUnit *currentObject, xVec2 &destinat
//"lock" thread
action = false;

//set destination for enemy calculation
currentObject->_finalPosition = destination;

#ifdef __EMSCRIPTEN__

Expand Down Expand Up @@ -1255,6 +1257,11 @@ bool AIPlayer::repairUnitTask(std::vector<GameUnit *> & units,
}
}

if (currBase == nullptr) {

return shouldContinue;
}

//loop points
xVec2 basePos = currBase->getPosition();
bool baseFound = false;
Expand Down Expand Up @@ -1911,31 +1918,43 @@ bool AIPlayer::executeTask(std::vector<GameUnit *> & units,

case AI_BUILD_UNIT: {

std::cout<<"AI_BUILD_UNIT"<<std::endl;
shouldContinue = buildUnitTask(units, bases, map, actual, currentTask, unitsToRemove);
}
break;

case AI_DEFENSE_BASES:
case AI_DEFENSE_BASES:
case AI_ATTACK_ENEMY: {

std::cout<<"AI_ATTACK_ENEMY"<<std::endl;
shouldContinue = attackUnitTask(units, bases, map, actual, currentTask, unitsToRemove);
}
break;

case AI_REPAIR_UNIT: {

std::cout<<"AI_REPAIR_UNIT"<<std::endl;
shouldContinue = repairUnitTask(units, bases, map, actual, currentTask, unitsToRemove);
}
break;

case AI_CAPTURE_BASES: {

std::cout<<"AI_CAPTURE_BASES"<<std::endl;
shouldContinue = captureBaseTask(units, bases, map, actual, currentTask, unitsToRemove);
}
break;

case AI_STAY_UNIT:
case AI_DO_NOTHING_PRIOR:
case AI_STAY_UNIT: {

std::cout<<"AI_STAY_UNIT"<<std::endl;
}
break;

case AI_DO_NOTHING_PRIOR: {

std::cout<<"AI_DO_NOTHING_PRIOR"<<std::endl;
}
break;

default:
Expand Down
23 changes: 23 additions & 0 deletions CORE/GameLogic/GameBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ GameBase::GameBase(const std::string & path, const xVec2 & size, const xVec2 & t
_scale = 1;
}

GameBase::GameBase(const GameBase & other)
: _map(other._map)
, _baseTex(other._baseTex)
, _digitTex(other._digitTex)
, _position(other._position)
, _outputPos(other._outputPos)
, _AABB(other._AABB)
, _radius(other._radius)
, _teamID(other._teamID)
, _turnsToUnlock(other._turnsToUnlock)
, _mode(other._mode)
, _baseAction(other._baseAction)
, _uniqueBaseID(other._uniqueBaseID)
, _requestUnitID(other._requestUnitID) {

_mesh = new Mesh2d(&_baseTex);
_number = new Mesh2d(&_digitTex);

_number->Color = FGLFC;
_mesh->Color = FGLFC;
_mesh->setAnimation(_teamID, 0);
}

GameBase::~GameBase() noexcept {

_map = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion CORE/GameLogic/GameBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GameBase final : public Drawable , public AIAware {
GameBase(const std::string & path, const xVec2 & size, const xVec2 & tile, const int & teamID);
~GameBase() noexcept;

GameBase(const GameBase & other) = delete;
GameBase(const GameBase & other);
GameBase(GameBase && other) noexcept = delete;

GameBase & operator= (const GameBase & other) = delete;
Expand Down
124 changes: 118 additions & 6 deletions CORE/GameLogic/GameLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ GameLogic::GameLogic(const float &screenWidth,

GameLogic::~GameLogic() noexcept {

clearUndo();

this->teardownOpenGL();

#ifndef __EMSCRIPTEN__
Expand Down Expand Up @@ -832,6 +834,8 @@ bool GameLogic::canEndTurn() {

void GameLogic::endTurn() {

clearUndo();

_selectedUnit = nullptr;
_mainMap->cleanRoad();

Expand Down Expand Up @@ -1083,16 +1087,107 @@ void GameLogic::unitFinishMoving(GameUnit *unit) {
}
}

bool GameLogic::loadUndo(const std::string & path) {
bool GameLogic::canUndo() {

//TODO: finish this
return true;
return !_undos.empty();
}

bool GameLogic::saveUndo(const std::string & path) {
void GameLogic::undo() {

//TODO: finish this
return true;
syncToMainLoop([&](void *, GameLogic*){

this->loadUndo();
});
}

void GameLogic::loadUndo() {

if (_undos.empty()) {
return;
}

auto undo = _undos.back();

for (auto it = _units.begin(); it != _units.end(); ++it) {

delete *it;
*it = nullptr;
}

for (auto it = _bases.begin(); it != _bases.end(); ++it) {

delete *it;
*it = nullptr;
}

_playerCash = undo.cash;

_units.clear();
_bases.clear();

_units.insert(_units.end(), undo.units.begin(), undo.units.end());
_bases.insert(_bases.end(), undo.bases.begin(), undo.bases.end());

_undos.pop_back();
}

void GameLogic::saveUndo() {

moveUndo currentUndo;

currentUndo.cash = _playerCash;
currentUndo.bases.clear();
currentUndo.units.clear();

for (auto u : _units) {

currentUndo.units.push_back(new GameUnit(*u));
}

for (auto b : _bases) {

currentUndo.bases.push_back(new GameBase(*b));
}

_undos.push_back(currentUndo);

if (_undos.size() > MAX_UNDO_ALLOWED) {

auto undo = _undos.front();

for (auto unit : undo.units) {

delete unit;
}

for (auto base : undo.bases) {

delete base;
}

_undos.erase(_undos.begin());
}
}

void GameLogic::clearUndo() {

for (auto undo : _undos) {

for (auto unit : undo.units) {

delete unit;
}

for (auto base : undo.bases) {

delete base;
}

undo.units.clear();
undo.bases.clear();
}

_undos.clear();
}

float GameLogic::multiplyTime() {
Expand Down Expand Up @@ -1395,6 +1490,10 @@ void GameLogic::setDirectionVec(const xVec2 & vec) {

void GameLogic::touchDownAtPoint(const xVec2 & position) {

if (_botsThinking) {
return;
}

if (_gameLoaded == false) {
return;
}
Expand Down Expand Up @@ -1499,6 +1598,8 @@ void GameLogic::touchDownAtPoint(const xVec2 & position) {

if (_mainMap->canAttackAtTestPoint()) {

saveUndo();

//cleanup old explosion
for (auto it = _explosions.begin(); it != _explosions.end(); ) {

Expand Down Expand Up @@ -1609,6 +1710,7 @@ void GameLogic::touchDownAtPoint(const xVec2 & position) {

if (readyToMove && _mainMap->isPointEqual(lastTouchedPoint, touch)) {

saveUndo();
unit->makeMove();

//set our unit that its cannot move anymore in this turn
Expand Down Expand Up @@ -2117,6 +2219,16 @@ void GameLogic::roadForUnit(GameUnit *current) {
this->unitsToFight(current);
}

void GameLogic::buildNewUnitFromBase(GameBase *base, int unitId, int remainingCash) {

saveUndo();

base->setUnitToBuild(unitId);
setPlayerCash(remainingCash);
buildUnit(base);
base = nullptr;
}

GameUnit* GameLogic::buildUnit(GameBase *base) {

xVec2 pos = base->getPosition();
Expand Down
45 changes: 33 additions & 12 deletions CORE/GameLogic/GameLogic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class GameLogic final {

static const int32_t headerID;

constexpr static const int MAX_UNDO_ALLOWED = 3;

//timer
constexpr static const float MAX_FRAME_TIME = 0.01;
float _accumulator;
Expand Down Expand Up @@ -73,8 +75,9 @@ class GameLogic final {
//only valid context is 2.0ES / 3.2Core
void Render();

bool loadUndo(const std::string & path);
bool saveUndo(const std::string & path);
//undo logic
bool canUndo();
void undo();

float multiplyTime();

Expand All @@ -94,8 +97,11 @@ class GameLogic final {
std::function<void(void *, GameBase *)> baseSelectedCallback;

//use this function after showing build menu using base recivied from `baseSelectedCallback`
void buildNewUnitFromBase(GameBase *base, int unitId, int remainingCash);

//required by AI
GameUnit* buildUnit(GameBase *base);

void setHardAI(const bool & hardAI);

int & getPlayerCash() { return _playerCash; }
Expand All @@ -107,12 +113,27 @@ class GameLogic final {

private:

void loadUndo();
void saveUndo();
void clearUndo();

void think(const float & delta);
void unitFinishMoving(GameUnit *unit);
void unitsToMerge(GameUnit *unit);
void unitsToFight(GameUnit *unit);
void roadForUnit(GameUnit *unit);

bool loadLogic(const std::string & logicPath);
bool loadMap(const std::string & mapPath);

void setupOpenGL();
void teardownOpenGL();

void sortUnits();
void proceedBotsLogic();

private:

struct unitdata {
xVec2 pos;
int32_t TEAM_ID;
Expand Down Expand Up @@ -147,15 +168,6 @@ class GameLogic final {
float scale;
};

bool loadLogic(const std::string & logicPath);
bool loadMap(const std::string & mapPath);

void setupOpenGL();
void teardownOpenGL();

void sortUnits();
void proceedBotsLogic();

float _screenWidth;
float _screenHeight;

Expand Down Expand Up @@ -203,4 +215,13 @@ class GameLogic final {
GameUnit *_selectedUnit;
bool _hardAI;
bool _botsThinking;

struct moveUndo {
int cash;
std::vector<GameUnit *> units;
std::vector<GameBase *> bases;
};

std::vector<moveUndo> _undos;
};

Loading

0 comments on commit 8031986

Please sign in to comment.