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.

This commit is contained in:
2019-08-13 10:18:30 -05:00
parent 016e8109de
commit 7ebf65f535
4 changed files with 78 additions and 73 deletions

View File

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

View File

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

View File

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

View File

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