Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------------
- -- COMMANDS
- -------------------------------------------------------------
- local command = {}
- command.new = function()
- local self = {}
- self.name = "default"
- function self:execute()
- table.insert(Tu.commandStack, self)
- print(string.format("execute: %s", self.name))
- end
- function self:undo()
- print(string.format("undo: %s", self.name))
- end
- return self
- end
- -- command forward
- local com_forward = {}
- com_forward.new = function ()
- local self = command.new()
- self.name = "forward"
- local super_execute = self.execute
- function self:execute()
- local valid = turtle.forward()
- if valid == false then
- return false
- end
- super_execute(self)
- return true
- end
- local super_undo = self.undo
- function self:undo()
- local valid = turtle.back()
- if valid == false then
- return false
- end
- super_undo(self)
- return true
- end
- return self
- end
- -- command back
- local com_back = {}
- com_back.new = function ()
- local self = command.new()
- self.name = "back"
- local super_execute = self.execute
- function self:execute()
- local valid = turtle.back()
- if valid == false then
- return false
- end
- super_execute(self)
- return true
- end
- local super_undo = self.undo
- function self:undo()
- local valid = turtle.forward()
- if valid == false then
- return false
- end
- super_undo(self)
- return true
- end
- return self
- end
- -- command up
- local com_up = {}
- com_up.new = function ()
- local self = command.new()
- self.name = "up"
- local super_execute = self.execute
- function self:execute()
- local valid = turtle.up()
- if valid == false then
- return false
- end
- super_execute(self)
- return true
- end
- local super_undo = self.undo
- function self:undo()
- local valid = turtle.down()
- if valid == false then
- return false
- end
- super_undo(self)
- return true
- end
- return self
- end
- -- command down
- local com_down = {}
- com_down.new = function ()
- local self = command.new()
- self.name = "down"
- local super_execute = self.execute
- function self:execute()
- local valid = turtle.down()
- if valid == false then
- return false
- end
- super_execute(self)
- return true
- end
- local super_undo = self.undo
- function self:undo()
- local valid = turtle.up()
- if valid == false then
- return false
- end
- super_undo(self)
- return true
- end
- return self
- end
- -- command turnLeft
- local com_turnLeft = {}
- com_turnLeft.new = function ()
- local self = command.new()
- self.name = "down"
- local super_execute = self.execute
- function self:execute()
- local valid = turtle.turnLeft()
- if valid == false then
- return false
- end
- super_execute(self)
- return true
- end
- local super_undo = self.undo
- function self:undo()
- local valid = turtle.turnRight()
- if valid == false then
- return false
- end
- super_undo(self)
- return true
- end
- return self
- end
- -- command turnRight
- local com_turnRight = {}
- com_turnRight.new = function ()
- local self = command.new()
- self.name = "down"
- local super_execute = self.execute
- function self:execute()
- local valid = turtle.turnRight()
- if valid == false then
- return false
- end
- super_execute(self)
- return true
- end
- local super_undo = self.undo
- function self:undo()
- local valid = turtle.turnLeft()
- if valid == false then
- return false
- end
- super_undo(self)
- return true
- end
- return self
- end
- -------------------------------------------------------------
- -- INSTANCE
- -------------------------------------------------------------
- -- instance
- Tu = {}
- Tu.active = false
- Tu.updateTime = 1.0
- Tu.elapsedTime = 0.0
- -- commands
- Tu.commandStack = {}
- Tu.forward = com_forward.new()
- Tu.back = com_back.new()
- Tu.up = com_up.new()
- Tu.down = com_down.new()
- Tu.turnLeft = com_turnLeft.new()
- Tu.turnRight = com_turnRight.new()
- function Tu.undoStack()
- for i = 1, #Tu.commandStack, 1 do
- Tu.commandStack[i]:undo()
- end
- end
- function Tu.getLastCommand()
- if #Tu.commandStack == 0 then
- return nil
- end
- return Tu.commandStack[#Tu.commandStack]
- end
- function Tu.start()
- print("start")
- Tu.turnRight:execute()
- Tu.turnLeft:execute()
- for i = 1, 20, 1 do
- turtle.digDown()
- Tu.down:execute()
- end
- Tu.undoStack()
- end
- function Tu.update()
- if turtle.getFuelLevel() > 0 then
- end
- end
- Tu.start()
- while Tu.active == true do
- Tu.elapsedTime = os.clock()
- if Tu.updateTime == 0.0 then
- Tu.update()
- return
- end
- local currentTime = Tu.elapsedTime % Tu.updateTime
- if currentTime == 0.0 then
- Tu.update()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement