Begone method pointers! Go back to C

physics
E. Almqvist 3 years ago
parent b3d64c481b
commit 7b569eda1e
  1. 23
      src/app.rb
  2. 23
      src/config.rb
  3. 49
      src/lib/keyhook.rb
  4. 10
      src/utils/debug.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

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

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

@ -0,0 +1,10 @@
def debug(str)
if ENABLE_DEBUG then
puts("[DEBUG] #{str}")
end
end
def error(str)
puts("[ERROR] #{str}!!!!")
end
Loading…
Cancel
Save