Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions demos/TicTacToe/Classes/AppDelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "AppDelegate.h"

#include "TicTacToeScene.h"

USING_NS_CC;

const float kFrameWidth = 600;
const float kFrameHeight = 600;

AppDelegate::AppDelegate() {}

AppDelegate::~AppDelegate() {}

bool AppDelegate::applicationDidFinishLaunching() {
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if (!glview) {
glview = GLViewImpl::create("Tic-Tac-Toe");
glview->setFrameSize(kFrameWidth, kFrameHeight);
director->setOpenGLView(glview);
}

auto scene = TicTacToe::createScene();
director->runWithScene(scene);

return true;
}

void AppDelegate::applicationDidEnterBackground() {}

void AppDelegate::applicationWillEnterForeground() {}
13 changes: 13 additions & 0 deletions demos/TicTacToe/Classes/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#include "cocos2d.h"

class AppDelegate : private cocos2d::Application {
public:
AppDelegate();
~AppDelegate() override;

bool applicationDidFinishLaunching() override;
void applicationDidEnterBackground() override;
void applicationWillEnterForeground() override;
};
78 changes: 78 additions & 0 deletions demos/TicTacToe/Classes/TicTacToeScene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "TicTacToeScene.h"

#include <array>
#include <cstdlib>

#include "cocos2d.h"

USING_NS_CC;

static const int kTilesX = 3;
static const int kTilesY = 3;
static const int kNumberOfTiles = kTilesX * kTilesY;
static const int kMaxMovesPerPlayer = 1 + kNumberOfTiles / 2;
static const double kScreenWidth = 600;
static const double kScreenHeight = 600;
static const double kTileWidth = (kScreenWidth / kTilesX);
static const double kTileHeight = (kScreenHeight / kTilesY);
static const int kNumberOfPlayers = 2;
static const char* kBoardImageFileName = "tic_tac_toe_board.png";
std::array<const char*, kNumberOfPlayers> kMoveImageFileNames = {
"tic_tac_toe_x.png", "tic_tac_toe_o.png"};

Scene* TicTacToe::createScene() {
auto scene = Scene::create();
auto layer = TicTacToe::create();

scene->addChild(layer);

return scene;
}

bool TicTacToe::init() {
if (!Layer::init()) {
return false;
}
int current_player_index = 0;
auto file_names_it = std::begin(kMoveImageFileNames);

// TODO(grantpostma): This should reflect the size that is set in AppDelegate.
// Should modify kTileWidth and kTileHeight based on that size auto
// kScreenWidth = Director::getInstance()->getWinSize().width; auto
// kScreenHeight = Director::getInstance()->getWinSize().height;

auto board_sprite = Sprite::create(kBoardImageFileName);
board_sprite->setPosition(0, 0);
board_sprite->setAnchorPoint(Vec2(0.0, 0.0));

auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = [board_sprite, current_player_index](
Touch* touch,
Event* event) mutable -> bool {
auto bounds = event->getCurrentTarget()->getBoundingBox();

if (bounds.containsPoint(touch->getLocation())) {
// This takes the touch location and calculates which tile number [0-8]
// corresponds to that location
int move_tile = floor(touch->getLocation().x / kTileWidth) +
kTilesX * floor(touch->getLocation().y / kTileHeight);

auto sprite = Sprite::create(kMoveImageFileNames[current_player_index]);
// This calculates and sets the position of the sprite based on the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Calculate and set..."

// move_tile and the constant screen variables.
sprite->setPosition((.5 + move_tile % kTilesX) * kTileWidth,
(.5 + move_tile / kTilesY) * kTileHeight);
board_sprite->addChild(sprite);
current_player_index = (current_player_index + 1) % kNumberOfPlayers;
}
return true;
};

Director::getInstance()
->getEventDispatcher()
->addEventListenerWithSceneGraphPriority(touchListener, board_sprite);

this->addChild(board_sprite);

return true;
}
13 changes: 13 additions & 0 deletions demos/TicTacToe/Classes/TicTacToeScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#include <array>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need any of these in the header. None of them are needed for what is exposed in this header.

#include <cstdlib>

#include "cocos2d.h"

class TicTacToe : public cocos2d::Layer {
public:
static cocos2d::Scene* createScene();
virtual bool init();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

override

CREATE_FUNC(TicTacToe);
};
Binary file added demos/TicTacToe/Resources/fonts/Marker Felt.ttf
Binary file not shown.
Binary file added demos/TicTacToe/Resources/fonts/arial.ttf
Binary file not shown.
Empty file.
Binary file added demos/TicTacToe/Resources/tic_tac_toe_board.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demos/TicTacToe/Resources/tic_tac_toe_o.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demos/TicTacToe/Resources/tic_tac_toe_x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.