22
33#include " controlblocks.h"
44#include " ../engine/compiler.h"
5+ #include < cassert>
56
67namespace libscratchcpp
78{
@@ -15,12 +16,14 @@ ControlBlocks::ControlBlocks()
1516 addCompileFunction (" control_if" , &compileIfStatement);
1617 addCompileFunction (" control_if_else" , &compileIfElseStatement);
1718 addCompileFunction (" control_stop" , &compileStop);
19+ addCompileFunction (" control_wait" , &compileWait);
1820
1921 // Inputs
2022 addInput (" SUBSTACK" , SUBSTACK);
2123 addInput (" SUBSTACK2" , SUBSTACK2);
2224 addInput (" TIMES" , TIMES);
2325 addInput (" CONDITION" , CONDITION);
26+ addInput (" DURATION" , DURATION);
2427
2528 // Fields
2629 addField (" STOP_OPTION" , STOP_OPTION);
@@ -122,6 +125,13 @@ void ControlBlocks::compileStop(Compiler *compiler)
122125 }
123126}
124127
128+ void ControlBlocks::compileWait (Compiler *compiler)
129+ {
130+ compiler->addInput (DURATION);
131+ compiler->addFunctionCall (&startWait);
132+ compiler->addFunctionCall (&wait);
133+ }
134+
125135unsigned int ControlBlocks::stopAll (VirtualMachine *vm)
126136{
127137 vm->engine ()->stop ();
@@ -134,4 +144,24 @@ unsigned int ControlBlocks::stopOtherScriptsInSprite(VirtualMachine *vm)
134144 return 0 ;
135145}
136146
147+ unsigned int ControlBlocks::startWait (VirtualMachine *vm)
148+ {
149+ auto currentTime = std::chrono::steady_clock::now ();
150+ assert (m_startTimeMap.count (vm) == 0 );
151+ m_timeMap[vm] = { currentTime, vm->getInput (0 , 1 )->toDouble () * 1000 };
152+ return 1 ;
153+ }
154+
155+ unsigned int ControlBlocks::wait (VirtualMachine *vm)
156+ {
157+ auto currentTime = std::chrono::steady_clock::now ();
158+ assert (m_startTimeMap.count (vm) == 1 );
159+ if (std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - m_timeMap[vm].first ).count () >= m_timeMap[vm].second ) {
160+ m_timeMap.erase (vm);
161+ vm->stop (true , true , false );
162+ } else
163+ vm->stop (true , true , true );
164+ return 0 ;
165+ }
166+
137167} // namespace libscratchcpp
0 commit comments