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 = {} @@keymap = {}
@@keys = Hash.new(false) @@keys = Hash.new(false)
def self.keymap
@@keymap
end
def self.keys
@@keys
end
def self.keydown(id_or_action) def self.keydown(id_or_action)
if id_or_action.is_a?(Integer) if id_or_action.is_a?(Integer)
@@keys[id_or_action] = true @@keys[id_or_action] = true
@@ -77,8 +85,6 @@ class IMICFPS
def self.action(key) def self.action(key)
answer = nil answer = nil
@@keymap.each do |action, value| @@keymap.each do |action, value|
p action, value
if value.is_a?(Array) if value.is_a?(Array)
if value.include?(key) if value.include?(key)
answer = action answer = action
@@ -93,7 +99,6 @@ class IMICFPS
end end
end end
raise "InputMapper.action(#{key}) is nil!" unless answer
answer answer
end end

View File

@@ -93,44 +93,51 @@ class IMICFPS
# Do not handle movement if mouse is not captured # Do not handle movement if mouse is not captured
return if @camera && !@camera.mouse_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 if @_time_in_air
air_time = (Gosu.milliseconds - @_time_in_air) / 1000.0 air_time = (Gosu.milliseconds - @_time_in_air) / 1000.0
@velocity.y -= IMICFPS::GRAVITY * air_time * delta_time @velocity.y -= IMICFPS::GRAVITY * air_time * delta_time
end 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 if InputMapper.down?(:jump) && !@jumping
@jumping = true @jumping = true
@_time_in_air = Gosu.milliseconds @_time_in_air = Gosu.milliseconds
@@ -151,20 +158,17 @@ class IMICFPS
@falling = true @falling = true
end end
end end
super
end end
def button_down(id) def toggle_first_person_view
if InputMapper.is?(:toggle_first_person_view, id) @first_person_view = !@first_person_view
@first_person_view = !@first_person_view @visible = !@first_person_view
@visible = !@first_person_view puts "First Person? #{@first_person_view}"
puts "First Person? #{@first_person_view}" end
elsif InputMapper.is?(:turn_180, id) def turn_180
@rotation.y = @rotation.y + 180 @rotation.y = @rotation.y + 180
@rotation.y %= 360 @rotation.y %= 360
end
end end
end end
end end

View File

@@ -89,10 +89,23 @@ class IMICFPS
@collision_manager.update @collision_manager.update
@entities.each(&:update) @entities.each(&:update)
control_player
@skydome.update if @skydome.renderable @skydome.update if @skydome.renderable
@camera.update @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 ARGV.join.include?("--playdemo")
if @demo_data[@demo_index]&.start_with?("tick") if @demo_data[@demo_index]&.start_with?("tick")
if @demo_tick == @demo_data[@demo_index].split(" ").last.to_i if @demo_tick == @demo_data[@demo_index].split(" ").last.to_i
@@ -149,7 +162,6 @@ class IMICFPS
end end
def update_text def update_text
begin
string = <<-eos string = <<-eos
OpenGL Vendor: #{glGetString(GL_VENDOR)} OpenGL Vendor: #{glGetString(GL_VENDOR)}
OpenGL Renderer: #{glGetString(GL_RENDERER)} 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} #{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} Field Of View: #{@camera.field_of_view}
Mouse Sesitivity: #{@camera.mouse_sensitivity} 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)} Vertices: #{formatted_number(window.number_of_vertices)}
Faces: #{formatted_number(window.number_of_vertices/3)} Faces: #{formatted_number(window.number_of_vertices/3)}
Draw Skydome: #{@draw_skydome} Draw Skydome: #{@draw_skydome}
eos eos
rescue ArgumentError end
string = <<-eos
Unable to call glGetString!
Camera pitch: #{@camera.pitch.round(2)} Yaw: #{@camera.yaw.round(2)} Roll #{@camera.roll.round(2)} def control_player
Camera X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)} InputMapper.keys.each do |key, pressed|
#{if @camera.entity then "Actor X:#{@camera.entity.x.round(2)} Y:#{@camera.entity.y.round(2)} Z:#{@camera.entity.z.round(2)}";end} next unless pressed
Field Of View: #{@camera.field_of_view}
Mouse Sesitivity: #{@camera.mouse_sensitivity}
Last Frame: #{delta_time*1000.0}ms (#{Gosu.fps} fps)
Vertices: #{formatted_number(window.number_of_vertices)} action = InputMapper.action(key)
Faces: #{formatted_number(window.number_of_vertices/3)} next unless action
Draw Skydome: #{@draw_skydome} @player.send(action) if @player.respond_to?(action)
eos
end 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 end
def button_down(id) def button_down(id)

View File

@@ -123,7 +123,7 @@ class IMICFPS
if list.size == 1 if list.size == 1
@text_input.text = "#{list.first} " @text_input.text = "#{list.first} "
else 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 end
else else
if split.size > 0 && cmd = Commands::Command.find(split.first) if split.size > 0 && cmd = Commands::Command.find(split.first)