44#include < scratchcpp/compiler.h>
55#include < scratchcpp/sprite.h>
66#include < scratchcpp/input.h>
7+ #include < scratchcpp/field.h>
78
89#include " motionblocks.h"
910#include " ../engine/internal/randomgenerator.h"
@@ -36,6 +37,11 @@ void MotionBlocks::registerBlocks(IEngine *engine)
3637 engine->addCompileFunction (this , " motion_changexby" , &compileChangeXBy);
3738 engine->addCompileFunction (this , " motion_setx" , &compileSetX);
3839 engine->addCompileFunction (this , " motion_changeyby" , &compileChangeYBy);
40+ engine->addCompileFunction (this , " motion_sety" , &compileSetY);
41+ engine->addCompileFunction (this , " motion_setrotationstyle" , &compileSetRotationStyle);
42+ engine->addCompileFunction (this , " motion_xposition" , &compileXPosition);
43+ engine->addCompileFunction (this , " motion_yposition" , &compileYPosition);
44+ engine->addCompileFunction (this , " motion_direction" , &compileDirection);
3945
4046 // Inputs
4147 engine->addInput (this , " STEPS" , STEPS);
@@ -48,6 +54,9 @@ void MotionBlocks::registerBlocks(IEngine *engine)
4854 engine->addInput (this , " SECS" , SECS);
4955 engine->addInput (this , " DX" , DX);
5056 engine->addInput (this , " DY" , DY);
57+
58+ // Fields
59+ engine->addField (this , " STYLE" , STYLE);
5160}
5261
5362void MotionBlocks::compileMoveSteps (Compiler *compiler)
@@ -181,6 +190,49 @@ void MotionBlocks::compileChangeYBy(Compiler *compiler)
181190 compiler->addFunctionCall (&changeYBy);
182191}
183192
193+ void MotionBlocks::compileSetY (Compiler *compiler)
194+ {
195+ compiler->addInput (Y);
196+ compiler->addFunctionCall (&setY);
197+ }
198+
199+ void MotionBlocks::compileSetRotationStyle (Compiler *compiler)
200+ {
201+ int option = compiler->field (STYLE)->specialValueId ();
202+
203+ switch (option) {
204+ case LeftRight:
205+ compiler->addFunctionCall (&setLeftRightRotationStyle);
206+ break ;
207+
208+ case DoNotRotate:
209+ compiler->addFunctionCall (&setDoNotRotateRotationStyle);
210+ break ;
211+
212+ case AllAround:
213+ compiler->addFunctionCall (&setAllAroundRotationStyle);
214+ break ;
215+
216+ default :
217+ break ;
218+ }
219+ }
220+
221+ void MotionBlocks::compileXPosition (Compiler *compiler)
222+ {
223+ compiler->addFunctionCall (&xPosition);
224+ }
225+
226+ void MotionBlocks::compileYPosition (Compiler *compiler)
227+ {
228+ compiler->addFunctionCall (&yPosition);
229+ }
230+
231+ void MotionBlocks::compileDirection (Compiler *compiler)
232+ {
233+ compiler->addFunctionCall (&direction);
234+ }
235+
184236unsigned int MotionBlocks::moveSteps (VirtualMachine *vm)
185237{
186238 Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
@@ -572,3 +624,79 @@ unsigned int MotionBlocks::changeYBy(VirtualMachine *vm)
572624
573625 return 1 ;
574626}
627+
628+ unsigned int MotionBlocks::setY (VirtualMachine *vm)
629+ {
630+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
631+
632+ if (sprite)
633+ sprite->setY (vm->getInput (0 , 1 )->toDouble ());
634+
635+ return 1 ;
636+ }
637+
638+ unsigned int MotionBlocks::setLeftRightRotationStyle (VirtualMachine *vm)
639+ {
640+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
641+
642+ if (sprite)
643+ sprite->setRotationStyle (Sprite::RotationStyle::LeftRight);
644+
645+ return 0 ;
646+ }
647+
648+ unsigned int MotionBlocks::setDoNotRotateRotationStyle (VirtualMachine *vm)
649+ {
650+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
651+
652+ if (sprite)
653+ sprite->setRotationStyle (Sprite::RotationStyle::DoNotRotate);
654+
655+ return 0 ;
656+ }
657+
658+ unsigned int MotionBlocks::setAllAroundRotationStyle (VirtualMachine *vm)
659+ {
660+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
661+
662+ if (sprite)
663+ sprite->setRotationStyle (Sprite::RotationStyle::AllAround);
664+
665+ return 0 ;
666+ }
667+
668+ unsigned int MotionBlocks::xPosition (VirtualMachine *vm)
669+ {
670+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
671+
672+ if (sprite)
673+ vm->addReturnValue (sprite->x ());
674+ else
675+ vm->addReturnValue (0 );
676+
677+ return 0 ;
678+ }
679+
680+ unsigned int MotionBlocks::yPosition (VirtualMachine *vm)
681+ {
682+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
683+
684+ if (sprite)
685+ vm->addReturnValue (sprite->y ());
686+ else
687+ vm->addReturnValue (0 );
688+
689+ return 0 ;
690+ }
691+
692+ unsigned int MotionBlocks::direction (VirtualMachine *vm)
693+ {
694+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
695+
696+ if (sprite)
697+ vm->addReturnValue (sprite->direction ());
698+ else
699+ vm->addReturnValue (0 );
700+
701+ return 0 ;
702+ }
0 commit comments