From 7b569eda1e152586a1fbc70d40b28df1b6515310 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 22 Nov 2021 09:10:45 +0100 Subject: [PATCH] Begone method pointers! Go back to C --- src/app.rb | 23 ++++++++-------------- src/config.rb | 23 +++++++++++----------- src/lib/keyhook.rb | 49 ++++++++++++++++++++++++++++------------------ src/utils/debug.rb | 10 ++++++++++ 4 files changed, 59 insertions(+), 46 deletions(-) create mode 100644 src/utils/debug.rb diff --git a/src/app.rb b/src/app.rb index b5bfdcc..2ec9161 100755 --- a/src/app.rb +++ b/src/app.rb @@ -2,6 +2,8 @@ require "matrix" require "gosu" + +require_relative "utils/debug.rb" require_relative "lib/gosu_plugin.rb" require_relative "config.rb" @@ -15,16 +17,6 @@ require_relative "lib/world.rb" require_relative "ui/mainmenu.rb" -def pause - puts "Hej" -end - -def fire -end -def select -end - - class Window < Gosu::Window attr_accessor :caption, :ui, :world, :mainmenu, :key_events attr_reader :width, :height, :fonts @@ -46,11 +38,6 @@ class Window < Gosu::Window # Keyhook for down presses @down_keyhook = KeyHook.new - KEY_EVENTS[:general].each do |key, event| - puts "Binding key '#{key}' to '#{event}'" - @up_keyhook.add(key, :main, event) - end - @ui = [] @fonts = { normal: Gosu::Font.new(self, MAIN_FONT, 18), @@ -102,6 +89,8 @@ class Window < Gosu::Window print "up: " p id + @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 @@ -131,6 +120,10 @@ class Window < Gosu::Window def update # Call all keybind hooks + @key_events[:down].each do |key| + @down_keyhook.call(key) + end + @key_events[:up].each do |key| @up_keyhook.call(key) end diff --git a/src/config.rb b/src/config.rb index f1a90d8..cdefb46 100644 --- a/src/config.rb +++ b/src/config.rb @@ -1,3 +1,6 @@ +# Utils +ENABLE_DEBUG = true + # World WORLD_SEED = 123456789 @@ -26,19 +29,15 @@ MAIN_FONT = "monospace" # Key events KEY_EVENTS = { # General - general: { - BIND_PAUSE => :pause, - BIND_FIRE => :fire, - BIND_SELECT => :select, - }, + BIND_PAUSE => :pause, + BIND_FIRE => :fire, + BIND_SELECT => :select, # Player Controlls - player: { - BIND_TURN_LEFT => :turn_left, - BIND_TURN_RIGHT => :turn_right, - BIND_INCREASE_THRUST => :increase_thrust, - BIND_DECREASE_THRUST => :decrease_thrust, - BIND_TOGGLE_ENGINE => :toggle_engine - } + BIND_TURN_LEFT => :turn_left, + BIND_TURN_RIGHT => :turn_right, + BIND_INCREASE_THRUST => :increase_thrust, + BIND_DECREASE_THRUST => :decrease_thrust, + BIND_TOGGLE_ENGINE => :toggle_engine } diff --git a/src/lib/keyhook.rb b/src/lib/keyhook.rb index 81b0687..8c0e8d9 100644 --- a/src/lib/keyhook.rb +++ b/src/lib/keyhook.rb @@ -1,33 +1,44 @@ -# Empty method that does nothing (placeholder for keyhooks) -def nullmethod - return -end - -# TODO: use Object.create_method -# https://apidock.com/ruby/Module/define_method - # Handler for key events -class KeyHook - attr_reader :nullmethod_ptr - attr_accessor :key_hooks +class MethodContainer + attr_reader :method_registry def initialize - @nullmethod_ptr = method(:nullmethod) + @method_registry = [] + end - @key_hooks = Hash.new(Hash.new(@nullmethod_ptr)) - # Each keyhook contains a hash of method pointers + # Empty method that does nothing (placeholder for keyhooks) + def nullmethod + return nil end - def add(hook, event_name=:main, method_sym=:nullmethod) - self.key_hooks[hook][event_name] = method(method_sym) + def create_method(name, &block) + method_registry << name + self.class.send(:define_method, name, &block) + end +end + +class KeyHook + attr_accessor :key_hooks, :method_container + + def initialize + @method_container = MethodContainer.new + @key_hooks = Hash.new([]) end - def remove(hook, event_name=:main) - self.key_hooks[hook][event_name] = self.nullmethod_ptr + def add(hook, event_name=:main, &block) + if( @key_hooks[hook].include? event_name ) then + error("Duplicate event_name ('#{event_name}') for hook '#{hook}'!") + else + @method_container.create_method(event_name, &block) + @key_hooks[hook] << event_name + return event_name + end end def call(hook, *args) - self.key_hooks[hook].each { |h| h.call(*args) } + @key_hooks[hook].each do |event_name| + @method_container.send(event_name, *args) + end end def get_hooks diff --git a/src/utils/debug.rb b/src/utils/debug.rb new file mode 100644 index 0000000..a4cd0c1 --- /dev/null +++ b/src/utils/debug.rb @@ -0,0 +1,10 @@ + +def debug(str) + if ENABLE_DEBUG then + puts("[DEBUG] #{str}") + end +end + +def error(str) + puts("[ERROR] #{str}!!!!") +end