From 7ebf65f5352f31dba2d08344ca6f1825a07200bc Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Tue, 13 Aug 2019 10:18:30 -0500 Subject: [PATCH] Player entity no longer directly handles raw button input (turn 180 and fpv toggle need more work), fixed Console printing empty lines for tab completion when available commands list is empty. --- lib/managers/input_mapper.rb | 11 ++-- lib/objects/entities/player.rb | 92 ++++++++++++++++++---------------- lib/states/game_states/game.rb | 46 ++++++++--------- lib/ui/console.rb | 2 +- 4 files changed, 78 insertions(+), 73 deletions(-) diff --git a/lib/managers/input_mapper.rb b/lib/managers/input_mapper.rb index 18febb5..cdc649f 100644 --- a/lib/managers/input_mapper.rb +++ b/lib/managers/input_mapper.rb @@ -3,6 +3,14 @@ class IMICFPS @@keymap = {} @@keys = Hash.new(false) + def self.keymap + @@keymap + end + + def self.keys + @@keys + end + def self.keydown(id_or_action) if id_or_action.is_a?(Integer) @@keys[id_or_action] = true @@ -77,8 +85,6 @@ class IMICFPS def self.action(key) answer = nil @@keymap.each do |action, value| - p action, value - if value.is_a?(Array) if value.include?(key) answer = action @@ -93,7 +99,6 @@ class IMICFPS end end - raise "InputMapper.action(#{key}) is nil!" unless answer answer end diff --git a/lib/objects/entities/player.rb b/lib/objects/entities/player.rb index 82d5eb5..40f564c 100644 --- a/lib/objects/entities/player.rb +++ b/lib/objects/entities/player.rb @@ -93,44 +93,51 @@ class IMICFPS # Do not handle movement if mouse is not captured return if @camera && !@camera.mouse_captured - relative_speed = @speed - if InputMapper.down?(:sprint) - relative_speed = @running_speed - else - relative_speed = @speed - end - - relative_y_rotation = @rotation.y * -1 - - if InputMapper.down?(:forward) - @velocity.z += Math.cos(relative_y_rotation * Math::PI / 180) * relative_speed - @velocity.x -= Math.sin(relative_y_rotation * Math::PI / 180) * relative_speed - end - if InputMapper.down?(:backward) - @velocity.z -= Math.cos(relative_y_rotation * Math::PI / 180) * relative_speed - @velocity.x += Math.sin(relative_y_rotation * Math::PI / 180) * relative_speed - end - if InputMapper.down?(:strife_left) - @velocity.z += Math.sin(relative_y_rotation * Math::PI / 180) * relative_speed - @velocity.x += Math.cos(relative_y_rotation * Math::PI / 180) * relative_speed - end - if InputMapper.down?(:strife_right) - @velocity.z -= Math.sin(relative_y_rotation * Math::PI / 180) * relative_speed - @velocity.x -= Math.cos(relative_y_rotation * Math::PI / 180) * relative_speed - end - - if InputMapper.down?(:turn_left) - @rotation.y += @turn_speed * delta_time - end - if InputMapper.down?(:turn_right) - @rotation.y -= @turn_speed * delta_time - end - if @_time_in_air air_time = (Gosu.milliseconds - @_time_in_air) / 1000.0 @velocity.y -= IMICFPS::GRAVITY * air_time * delta_time end + super + end + + def relative_speed + InputMapper.down?(:sprint) ? @running_speed : @speed + end + + def relative_y_rotation + @rotation.y * -1 + end + + def forward + @velocity.z += Math.cos(relative_y_rotation * Math::PI / 180) * relative_speed + @velocity.x -= Math.sin(relative_y_rotation * Math::PI / 180) * relative_speed + end + + def backward + @velocity.z -= Math.cos(relative_y_rotation * Math::PI / 180) * relative_speed + @velocity.x += Math.sin(relative_y_rotation * Math::PI / 180) * relative_speed + end + + def strife_left + @velocity.z += Math.sin(relative_y_rotation * Math::PI / 180) * relative_speed + @velocity.x += Math.cos(relative_y_rotation * Math::PI / 180) * relative_speed + end + + def strife_right + @velocity.z -= Math.sin(relative_y_rotation * Math::PI / 180) * relative_speed + @velocity.x -= Math.cos(relative_y_rotation * Math::PI / 180) * relative_speed + end + + def turn_left + @rotation.y += @turn_speed * delta_time + end + + def turn_right + @rotation.y -= @turn_speed * delta_time + end + + def jump if InputMapper.down?(:jump) && !@jumping @jumping = true @_time_in_air = Gosu.milliseconds @@ -151,20 +158,17 @@ class IMICFPS @falling = true end end - - super end - def button_down(id) - if InputMapper.is?(:toggle_first_person_view, id) - @first_person_view = !@first_person_view - @visible = !@first_person_view - puts "First Person? #{@first_person_view}" + def toggle_first_person_view + @first_person_view = !@first_person_view + @visible = !@first_person_view + puts "First Person? #{@first_person_view}" + end - elsif InputMapper.is?(:turn_180, id) - @rotation.y = @rotation.y + 180 - @rotation.y %= 360 - end + def turn_180 + @rotation.y = @rotation.y + 180 + @rotation.y %= 360 end end end diff --git a/lib/states/game_states/game.rb b/lib/states/game_states/game.rb index 52cd023..713925c 100644 --- a/lib/states/game_states/game.rb +++ b/lib/states/game_states/game.rb @@ -89,10 +89,23 @@ class IMICFPS @collision_manager.update @entities.each(&:update) + control_player + @skydome.update if @skydome.renderable @camera.update + if $debug.get(:stats) + @text.text = update_text + elsif $debug.get(:fps) + @text.text = "FPS: #{Gosu.fps}" + else + @text.text = "" + end + + @draw_skydome = $debug.get(:skydome) + @skydome.renderable = @draw_skydome + if ARGV.join.include?("--playdemo") if @demo_data[@demo_index]&.start_with?("tick") if @demo_tick == @demo_data[@demo_index].split(" ").last.to_i @@ -149,7 +162,6 @@ class IMICFPS end def update_text - begin string = <<-eos OpenGL Vendor: #{glGetString(GL_VENDOR)} OpenGL Renderer: #{glGetString(GL_RENDERER)} @@ -161,40 +173,24 @@ Camera X:#{@camera.position.x.round(2)} Y:#{@camera.position.y.round(2)} Z:#{@ca #{if @camera.entity then "Actor X:#{@camera.entity.position.x.round(2)} Y:#{@camera.entity.position.y.round(2)} Z:#{@camera.entity.position.z.round(2)}";end} Field Of View: #{@camera.field_of_view} Mouse Sesitivity: #{@camera.mouse_sensitivity} -Last Frame: #{delta_time*1000.0}ms (#{Gosu.fps} fps) +Last Frame: #{delta_time * 1000.0}ms (#{Gosu.fps} fps) Vertices: #{formatted_number(window.number_of_vertices)} Faces: #{formatted_number(window.number_of_vertices/3)} Draw Skydome: #{@draw_skydome} eos - rescue ArgumentError - string = <<-eos -Unable to call glGetString! + end -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)} -#{if @camera.entity then "Actor X:#{@camera.entity.x.round(2)} Y:#{@camera.entity.y.round(2)} Z:#{@camera.entity.z.round(2)}";end} -Field Of View: #{@camera.field_of_view} -Mouse Sesitivity: #{@camera.mouse_sensitivity} -Last Frame: #{delta_time*1000.0}ms (#{Gosu.fps} fps) + def control_player + InputMapper.keys.each do |key, pressed| + next unless pressed -Vertices: #{formatted_number(window.number_of_vertices)} -Faces: #{formatted_number(window.number_of_vertices/3)} + action = InputMapper.action(key) + next unless action -Draw Skydome: #{@draw_skydome} -eos + @player.send(action) if @player.respond_to?(action) end - if $debug.get(:stats) - @text.text = string - elsif $debug.get(:fps) - @text.text = "FPS: #{Gosu.fps}" - else - @text.text = "" - end - - @draw_skydome = $debug.get(:skydome) - @skydome.renderable = @draw_skydome end def button_down(id) diff --git a/lib/ui/console.rb b/lib/ui/console.rb index e436582..c82c518 100644 --- a/lib/ui/console.rb +++ b/lib/ui/console.rb @@ -123,7 +123,7 @@ class IMICFPS if list.size == 1 @text_input.text = "#{list.first} " else - stdin("\n#{list.map { |cmd| Commands::Style.highlight(cmd)}.join(", ")}") + stdin("\n#{list.map { |cmd| Commands::Style.highlight(cmd)}.join(", ")}") if list.size > 0 end else if split.size > 0 && cmd = Commands::Command.find(split.first)