From b0cf4ad73fb5516b3c08b8777084b4b399c554eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9tro?= <yann.roberge@polymtl.ca> Date: Fri, 23 Oct 2020 23:25:13 -0400 Subject: [PATCH] =?UTF-8?q?Prise=20en=20compte=20du=20param=C3=A8tre=20'ac?= =?UTF-8?q?tion'=20dans=20goThroughArea?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rsiTurtle.lua | 47 +++++++++++++++++++++++++++++++++++++---------- test.lua | 4 ++-- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/rsiTurtle.lua b/rsiTurtle.lua index 4b49e1a..8303496 100644 --- a/rsiTurtle.lua +++ b/rsiTurtle.lua @@ -207,8 +207,8 @@ end -- \brief Déplacement avant multiple -- \param distance Distance à parcourir -- \return bool Succès -function RSI_Turtle:moveForwardGrounded(distance) - return self:moveForward(distance, true) +function RSI_Turtle:moveForwardGrounded(distance, action) + return self:moveForward(distance, true, action) end @@ -277,9 +277,10 @@ end --- -- \brief Déplacement avant multiple -- \param distance Distance à parcourir --- \param bool grounded Se déplacer en restant toujours au niveau du sol +-- \param bool grounded Se déplacer en restant toujours au niveau du sol +-- \param function_pointer action Exécuté après chaque déplacement -- \return bool Succès -function RSI_Turtle:moveForward(distance, grounded) +function RSI_Turtle:moveForward(distance, grounded, action) local traveled = 0 if (grounded) @@ -290,6 +291,11 @@ function RSI_Turtle:moveForward(distance, grounded) while (traveled < distance) and turtle.forward() do + if (action ~= nil) + then + action() + end + if (grounded) then self:ground() @@ -324,12 +330,31 @@ end --- -- \brief Déplacement arrière -- \param distance Distance à parcourir +-- \param bool grounded Se déplacer en restant toujours au niveau du sol +-- \param function_pointer action Exécuté après chaque déplacement -- \return bool Succès -function RSI_Turtle:moveBackward(distance) +function RSI_Turtle:moveBackward(distance, grounded, action) local traveled = 0 + if (grounded) + then + self:ground() + self:climb() + end + while (traveled < distance) and turtle.back() do + if (action ~= nil) + then + action() + end + + if (grounded) + then + self:ground() + self:climb() + end + traveled = traveled + 1 end @@ -441,12 +466,12 @@ end -- ligne. N'affecte pas l'altitude. -- \param pointeur_de_fonction action Pointeur de l'action à réaliser juste -- après être arrivé sur chaque nouveau bloc. --- \param int lines Nombre de lignes (direction: avant) --- \param int cols Nombre de colonnes (direction: droite) --- \param bool grounded Se déplacer en restant toujours au niveau du sol +-- \param int lines Nombre de lignes (direction: avant) +-- \param int cols Nombre de colonnes (direction: droite) +-- \param function_pointer action Action immédiatement après chaque déplacement +-- \param bool grounded Se déplacer en restant toujours au niveau du sol -- \return bool Succès function RSI_Turtle:goThroughArea(lines, cols, action, grounded) - --TODO: Prendre en compte le paramètre "action" local success = true assert(lines >= 1) @@ -459,11 +484,12 @@ function RSI_Turtle:goThroughArea(lines, cols, action, grounded) then self:ground() end + action() for i=1,cols,1 do print("Debut de colonne.") - success = self:moveForward(lines, grounded) + success = self:moveForward(lines, grounded, action) if not success then @@ -478,6 +504,7 @@ function RSI_Turtle:goThroughArea(lines, cols, action, grounded) success = self:right(grounded) self:turnRight() end + action() end return success diff --git a/test.lua b/test.lua index 610e8ab..498543d 100644 --- a/test.lua +++ b/test.lua @@ -146,8 +146,8 @@ end -- assert(t:goThroughArea(5, 5, nil)) -- assert(checkPos(4,0,4)) --- assert(t:goThroughArea(15, 15, nil, true)) -assert(t:goThroughAreaGrounded(3, 3)) +-- assert(t:goThroughArea(15, 15, true, nil)) +assert(t:goThroughAreaGrounded(3, 3, turtle.placeUp)) assert(t:moveToOrigin()) print(t:getPos()) -- GitLab