Skip to content

Commit 2000df5

Browse files
committed
Implement looks_thinkforsecs block
1 parent dfdaf52 commit 2000df5

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
2929
// Blocks
3030
engine->addCompileFunction(this, "looks_sayforsecs", &compileSayForSecs);
3131
engine->addCompileFunction(this, "looks_say", &compileSay);
32+
engine->addCompileFunction(this, "looks_thinkforsecs", &compileThinkForSecs);
3233
engine->addCompileFunction(this, "looks_show", &compileShow);
3334
engine->addCompileFunction(this, "looks_hide", &compileHide);
3435
engine->addCompileFunction(this, "looks_changeeffectby", &compileChangeEffectBy);
@@ -97,6 +98,14 @@ void LooksBlocks::compileSay(Compiler *compiler)
9798
compiler->addFunctionCall(&say);
9899
}
99100

101+
void LooksBlocks::compileThinkForSecs(Compiler *compiler)
102+
{
103+
compiler->addInput(MESSAGE);
104+
compiler->addInput(SECS);
105+
compiler->addFunctionCall(&startThinkForSecs);
106+
compiler->addFunctionCall(&thinkForSecs);
107+
}
108+
100109
void LooksBlocks::compileShow(Compiler *compiler)
101110
{
102111
compiler->addFunctionCall(&show);
@@ -619,6 +628,24 @@ unsigned int LooksBlocks::say(VirtualMachine *vm)
619628
return 1;
620629
}
621630

631+
unsigned int LooksBlocks::startThinkForSecs(VirtualMachine *vm)
632+
{
633+
Target *target = vm->target();
634+
635+
if (target) {
636+
showBubble(vm, Target::BubbleType::Think, vm->getInput(0, 2)->toString());
637+
m_waitingBubbles[target] = vm;
638+
startWait(vm, vm->getInput(1, 2)->toDouble());
639+
}
640+
641+
return 2;
642+
}
643+
644+
unsigned int LooksBlocks::thinkForSecs(VirtualMachine *vm)
645+
{
646+
return sayForSecs(vm); // there isn't any difference
647+
}
648+
622649
unsigned int LooksBlocks::show(VirtualMachine *vm)
623650
{
624651
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());

src/blocks/looksblocks.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class LooksBlocks : public IBlockSection
6464

6565
static void compileSayForSecs(Compiler *compiler);
6666
static void compileSay(Compiler *compiler);
67+
static void compileThinkForSecs(Compiler *compiler);
6768
static void compileShow(Compiler *compiler);
6869
static void compileHide(Compiler *compiler);
6970
static void compileChangeEffectBy(Compiler *compiler);
@@ -95,6 +96,9 @@ class LooksBlocks : public IBlockSection
9596
static unsigned int sayForSecs(VirtualMachine *vm);
9697
static unsigned int say(VirtualMachine *vm);
9798

99+
static unsigned int startThinkForSecs(VirtualMachine *vm);
100+
static unsigned int thinkForSecs(VirtualMachine *vm);
101+
98102
static unsigned int show(VirtualMachine *vm);
99103
static unsigned int hide(VirtualMachine *vm);
100104

test/blocks/looks_blocks_test.cpp

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ TEST_F(LooksBlocksTest, RegisterBlocks)
108108
// Blocks
109109
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_sayforsecs", &LooksBlocks::compileSayForSecs));
110110
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_say", &LooksBlocks::compileSay));
111+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_thinkforsecs", &LooksBlocks::compileThinkForSecs));
111112
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_show", &LooksBlocks::compileShow));
112113
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_hide", &LooksBlocks::compileHide));
113114
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_changeeffectby", &LooksBlocks::compileChangeEffectBy));
@@ -370,6 +371,166 @@ TEST_F(LooksBlocksTest, SayImpl)
370371
ASSERT_EQ(target.bubbleText(), "test");
371372
}
372373

