Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###
- # Base
- ###
- class CommandQueue
- def initialize(*args)
- @array = args
- @current = nil
- end
- def <<(*args)
- @array << args
- @array.flatten!
- end
- alias add <<
- def addFront(*args)
- @array.unshift(args)
- @array.flatten!
- end
- def update
- unless @current.nil?
- @current.update
- if @current.delay_finished?
- @current.finalize
- @current = nil
- end
- end
- while @current.nil?
- return :continue if @array.empty?
- @current = @array.shift
- @current.initiate
- if @current.delay_finished?
- @current.finalize
- @current = nil
- end
- end
- :wait
- end
- def store
- @back_array = @array.clone
- end
- def retrieve
- @array = @back_array
- end
- end
- class Command
- attr_accessor :delay_enabled
- def initialize
- @delay_enabled = true
- @delay = @max_delay = 10
- end
- def initiate
- # For subclasses to override.
- end
- def update
- # For subclasses to override.
- # Note that the method is only triggered
- # if the command is not finished.
- end
- def finished?
- # For subclasses to override if the command
- # takes time to execute
- return true
- end
- def finalize
- @delay = @max_delay
- end
- def delay_finished?
- return finished? unless @delay_enabled
- if @delay < 0
- finished?
- else
- @delay -= 1
- false
- end
- end
- end
- class Command_Event < Command
- def initialize(event_id, *args)
- super(*args)
- @event_id = event_id
- end
- def event
- return $game_map.events[@event_id]
- end
- end
- class Command_Macro < Command
- attr_accessor :queue
- attr_accessor :commands
- def initialize(queue, *args)
- super(*args)
- self.queue = queue
- @commands = []
- end
- def initiate
- queue.addFront(*@commands.compact)
- end
- end
- ###
- # Specific commands
- ###
- class Command_Forward < Command_Event
- def initiate
- event.move_forward
- end
- def finished?
- !event.moving?
- end
- end
- class Command_Left < Command_Event
- def initiate
- event.move_left
- end
- def finished?
- !event.moving?
- end
- end
- class Command_Right < Command_Event
- def initiate
- event.move_right
- end
- def finished?
- !event.moving?
- end
- end
- class Command_Down < Command_Event
- def initiate
- event.move_down
- end
- def finished?
- !event.moving?
- end
- end
- class Command_Up < Command_Event
- def initiate
- event.move_up
- end
- def finished?
- !event.moving?
- end
- end
- class Command_LeftTurn < Command_Event
- def initiate
- event.turn_left_90
- end
- end
- class Command_RightTurn < Command_Event
- def initiate
- event.turn_right_90
- end
- end
- class Command_Flip < Command_Event
- def initiate
- event.turn_180
- end
- end
- __END__
- # Test code
- $test = CommandQueue.new
- $test << Command_Right.new(1)
- $test << Command_Up.new(1)
- $test << Command_Left.new(1)
- $test << Command_Left.new(1)
- c1 = Command_Forward.new(1)
- c2 = Command_Forward.new(1)
- c3 = Command_LeftTurn.new(1)
- c4 = Command_RightTurn.new(1)
- c5 = Command_Flip.new(1)
- $test << c1
- $test << c2
- $test << c3
- $test << c1
- $test << c3
- $test << c1
- $test << c4
- $test << c2
- $test << c2
- $test << c5
- $test << c5
- $test << c5
- $test << c2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement