From 98b8465436daed44310553c843c0b269eb63c867 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 31 Jul 2023 16:40:11 +0200 Subject: [PATCH] isTurtle check --- src/bin/Turtle.hx | 20 ++++++++++++++------ src/kernel/turtle/Turtle.hx | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/bin/Turtle.hx b/src/bin/Turtle.hx index 0bc28ab..727826f 100644 --- a/src/bin/Turtle.hx +++ b/src/bin/Turtle.hx @@ -7,30 +7,38 @@ using tink.CoreApi; class Turtle extends CLIAppBase { public function new() { registerSyncSubcommand("forward", (args) -> { - return perform(kernel.turtle.Turtle.instance.forward()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.forward()); }); registerSyncSubcommand("back", (args) -> { - return perform(kernel.turtle.Turtle.instance.back()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.back()); }); registerSyncSubcommand("left", (args) -> { - return perform(kernel.turtle.Turtle.instance.turnLeft()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.turnLeft()); }); registerSyncSubcommand("right", (args) -> { - return perform(kernel.turtle.Turtle.instance.turnRight()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.turnRight()); }); registerSyncSubcommand("up", (args) -> { - return perform(kernel.turtle.Turtle.instance.up()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.up()); }); registerSyncSubcommand("down", (args) -> { - return perform(kernel.turtle.Turtle.instance.down()); + return checkAvailable() && perform(kernel.turtle.Turtle.instance.down()); }); } + private function checkAvailable(): Bool { + if (!kernel.turtle.Turtle.isTurtle()){ + handle.write("No turtle attached"); + return false; + } + return true; + } + private function perform(outcome:Outcome):Bool { switch outcome { case Success(_): diff --git a/src/kernel/turtle/Turtle.hx b/src/kernel/turtle/Turtle.hx index ac25769..79a6584 100644 --- a/src/kernel/turtle/Turtle.hx +++ b/src/kernel/turtle/Turtle.hx @@ -18,7 +18,7 @@ class Turtle { } public static function isTurtle():Bool { - return true; // TODO: Implement + return cc.Turtle != null; } private function conterToOutcome(r:cc.Turtle.TurtleActionResult):Outcome {