TestScene.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // TestScene.cpp
  3. // demo
  4. //
  5. // Created by Red_mini on 2024/10/11.
  6. //
  7. #include "TestScene.h"
  8. #include "RUPlatform.h"
  9. #include "IAPCtlShop.hpp"
  10. #include "IAPDefine.hpp"
  11. #include "IAPTestCard.hpp"
  12. #include "IAPDelegateImpl.hpp"
  13. USING_NS_CC;
  14. Scene* TestScene::createScene(){
  15. // create the scene with physics enabled
  16. auto scene = Scene::create();
  17. auto layer = TestScene::create();
  18. scene->addChild(layer);
  19. return scene;
  20. }
  21. bool TestScene::init(){
  22. if (!Layer::init()) {
  23. return false;
  24. }
  25. std::string cfgFN = "shop_5.json";
  26. std::string failFN = "purchaseBannerConfig.csv";
  27. _iapShop = iapshop::IAPCtlShop::createWith();
  28. auto delegate = IAPDelegateImpl::createWith();
  29. _iapShop->setDelegate(delegate);
  30. _iapShop->init(cfgFN);
  31. _iapShop->setCfg4Failure(failFN);
  32. // 第二个版位
  33. // iapshop::IAPPlacement plInfo2;
  34. // plInfo2.id = "2";
  35. // IAPTestCard* testCard3 = new IAPTestCard();
  36. // IAPTestCard* testCard4 = new IAPTestCard();
  37. // plInfo2.cards.push_back(testCard3);
  38. // plInfo2.cards.push_back(testCard4);
  39. //
  40. // _iapShop->addAPlacement(plInfo2);
  41. // 在此处添加场景初始化的代码,比如创建背景、角色、UI等
  42. createBoard();
  43. schedule(CC_SCHEDULE_SELECTOR(TestScene::update), 1.0f);
  44. return true;
  45. }
  46. void TestScene::createBoard(){
  47. auto lyMap = redutils::RUReboltLayer::createReboltLayer("lyMap.redream");
  48. this->addChild(lyMap);
  49. // log(lyMap->getChildrenCount());
  50. lyMap->registerOnNotify([this](const redutils::ReboltNotifyData& data){
  51. onNotifyDevelopment(data);
  52. });
  53. lyMap->runBehaviacWhitFunName("初始化");
  54. }
  55. void TestScene::onNotifyDevelopment(const redutils::ReboltNotifyData& data){
  56. if(data.notify == "点击显示商店"){
  57. log("点击显示商店按钮");
  58. iapshop::ShopRequirement shopRequirement;
  59. shopRequirement.coinsMin = 0;
  60. _iapShop->showInNode(this, shopRequirement);
  61. }else if(data.notify == "点击设备1"){
  62. _iapShop->setDeviceLevel(1);
  63. log("设置为低等级设备");
  64. }else if(data.notify == "点击设备2"){
  65. _iapShop->setDeviceLevel(3);
  66. log("设置为高等级设备");
  67. }else if(data.notify == "点击失败"){
  68. log("点击失败时金币不够按钮");
  69. // iapshop::ShopRequirement shopRequirement;
  70. // shopRequirement.coinsMin = 10000;
  71. //
  72. // _iapShop->showInNode(this, shopRequirement);
  73. std::vector<std::string> names;
  74. // names.push_back("礼包1");
  75. names.push_back("tilefavor");
  76. names.push_back("tilepack");
  77. names.push_back("tilepass");
  78. _iapShop->displayFailureCardIn(this, names);
  79. }else if(data.notify == "点击失败时进入商店"){
  80. auto editBox = dynamic_cast<REDEditBox*>(data.outNode);
  81. if(editBox){
  82. std::string text = editBox->getText();
  83. iapshop::ShopRequirement req;
  84. req.coinsMin = 0;
  85. try {
  86. req.coinsMin = stoi(text);
  87. }
  88. catch (const std::invalid_argument&) {
  89. log("转化失败");
  90. }
  91. catch (const std::out_of_range&) {
  92. log("超过int值");
  93. }
  94. _iapShop->showInNode(this, req);
  95. log("点击失败时进入商店");
  96. }
  97. }else if(data.notify == "点击重置"){
  98. _iapShop->setDeviceLevel(1);
  99. _iapShop->clearUserBuyInfo();
  100. _iapShop->clearPlacement();
  101. _placementIDs.clear();
  102. log("重置为低等级设备,清除用户购买信息,清除活动版位");
  103. }else if(data.notify == "点击添加一个活动版位"){
  104. iapshop::IAPPlacement plInfo;
  105. plInfo.id = _placementIDs.size();
  106. _placementIDs.push_back(plInfo.id);
  107. _iapShop->addAPlacement(plInfo);
  108. }else if(data.notify == "点击给活动版位添加卡片"){
  109. auto editBox = dynamic_cast<REDEditBox*>(data.outNode);
  110. if(editBox){
  111. std::string text = editBox->getText();
  112. int index = 0;
  113. try {
  114. index = stoi(text);
  115. }
  116. catch (const std::invalid_argument&) {
  117. log("转化失败");
  118. }
  119. catch (const std::out_of_range&) {
  120. log("超过int值");
  121. }
  122. if(index < _placementIDs.size()){
  123. IAPTestCard* testCard = new IAPTestCard();
  124. _iapShop->addCardToPlacement(_placementIDs[index], testCard);
  125. }
  126. }
  127. }
  128. }
  129. void TestScene::update(float dt){
  130. }