Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
yepMad committed Nov 30, 2021
2 parents 0e19acf + 1b23c23 commit d51b601
Show file tree
Hide file tree
Showing 219 changed files with 53,332 additions and 7 deletions.
230 changes: 230 additions & 0 deletions ALL_BUILD.vcxproj

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions ALL_BUILD.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<CustomBuild Include="C:\Users\eudes\Desktop\Manifesto\Minesweeper-Cpp\CMakeLists.txt" />
</ItemGroup>
<ItemGroup>
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ list(APPEND GAME_HEADER
Classes/GameScene.h
Classes/BoardMap.h
Classes/Mine.h
Classes/json.hpp
)

if(ANDROID)
Expand Down
90 changes: 84 additions & 6 deletions Classes/GameScene.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include "GameScene.h"

#include <CCUserDefault.h>
#include <FilesPaths.h>
#include <iomanip>

#include "ui/UIButton.h"
#include <json.hpp>

using json = nlohmann::json;

USING_NS_CC;

Expand All @@ -24,7 +28,8 @@ bool GameScene::init() {
return false;
}

boardMap.initData(18, 14, 40);
const auto gameData = getGameData();
boardMap.initData(gameData.width, gameData.height, gameData.qtyMines);
boardMap.drawMap(this);

this->enableHeaderItems();
Expand Down Expand Up @@ -61,6 +66,19 @@ void GameScene::enableHeaderItems() {
to_string(boardMap.qtyBombs),
icon_flag,
Vec2(VISIBLE_SIZE.width - 100, VISIBLE_SIZE.height - 60));

// Easy button
const auto easyButton = createDifficultyButton("Fácil");
easyButton->setPosition(Vec2(60,VISIBLE_SIZE.height - 105));
easyButton->addTouchEventListener(CC_CALLBACK_0(GameScene::setEasyDifficulty, this));

// Medium
const auto mediumButton = createDifficultyButton("Médio");
mediumButton->setPosition(Vec2(60,VISIBLE_SIZE.height - 135));
mediumButton->addTouchEventListener(CC_CALLBACK_0(GameScene::setMediumDifficulty, this));

this->addChild(easyButton);
this->addChild(mediumButton);
}

void GameScene::clockTick() {
Expand All @@ -80,13 +98,13 @@ void GameScene::win() {
const auto restartButton = createRestartButton();
const auto labelText = Label::createWithTTF("VOCÊ GANHOU!", font_arial, 20);

labelText->setColor(Color3B::GREEN);
labelText->setPosition(Vec2(VISIBLE_SIZE.width / 2,
timerHeaderItem->getPosition().y));

this->addChild(labelText, 2);
this->addChild(restartButton, 3);

labelText->setColor(Color3B::MAGENTA);
labelText->setPosition(Vec2(VISIBLE_SIZE.width / 2,
VISIBLE_SIZE.height - 50));

this->unschedule(CLOCK_TICK_SCHEDULE_KEY);
}

Expand All @@ -104,11 +122,21 @@ void GameScene::gameOver() {
this->unschedule(CLOCK_TICK_SCHEDULE_KEY);
}

void GameScene::restart() {
void GameScene::restart() const {
_eventDispatcher->removeAllEventListeners();
Director::getInstance()->replaceScene(TransitionFade::create(1, createScene()));
}

void GameScene::setEasyDifficulty() {
setGameData(10, 8, 10);
restart();
}

void GameScene::setMediumDifficulty() {
setGameData(18, 14, 40);
restart();
}


bool GameScene::onTouchesBegan(const Touch* touch, Event* event) {
if (boardMap.hasEndGame()) {
Expand Down Expand Up @@ -190,3 +218,53 @@ Label* GameScene::createHeaderItem(const string initialText,
this->addChild(sprite);
return label;
}

ui::Button* GameScene::createDifficultyButton(const string label) const {
const auto easyButton = ui::Button::create(labels_white);
easyButton->setScale(0.5);

const auto easyText = Label::createWithTTF(label, font_arial, 20);
easyText->setColor(Color3B::BLACK);
easyText->setPosition(Vec2(easyButton->getContentSize().width / 2,
easyButton->getContentSize().height / 2));

easyButton->addChild(easyText, 2);
return easyButton;
}


GameData GameScene::getGameData() const {
const auto fs = FileUtils::getInstance();
const auto widthPath = fs->getWritablePath() + "settings.json";

if (!fs->isFileExist(widthPath)) {
constexpr auto width = 18;
constexpr auto height = 14;
constexpr auto qtyMines = 40;

setGameData(width, height, qtyMines);
return { width, height, qtyMines };
}

const auto content = fs->getStringFromFile(widthPath);
json jsonData = json::parse(content);

const auto width = std::stoi(to_string(jsonData.at("width")));
const auto height = std::stoi(to_string(jsonData.at("height")));
const auto qtyMines = std::stoi(to_string(jsonData.at("qtyMines")));

return { width, height, qtyMines };
}

void GameScene::setGameData(int width, int height, int qtyMines) {
const json jsonGameData = {
{"width", width},
{"height", height},
{"qtyMines", qtyMines},
};

const auto fs = FileUtils::getInstance();
const auto settingPath = fs->getWritablePath() + "settings.json";

const auto success = fs->writeStringToFile(jsonGameData.dump(), settingPath);
}
14 changes: 13 additions & 1 deletion Classes/GameScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@

#include "cocos2d.h"
#include "BoardMap.h"
#include "ui/UIButton.h"

#define VISIBLE_SIZE Director::getInstance()->getVisibleSize()
#define CLOCK_TICK_SCHEDULE_KEY "clock_tick"

USING_NS_CC;

struct GameData {
int width;
int height;
int qtyMines;
};

class GameScene final : public Scene {
public:
static Scene* createScene();
Expand Down Expand Up @@ -38,8 +45,13 @@ class GameScene final : public Scene {

Node* createRestartButton();
Label* createHeaderItem(string initialText, string iconPath, Vec2 position);
ui::Button* createDifficultyButton(const string label) const;
GameData getGameData() const;
static void setGameData(int width, int height, int qtyMines);

void restart();
void restart() const;
void setEasyDifficulty();
void setMediumDifficulty();
};

#endif // __GAMESCENE_H__
Loading

0 comments on commit d51b601

Please sign in to comment.