Skip to content

Commit 38cf720

Browse files
committed
Implement looks_thinkforsecs block
1 parent dfdaf52 commit 38cf720

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ void LooksBlocks::compileSay(Compiler *compiler)
9797
compiler->addFunctionCall(&say);
9898
}
9999

100+
void LooksBlocks::compileThinkForSecs(Compiler *compiler)
101+
{
102+
compiler->addInput(MESSAGE);
103+
compiler->addInput(SECS);
104+
compiler->addFunctionCall(&startThinkForSecs);
105+
compiler->addFunctionCall(&thinkForSecs);
106+
}
107+
100108
void LooksBlocks::compileShow(Compiler *compiler)
101109
{
102110
compiler->addFunctionCall(&show);
@@ -619,6 +627,24 @@ unsigned int LooksBlocks::say(VirtualMachine *vm)
619627
return 1;
620628
}
621629

630+
unsigned int LooksBlocks::startThinkForSecs(VirtualMachine *vm)
631+
{
632+
Target *target = vm->target();
633+
634+
if (target) {
635+
showBubble(vm, Target::BubbleType::Think, vm->getInput(0, 2)->toString());
636+
m_waitingBubbles[target] = vm;
637+
startWait(vm, vm->getInput(1, 2)->toDouble());
638+
}
639+
640+
return 2;
641+
}
642+
643+
unsigned int LooksBlocks::thinkForSecs(VirtualMachine *vm)
644+
{
645+
return sayForSecs(vm); // there isn't any difference
646+
}
647+
622648
unsigned int LooksBlocks::show(VirtualMachine *vm)
623649
{
624650
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: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,166 @@ TEST_F(LooksBlocksTest, SayImpl)
370370
ASSERT_EQ(target.bubbleText(), "test");
371371
}
372372

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

0 commit comments

Comments
 (0)