- Notifications
You must be signed in to change notification settings - Fork 134
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits Select commit Hold shift + click to select a range
a1d7ea7 Changed database testapp to run Tic-Tac-Toe
Grant-Postma fe6b14f Changing pointers alignment back
Grant-Postma a6e9567 Class and resource files required for cocos2dx
Grant-Postma 441998b Merge pull request #1 from Grant-Postma/cocos2dx-init
Grant-Postma 1826d7d Cocos basic functionality for placing sprites
Grant-Postma 9d0c311 Reverting common_main TicTacToe changes
Grant-Postma eedf812 Clang-format corrections
Grant-Postma 1777aff Suggested changes from @alexames review
Grant-Postma 12d03ce Suggested changes from @DellaBitta.
Grant-Postma 11aed62 freeing allocations and exiting on nullptrs
Grant-Postma 7322220 AppDelagate free glview & director + return false
Grant-Postma 5caac8a Removing nullptr checks & pragma once statements
Grant-Postma 2b8b2b9 Replacing pragma with header guards
Grant-Postma c6be411 adding header guards
Grant-Postma 3ce4079 Moving touchListener Comment up; snake_case fix
Grant-Postma 78775cf Comment change for more professional readablity
Grant-Postma 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
Suggested changes from @DellaBitta.
- Loading branch information
commit 12d03ce89880fd6040cc3ac0b56af6006b1f0865
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -17,14 +17,22 @@ 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 = { | ||
| std::array<const char*, kNumberOfPlayers> kPlayerTokenFileNames = { | ||
| "tic_tac_toe_x.png", "tic_tac_toe_o.png"}; | ||
| | ||
| Scene* TicTacToe::createScene() { | ||
| auto scene = Scene::create(); | ||
| auto layer = TicTacToe::create(); | ||
| // 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(); | ||
| | ||
| if (scene == nullptr || tic_tac_toe_layer == nullptr) { | ||
| log("Error while creating the scene and tictactoe layer."); | ||
DellaBitta marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| exit(true); | ||
| } | ||
| | ||
| scene->addChild(layer); | ||
| scene->addChild(tic_tac_toe_layer); | ||
| | ||
| return scene; | ||
| } | ||
| | @@ -34,34 +42,48 @@ bool TicTacToe::init() { | |
| return false; | ||
| } | ||
| int current_player_index = 0; | ||
| auto file_names_it = std::begin(kMoveImageFileNames); | ||
| auto file_names_it = std::begin(kPlayerTokenFileNames); | ||
| | ||
| // TODO(grantpostma): This should reflect the size that is set in AppDelegate. | ||
| // Should modify kTileWidth and kTileHeight based on that size auto | ||
| // 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); | ||
| // 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("notFoundImage.png"); | ||
| 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)); | ||
| | ||
| auto touchListener = EventListenerTouchOneByOne::create(); | ||
DellaBitta marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| // Adding a function to determine which tile was selected to the onTouchBegan | ||
| // listener. | ||
| 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]); | ||
| // 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) { | ||
| log("kPlayerTokenFileNames: %s file not found.", | ||
| kPlayerTokenFileNames[current_player_index]); | ||
| exit(true); | ||
| } | ||
| // 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); | ||
| sprite->setPosition((.5 + selected_tile % kTilesX) * kTileWidth, | ||
| (.5 + selected_tile / kTilesY) * kTileHeight); | ||
| board_sprite->addChild(sprite); | ||
| current_player_index = (current_player_index + 1) % kNumberOfPlayers; | ||
| } | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.