From 5a97d292c0a79e93a09ac9009c0945f8284d22ea Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Sun, 17 Feb 2019 14:14:39 -0600 Subject: [PATCH] remove glu from gemfile, added InputMapper, using InputMapper for Camera and Player. --- Gemfile | 2 -- Gemfile.lock | 3 -- i-mic-fps.rb | 3 +- lib/managers/collision_manager.rb | 2 +- lib/managers/input_mapper.rb | 45 ++++++++++++++++++++++++++++++ lib/objects/game_objects/camera.rb | 37 ++++++++++++++---------- lib/objects/game_objects/player.rb | 36 ++++++++++++++++-------- lib/objects/model_loader.rb | 2 +- lib/states/game_states/game.rb | 9 ++++-- lib/wavefront/material.rb | 2 +- 10 files changed, 103 insertions(+), 38 deletions(-) create mode 100644 lib/managers/input_mapper.rb diff --git a/Gemfile b/Gemfile index b57046b..92e7f3d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,3 @@ source "https://rubygems.org" gem "opengl-bindings" -gem "glu" -# gem "glut" gem "gosu" \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index f65005a..55c7f10 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,8 +1,6 @@ GEM remote: https://rubygems.org/ specs: - glu (8.2.2) - glu (8.2.2-x86-mingw32) gosu (0.14.5) gosu (0.14.5-x86-mingw32) opengl-bindings (1.6.7) @@ -12,7 +10,6 @@ PLATFORMS x86-mingw32 DEPENDENCIES - glu gosu opengl-bindings diff --git a/i-mic-fps.rb b/i-mic-fps.rb index 4f7c66d..2ec72bc 100644 --- a/i-mic-fps.rb +++ b/i-mic-fps.rb @@ -61,6 +61,7 @@ end $debug = ARGV.join.include?("--debug") ? true : false require_relative "lib/common_methods" +require_relative "lib/managers/input_mapper" require_relative "lib/managers/shader_manager" require_relative "lib/managers/object_manager" require_relative "lib/managers/light_manager" @@ -95,7 +96,7 @@ require_relative "lib/wavefront/model" require_relative "lib/window" -MODEL_METER_SCALE = 1.0 # Objects exported from blender using the millimeter object scale will be close to 1 GL unit +MODEL_METER_SCALE = 1.0 # Objects exported from blender using the default or meter object scale will be close to 1 GL unit if ARGV.join.include?("--profile") diff --git a/lib/managers/collision_manager.rb b/lib/managers/collision_manager.rb index 2bd5a3d..18f490e 100644 --- a/lib/managers/collision_manager.rb +++ b/lib/managers/collision_manager.rb @@ -17,7 +17,7 @@ class IMICFPS b.debug_color = Color.new(1.0,0.0,0.0) # @game_state.game_objects.delete(object) unless object.is_a?(Player) - puts "#{object} is intersecting #{b}" if object.is_a?(Player) + # puts "#{object} is intersecting #{b}" if object.is_a?(Player) else object.debug_color = Color.new(0,1,0) b.debug_color = Color.new(0,1,0) diff --git a/lib/managers/input_mapper.rb b/lib/managers/input_mapper.rb new file mode 100644 index 0000000..a0900b2 --- /dev/null +++ b/lib/managers/input_mapper.rb @@ -0,0 +1,45 @@ +class IMICFPS + class InputMapper + @@keymap = {} + + def self.get(category, action) + key = @@keymap.dig(category, action) + end + + def self.set(category, action, key) + raise "category must be a symbol" unless category.is_a?(Symbol) + raise "action must be a symbol" unless action.is_a?(Symbol) + raise "key must be a whole number or Array of whole numbers, got #{key}" unless key.is_a?(Integer) || key.is_a?(Array) + + @@keymap[category] ||= {} + + warn "InputMapper.set(:#{category}, :#{action}) is already defined as #{@@keymap[category][action]}" if @@keymap[category][action] + + @@keymap[category][action] = key + end + + def self.down?(category, action) + keys = get(category, action) + + if keys.is_a?(Array) + keys.detect do |key| + Gosu.button_down?(key) + end + else + Gosu.button_down?(keys) + end + end + + def self.is?(category, action, query_key) + keys = get(category, action) + + if keys.is_a?(Array) + keys.detect do |key| + query_key == key + end + else + query_key == keys + end + end + end +end \ No newline at end of file diff --git a/lib/objects/game_objects/camera.rb b/lib/objects/game_objects/camera.rb index 7e20851..e428ce1 100644 --- a/lib/objects/game_objects/camera.rb +++ b/lib/objects/game_objects/camera.rb @@ -23,6 +23,16 @@ class IMICFPS @mouse_sensitivity = 20.0 @mouse_captured = true @mouse_checked = 0 + + InputMapper.set(:camera, :ascend, Gosu::KbSpace) + InputMapper.set(:camera, :descend, [Gosu::KbLeftControl, Gosu::KbRightControl]) + InputMapper.set(:camera, :release_mouse, [Gosu::KbLeftAlt, Gosu::KbRightAlt]) + InputMapper.set(:camera, :capture_mouse, Gosu::MsLeft) + InputMapper.set(:camera, :increase_mouse_sensitivity, Gosu::KB_NUMPAD_PLUS) + InputMapper.set(:camera, :decrease_mouse_sensitivity, Gosu::KB_NUMPAD_MINUS) + InputMapper.set(:camera, :reset_mouse_sensitivity, Gosu::KB_NUMPAD_MULTIPLY) + InputMapper.set(:camera, :increase_view_distance, Gosu::MsWheelUp) + InputMapper.set(:camera, :decrease_view_distance, Gosu::MsWheelDown) end def attach_to(game_object) @@ -111,56 +121,55 @@ class IMICFPS relative_y_rotation = (@yaw + 180) relative_speed = 0.5 - if button_down?(Gosu::KbUp) || button_down?(Gosu::KbW) + if InputMapper.down?(:character, :forward) @z+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed @x-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed end - if button_down?(Gosu::KbDown) || button_down?(Gosu::KbS) + if InputMapper.down?(:character, :backward) @z-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed @x+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed end - if button_down?(Gosu::KbA) + if InputMapper.down?(:character, :strife_left) @z+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed @x+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed end - if button_down?(Gosu::KbD) + if InputMapper.down?(:character, :strife_right) @z-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed @x-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed end - if button_down?(Gosu::KbSpace) + if InputMapper.down?(:camera, :ascend) @y+=relative_speed end - if button_down?(Gosu::KbLeftShift) || button_down?(Gosu::KbRightShift) + if InputMapper.down?(:camera, :descend) @y-=relative_speed end end def button_up(id) - case id - when Gosu::KbLeftAlt, Gosu::KbRightAlt + if InputMapper.is?(:camera, :release_mouse, id) @mouse_captured = false $window.needs_cursor = true - when Gosu::MsLeft + elsif InputMapper.is?(:camera, :capture_mouse, id) @mouse_captured = true $window.needs_cursor = false - when Gosu::KB_NUMPAD_PLUS + elsif InputMapper.is?(:camera, :increase_mouse_sensitivity, id) @mouse_sensitivity+=1 @mouse_sensitivity = @mouse_sensitivity.clamp(1.0, 100.0) - when Gosu::KbMinus, Gosu::KB_NUMPAD_MINUS + elsif InputMapper.is?(:camera, :decrease_mouse_sensitivity, id) @mouse_sensitivity-=1 @mouse_sensitivity = @mouse_sensitivity.clamp(1.0, 100.0) - when Gosu::KB_NUMPAD_MULTIPLY + elsif InputMapper.is?(:camera, :reset_mouse_sensitivity, id) @mouse_sensitivity = 20.0 - when Gosu::MsWheelUp + elsif InputMapper.is?(:camera, :increase_view_distance, id) # @field_of_view += 1 # @field_of_view = @field_of_view.clamp(1, 100) @view_distance += 1 @view_distance = @view_distance.clamp(1, 1000) - when Gosu::MsWheelDown + elsif InputMapper.is?(:camera, :decrease_view_distance, id) # @field_of_view -= 1 # @field_of_view = @field_of_view.clamp(1, 100) @view_distance -= 1 diff --git a/lib/objects/game_objects/player.rb b/lib/objects/game_objects/player.rb index ad46ac7..e322865 100644 --- a/lib/objects/game_objects/player.rb +++ b/lib/objects/game_objects/player.rb @@ -8,6 +8,18 @@ class IMICFPS def setup bind_model("base", "biped") + InputMapper.set(:character, :forward, [Gosu::KbUp, Gosu::KbW]) + InputMapper.set(:character, :backward, [Gosu::KbDown, Gosu::KbS]) + InputMapper.set(:character, :strife_left, Gosu::KbA) + InputMapper.set(:character, :strife_right, Gosu::KbD) + InputMapper.set(:character, :turn_left, Gosu::KbLeft) + InputMapper.set(:character, :turn_right, Gosu::KbRight) + InputMapper.set(:character, :jump, Gosu::KbSpace) + InputMapper.set(:character, :sprint, [Gosu::KbLeftControl]) + + InputMapper.set(:character, :turn_180, Gosu::KbX) + InputMapper.set(:character, :toggle_first_person_view, Gosu::KbF) + @speed = 2.5 # meter's per second @running_speed = 6.8 # meter's per second @old_speed = @speed @@ -90,7 +102,7 @@ class IMICFPS @floor = @terrain.height_at(self, 4.5) relative_speed = @speed - if button_down?(Gosu::KbLeftControl) + if InputMapper.down?(:character, :sprint) relative_speed = (@running_speed)*(delta_time) else relative_speed = @speed*(delta_time) @@ -98,27 +110,27 @@ class IMICFPS relative_y_rotation = @y_rotation*-1 - if button_down?(Gosu::KbUp) || button_down?(Gosu::KbW) + if InputMapper.down?(:character, :forward) @z+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed @x-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed end - if button_down?(Gosu::KbDown) || button_down?(Gosu::KbS) + if InputMapper.down?(:character, :backward) @z-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed @x+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed end - if button_down?(Gosu::KbA) + if InputMapper.down?(:character, :strife_left) @z+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed @x+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed end - if button_down?(Gosu::KbD) + if InputMapper.down?(:character, :strife_right) @z-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed @x-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed end - if button_down?(Gosu::KbLeft) + if InputMapper.down?(:character, :turn_left) @y_rotation+=(relative_speed*1000)*delta_time end - if button_down?(Gosu::KbRight) + if InputMapper.down?(:character, :turn_right) @y_rotation-=(relative_speed*1000)*delta_time end @@ -127,7 +139,7 @@ class IMICFPS @y_velocity-=(IMICFPS::GRAVITY*air_time)*delta_time end - if button_down?(Gosu::KbSpace) && !@jumping + if InputMapper.down?(:character, :jump) && !@jumping @jumping = true @_time_in_air = Gosu.milliseconds elsif !@jumping && @y > @floor @@ -141,7 +153,7 @@ class IMICFPS end end if @jumping && !@falling - if button_down?(Gosu::KbSpace) + if InputMapper.down?(:character, :jump) @y_velocity = 1.5 @falling = true end @@ -158,11 +170,11 @@ class IMICFPS end def button_up(id) - case id - when Gosu::KbX + if InputMapper.is?(:character, :turn_180, id) @y_rotation = @y_rotation+180 @y_rotation %= 360 - when Gosu::KbF + + elsif InputMapper.is?(:character, :toggle_first_person_view, id) @first_person_view = !@first_person_view puts "First Person? #{@first_person_view}" end diff --git a/lib/objects/model_loader.rb b/lib/objects/model_loader.rb index 2e65b65..e84fb86 100644 --- a/lib/objects/model_loader.rb +++ b/lib/objects/model_loader.rb @@ -10,7 +10,7 @@ class IMICFPS def initialize(manifest_file:, game_object: nil) @manifest = YAML.load(File.read(manifest_file)) - pp @manifest + # pp @manifest @file_path = File.expand_path("./../model/", manifest_file) + "/#{@manifest["model"]}" @name = @manifest["name"] diff --git a/lib/states/game_states/game.rb b/lib/states/game_states/game.rb index d0de75e..a7eed4e 100644 --- a/lib/states/game_states/game.rb +++ b/lib/states/game_states/game.rb @@ -25,7 +25,7 @@ class IMICFPS @crosshair_color = Gosu::Color.rgb(255,127,0) # @font = Gosu::Font.new(18, name: "DejaVu Sans") - @text = MultiLineText.new("Pending...", x: 10, y: 10, z: 1, size: 18, font: "DejaVu Sans") + @text = Text.new("Pending...", x: 10, y: 10, z: 1, size: 18, font: "DejaVu Sans") Light.new(x: 3, y: -6, z: 6, game_state: self) Light.new(x: 0, y: 100, z: 0, diffuse: Color.new(1.0, 0.5, 0.1), game_state: self) @@ -76,7 +76,6 @@ OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)} Camera pitch: #{@camera.pitch.round(2)} Yaw: #{@camera.yaw.round(2)} Roll #{@camera.roll.round(2)} Camera X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)} -Compensating faulty mouse centering? #{@camera.broken_mouse_centering} #{if @camera.game_object then "Actor X:#{@camera.game_object.x.round(2)} Y:#{@camera.game_object.y.round(2)} Z:#{@camera.game_object.z.round(2)}";end} Field Of View: #{@camera.field_of_view} Mouse Sesitivity: #{@camera.mouse_sensitivity} @@ -106,7 +105,11 @@ Draw Skydome: #{@draw_skydome} Debug mode: #{$debug} eos end - @text.text = string + if $debug + @text.text = string + else + @text.text = "FPS: #{Gosu.fps}" + end @collision_manager.update @game_objects.each(&:update) diff --git a/lib/wavefront/material.rb b/lib/wavefront/material.rb index 4717632..77759db 100644 --- a/lib/wavefront/material.rb +++ b/lib/wavefront/material.rb @@ -14,7 +14,7 @@ class IMICFPS end def set_texture(texture_path) - puts "#{name} texture #{texture_path}" + # puts "#{name} texture #{texture_path}" @texture = Gosu::Image.new(texture_path, retro: false) array_of_pixels = @texture.to_blob