Advertisement
Zeriab

Robot movement prototype

Dec 30th, 2012
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.32 KB | None | 0 0
  1. ###
  2. # Base
  3. ###
  4.  
  5. class CommandQueue
  6.   def initialize(*args)
  7.     @array = args
  8.     @current = nil
  9.   end
  10.  
  11.   def <<(*args)
  12.     @array << args
  13.     @array.flatten!
  14.   end
  15.   alias add <<
  16.  
  17.   def addFront(*args)
  18.     @array.unshift(args)
  19.     @array.flatten!
  20.   end
  21.  
  22.   def update
  23.     unless @current.nil?
  24.       @current.update
  25.       if @current.delay_finished?
  26.         @current.finalize
  27.         @current = nil
  28.       end
  29.     end
  30.     while @current.nil?
  31.       return :continue if @array.empty?
  32.       @current = @array.shift
  33.       @current.initiate
  34.       if @current.delay_finished?
  35.         @current.finalize
  36.         @current = nil
  37.       end
  38.     end
  39.     :wait
  40.   end
  41.  
  42.   def store
  43.     @back_array = @array.clone
  44.   end
  45.   def retrieve
  46.     @array = @back_array
  47.   end
  48.  
  49. end
  50.  
  51. class Command
  52.   attr_accessor :delay_enabled
  53.   def initialize
  54.     @delay_enabled = true
  55.     @delay = @max_delay = 10
  56.   end
  57.  
  58.   def initiate
  59.     # For subclasses to override.
  60.   end
  61.  
  62.   def update
  63.     # For subclasses to override.
  64.     # Note that the method is only triggered
  65.     # if the command is not finished.
  66.   end
  67.  
  68.   def finished?
  69.     # For subclasses to override if the command
  70.     # takes time to execute
  71.     return true
  72.   end
  73.  
  74.   def finalize
  75.     @delay = @max_delay
  76.   end
  77.  
  78.   def delay_finished?
  79.     return finished? unless @delay_enabled
  80.     if @delay < 0
  81.       finished?
  82.     else
  83.       @delay -= 1
  84.       false
  85.     end
  86.   end
  87. end
  88.  
  89.  
  90. class Command_Event < Command
  91.   def initialize(event_id, *args)
  92.     super(*args)
  93.     @event_id = event_id
  94.   end
  95.  
  96.   def event
  97.     return $game_map.events[@event_id]
  98.   end
  99. end
  100.  
  101.  
  102. class Command_Macro < Command
  103.   attr_accessor :queue
  104.   attr_accessor :commands
  105.   def initialize(queue, *args)
  106.     super(*args)
  107.     self.queue = queue
  108.     @commands = []
  109.   end
  110.  
  111.   def initiate
  112.     queue.addFront(*@commands.compact)
  113.   end
  114. end
  115.  
  116.  
  117.  
  118. ###
  119. # Specific commands
  120. ###
  121.  
  122. class Command_Forward < Command_Event
  123.   def initiate
  124.     event.move_forward
  125.   end
  126.   def finished?
  127.     !event.moving?
  128.   end
  129. end
  130.  
  131. class Command_Left < Command_Event
  132.   def initiate
  133.     event.move_left
  134.   end
  135.   def finished?
  136.     !event.moving?
  137.   end
  138. end
  139.  
  140. class Command_Right < Command_Event
  141.   def initiate
  142.     event.move_right
  143.   end
  144.   def finished?
  145.     !event.moving?
  146.   end
  147. end
  148.  
  149. class Command_Down < Command_Event
  150.   def initiate
  151.     event.move_down
  152.   end
  153.   def finished?
  154.     !event.moving?
  155.   end
  156. end
  157.  
  158. class Command_Up < Command_Event
  159.   def initiate
  160.     event.move_up
  161.   end
  162.   def finished?
  163.     !event.moving?
  164.   end
  165. end
  166.  
  167. class Command_LeftTurn < Command_Event
  168.   def initiate
  169.     event.turn_left_90
  170.   end
  171. end
  172.  
  173. class Command_RightTurn < Command_Event
  174.   def initiate
  175.     event.turn_right_90
  176.   end
  177. end
  178.  
  179. class Command_Flip < Command_Event
  180.   def initiate
  181.     event.turn_180
  182.   end
  183. end
  184.  
  185.  
  186. __END__
  187. # Test code
  188. $test = CommandQueue.new
  189. $test << Command_Right.new(1)
  190. $test << Command_Up.new(1)
  191. $test << Command_Left.new(1)
  192. $test << Command_Left.new(1)
  193.  
  194. c1 = Command_Forward.new(1)
  195. c2 = Command_Forward.new(1)
  196. c3 = Command_LeftTurn.new(1)
  197. c4 = Command_RightTurn.new(1)
  198. c5 = Command_Flip.new(1)
  199. $test << c1
  200. $test << c2
  201. $test << c3
  202. $test << c1
  203. $test << c3
  204. $test << c1
  205. $test << c4
  206. $test << c2
  207. $test << c2
  208. $test << c5
  209. $test << c5
  210. $test << c5
  211. $test << c2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement