game_center.mm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*************************************************************************/
  2. /* game_center.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifdef GAME_CENTER_ENABLED
  30. #include "game_center.h"
  31. extern "C" {
  32. #import <GameKit/GameKit.h>
  33. };
  34. GameCenter* GameCenter::instance = NULL;
  35. void GameCenter::_bind_methods() {
  36. ObjectTypeDB::bind_method(_MD("connect"),&GameCenter::connect);
  37. ObjectTypeDB::bind_method(_MD("is_connected"),&GameCenter::is_connected);
  38. ObjectTypeDB::bind_method(_MD("post_score"),&GameCenter::post_score);
  39. ObjectTypeDB::bind_method(_MD("award_achievement"),&GameCenter::award_achievement);
  40. ObjectTypeDB::bind_method(_MD("get_pending_event_count"),&GameCenter::get_pending_event_count);
  41. ObjectTypeDB::bind_method(_MD("pop_pending_event"),&GameCenter::pop_pending_event);
  42. };
  43. Error GameCenter::connect() {
  44. GKLocalPlayer* player = [GKLocalPlayer localPlayer];
  45. [player authenticateWithCompletionHandler:^(NSError* error) {
  46. Dictionary ret;
  47. ret["type"] = "authentication";
  48. if (player.isAuthenticated) {
  49. ret["result"] = "ok";
  50. GameCenter::get_singleton()->connected = true;
  51. } else {
  52. ret["result"] = "error";
  53. ret["error_code"] = error.code;
  54. ret["error_description"] = [error.localizedDescription UTF8String];
  55. GameCenter::get_singleton()->connected = false;
  56. };
  57. pending_events.push_back(ret);
  58. }];
  59. return OK;
  60. };
  61. bool GameCenter::is_connected() {
  62. return connected;
  63. };
  64. Error GameCenter::post_score(Variant p_score) {
  65. Dictionary params = p_score;
  66. ERR_FAIL_COND_V(!params.has("score") || !params.has("category"), ERR_INVALID_PARAMETER);
  67. float score = params["score"];
  68. String category = params["category"];
  69. NSString* cat_str = [[[NSString alloc] initWithUTF8String:category.utf8().get_data()] autorelease];
  70. GKScore* reporter = [[[GKScore alloc] initWithCategory:cat_str] autorelease];
  71. reporter.value = score;
  72. [reporter reportScoreWithCompletionHandler:^(NSError* error) {
  73. Dictionary ret;
  74. ret["type"] = "post_score";
  75. if (error == nil) {
  76. ret["result"] = "ok";
  77. } else {
  78. ret["result"] = "error";
  79. ret["error_code"] = error.code;
  80. ret["error_description"] = [error.localizedDescription UTF8String];
  81. };
  82. pending_events.push_back(ret);
  83. }];
  84. return OK;
  85. };
  86. Error GameCenter::award_achievement(Variant p_params) {
  87. Dictionary params = p_params;
  88. ERR_FAIL_COND_V(!params.has("name") || !params.has("progress"), ERR_INVALID_PARAMETER);
  89. String name = params["name"];
  90. float progress = params["progress"];
  91. NSString* name_str = [[[NSString alloc] initWithUTF8String:name.utf8().get_data()] autorelease];
  92. GKAchievement* achievement = [[[GKAchievement alloc] initWithIdentifier: name_str] autorelease];
  93. ERR_FAIL_COND_V(!achievement, FAILED);
  94. achievement.percentComplete = progress;
  95. [achievement reportAchievementWithCompletionHandler:^(NSError* error) {
  96. Dictionary ret;
  97. ret["type"] = "award_achievement";
  98. if (error == nil) {
  99. ret["result"] = "ok";
  100. } else {
  101. ret["result"] = "error";
  102. ret["error_code"] = error.code;
  103. };
  104. pending_events.push_back(ret);
  105. }];
  106. return OK;
  107. };
  108. int GameCenter::get_pending_event_count() {
  109. return pending_events.size();
  110. };
  111. Variant GameCenter::pop_pending_event() {
  112. Variant front = pending_events.front()->get();
  113. pending_events.pop_front();
  114. return front;
  115. };
  116. GameCenter* GameCenter::get_singleton() {
  117. return instance;
  118. };
  119. GameCenter::GameCenter() {
  120. ERR_FAIL_COND(instance != NULL);
  121. instance = this;
  122. connected = false;
  123. };
  124. GameCenter::~GameCenter() {
  125. };
  126. #endif