- Notifications
You must be signed in to change notification settings - Fork 135
TicTacToe demo cocos2dx functionality #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
a1d7ea7
fe6b14f
a6e9567
441998b
1826d7d
9d0c311
eedf812
1777aff
12d03ce
11aed62
7322220
5caac8a
2b8b2b9
c6be411
3ce4079
78775cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "AppDelegate.h" | ||
| ||
#include "TicTacToeScene.h" | ||
| ||
USING_NS_CC; | ||
| ||
extern const float kFrameWidth = 600; | ||
extern const float kFrameHeight = 600; | ||
DellaBitta marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ||
AppDelegate::AppDelegate() {} | ||
| ||
AppDelegate::~AppDelegate() {} | ||
| ||
bool AppDelegate::applicationDidFinishLaunching() { | ||
auto director = Director::getInstance(); | ||
DellaBitta marked this conversation as resolved. Show resolved Hide resolved | ||
auto glview = director->getOpenGLView(); | ||
if (!glview) { | ||
DellaBitta marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
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() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
| ||
| ||
#include "cocos2d.h" | ||
| ||
class AppDelegate : private cocos2d::Application { | ||
public: | ||
AppDelegate(); | ||
virtual ~AppDelegate(); | ||
| ||
| ||
virtual bool applicationDidFinishLaunching(); | ||
virtual void applicationDidEnterBackground(); | ||
virtual void applicationWillEnterForeground(); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "TicTacToeScene.h" | ||
| ||
#include <array> | ||
#include <cstdlib> | ||
#include <iostream> | ||
DellaBitta marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ||
#include "cocos2d.h" | ||
| ||
USING_NS_CC; | ||
| ||
const int kNumberOfTiles = 9; | ||
| ||
const int kMaxMovesPerPlayer = 1 + kNumberOfTiles / 2; | ||
const int kTilesX = std::sqrt(kNumberOfTiles); | ||
| ||
const int kTilesY = std::sqrt(kNumberOfTiles); | ||
const double kScreenWidth = 600; | ||
const double kScreenHeight = 600; | ||
const double kTileWidth = (kScreenWidth / kTilesX); | ||
const double kTileHeight = (kScreenHeight / kTilesY); | ||
const int kNumberOfPlayers = 2; | ||
std::array<std::string, kNumberOfPlayers> kMoveImageFileNames = { | ||
| ||
"tic_tac_toe_x.png", "tic_tac_toe_o.png"}; | ||
| ||
Scene* TicTacToe::createScene() { | ||
auto scene = Scene::create(); | ||
DellaBitta marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
auto layer = TicTacToe::create(); | ||
| ||
scene->addChild(layer); | ||
| ||
return scene; | ||
} | ||
| ||
bool TicTacToe::init() { | ||
if (!Layer::init()) { | ||
return false; | ||
} | ||
| ||
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("tic_tac_toe_board.png"); | ||
| ||
board_sprite->setPosition(0, 0); | ||
board_sprite->setAnchorPoint(Vec2(0.0, 0.0)); | ||
| ||
auto touchListener = EventListenerTouchOneByOne::create(); | ||
DellaBitta marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
touchListener->onTouchBegan = [=](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 | ||
DellaBitta marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
int move_tile = floor(touch->getLocation().x / kTileWidth) + | ||
DellaBitta marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
kTilesX * floor(touch->getLocation().y / kTileHeight); | ||
| ||
auto sprite = Sprite::create(*file_names_it); | ||
// This calculates and sets the position of the sprite based on the | ||
| ||
// move_tile and the constant screen variables. | ||
sprite->setPosition((.5 + move_tile % kTilesX) * kTileWidth, | ||
(.5 + move_tile / kTilesY) * kTileHeight); | ||
board_sprite->addChild(sprite); | ||
| ||
file_names_it++; | ||
DellaBitta marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
if (file_names_it == std::end(kMoveImageFileNames)) { | ||
file_names_it = std::begin(kMoveImageFileNames); | ||
} | ||
} | ||
return true; | ||
}; | ||
| ||
Director::getInstance() | ||
DellaBitta marked this conversation as resolved. Show resolved Hide resolved | ||
->getEventDispatcher() | ||
->addEventListenerWithSceneGraphPriority(touchListener, board_sprite); | ||
| ||
this->addChild(board_sprite); | ||
| ||
return true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
| ||
| ||
#include <array> | ||
| ||
#include <cstdlib> | ||
#include <iostream> | ||
| ||
#include "cocos2d.h" | ||
| ||
class TicTacToe : public cocos2d::Layer { | ||
public: | ||
static cocos2d::Scene* createScene(); | ||
DellaBitta marked this conversation as resolved. Show resolved Hide resolved | ||
virtual bool init(); | ||
| ||
CREATE_FUNC(TicTacToe); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're not really touching this file, can you just revert this change completely?