Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DIRECTS = {
- FRONT = 1,
- RIGHT = 2,
- BACK = 3,
- LEFT = 4
- }
- ObjTurtle = {
- direct = DIRECTS.RIGHT,
- pos = {x = 0,y = 0},
- }
- local function printDirect(direct)
- if direct == DIRECTS.FRONT then
- print("FRONT")
- elseif direct == DIRECTS.RIGHT then
- print("RIGHT")
- elseif direct == DIRECTS.BACK then
- print("BACK")
- elseif direct == DIRECTS.LEFT then
- print("LEFT")
- end
- end
- local function xor(conditionA,conditionB)
- if conditionA then
- if conditionB then
- return false
- else
- return true
- end
- else
- if conditionB then
- return true
- else
- return false
- end
- end
- end
- function ObjTurtle:toDirect(targetDirect)
- local gap = targetDirect - self.direct
- local absGap = math.abs(gap)
- local cb_turn
- if xor(absGap < 3, gap < 0) then
- cb_turn = function ()
- print("turnRight")
- self.direct = self.direct + 1
- if self.direct > 4 then
- self.direct = 1
- end
- end
- else
- cb_turn = function ()
- print("turnLeft")
- self.direct = self.direct - 1
- if self.direct < 1 then
- self.direct = 4
- end
- end
- end
- while self.direct ~= targetDirect do
- cb_turn()
- end
- end
- function ObjTurtle:recordMove(targetDirect)
- if targetDirect == DIRECTS.FRONT then
- self.pos.y = self.pos.y + 1
- elseif targetDirect == DIRECTS.BACK then
- self.pos.y = self.pos.y - 1
- elseif targetDirect == DIRECTS.RIGHT then
- self.pos.x = self.pos.x + 1
- elseif targetDirect == DIRECTS.LEFT then
- self.pos.x = self.pos.x - 1
- end
- end
- local function get_mainDirect(route)
- local mainDirect = {
- first = DIRECTS.FRONT,
- second = DIRECTS.RIGHT
- }
- if math.abs(route.x) > math.abs(route.y) then
- if route.x < 0 then
- mainDirect.first = DIRECTS.LEFT
- else
- mainDirect.first = DIRECTS.RIGHT
- end
- if route.y < 0 then
- mainDirect.second = DIRECTS.BACK
- else
- mainDirect.second = DIRECTS.FRONT
- end
- else
- if route.y < 0 then
- mainDirect.first = DIRECTS.BACK
- else
- mainDirect.first = DIRECTS.FRONT
- end
- if route.x < 0 then
- mainDirect.second = DIRECTS.LEFT
- else
- mainDirect.second = DIRECTS.RIGHT
- end
- end
- return mainDirect
- end
- local tst_route = {
- x = 12,
- y = 4,
- }
- function ObjTurtle:tryMove(targetDirect)
- if self.direct == targetDirect then
- -- turtle.forward()
- print("forward")
- else
- self.toDirect(targetDirect)
- -- turtle.forward()
- printDirect(targetDirect)
- end
- self.recordMove(targetDirect)
- end
- function edge_check(selfPos,targetPos,mainDirect)
- -- 检查边缘
- end
- function ObjTurtle:goTo(route)
- -- self-check
- local mainDirect = get_mainDirect(route)
- while true do
- -- self.tryMove(mainDirect.first)
- end
- end
- -- ObjTurtle:toDirect(DIRECTS.FRONT)
- -- 根据主要方向进行移动和计算
- ---[[
- --- 海龟需要一个对象去持有面向方向和当前相对坐标
- --- 海龟可以移动和旋转
- --- 根据路线记录主要前进方向,根据前进方向初始化姿态
- --- 移动时的输入办法: 传入实际方向 -> 根据海龟面向方向进行实际判断运动逻辑
- --- 定位办法: 记录海龟实际相对坐标 -> 比对实际坐标进行判断是否到达目标点(需要根据主要前进方向进行不同的判断)
- ---
- ---]]
- ---
- ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement