Skip to content

Commit 30d945b

Browse files
committed
Implement looks_think block
1 parent 2000df5 commit 30d945b

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

src/blocks/looksblocks.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
3030
engine->addCompileFunction(this, "looks_sayforsecs", &compileSayForSecs);
3131
engine->addCompileFunction(this, "looks_say", &compileSay);
3232
engine->addCompileFunction(this, "looks_thinkforsecs", &compileThinkForSecs);
33+
engine->addCompileFunction(this, "looks_think", &compileThink);
3334
engine->addCompileFunction(this, "looks_show", &compileShow);
3435
engine->addCompileFunction(this, "looks_hide", &compileHide);
3536
engine->addCompileFunction(this, "looks_changeeffectby", &compileChangeEffectBy);
@@ -106,6 +107,12 @@ void LooksBlocks::compileThinkForSecs(Compiler *compiler)
106107
compiler->addFunctionCall(&thinkForSecs);
107108
}
108109

110+
void LooksBlocks::compileThink(Compiler *compiler)
111+
{
112+
compiler->addInput(MESSAGE);
113+
compiler->addFunctionCall(&think);
114+
}
115+
109116
void LooksBlocks::compileShow(Compiler *compiler)
110117
{
111118
compiler->addFunctionCall(&show);
@@ -646,6 +653,12 @@ unsigned int LooksBlocks::thinkForSecs(VirtualMachine *vm)
646653
return sayForSecs(vm); // there isn't any difference
647654
}
648655

656+
unsigned int LooksBlocks::think(VirtualMachine *vm)
657+
{
658+
showBubble(vm, Target::BubbleType::Think, vm->getInput(0, 1)->toString());
659+
return 1;
660+
}
661+
649662
unsigned int LooksBlocks::show(VirtualMachine *vm)
650663
{
651664
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());

src/blocks/looksblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class LooksBlocks : public IBlockSection
6565
static void compileSayForSecs(Compiler *compiler);
6666
static void compileSay(Compiler *compiler);
6767
static void compileThinkForSecs(Compiler *compiler);
68+
static void compileThink(Compiler *compiler);
6869
static void compileShow(Compiler *compiler);
6970
static void compileHide(Compiler *compiler);
7071
static void compileChangeEffectBy(Compiler *compiler);
@@ -98,6 +99,7 @@ class LooksBlocks : public IBlockSection
9899

99100
static unsigned int startThinkForSecs(VirtualMachine *vm);
100101
static unsigned int thinkForSecs(VirtualMachine *vm);
102+
static unsigned int think(VirtualMachine *vm);
101103

102104
static unsigned int show(VirtualMachine *vm);
103105
static unsigned int hide(VirtualMachine *vm);

test/blocks/looks_blocks_test.cpp

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ TEST_F(LooksBlocksTest, RegisterBlocks)
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));
111111
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_thinkforsecs", &LooksBlocks::compileThinkForSecs));
112+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_think", &LooksBlocks::compileThink));
112113
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_show", &LooksBlocks::compileShow));
113114
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_hide", &LooksBlocks::compileHide));
114115
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_changeeffectby", &LooksBlocks::compileChangeEffectBy));
@@ -395,9 +396,9 @@ TEST_F(LooksBlocksTest, ThinkForSecs)
395396
TEST_F(LooksBlocksTest, ThinkForSecsImpl)
396397
{
397398
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 bytecode2[] = { vm::OP_START, vm::OP_CONST, 2, vm::OP_EXEC, 2, vm::OP_HALT };
399400
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 BlockFunc functions[] = { &LooksBlocks::startThinkForSecs, &LooksBlocks::thinkForSecs, &LooksBlocks::think };
401402
static Value constValues[] = { "test", 5.5, "hello" };
402403

403404
Target target;
@@ -452,7 +453,7 @@ TEST_F(LooksBlocksTest, ThinkForSecsImpl)
452453
ASSERT_TRUE(target.bubbleText().empty());
453454

454455
// Run the say block while waiting
455-
/*VirtualMachine vm2(&target, &m_engineMock, nullptr);
456+
VirtualMachine vm2(&target, &m_engineMock, nullptr);
456457
vm2.setFunctions(functions);
457458
vm2.setConstValues(constValues);
458459
vm2.setBytecode(bytecode2);
@@ -526,11 +527,57 @@ TEST_F(LooksBlocksTest, ThinkForSecsImpl)
526527
ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm2) == LooksBlocks::m_timeMap.cend());
527528
ASSERT_TRUE(vm2.atEnd());
528529
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
529-
ASSERT_EQ(target.bubbleText(), "test");*/
530+
ASSERT_EQ(target.bubbleText(), "test");
530531

531532
LooksBlocks::clock = Clock::instance().get();
532533
}
533534

535+
TEST_F(LooksBlocksTest, Think)
536+
{
537+
Compiler compiler(&m_engineMock);
538+
539+
// say "Hmm..."
540+
auto block = std::make_shared<Block>("a", "looks_think");
541+
addValueInput(block, "MESSAGE", LooksBlocks::MESSAGE, "Hmm...");
542+
543+
EXPECT_CALL(m_engineMock, functionIndex(&LooksBlocks::think)).WillOnce(Return(0));
544+
545+
compiler.init();
546+
compiler.setBlock(block);
547+
LooksBlocks::compileThink(&compiler);
548+
compiler.end();
549+
550+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }));
551+
ASSERT_EQ(compiler.constValues().size(), 1);
552+
ASSERT_EQ(compiler.constValues()[0].toString(), "Hmm...");
553+
}
554+
555+
TEST_F(LooksBlocksTest, ThinkImpl)
556+
{
557+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
558+
static BlockFunc functions[] = { &LooksBlocks::think };
559+
static Value constValues[] = { "test" };
560+
561+
Target target;
562+
VirtualMachine vm(&target, nullptr, nullptr);
563+
vm.setBytecode(bytecode);
564+
vm.setFunctions(functions);
565+
vm.setConstValues(constValues);
566+
vm.run();
567+
568+
ASSERT_EQ(vm.registerCount(), 0);
569+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
570+
ASSERT_EQ(target.bubbleText(), "test");
571+
572+
target.setBubbleType(Target::BubbleType::Say);
573+
vm.reset();
574+
vm.run();
575+
576+
ASSERT_EQ(vm.registerCount(), 0);
577+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
578+
ASSERT_EQ(target.bubbleText(), "test");
579+
}
580+
534581
TEST_F(LooksBlocksTest, Show)
535582
{
536583
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)