Skip to content

Commit e9e608f

Browse files
committed
ControlBlocks: Add wait block
1 parent 7aa6bd2 commit e9e608f

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/blocks/controlblocks.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "controlblocks.h"
44
#include "../engine/compiler.h"
5+
#include <cassert>
56

67
namespace 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+
125135
unsigned 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

src/blocks/controlblocks.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class LIBSCRATCHCPP_EXPORT ControlBlocks : public IBlockSection
1616
SUBSTACK,
1717
SUBSTACK2,
1818
TIMES,
19-
CONDITION
19+
CONDITION,
20+
DURATION
2021
};
2122

2223
enum Fields
@@ -42,9 +43,15 @@ class LIBSCRATCHCPP_EXPORT ControlBlocks : public IBlockSection
4243
static void compileIfStatement(Compiler *compiler);
4344
static void compileIfElseStatement(Compiler *compiler);
4445
static void compileStop(Compiler *compiler);
46+
static void compileWait(Compiler *compiler);
4547

48+
private:
4649
static unsigned int stopAll(VirtualMachine *vm);
4750
static unsigned int stopOtherScriptsInSprite(VirtualMachine *vm);
51+
static unsigned int startWait(VirtualMachine *vm);
52+
static unsigned int wait(VirtualMachine *vm);
53+
54+
static inline std::unordered_map<VirtualMachine *, std::pair<std::chrono::steady_clock::time_point, int>> m_timeMap;
4855
};
4956

5057
} // namespace libscratchcpp

0 commit comments

Comments
 (0)