374+
TEST_F(LooksBlocksTest, ThinkForSecs)
375+
{
376+
Compiler compiler(&m_engineMock);
377+
378+
// think "Hmm..." for 3.5 seconds
379+
auto block = std::make_shared<Block>("a", "looks_thinkforsecs");
380+
addValueInput(block, "MESSAGE", LooksBlocks::MESSAGE, "Hmm...");
381+
addValueInput(block, "SECS", LooksBlocks::SECS, 3.5);
382+
383+
EXPECT_CALL(m_engineMock, functionIndex(&LooksBlocks::startThinkForSecs)).WillOnce(Return(0));
384+
EXPECT_CALL(m_engineMock, functionIndex(&LooksBlocks::thinkForSecs)).WillOnce(Return(1));
385+
386+
compiler.init();
387+
compiler.setBlock(block);
388+
LooksBlocks::compileThinkForSecs(&compiler);
389+
compiler.end();
390+
391+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_EXEC, 1, vm::OP_HALT }));
392+
ASSERT_EQ(compiler.constValues(), std::vector<Value>({ "Hmm...", 3.5 }));
393+
}
394+
395+
TEST_F(LooksBlocksTest, ThinkForSecsImpl)
396+
{
397+
static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_EXEC, 1, vm::OP_HALT };
398+
// static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 2, vm::OP_EXEC, 2, vm::OP_HALT };
399+
static unsigned int bytecode3[] = { vm::OP_START, vm::OP_CONST, 2, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_EXEC, 1, vm::OP_HALT };
400+
static BlockFunc functions[] = { &LooksBlocks::startThinkForSecs, &LooksBlocks::thinkForSecs /*, &LooksBlocks::think*/ };
401+
static Value constValues[] = { "test", 5.5, "hello" };
402+
403+
Target target;
404+
target.setBubbleType(Target::BubbleType::Say);
405+
VirtualMachine vm(&target, &m_engineMock, nullptr);
406+
vm.setFunctions(functions);
407+
vm.setConstValues(constValues);
408+
vm.setBytecode(bytecode1);
409+
410+
ClockMock clock;
411+
LooksBlocks::clock = &clock;
412+
413+
std::chrono::steady_clock::time_point startTime(std::chrono::milliseconds(1000));
414+
EXPECT_CALL(clock, currentSteadyTime()).Times(2).WillRepeatedly(Return(startTime));
415+
EXPECT_CALL(m_engineMock, requestRedraw());
416+
vm.run();
417+
418+
ASSERT_EQ(vm.registerCount(), 0);
419+
ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm) != LooksBlocks::m_timeMap.cend());
420+
ASSERT_FALSE(vm.atEnd());
421+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
422+
ASSERT_EQ(target.bubbleText(), "test");
423+
424+
std::chrono::steady_clock::time_point time1(std::chrono::milliseconds(6450));
425+
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time1));
426+
target.setBubbleType(Target::BubbleType::Say);
427+
target.setBubbleText("another");
428+
vm.run();
429+
430+
ASSERT_EQ(vm.registerCount(), 0);
431+
ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm) != LooksBlocks::m_timeMap.cend());
432+
ASSERT_FALSE(vm.atEnd());
433+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Say);
434+
ASSERT_EQ(target.bubbleText(), "another");
435+
436+
std::chrono::steady_clock::time_point time2(std::chrono::milliseconds(6500));
437+
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time2));
438+
vm.run();
439+
440+
ASSERT_EQ(vm.registerCount(), 0);
441+
ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm) == LooksBlocks::m_timeMap.cend());
442+
ASSERT_FALSE(vm.atEnd());
443+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Say);
444+
ASSERT_TRUE(target.bubbleText().empty());
445+
446+
vm.run();
447+
448+
ASSERT_EQ(vm.registerCount(), 0);
449+
ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm) == LooksBlocks::m_timeMap.cend());
450+
ASSERT_TRUE(vm.atEnd());
451+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Say);
452+
ASSERT_TRUE(target.bubbleText().empty());
453+
454+
// Run the say block while waiting
455+
/*VirtualMachine vm2(&target, &m_engineMock, nullptr);
456+
vm2.setFunctions(functions);
457+
vm2.setConstValues(constValues);
458+
vm2.setBytecode(bytecode2);
459+
460+
EXPECT_CALL(clock, currentSteadyTime()).Times(2).WillRepeatedly(Return(startTime));
461+
EXPECT_CALL(m_engineMock, requestRedraw());
462+
vm.reset();
463+
vm.run();
464+
465+
ASSERT_EQ(vm.registerCount(), 0);
466+
ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm) != LooksBlocks::m_timeMap.cend());
467+
ASSERT_FALSE(vm.atEnd());
468+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
469+
ASSERT_EQ(target.bubbleText(), "test");
470+
471+
vm2.run();
472+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
473+
ASSERT_EQ(target.bubbleText(), "hello");
474+
475+
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time2));
476+
vm.run();
477+
478+
ASSERT_EQ(vm.registerCount(), 0);
479+
ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm) == LooksBlocks::m_timeMap.cend());
480+
ASSERT_FALSE(vm.atEnd());
481+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
482+
ASSERT_EQ(target.bubbleText(), "hello");
483+
484+
vm.run();
485+
486+
ASSERT_EQ(vm.registerCount(), 0);
487+
ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm) == LooksBlocks::m_timeMap.cend());
488+
ASSERT_TRUE(vm.atEnd());
489+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
490+
ASSERT_EQ(target.bubbleText(), "hello");
491+
492+
// Run the say for secs block while waiting
493+
vm2.reset();
494+
vm2.setBytecode(bytecode3);
495+
496+
EXPECT_CALL(clock, currentSteadyTime()).Times(2).WillRepeatedly(Return(startTime));
497+
EXPECT_CALL(m_engineMock, requestRedraw());
498+
vm2.reset();
499+
vm2.run();
500+
501+
ASSERT_EQ(vm2.registerCount(), 0);
502+
ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm2) != LooksBlocks::m_timeMap.cend());
503+
ASSERT_FALSE(vm2.atEnd());
504+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
505+
ASSERT_EQ(target.bubbleText(), "hello");
506+
507+
EXPECT_CALL(clock, currentSteadyTime()).Times(2).WillRepeatedly(Return(startTime));
508+
EXPECT_CALL(m_engineMock, requestRedraw());
509+
vm.reset();
510+
vm.run();
511+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
512+
ASSERT_EQ(target.bubbleText(), "test");
513+
514+
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time2));
515+
vm2.run();
516+
517+
ASSERT_EQ(vm2.registerCount(), 0);
518+
ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm2) == LooksBlocks::m_timeMap.cend());
519+
ASSERT_FALSE(vm2.atEnd());
520+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
521+
ASSERT_EQ(target.bubbleText(), "test");
522+
523+
vm2.run();
524+
525+
ASSERT_EQ(vm2.registerCount(), 0);
526+
ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm2) == LooksBlocks::m_timeMap.cend());
527+
ASSERT_TRUE(vm2.atEnd());
528+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
529+
ASSERT_EQ(target.bubbleText(), "test");*/
530+
531+
LooksBlocks::clock = Clock::instance().get();
532+
}
533+
373534
TEST_F(LooksBlocksTest, Show)
374535
{
375536
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)