Blame view

ios/Runner/Wowgame/Classes/Parsing/ScenarioParsing/TimerActionParser.cpp 5.14 KB
cb213901   xiaoyu   添加一个游戏的源码和编译选项
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  //
  //  TimerActionParser.cpp
  //  WattsenglishToyApp-mobile
  //
  //  Created by Katarzyna Kalinowska-Górska on 28/12/2019.
  //
  
  #include <stdio.h>
  #include "TimerActionParser.h"
  #include "JSONParseUtils.h"
  #include "ValueStorage.h"
  
  // main parse function
  
  cocos2d::Action* TimerActionParser::parseJSONAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
  {
      cocos2d::Action* parsedAction = NULL;
      if(JSONParseUtils::hasMemberString(jsonActionObject, "actionType")){
          
          std::string actionType = jsonActionObject["actionType"].GetString();
          if(actionType == "startTimer"){
              parsedAction = this->parseStartTimerAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
          }
          else if(actionType == "stopTimer"){
              parsedAction = this->parseStopTimerAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
          }
      }
      
      return parsedAction;
  }
  
  // functions for parsing differenct actions
  
  /*
      "actionType" = "startTimer"
      "seconds" [float]
       "onProgressActions" : [],
       "onTimeUpActions" : [],
       "finishScenarioAfterTimeUp" : true,
       "timeIndicatorObject" : "timeSlider" //needs to conform to the TimeIndicatorInterface
  */
      
  cocos2d::Action* TimerActionParser::parseStartTimerAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
  {
      if(JSONParseUtils::hasMemberFloat(jsonActionObject, "seconds")){
      
          float seconds = jsonActionObject["seconds"].GetFloat();
          
          if(notifyDelegateWhenFinished){
              
              auto storedValueKey = ValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getValueStorageContainerName());
              
              auto actionFunction = [](float pSeconds, std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
              
                  auto storedValue = ValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getValueStorageContainerName());
                  ScenarioObject* sliderObject = ActionParser::getInstance().parseObject("timeIndicatorObject", pParseDelegate);
  
                  if(sliderObject != nullptr){
                      //TODO
                  }
  
                  
                  static int keyModifier = 0;
                  static int modulus = 100;
                  static std::string keyBase = "startTimerAction";
                  std::string key = keyBase + std::to_string((++keyModifier)%modulus);
              
                  
                  
  //                auto action = ActionParser::getInstance().parseJSONAction(array[i], parseDelegate, false);
                  
                  pParseDelegate->scheduleOnce(std::bind([](float, std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){
                      
                      p2ParseDelegate->actionFinished(*ValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getValueStorageContainerName()));
                      ValueStorage::getInstance().removeStoredValue(p2StoredValueKey, p2ParseDelegate->getValueStorageContainerName()); // if the callback is unscheduled, this will not be called. However, when the scene changes, the ValueStorage is always cleared.
                      
                  }, std::placeholders::_1, pStoredValueKey, pParseDelegate), pSeconds, key);
              };
              
              return cocos2d::CallFunc::create(std::bind(actionFunction, seconds, storedValueKey, parseDelegate));
          }
          
          return cocos2d::DelayTime::create(seconds);
      }
      
      return nullptr;
  }
      
  /*
      "actionType" = "stopTimer"
  */
          
  cocos2d::Action* TimerActionParser::parseStopTimerAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
  {
  //    auto storedValueKey = ValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getValueStorageContainerName());
  //
  //    auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
  //
  //        auto storedJsonActionObject = ValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getValueStorageContainerName());
  //
  //        if(ActionParser::getInstance().checkLateCondition(*storedJsonActionObject, pParseDelegate)){
  //
  //            if(JSONParseUtils::hasMemberString(*storedJsonActionObject, "objectClass") && JSONParseUtils::hasMemberString(*storedJsonActionObject, "objectName")){
  //                DynamicObjectMapper mapper;
  //                auto newObject = mapper.createObjectFromClassName((*storedJsonActionObject)["objectClass"].GetString(), *storedJsonActionObject, pParseDelegate);
  //                pParseDelegate->addNewObject((*storedJsonActionObject)["objectName"].GetString(), newObject);
  //            }
  //       }
  //
  //       ValueStorage::getInstance().removeStoredValue(pStoredValueKey, pParseDelegate->getValueStorageContainerName());
  //    };
      
      //TODO !!!!!! --->
  //    return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, std::bind(actionFunction, storedValueKey, parseDelegate));
  }