Update 'Turtle/Turtle.lua'

This commit is contained in:
manuel 2023-01-08 16:36:24 +00:00
parent d46d80d6f2
commit 7006e03461

View File

@ -1,4 +1,5 @@
Turtle = {} Turtle = {}
require("movement")
function Turtle:new(t) function Turtle:new(t)
t = t or {} t = t or {}
@ -25,4 +26,42 @@ function Turtle:assertInvUpdate(update)
if update == true then if update == true then
self:updateInv() self:updateInv()
end end
end
function Turtle:moveTo(target)
target = self:resolveTarget(target)
self:log("moving to target")
moveTo(target[1], target[2], target[3], target[4], self.dir)
self.x, self.y, self.z, self.dir = target[1], target[2], target[3], target[4]
self:log("target reached")
end
function Turtle:offset(position, dir, offset)
self:assertPositionFormat(position)
assert(type(dir) == "number", "dir must be a number")
assert(type(offset) == "number", "offset must be a number")
local newPosition = self:copyPositionTable(position)
dir = dir % 4
if (dir == 0) then newPosition[1] = newPosition[1] - offset
elseif (dir == 1) then newPosition[3] = newPosition[3] - offset
elseif (dir == 2) then newPosition[1] = newPosition[1] + offset
else newPosition[3] = newPosition[3] + offset
end
return newPosition
end
function Turtle:assertPositionFormat(position)
assert(type(position) == "table", "the target is not a table")
assert(type(position[1]) == "number" and type(position[2]) == "number" and type(position[3]) == "number", "the table doesn't contain the position")
assert(type(position[4]) == "number", "direction is missing")
end
function Turtle:log(message)
if self.verbose == true then
print(message)
end
end end