adds the simple object dependency
where the next object is dependent on the previous one and just behind it, the padded_length away
This commit is contained in:
31
lib/risc/position/object_listener.rb
Normal file
31
lib/risc/position/object_listener.rb
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
module Risc
|
||||
module Position
|
||||
|
||||
# Listeners localise the changes that need to happen.
|
||||
#
|
||||
# An object listener assmes it is set up to the previous object.
|
||||
# so when position changes, it places itself just behind the previous object
|
||||
#
|
||||
# This is handy, since the "normal" chaining of object is forward
|
||||
# But the dependencies are backwards. This way we don't clutter the
|
||||
# actual object (or even the position), but keep the logic seperate.
|
||||
class ObjectListener
|
||||
|
||||
# initialize with the object that needs to react to change
|
||||
def initialize(object)
|
||||
@object = object
|
||||
end
|
||||
|
||||
# when the argument changes position, we update the objects
|
||||
# position to reflect that change
|
||||
#
|
||||
def position_changed(previous)
|
||||
me = previous.at + previous.object.padded_length
|
||||
object_pos = Position.get(@object)
|
||||
return if me == object_pos.at
|
||||
Position.set(@object , me)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -31,6 +31,7 @@ module Risc
|
||||
end
|
||||
def reset_to(pos , guaranteed_nil )
|
||||
return false if pos == at
|
||||
trigger(:position_changed , self)
|
||||
if((at - pos).abs > 1000)
|
||||
raise "position set too far off #{pos}!=#{at} for #{object}:#{object.class}"
|
||||
end
|
||||
|
@ -105,5 +105,6 @@ module Risc
|
||||
end
|
||||
end
|
||||
require_relative "object_position"
|
||||
require_relative "object_listener"
|
||||
require_relative "instruction_position"
|
||||
require_relative "code_position"
|
||||
|
Reference in New Issue
Block a user