hevohevo

CC: rc_receive1

May 2nd, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.99 KB | None | 0 0
  1. -- rc_receive1
  2. -- ラジコンプログラム(タートル側)
  3.  
  4. -- Functions
  5. function strafeRight()
  6.   turtle.turnRight()
  7.   turtle.forward()
  8.   turtle.turnLeft()
  9. end
  10.  
  11. function strafeLeft()
  12.   turtle.turnLeft()
  13.   turtle.forward()
  14.   turtle.turnRight()
  15. end
  16.  
  17. -- Condition Table(Luaテーブルに関数を入れることで条件分岐を実現)
  18. local cond = {}
  19. cond["strafe_left"] = strafeLeft  -- "strafe_left"メッセージが来たらstrafeLeft関数実行
  20. cond["strafe_right"] = strafeRight
  21. cond["go_forward"] = turtle.forward
  22. cond["go_back"] = turtle.back
  23.  
  24. -- Main
  25. rednet.open("left")  -- 左側に設置したモデムを使うという宣言
  26.  
  27. while true do
  28.   local id, msg = rednet.receive()  -- メッセージを待ち受ける
  29.   print(id, msg)
  30.  
  31.   if cond[msg] then  -- cond[msg]で関数を取り出すことができたら、
  32.     cond[msg]()  -- その関数を実行
  33.   else  -- cond[msg]で関数を取り出せなかったら、
  34.     print("wrong")
  35.   end
  36. end
Add Comment
Please, Sign In to add comment