cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
1 
 | 
  //
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
2 
 | 
  //  ToyRepeatedActionScheduler.cpp
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
3
4
5
6
7
8
9 
 | 
  //  SteveMaggieCpp
  //
  //  Created by Katarzyna Kalinowska-Górska on 27.05.2017.
  //
  //
  
  #include <stdio.h>
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
10
11
12
13
14 
 | 
  #include "ToyRepeatedActionScheduler.h"
  #include "ToyActionParser.h"
  #include "ToyJSONParseUtils.h"
  #include "ToyMiscUtils.h"
  #include "ToyValueStorage.h"
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
15 
 | 
  
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
16 
 | 
  void ToyRepeatedActionScheduler::clearAllScheduledActions(ActionParseDelegate* parseDelegate)
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
17
18
19 
 | 
  {
      for(auto pair : _scheduledActionsData){
          parseDelegate->unschedule(pair.second.callbackKey);
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
20 
 | 
          ToyValueStorage::getInstance().removeStoredValue(pair.second.storedValueKey, parseDelegate->getToyValueStorageContainerName());
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
21
22
23
24
25
26
27 
 | 
      }
      
      _scheduledActionsData.clear();
      _waitingFunctions.clear();
  }
  
  //todo write all the stuff we can put to the repeated saction e.g. stpCondition (??)
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
28 
 | 
  cocos2d::Action* ToyRepeatedActionScheduler::scheduleActionIfNeeded(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished){
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
29
30
31
32
33
34
35 
 | 
  
      // if given action is a repeated action and it is not already scheduled
      if(this->isActionValidRepeatedAction(jsonActionObject)){
      
          std::string actionTag = jsonActionObject["repeatActionTag"].GetString();
          if(_scheduledActionsData.find(actionTag) == _scheduledActionsData.end()){
              
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
36 
 | 
              auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
37
38
39 
 | 
          
              auto repeatedFunction = [&](float dt, std::string pStoredValueKey, ActionParseDelegate* pParseDelegate, bool pNotifyDelegate){
                  
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
40
41 
 | 
                  auto& actionParser = ToyActionParser::getInstance();
                  auto storedJsonActionObject = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
42
43 
 | 
                  
                  std::string stopCondition;
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
44
45 
 | 
                  if(ToyJSONParseUtils::hasMemberBool(*storedJsonActionObject, "stopCondition")){
                      stopCondition = ToyMiscUtils::boolToString((*storedJsonActionObject)["stopCondition"].GetBool());
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
46
47
48
49
50
51
52
53
54 
 | 
                  } else {
                      stopCondition = (*storedJsonActionObject)["stopCondition"].GetString();
                  }
                  
                  std::string repeatActionTag = (*storedJsonActionObject)["repeatActionTag"].GetString();
                  
                  if(actionParser.checkCondition(stopCondition, pParseDelegate) == true){
                      pParseDelegate->unschedule(repeatActionTag);
                      _scheduledActionsData.erase(repeatActionTag);
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
55 
 | 
                      ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
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 
 | 
                  
                  } else {
                  
                      auto repeatIntervalSeconds = (*storedJsonActionObject)["repeatIntervalSeconds"].GetFloat();
                      auto repeatTimeSinceUserInteraction = (*storedJsonActionObject)["repeatTimeSinceUserInteraction"].GetFloat();
                  
                      auto now = cocos2d::utils::getTimeInMilliseconds();
                      auto timeSinceLastRun = _scheduledActionsData[repeatActionTag].lastTimeRun == -1 ? repeatIntervalSeconds + 1 : (now - _scheduledActionsData[repeatActionTag].lastTimeRun)/1000;
  
                      auto lastTouchTime = pParseDelegate->getLastScreenTouchTime();
                      auto timeSinceLastUserTouch = lastTouchTime == -1 ? repeatIntervalSeconds + 1 : (now - lastTouchTime)/1000.0;
  
                      if(_scheduledActionsData.find(repeatActionTag) != _scheduledActionsData.end() && (timeSinceLastRun >= repeatIntervalSeconds && timeSinceLastUserTouch >= repeatTimeSinceUserInteraction)){
                      
                          actionParser.parseAndRunAction(*storedJsonActionObject, pParseDelegate, pNotifyDelegate);
                          
                          if(_scheduledActionsData.find(repeatActionTag) != _scheduledActionsData.end()){
                               _scheduledActionsData[repeatActionTag].lastTimeRun = now;
                          }
                           
                      }
                  }
              };
              
              std::function<void(float)> boundRepeatedFunction = std::bind(repeatedFunction, std::placeholders::_1, storedValueKey, parseDelegate, notifyDelegateWhenFinished);
              
              _waitingFunctions.insert({actionTag, boundRepeatedFunction});
              
              auto actionFunction = [&](std::string pStoredValueKey, std::string pActionTag, ActionParseDelegate* pParseDelegate){
  //                log("scheduling repeated funciton");
                  ScheduledActionsData data;
                  data.lastTimeRun = -1;
                  data.callbackKey = pActionTag;
                  data.storedValueKey = pStoredValueKey;
                  _scheduledActionsData[pActionTag] = data;
                  if(_waitingFunctions.find(pActionTag) != _waitingFunctions.end()){
                      auto pRepeatedFunction = _waitingFunctions.at(pActionTag);
                      pParseDelegate->schedule(pRepeatedFunction, pActionTag, 1);
                      _waitingFunctions.erase(pActionTag);
                  }
              };
              
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
98 
 | 
              return ToyActionParser::getInstance().embedFunctionInAction(std::bind(actionFunction, storedValueKey, actionTag, parseDelegate), jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
99
100
101
102
103
104
105 
 | 
              
          }
      }
      
      return NULL;
  }
  
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
106 
 | 
  bool ToyRepeatedActionScheduler::isActionValidRepeatedAction(const rapidjson::Value& jsonActionObject)
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
107 
 | 
  { //todo maybe introduce such method to all parsed actions
 
 | 
5daad4bc
 
  xiaoyu
 
游戏源码添加编译(现存问题:游戏内...
 | 
108 
 | 
      return ToyJSONParseUtils::checkMemberBool(jsonActionObject, "repeat", true) && ToyJSONParseUtils::hasMemberFloat(jsonActionObject, "repeatIntervalSeconds") && ToyJSONParseUtils::hasMemberString(jsonActionObject, "repeatActionTag");
 
 | 
cb213901
 
  xiaoyu
 
添加一个游戏的源码和编译选项
 | 
109 
 | 
  }
 
 |