diff --git a/src/app.rb b/src/app.rb index 2ec9161..db61f15 100755 --- a/src/app.rb +++ b/src/app.rb @@ -89,10 +89,9 @@ class Window < Gosu::Window print "up: " p id + # No queue needed for up keys @up_keyhook.call(KEY_EVENTS[id]) - @key_events[:down].delete(id) # when the key is released: stop holding it - @key_events[:up] << id # append the key event to the queue # if( @world && @world.controller ) then # @world.controller.button_up(id) @@ -119,14 +118,11 @@ class Window < Gosu::Window end def update - # Call all keybind hooks - @key_events[:down].each do |key| - @down_keyhook.call(key) + # Call all down key events each tick + @key_events[:down].each do |keyid| + @down_keyhook.call(KEY_EVENTS[keyid]) end - @key_events[:up].each do |key| - @up_keyhook.call(key) - end if( @world ) then @world.tick diff --git a/src/lib/keyhook.rb b/src/lib/keyhook.rb index 8c0e8d9..3819985 100644 --- a/src/lib/keyhook.rb +++ b/src/lib/keyhook.rb @@ -6,15 +6,17 @@ class MethodContainer @method_registry = [] end - # Empty method that does nothing (placeholder for keyhooks) - def nullmethod - return nil - end - def create_method(name, &block) method_registry << name self.class.send(:define_method, name, &block) end + + def call(method_name, *args) + debug("Calling method '#{method_name}' for #{self}") + if( @method_registry.include? method_name ) then + self.send(method_name, *args) + end + end end class KeyHook @@ -36,8 +38,10 @@ class KeyHook end def call(hook, *args) - @key_hooks[hook].each do |event_name| - @method_container.send(event_name, *args) + if( @key_hooks.key? hook ) then + @key_hooks[hook].each do |event_name| + @method_container.send(event_name, *args) + end end end