diff --git a/demos/TicTacToe/Classes/AppDelegate.cpp b/demos/TicTacToe/Classes/AppDelegate.cpp new file mode 100644 index 00000000..775b5981 --- /dev/null +++ b/demos/TicTacToe/Classes/AppDelegate.cpp @@ -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 == NULL) { + 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() {} \ No newline at end of file diff --git a/demos/TicTacToe/Classes/AppDelegate.h b/demos/TicTacToe/Classes/AppDelegate.h new file mode 100644 index 00000000..49ad5f31 --- /dev/null +++ b/demos/TicTacToe/Classes/AppDelegate.h @@ -0,0 +1,14 @@ +#ifndef TICTACTOE_DEMO_CLASSES_APPDELEGATE_SCENE_H_ +#define TICTACTOE_DEMO_CLASSES_APPDELEGATE_SCENE_H_ +#include "cocos2d.h" + +class AppDelegate : private cocos2d::Application { + public: + AppDelegate(); + ~AppDelegate() override; + + bool applicationDidFinishLaunching() override; + void applicationDidEnterBackground() override; + void applicationWillEnterForeground() override; +}; +#endif // TICTACTOE_DEMO_CLASSES_APPDELEGATE_SCENE_H_ \ No newline at end of file diff --git a/demos/TicTacToe/Classes/TicTacToeScene.cpp b/demos/TicTacToe/Classes/TicTacToeScene.cpp new file mode 100644 index 00000000..92649a7a --- /dev/null +++ b/demos/TicTacToe/Classes/TicTacToeScene.cpp @@ -0,0 +1,96 @@ +#include "TicTacToeScene.h" + +#include +#include + +#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 kPlayerTokenFileNames = { + "tic_tac_toe_x.png", "tic_tac_toe_o.png"}; + +Scene* TicTacToe::createScene() { + // Builds a simple scene that uses the bottom left cordinate point as (0,0) + // and can have sprites, labels and layers added onto it. + Scene* scene = Scene::create(); + + // Builds a layer to be placed onto the scene which has access to TouchEvents. + TicTacToe* tic_tac_toe_layer = TicTacToe::create(); + + scene->addChild(tic_tac_toe_layer); + + return scene; +} + +bool TicTacToe::init() { + if (!Layer::init()) { + return false; + } + int current_player_index = 0; + auto file_names_it = std::begin(kPlayerTokenFileNames); + + // TODO(grantpostma): This should reflect the size that is set in AppDelegate. + // (GetVisableSize) Should modify kTileWidth and kTileHeight based on that + // size. auto kScreenWidth = Director::getInstance()->getWinSize().width; auto + // kScreenHeight = Director::getInstance()->getWinSize().height; + + // Creating the board sprite , setting the position to the bottom left of the + // frame (0,0), and finally moving the anchor point from the center of the + // image(default) to the bottom left, Vec2(0.0,0.0). + Sprite* board_sprite = Sprite::create(kBoardImageFileName); + if (!board_sprite) { + log("kBoardImageFileName: %s file not found.", kBoardImageFileName); + exit(true); + } + board_sprite->setPosition(0, 0); + board_sprite->setAnchorPoint(Vec2(0.0, 0.0)); + + // Adding a function to determine which tile was selected to the onTouchBegan + // listener. + auto touch_listener = EventListenerTouchOneByOne::create(); + touch_listener->onTouchBegan = [board_sprite, current_player_index]( + Touch* touch, + Event* event) mutable -> bool { + auto bounds = event->getCurrentTarget()->getBoundingBox(); + + if (bounds.containsPoint(touch->getLocation())) { + // Calculates the tile number [0-8] which corresponds to the touch + // location. + int selected_tile = floor(touch->getLocation().x / kTileWidth) + + kTilesX * floor(touch->getLocation().y / kTileHeight); + + auto sprite = Sprite::create(kPlayerTokenFileNames[current_player_index]); + if (sprite == NULL) { + log("kPlayerTokenFileNames: %s file not found.", + kPlayerTokenFileNames[current_player_index]); + exit(true); + } + // Calculate and set the position of the sprite based on the + // move_tile and the constant screen variables. + sprite->setPosition((.5 + selected_tile % kTilesX) * kTileWidth, + (.5 + selected_tile / kTilesY) * kTileHeight); + board_sprite->addChild(sprite); + current_player_index = (current_player_index + 1) % kNumberOfPlayers; + } + return true; + }; + + Director::getInstance() + ->getEventDispatcher() + ->addEventListenerWithSceneGraphPriority(touch_listener, board_sprite); + + this->addChild(board_sprite); + + return true; +} diff --git a/demos/TicTacToe/Classes/TicTacToeScene.h b/demos/TicTacToe/Classes/TicTacToeScene.h new file mode 100644 index 00000000..ba304520 --- /dev/null +++ b/demos/TicTacToe/Classes/TicTacToeScene.h @@ -0,0 +1,16 @@ +#ifndef TICTACTOE_DEMO_CLASSES_TICTACTOE_SCENE_H_ +#define TICTACTOE_DEMO_CLASSES_TICTACTOE_SCENE_H_ +#include "cocos2d.h" + +class TicTacToe : public cocos2d::Layer { + public: + // Builds a simple scene that uses the bottom left cordinate point as (0,0) + // and can have sprites, labels and nodes added onto it. + static cocos2d::Scene* createScene(); + // Initializes the instance of a Node and returns a boolean based on if it was + // successful in doing so. + bool init() override; + // Defines a create type for a specific type, in this case a Layer. + CREATE_FUNC(TicTacToe); +}; +#endif // TICTACTOE_DEMO_CLASSES_TICTACTOE_SCENE_H_ \ No newline at end of file diff --git a/demos/TicTacToe/Resources/fonts/Marker Felt.ttf b/demos/TicTacToe/Resources/fonts/Marker Felt.ttf new file mode 100644 index 00000000..3752ef31 Binary files /dev/null and b/demos/TicTacToe/Resources/fonts/Marker Felt.ttf differ diff --git a/demos/TicTacToe/Resources/fonts/arial.ttf b/demos/TicTacToe/Resources/fonts/arial.ttf new file mode 100644 index 00000000..abc899cd Binary files /dev/null and b/demos/TicTacToe/Resources/fonts/arial.ttf differ diff --git a/demos/TicTacToe/Resources/res/.gitkeep b/demos/TicTacToe/Resources/res/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/demos/TicTacToe/Resources/tic_tac_toe_board.png b/demos/TicTacToe/Resources/tic_tac_toe_board.png new file mode 100644 index 00000000..5dfe8adb Binary files /dev/null and b/demos/TicTacToe/Resources/tic_tac_toe_board.png differ diff --git a/demos/TicTacToe/Resources/tic_tac_toe_o.png b/demos/TicTacToe/Resources/tic_tac_toe_o.png new file mode 100644 index 00000000..7b37d103 Binary files /dev/null and b/demos/TicTacToe/Resources/tic_tac_toe_o.png differ diff --git a/demos/TicTacToe/Resources/tic_tac_toe_x.png b/demos/TicTacToe/Resources/tic_tac_toe_x.png new file mode 100644 index 00000000..c276e31a Binary files /dev/null and b/demos/TicTacToe/Resources/tic_tac_toe_x.png differ