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:
Torsten Ruger
2018-05-31 20:41:04 +03:00
parent 21a9c56ba9
commit 46fbfb7101
5 changed files with 79 additions and 12 deletions

View 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

View File

@ -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

View File

@ -105,5 +105,6 @@ module Risc
end
end
require_relative "object_position"
require_relative "object_listener"
require_relative "instruction_position"
require_relative "code_position"