Advertisement
Lanzr

new placer

Nov 23rd, 2024 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1.  
  2. local DIRECTS = {
  3.     FRONT = 1,
  4.     RIGHT = 2,
  5.     BACK = 3,
  6.     LEFT = 4
  7. }
  8. ObjTurtle = {
  9.     direct = DIRECTS.RIGHT,
  10.     pos = {x = 0,y = 0},
  11. }
  12.  
  13. local function printDirect(direct)
  14.     if direct == DIRECTS.FRONT then
  15.         print("FRONT")
  16.     elseif direct == DIRECTS.RIGHT then
  17.         print("RIGHT")
  18.     elseif direct == DIRECTS.BACK then
  19.         print("BACK")
  20.     elseif direct == DIRECTS.LEFT then
  21.         print("LEFT")
  22.     end
  23. end
  24.  
  25. local function xor(conditionA,conditionB)
  26.     if conditionA then
  27.         if conditionB then
  28.             return false
  29.         else
  30.             return true
  31.         end
  32.     else
  33.         if conditionB then
  34.             return true
  35.         else
  36.             return false
  37.         end
  38.     end
  39.    
  40. end
  41. function ObjTurtle:toDirect(targetDirect)
  42.     local gap =  targetDirect - self.direct
  43.     local absGap = math.abs(gap)
  44.     local cb_turn
  45.  
  46.     if xor(absGap < 3, gap < 0) then
  47.         cb_turn = function ()
  48.             print("turnRight")
  49.             self.direct = self.direct + 1
  50.             if self.direct > 4 then
  51.                 self.direct = 1
  52.             end
  53.         end
  54.     else
  55.         cb_turn = function ()
  56.             print("turnLeft")
  57.             self.direct = self.direct - 1
  58.             if self.direct < 1 then
  59.                 self.direct = 4
  60.             end
  61.         end
  62.     end
  63.    
  64.     while self.direct ~= targetDirect do
  65.         cb_turn()
  66.     end
  67. end
  68.  
  69. function ObjTurtle:recordMove(targetDirect)
  70.     if targetDirect == DIRECTS.FRONT then
  71.         self.pos.y = self.pos.y + 1
  72.     elseif targetDirect == DIRECTS.BACK then
  73.         self.pos.y = self.pos.y - 1
  74.     elseif targetDirect == DIRECTS.RIGHT then
  75.         self.pos.x = self.pos.x + 1
  76.     elseif targetDirect == DIRECTS.LEFT then
  77.         self.pos.x = self.pos.x - 1
  78.     end
  79. end
  80.  
  81.  
  82.  
  83. local function get_mainDirect(route)
  84.     local mainDirect = {
  85.         first = DIRECTS.FRONT,
  86.         second = DIRECTS.RIGHT
  87.     }
  88.    
  89.     if math.abs(route.x) > math.abs(route.y) then
  90.         if route.x < 0 then
  91.             mainDirect.first = DIRECTS.LEFT
  92.         else
  93.             mainDirect.first = DIRECTS.RIGHT
  94.         end
  95.         if route.y < 0 then
  96.             mainDirect.second = DIRECTS.BACK
  97.         else
  98.             mainDirect.second = DIRECTS.FRONT
  99.         end
  100.     else
  101.         if route.y < 0 then
  102.             mainDirect.first = DIRECTS.BACK
  103.         else
  104.             mainDirect.first = DIRECTS.FRONT
  105.         end
  106.         if route.x < 0 then
  107.             mainDirect.second = DIRECTS.LEFT
  108.         else
  109.             mainDirect.second = DIRECTS.RIGHT
  110.         end
  111.     end
  112.     return mainDirect
  113. end
  114.  
  115. local tst_route = {
  116.     x = 12,
  117.     y = 4,
  118. }
  119.  
  120. function ObjTurtle:tryMove(targetDirect)
  121.     if self.direct == targetDirect then
  122.         -- turtle.forward()
  123.         print("forward")
  124.     else
  125.         self.toDirect(targetDirect)
  126.         -- turtle.forward()
  127.         printDirect(targetDirect)
  128.     end
  129.     self.recordMove(targetDirect)
  130. end
  131.  
  132. function edge_check(selfPos,targetPos,mainDirect)
  133.     -- 检查边缘
  134. end
  135.  
  136. function ObjTurtle:goTo(route)
  137.     -- self-check
  138.     local mainDirect = get_mainDirect(route)
  139.     while true do
  140.         -- self.tryMove(mainDirect.first)
  141.     end
  142. end
  143.  
  144.  
  145. -- ObjTurtle:toDirect(DIRECTS.FRONT)
  146. -- 根据主要方向进行移动和计算
  147. ---[[
  148. --- 海龟需要一个对象去持有面向方向和当前相对坐标
  149. --- 海龟可以移动和旋转
  150. --- 根据路线记录主要前进方向,根据前进方向初始化姿态
  151. --- 移动时的输入办法: 传入实际方向 -> 根据海龟面向方向进行实际判断运动逻辑
  152. --- 定位办法: 记录海龟实际相对坐标 -> 比对实际坐标进行判断是否到达目标点(需要根据主要前进方向进行不同的判断)
  153. ---
  154. ---]]
  155. ---
  156. ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement