physics
E. Almqvist 3 years ago
parent 653e746172
commit 3d5a8ecb96
  1. 15
      src/app.rb
  2. 13
      src/config.rb
  3. 30
      src/lib/keyhook.rb

@ -6,6 +6,7 @@ require_relative "lib/gosu_plugin.rb"
require_relative "config.rb" require_relative "config.rb"
require_relative "lib/keyhook.rb"
require_relative "lib/ui.rb" require_relative "lib/ui.rb"
require_relative "lib/physobj.rb" require_relative "lib/physobj.rb"
require_relative "lib/objects.rb" require_relative "lib/objects.rb"
@ -23,11 +24,23 @@ class Window < Gosu::Window
@width, @height = width, height @width, @height = width, height
self.caption = "#{title}| #{width}x#{height}" self.caption = "#{title}| #{width}x#{height}"
# Key event queue
@key_events = { @key_events = {
up: [], up: [],
down: [] down: []
} }
# Keyhook for up releases
@up_keyhook = KeyHook.new
# Keyhook for down presses
@down_keyhook = KeyHook.new
KEY_EVENTS[:general].each do |key, event|
puts "Binding key '#{key}' to '#{event}'"
@up_keyhook.add(event, :main,)
end
@ui = [] @ui = []
@fonts = { @fonts = {
normal: Gosu::Font.new(self, MAIN_FONT, 18), normal: Gosu::Font.new(self, MAIN_FONT, 18),
@ -123,6 +136,8 @@ class Window < Gosu::Window
end end
def update def update
# Call all keybind hooks
if( @world ) then if( @world ) then
@world.tick @world.tick
end end

@ -8,6 +8,11 @@ WINDOW_FULLSCREEN = false
MAIN_FONT = "monospace" MAIN_FONT = "monospace"
# Keybinds # Keybinds
# UI
BIND_FIRE = Gosu::MS_LEFT
BIND_FIRE2 = Gosu::MS_MIDDLE
BIND_SELECT = Gosu::MS_RIGHT
# General # General
BIND_PAUSE = Gosu::KbEscape BIND_PAUSE = Gosu::KbEscape
@ -20,14 +25,20 @@ MAIN_FONT = "monospace"
# Key events # Key events
KEY_EVENTS = { KEY_EVENTS = {
# UI # General
general: {
BIND_PAUSE => :pause, BIND_PAUSE => :pause,
BIND_FIRE => :fire,
BIND_SELECT => :select,
},
# Player Controlls # Player Controlls
player: {
BIND_TURN_LEFT => :turn_left, BIND_TURN_LEFT => :turn_left,
BIND_TURN_RIGHT => :turn_right, BIND_TURN_RIGHT => :turn_right,
BIND_INCREASE_THRUST => :increase_thrust, BIND_INCREASE_THRUST => :increase_thrust,
BIND_DECREASE_THRUST => :decrease_thrust, BIND_DECREASE_THRUST => :decrease_thrust,
BIND_TOGGLE_ENGINE => :toggle_engine BIND_TOGGLE_ENGINE => :toggle_engine
}
} }

@ -0,0 +1,30 @@
# Empty method that does nothing (placeholder for keyhooks)
def nullmethod
return
end
# Handler for key events
class KeyHook
attr_reader :nullmethod_ptr
attr_accessor :key_hooks
def initialize
@nullmethod_ptr = method(:nullmethod)
@key_hooks = Hash.new(Hash.new(@nullmethod_ptr))
# Each keyhook contains a hash of method pointers
end
def add(hook, event_name=:main, method_sym=:nullmethod)
self.key_hooks[hook][event_name] = method(method_sym)
end
def remove(hook, event_name=:main)
self.key_hooks[hook][event_name] = self.nullmethod_ptr
end
def call(hook, event_name, *args)
self.key_hooks[hook][event_name].call(*args)
end
end
Loading…
Cancel
Save