diff --git a/demos/TicTacToe/Classes/app_delegate.cc b/demos/TicTacToe/Classes/app_delegate.cc index 8849dcc5..15ea2a52 100644 --- a/demos/TicTacToe/Classes/app_delegate.cc +++ b/demos/TicTacToe/Classes/app_delegate.cc @@ -15,7 +15,6 @@ #include "app_delegate.h" #include "main_menu_scene.h" -#include "tic_tac_toe_scene.h" USING_NS_CC; diff --git a/demos/TicTacToe/Classes/app_delegate.h b/demos/TicTacToe/Classes/app_delegate.h index 75e7b832..ca4887a2 100644 --- a/demos/TicTacToe/Classes/app_delegate.h +++ b/demos/TicTacToe/Classes/app_delegate.h @@ -14,6 +14,7 @@ #ifndef TICTACTOE_DEMO_CLASSES_APPDELEGATE_SCENE_H_ #define TICTACTOE_DEMO_CLASSES_APPDELEGATE_SCENE_H_ + #include "cocos2d.h" class AppDelegate : private cocos2d::Application { diff --git a/demos/TicTacToe/Classes/main_menu_scene.cc b/demos/TicTacToe/Classes/main_menu_scene.cc index 3ad84af9..ae842628 100644 --- a/demos/TicTacToe/Classes/main_menu_scene.cc +++ b/demos/TicTacToe/Classes/main_menu_scene.cc @@ -15,12 +15,33 @@ #include "main_menu_scene.h" #include +#include #include "cocos2d.h" +#include "firebase/auth.h" +#include "firebase/database.h" +#include "firebase/future.h" #include "firebase/util.h" #include "tic_tac_toe_scene.h" #include "util.h" +using cocos2d::Event; +using cocos2d::Label; +using cocos2d::Scene; +using cocos2d::Size; +using cocos2d::Sprite; +using cocos2d::TextFieldTTF; +using cocos2d::Touch; +using cocos2d::Vec2; +using firebase::App; +using firebase::InitResult; +using firebase::kFutureStatusComplete; +using firebase::ModuleInitializer; +using firebase::auth::Auth; +using firebase::auth::kAuthErrorNone; +using firebase::database::Database; +using std::to_string; + static const char* kCreateGameImage = "create_game.png"; static const char* kTextFieldBorderImage = "text_field_border.png"; static const char* kJoinButtonImage = "join_game.png"; @@ -106,8 +127,8 @@ bool MainMenuScene::init() { auto anonymous_label_touch_listener = EventListenerTouchOneByOne::create(); - anonymous_label_touch_listener->onTouchBegan = - [this](cocos2d::Touch* touch, cocos2d::Event* event) -> bool { + anonymous_label_touch_listener->onTouchBegan = [this](Touch* touch, + Event* event) -> bool { // Returns false, not consuming the event, to exit the layer if // current_state_ is not in the kAuthState or is switching states. if (previous_state_ != current_state_ || current_state_ != kAuthState) { @@ -156,7 +177,7 @@ bool MainMenuScene::init() { Color4F(0, 0, 0, 0), 1, Color4F::WHITE); // Create a text field to enter the user's email. - email_text_field_ = cocos2d::TextFieldTTF::textFieldWithPlaceHolder( + email_text_field_ = TextFieldTTF::textFieldWithPlaceHolder( "enter an email address", email_text_field_size, TextHAlignment::LEFT, "Arial", email_font_size); email_text_field_->setPosition(email_text_field_position); @@ -166,8 +187,8 @@ bool MainMenuScene::init() { auto email_text_field_touch_listener = EventListenerTouchOneByOne::create(); - email_text_field_touch_listener->onTouchBegan = - [this](cocos2d::Touch* touch, cocos2d::Event* event) -> bool { + email_text_field_touch_listener->onTouchBegan = [this](Touch* touch, + Event* event) -> bool { // Returns false, not consuming the event, to exit the layer if // current_state_ is not in the kAuthState or is switching states. if (previous_state_ != current_state_ || current_state_ != kAuthState) { @@ -225,7 +246,7 @@ bool MainMenuScene::init() { password_border_corners, 4, Color4F(0, 0, 0, 0), 1, Color4F::WHITE); // Create a text field to enter the user's password. - password_text_field_ = cocos2d::TextFieldTTF::textFieldWithPlaceHolder( + password_text_field_ = TextFieldTTF::textFieldWithPlaceHolder( "enter a password", password_text_field_size, TextHAlignment::LEFT, "Arial", password_font_size); password_text_field_->setPosition(password_text_field_position); @@ -238,7 +259,7 @@ bool MainMenuScene::init() { EventListenerTouchOneByOne::create(); password_text_field_touch_listener->onTouchBegan = - [this](cocos2d::Touch* touch, cocos2d::Event* event) -> bool { + [this](Touch* touch, Event* event) -> bool { // Returns false, not consuming the event, to exit the layer if // current_state_ is not in the kAuthState or is switching states. if (previous_state_ != current_state_ || current_state_ != kAuthState) { @@ -362,9 +383,8 @@ bool MainMenuScene::init() { // Create, set the position and assign a placeholder to the text // field for the user to enter the join game uuid. - TextFieldTTF* join_text_field = - cocos2d::TextFieldTTF::textFieldWithPlaceHolder( - "code", cocos2d::Size(200, 100), TextHAlignment::LEFT, "Arial", 55.0); + TextFieldTTF* join_text_field = TextFieldTTF::textFieldWithPlaceHolder( + "code", Size(200, 100), TextHAlignment::LEFT, "Arial", 55.0); join_text_field->setPosition(420, 45); join_text_field->setAnchorPoint(Vec2(0, 0)); join_text_field->setColorSpaceHolder(Color3B::WHITE); @@ -380,8 +400,7 @@ bool MainMenuScene::init() { auto join_text_field_touch_listener = EventListenerTouchOneByOne::create(); join_text_field_touch_listener->onTouchBegan = - [join_text_field, this](cocos2d::Touch* touch, - cocos2d::Event* event) -> bool { + [join_text_field, this](Touch* touch, Event* event) -> bool { // Returns false, not consuming the event, to exit the layer if // current_state_ is not in the kGameMenuState or is switching states. if (previous_state_ != current_state_ || current_state_ != kGameMenuState) { @@ -550,12 +569,12 @@ bool MainMenuScene::init() { // are missing. void MainMenuScene::InitializeFirebase() { LogMessage("Initialize Firebase App."); - ::firebase::App* app; + App* app; #if defined(_ANDROID_) - app = ::firebase::App::Create(GetJniEnv(), GetActivity()); + app = App::Create(GetJniEnv(), GetActivity()); #else - app = ::firebase::App::Create(); + app = App::Create(); #endif // defined(ANDROID) LogMessage("Initialize Firebase Auth and Firebase Database."); @@ -566,25 +585,24 @@ void MainMenuScene::InitializeFirebase() { auth_ = nullptr; void* initialize_targets[] = {&auth_, &database_}; - const firebase::ModuleInitializer::InitializerFn initializers[] = { - [](::firebase::App* app, void* data) { + const ModuleInitializer::InitializerFn initializers[] = { + [](App* app, void* data) { LogMessage("Attempt to initialize Firebase Auth."); void** targets = reinterpret_cast(data); - ::firebase::InitResult result; - *reinterpret_cast<::firebase::auth::Auth**>(targets[0]) = - ::firebase::auth::Auth::GetAuth(app, &result); + InitResult result; + *reinterpret_cast(targets[0]) = Auth::GetAuth(app, &result); return result; }, - [](::firebase::App* app, void* data) { + [](App* app, void* data) { LogMessage("Attempt to initialize Firebase Database."); void** targets = reinterpret_cast(data); - ::firebase::InitResult result; - *reinterpret_cast<::firebase::database::Database**>(targets[1]) = - ::firebase::database::Database::GetInstance(app, &result); + InitResult result; + *reinterpret_cast(targets[1]) = + Database::GetInstance(app, &result); return result; }}; - ::firebase::ModuleInitializer initializer; + ModuleInitializer initializer; initializer.Initialize(app, initialize_targets, initializers, sizeof(initializers) / sizeof(initializers[0])); @@ -659,8 +677,8 @@ void MainMenuScene::onEnter() { void MainMenuScene::update(float /*delta*/) { if (current_state_ != previous_state_) { if (current_state_ == kWaitingAnonymousState) { - if (user_result_.status() == firebase::kFutureStatusComplete) { - if (user_result_.error() == firebase::auth::kAuthErrorNone) { + if (user_result_.status() == kFutureStatusComplete) { + if (user_result_.error() == kAuthErrorNone) { user_ = *user_result_.result(); user_uid_ = GenerateUid(10); @@ -670,8 +688,8 @@ void MainMenuScene::update(float /*delta*/) { } } } else if (current_state_ == kWaitingSignUpState) { - if (user_result_.status() == firebase::kFutureStatusComplete) { - if (user_result_.error() == firebase::auth::kAuthErrorNone) { + if (user_result_.status() == kFutureStatusComplete) { + if (user_result_.error() == kAuthErrorNone) { user_ = *user_result_.result(); user_uid_ = user_->uid(); @@ -686,8 +704,8 @@ void MainMenuScene::update(float /*delta*/) { } } } else if (current_state_ == kWaitingLoginState) { - if (user_result_.status() == firebase::kFutureStatusComplete) { - if (user_result_.error() == firebase::auth::kAuthErrorNone) { + if (user_result_.status() == kFutureStatusComplete) { + if (user_result_.error() == kAuthErrorNone) { user_ = *user_result_.result(); user_uid_ = user_->uid(); diff --git a/demos/TicTacToe/Classes/main_menu_scene.h b/demos/TicTacToe/Classes/main_menu_scene.h index f5181339..8f8c9914 100644 --- a/demos/TicTacToe/Classes/main_menu_scene.h +++ b/demos/TicTacToe/Classes/main_menu_scene.h @@ -15,14 +15,21 @@ #ifndef TICTACTOE_DEMO_CLASSES_MAINMENU_SCENE_H_ #define TICTACTOE_DEMO_CLASSES_MAINMENU_SCENE_H_ -#include +#include #include "cocos2d.h" #include "firebase/auth.h" #include "firebase/database.h" #include "firebase/future.h" -using std::to_string; +using cocos2d::Label; +using cocos2d::TextFieldTTF; +using firebase::Future; +using firebase::auth::Auth; +using firebase::auth::User; +using firebase::database::Database; +using firebase::database::DatabaseReference; +using std::string; class MainMenuScene : public cocos2d::Layer, public cocos2d::TextFieldDelegate { public: @@ -69,10 +76,10 @@ class MainMenuScene : public cocos2d::Layer, public cocos2d::TextFieldDelegate { cocos2d::DrawNode* auth_background_; // Labels and textfields for the authentication menu. - cocos2d::Label* invalid_login_label_; - cocos2d::Label* user_record_label_; - cocos2d::TextFieldTTF* email_text_field_; - cocos2d::TextFieldTTF* password_text_field_; + Label* invalid_login_label_; + Label* user_record_label_; + TextFieldTTF* email_text_field_; + TextFieldTTF* password_text_field_; // Variable to track the current state and previous state to check against to // see if the state changed. @@ -84,12 +91,12 @@ class MainMenuScene : public cocos2d::Layer, public cocos2d::TextFieldDelegate { int user_loses_; int user_ties_; - std::string user_uid_; - firebase::auth::User* user_; - firebase::Future user_result_; - firebase::database::Database* database_; - firebase::auth::Auth* auth_; - firebase::database::DatabaseReference ref_; + string user_uid_; + Auth* auth_; + User* user_; + Future user_result_; + Database* database_; + DatabaseReference ref_; }; #endif // TICTACTOE_DEMO_CLASSES_MAINMENU_SCENE_H_ diff --git a/demos/TicTacToe/Classes/tic_tac_toe_layer.cc b/demos/TicTacToe/Classes/tic_tac_toe_layer.cc index 45266dc8..b3f6410a 100644 --- a/demos/TicTacToe/Classes/tic_tac_toe_layer.cc +++ b/demos/TicTacToe/Classes/tic_tac_toe_layer.cc @@ -14,6 +14,32 @@ #include "tic_tac_toe_layer.h" +#include +#include +#include +#include +#include + +#include "cocos2d.h" +#include "firebase/database.h" +#include "firebase/variant.h" +#include "util.h" + +using cocos2d::Director; +using cocos2d::Event; +using cocos2d::Label; +using cocos2d::Sprite; +using cocos2d::Touch; +using firebase::Variant; +using firebase::database::DataSnapshot; +using firebase::database::Error; +using firebase::database::TransactionResult; +using firebase::database::ValueListener; +using std::array; +using std::make_unique; +using std::string; +using std::vector; + USING_NS_CC; // Player constants. @@ -34,8 +60,8 @@ static const enum kGameOutcome { // database outcome key. static const const char* kGameOutcomeStrings[] = {"wins", "loses", "ties", "disbanded"}; -static const std::array kGameOverStrings = { - "you won!", "you lost.", "you tied.", "user left."}; +static const array kGameOverStrings = {"you won!", "you lost.", + "you tied.", "user left."}; // Game board dimensions. extern const int kTilesX; @@ -54,62 +80,60 @@ static const int kEndGameFramesMax = 120; // Image file paths. static const char* kBoardImageFileName = "tic_tac_toe_board.png"; static const char* kLeaveButtonFileName = "leave_button.png"; -static std::array kPlayerTokenFileNames = { +static array kPlayerTokenFileNames = { "tic_tac_toe_x.png", "tic_tac_toe_o.png"}; // An example of a ValueListener object. This specific version will // simply log every value it sees, and store them in a list so we can // confirm that all values were received. -class SampleValueListener : public firebase::database::ValueListener { +class SampleValueListener : public ValueListener { public: - void OnValueChanged( - const firebase::database::DataSnapshot& snapshot) override { + void OnValueChanged(const DataSnapshot& snapshot) override { LogMessage(" ValueListener.OnValueChanged(%s)", snapshot.value().AsString().string_value()); last_seen_value_ = snapshot.value(); seen_values_.push_back(snapshot.value()); } - void OnCancelled(const firebase::database::Error& error_code, + void OnCancelled(const Error& error_code, const char* error_message) override { LogMessage("ERROR: SampleValueListener canceled: %d: %s", error_code, error_message); } - const firebase::Variant& last_seen_value() { return last_seen_value_; } - bool seen_value(const firebase::Variant& value) { + const Variant& last_seen_value() { return last_seen_value_; } + bool seen_value(const Variant& value) { return std::find(seen_values_.begin(), seen_values_.end(), value) != seen_values_.end(); } size_t num_seen_values() { return seen_values_.size(); } private: - firebase::Variant last_seen_value_; - std::vector seen_values_; + Variant last_seen_value_; + vector seen_values_; }; // An example ChildListener class. class SampleChildListener : public firebase::database::ChildListener { public: - void OnChildAdded(const firebase::database::DataSnapshot& snapshot, + void OnChildAdded(const DataSnapshot& snapshot, const char* previous_sibling) override { LogMessage(" ChildListener.OnChildAdded(%s)", snapshot.key()); - events_.push_back(std::string("added ") + snapshot.key()); + events_.push_back(string("added ") + snapshot.key()); } - void OnChildChanged(const firebase::database::DataSnapshot& snapshot, + void OnChildChanged(const DataSnapshot& snapshot, const char* previous_sibling) override { LogMessage(" ChildListener.OnChildChanged(%s)", snapshot.key()); - events_.push_back(std::string("changed ") + snapshot.key()); + events_.push_back(string("changed ") + snapshot.key()); } - void OnChildMoved(const firebase::database::DataSnapshot& snapshot, + void OnChildMoved(const DataSnapshot& snapshot, const char* previous_sibling) override { LogMessage(" ChildListener.OnChildMoved(%s)", snapshot.key()); - events_.push_back(std::string("moved ") + snapshot.key()); + events_.push_back(string("moved ") + snapshot.key()); } - void OnChildRemoved( - const firebase::database::DataSnapshot& snapshot) override { + void OnChildRemoved(const DataSnapshot& snapshot) override { LogMessage(" ChildListener.OnChildRemoved(%s)", snapshot.key()); - events_.push_back(std::string("removed ") + snapshot.key()); + events_.push_back(string("removed ") + snapshot.key()); } - void OnCancelled(const firebase::database::Error& error_code, + void OnCancelled(const Error& error_code, const char* error_message) override { LogMessage("ERROR: SampleChildListener canceled: %d: %s", error_code, error_message); @@ -119,7 +143,7 @@ class SampleChildListener : public firebase::database::ChildListener { size_t total_events() { return events_.size(); } // Get the number of times this event was seen. - int num_events(const std::string& event) { + int num_events(const string& event) { int count = 0; for (int i = 0; i < events_.size(); i++) { if (events_[i] == event) { @@ -132,16 +156,15 @@ class SampleChildListener : public firebase::database::ChildListener { public: // Vector of strings that contains the events in the order in which they // occurred. - std::vector events_; + vector events_; }; // A ValueListener that expects a specific value to be set. -class ExpectValueListener : public firebase::database::ValueListener { +class ExpectValueListener : public ValueListener { public: - explicit ExpectValueListener(firebase::Variant wait_value) + explicit ExpectValueListener(Variant wait_value) : wait_value_(wait_value.AsString()), got_value_(false) {} - void OnValueChanged( - const firebase::database::DataSnapshot& snapshot) override { + void OnValueChanged(const DataSnapshot& snapshot) override { if (snapshot.value().AsString() == wait_value_) { got_value_ = true; } else { @@ -149,7 +172,7 @@ class ExpectValueListener : public firebase::database::ValueListener { "FAILURE: ExpectValueListener did not receive the expected result."); } } - void OnCancelled(const firebase::database::Error& error_code, + void OnCancelled(const Error& error_code, const char* error_message) override { LogMessage("ERROR: ExpectValueListener canceled: %d: %s", error_code, error_message); @@ -158,7 +181,7 @@ class ExpectValueListener : public firebase::database::ValueListener { bool got_value() { return got_value_; } private: - firebase::Variant wait_value_; + Variant wait_value_; bool got_value_; }; @@ -219,14 +242,13 @@ TicTacToeLayer::TicTacToeLayer(string game_uuid, if (join_game_uuid_.empty()) { join_game_uuid_ = GenerateUid(4); ref_ = database_->GetReference("game_data").Child(join_game_uuid_); - firebase::Future future_create_game = - ref_.Child("total_players").SetValue(1); + future_create_game_ = ref_.Child("total_players").SetValue(1); future_current_player_index_ = ref_.Child("current_player_index_").SetValue(kPlayerOne); future_game_over_ = ref_.Child("game_over").SetValue(false); WaitForCompletion(future_game_over_, "setGameOver"); WaitForCompletion(future_current_player_index_, "setCurrentPlayerIndex"); - WaitForCompletion(future_create_game, "createGame"); + WaitForCompletion(future_create_game_, "createGame"); player_index_ = kPlayerOne; awaiting_opponenet_move_ = false; } else { @@ -241,7 +263,7 @@ TicTacToeLayer::TicTacToeLayer(string game_uuid, } else { ref_ = database_->GetReference("game_data").Child(join_game_uuid_); auto future_increment_total_users = - ref_.RunTransaction([](MutableData* data) { + ref_.RunTransaction([](firebase::database::MutableData* data) { auto total_players = data->Child("total_players").value(); // Complete the transaction based on the returned mutable data @@ -340,12 +362,11 @@ TicTacToeLayer::TicTacToeLayer(string game_uuid, // total_player_listener_ and CurrentPlayerIndexListener listener is set up // to recognise when the desired players have connected & when turns // alternate. - total_player_listener_ = - std::make_unique(kNumberOfPlayers); - game_over_listener_ = std::make_unique(true); + total_player_listener_ = make_unique(kNumberOfPlayers); + game_over_listener_ = make_unique(true); - current_player_index_listener_ = std::make_unique(); - last_move_listener_ = std::make_unique(); + current_player_index_listener_ = make_unique(); + last_move_listener_ = make_unique(); ref_.Child("total_players").AddValueListener(total_player_listener_.get()); ref_.Child("game_over").AddValueListener(game_over_listener_.get()); ref_.Child("current_player_index_") diff --git a/demos/TicTacToe/Classes/tic_tac_toe_layer.h b/demos/TicTacToe/Classes/tic_tac_toe_layer.h index deb4624f..9e535908 100644 --- a/demos/TicTacToe/Classes/tic_tac_toe_layer.h +++ b/demos/TicTacToe/Classes/tic_tac_toe_layer.h @@ -15,34 +15,20 @@ #ifndef TICTACTOE_DEMO_CLASSES_TICTACTOELAYER_SCENE_H_ #define TICTACTOE_DEMO_CLASSES_TICTACTOELAYER_SCENE_H_ -#include -#include -#include -#include -#include -#include -#include -#include -#include #include #include #include "cocos2d.h" -#include "tic_tac_toe_scene.h" -#include "util.h" +#include "firebase/database.h" +#include "firebase/future.h" -using cocos2d::Director; -using cocos2d::Event; +using cocos2d::Label; using cocos2d::Layer; -using cocos2d::LayerColor; -using cocos2d::Point; using cocos2d::Sprite; -using cocos2d::Touch; using firebase::Future; -using firebase::database::DataSnapshot; -using firebase::database::MutableData; -using firebase::database::TransactionResult; +using firebase::database::Database; using std::string; +using std::unique_ptr; // Tile Constants. static const int kTilesX = 3; @@ -56,7 +42,7 @@ class TicTacToeLayer : public Layer { public: // Derived from Layer class with input paramters for the game_uid, database // and user_uid and overrides Layer::update(). - TicTacToeLayer(std::string, firebase::database::Database*, std::string); + TicTacToeLayer(string, Database*, string); ~TicTacToeLayer(); private: @@ -73,34 +59,35 @@ class TicTacToeLayer : public Layer { // String for the join game code and initialize the database // reference. - std::string join_game_uuid_; + string join_game_uuid_; // User uid to update the user's record after the game is over. - std::string user_uid_; + string user_uid_; // Firebase Realtime Database, the entry point to all database operations. - firebase::database::Database* database_; + Database* database_; firebase::database::DatabaseReference ref_; // The database schema has a top level game_uuid object which includes // last_move, total_players and current_player_index_ fields. // Listeners for database values. - std::unique_ptr current_player_index_listener_; - std::unique_ptr last_move_listener_; - std::unique_ptr total_player_listener_; - std::unique_ptr game_over_listener_; + unique_ptr current_player_index_listener_; + unique_ptr last_move_listener_; + unique_ptr total_player_listener_; + unique_ptr game_over_listener_; // Lables and a sprites. Sprite* board_sprite_; Sprite* leave_button_sprite_; - cocos2d::Label* game_over_label_; - cocos2d::Label* waiting_label_; + Label* game_over_label_; + Label* waiting_label_; // Firebase futures for last_move and current_player_index_. Future future_last_move_; Future future_current_player_index_; Future future_game_over_; + Future future_create_game_; int current_player_index_; int player_index_; diff --git a/demos/TicTacToe/Classes/tic_tac_toe_scene.cc b/demos/TicTacToe/Classes/tic_tac_toe_scene.cc index de3f907d..2e63d7d3 100644 --- a/demos/TicTacToe/Classes/tic_tac_toe_scene.cc +++ b/demos/TicTacToe/Classes/tic_tac_toe_scene.cc @@ -14,6 +14,12 @@ #include "tic_tac_toe_scene.h" +#include + +#include "cocos2d.h" +#include "firebase/database.h" +#include "tic_tac_toe_layer.h" + using cocos2d::Scene; Scene* TicTacToe::createScene(const std::string& game_uuid, diff --git a/demos/TicTacToe/Classes/tic_tac_toe_scene.h b/demos/TicTacToe/Classes/tic_tac_toe_scene.h index 3232b1ca..f8f3073a 100644 --- a/demos/TicTacToe/Classes/tic_tac_toe_scene.h +++ b/demos/TicTacToe/Classes/tic_tac_toe_scene.h @@ -16,9 +16,7 @@ #define TICTACTOE_DEMO_CLASSES_TICTACTOE_SCENE_H_ #include "cocos2d.h" -#include "main_menu_scene.h" -#include "tic_tac_toe_layer.h" -#include "util.h" +#include "firebase/database.h" class TicTacToe : public cocos2d::Layer { public: