diff --git a/lib/camera_controller.rb b/lib/camera_controller.rb index 075d73f..fbb99c9 100644 --- a/lib/camera_controller.rb +++ b/lib/camera_controller.rb @@ -2,6 +2,8 @@ class IMICFPS class CameraController include CommonMethods + attr_accessor :mode, :camera, :entity, :distance, :origin_distance, + :constant_pitch, :mouse_sensitivity, :mouse_captured def initialize(mode: :fpv, camera:, entity:) # :fpv - First Person View # :tpv - Third Person View @@ -21,6 +23,10 @@ class IMICFPS @mouse_checked = 0 end + def first_person_view? + @mode == :fpv + end + def distance_from_object @distance end @@ -34,12 +40,10 @@ class IMICFPS end def position_camera - if defined?(@entity.first_person_view) - if @entity.first_person_view - @distance = 0 - else - @distance = @origin_distance - end + if first_person_view? + @distance = 0 + else + @distance = @origin_distance end x_offset = horizontal_distance_from_object * Math.sin(@entity.orientation.y.degrees_to_radians) @@ -75,13 +79,25 @@ class IMICFPS end def button_down(id) - case id - when Gosu::KB_LEFT_ALT, Gosu::KB_RIGHT_ALT + actions = InputMapper.actions(id) + + if actions.include?(:release_mouse) @mouse_captured = false window.needs_cursor = true - when Gosu::MS_LEFT, Gosu::MS_MIDDLE, Gosu::MS_RIGHT + elsif actions.include?(:capture_mouse) @mouse_captured = true window.needs_cursor = false + + elsif actions.include?(:decrease_view_distance) + @camera.max_view_distance -= 0.5 + elsif actions.include?(:increase_view_distance) + @camera.max_view_distance += 0.5 + elsif actions.include?(:toggle_first_person_view) + @mode = first_person_view? ? :tpv : :fpv + @entity.visible = !first_person_view? + elsif actions.include?(:turn_180) + @entity.orientation.y += 180 + @entity.orientation.y %= 360.0 end end diff --git a/lib/demo.rb b/lib/demo.rb index a5761e9..53d8100 100644 --- a/lib/demo.rb +++ b/lib/demo.rb @@ -30,7 +30,7 @@ class IMICFPS @file.puts("tick #{@index}") end - @file.puts("down #{InputMapper.action(id)}") + @file.puts("down #{InputMapper.actions(id)}") @changed = true end end @@ -42,7 +42,7 @@ class IMICFPS @file.puts("tick #{@index}") end - @file.puts("up #{InputMapper.action(id)}") + @file.puts("up #{InputMapper.actions(id)}") @changed = true end end diff --git a/lib/game_objects/entities/player.rb b/lib/game_objects/entities/player.rb index afbaeab..9dedf3a 100644 --- a/lib/game_objects/entities/player.rb +++ b/lib/game_objects/entities/player.rb @@ -1,76 +1,19 @@ -require "etc" - class IMICFPS class Player < Entity attr_accessor :speed - attr_reader :name, :bound_model, :first_person_view + attr_reader :name, :bound_model def setup bind_model + @speed = 2.5 # meter's per second @running_speed = 5.0 # meter's per second @turn_speed = 50.0 @old_speed = @speed @mass = 72 # kg - @first_person_view = true @visible = false @drag = 0.6 - - @devisor = 500.0 - @name_image = Gosu::Image.from_text("#{Etc.getlogin}", 100, font: "Consolas", align: :center) - @name_texture_id = Texture.new(image: @name_image).id - end - - def draw_nameplate - _width = (@name_image.width / @devisor) / 2 - _height = (@name_image.height / @devisor) - _y = 2#normalize_bounding_box(model.bounding_box).max_y+0.05 - glPushMatrix - glRotatef(180, 0, 1, 0) - glDisable(GL_LIGHTING) - glEnable(GL_COLOR_MATERIAL) - glEnable(GL_TEXTURE_2D) - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND) - glBindTexture(GL_TEXTURE_2D, @name_texture_id) - glBegin(GL_TRIANGLES) - glColor3f(1.0,1.0,1.0) - # TOP LEFT - glTexCoord2f(0, 0) - glVertex3f(0-_width,_y+_height,0) - - # TOP RIGHT - glTexCoord2f(1, 0) - glVertex3f(0+_width, _y+_height,0) - - # BOTTOM LEFT - glTexCoord2f(0, 1) - glVertex3f(0-_width,_y,0) - - # BOTTOM LEFT - glTexCoord2f(0, 1) - glVertex3f(0-_width,_y,0) - - # BOTTOM RIGHT - glTexCoord2f(1, 1) - glVertex3f(0+_width, _y,0) - - # TOP RIGHT - glTexCoord2f(1, 0) - glVertex3f(0+_width,_y+_height,0) - glEnd - # glDisable(GL_BLEND) - glDisable(GL_TEXTURE_2D) - glEnable(GL_LIGHTING) - glPopMatrix - end - - def draw - if !@first_person_view - super - draw_nameplate - end end def update @@ -121,15 +64,5 @@ class IMICFPS @velocity.y = 1.5 end end - - def toggle_first_person_view - @first_person_view = !@first_person_view - @visible = !@first_person_view - end - - def turn_180 - @orientation.y = @orientation.y + 180 - @orientation.y %= 360 - end end end diff --git a/lib/hud/widgets/radar.rb b/lib/hud/widgets/radar.rb index e821d51..df18e56 100644 --- a/lib/hud/widgets/radar.rb +++ b/lib/hud/widgets/radar.rb @@ -2,7 +2,11 @@ class IMICFPS class HUD class RadarWidget < HUD::Widget def setup - @size = 288 + @min_size = 148 + @max_size = 288 + @target_screen_width = 1920 + @size = @max_size + @border_color = Gosu::Color.new(0x88c64600) @radar_color = Gosu::Color.new(0x88212121) @@ -30,7 +34,10 @@ class IMICFPS end def update - @text.text = "RADAR: X #{@player.position.x.round(1)} Y #{@player.position.y.round(1)} Z #{@player.position.z.round(1)}" + @size = (window.width / @target_screen_width.to_f * @max_size).clamp(@min_size, @max_size) + @scale = (@size - Widget.padding * 2.0) / @image.width + + @text.text = "X: #{@player.position.x.round(1)} Y: #{@player.position.y.round(1)} Z: #{@player.position.z.round(1)}" @text.x = Widget.margin + @size / 2 - @text.width / 2 @text.y = window.height - (Widget.margin + @size + @text.height) end diff --git a/lib/hud/widgets/squad.rb b/lib/hud/widgets/squad.rb index ffa4d3b..26086a9 100644 --- a/lib/hud/widgets/squad.rb +++ b/lib/hud/widgets/squad.rb @@ -2,7 +2,11 @@ class IMICFPS class HUD class SquadWidget < HUD::Widget def setup - @size = 288 # RADAR size + @min_size = 148 + @max_size = 288 # RADAR size + @target_screen_width = 1920 + @size = @max_size + @color = Gosu::Color.new(0xff00aa00) @text = Text.new( @@ -22,6 +26,8 @@ class IMICFPS end def update + @size = (window.width / @target_screen_width.to_f * @max_size).clamp(@min_size, @max_size) + @text.x = Widget.margin + @size + Widget.padding @text.y = window.height - (Widget.margin + @text.height) end diff --git a/lib/managers/input_mapper.rb b/lib/managers/input_mapper.rb index 830c670..acdc119 100644 --- a/lib/managers/input_mapper.rb +++ b/lib/managers/input_mapper.rb @@ -82,7 +82,7 @@ class IMICFPS end end - def self.action(key) + def self.actions(key) @@keymap.select do |action, value| if value.is_a?(Array) action if value.include?(key) diff --git a/lib/map.rb b/lib/map.rb index 6f16fc4..a94d495 100644 --- a/lib/map.rb +++ b/lib/map.rb @@ -23,7 +23,7 @@ class IMICFPS add_entity(Skydome.new(map_entity: @map_parser.skydome, manifest: Manifest.new(package: @map_parser.skydome.package, name: @map_parser.skydome.name), backface_culling: false)) @map_parser.lights.each do |l| - add_light(Light.new(id: available_light, position: l.position, diffuse: l.diffuse, ambient: l.ambient, specular: l.specular, intensity: l.intensity)) + add_light(Light.new(id: available_light, type: l.type, position: l.position, diffuse: l.diffuse, ambient: l.ambient, specular: l.specular, intensity: l.intensity)) end @map_parser.entities.each do |ent| diff --git a/lib/map_parser.rb b/lib/map_parser.rb index 5335203..59cabe8 100644 --- a/lib/map_parser.rb +++ b/lib/map_parser.rb @@ -16,6 +16,17 @@ class IMICFPS parse(map_file) end + def light_type(type) + case type.downcase.strip + when "directional" + CyberarmEngine::Light::DIRECTIONAL + when "spot" + CyberarmEngine::Light::SPOT + else + CyberarmEngine::Light::POINT + end + end + def parse(file) data = JSON.parse(File.read(file)) @@ -80,7 +91,7 @@ class IMICFPS if section = data["lights"] section.each do |l| light = MapParser::Light.new - light.type = IMICFPS::Light::POINT # TODO: fix me + light.type = light_type(l["type"]) light.position = Vector.new( l["position"]["x"], l["position"]["y"], diff --git a/lib/scenes/turn_table.rb b/lib/scenes/turn_table.rb index d28acad..552b8f0 100644 --- a/lib/scenes/turn_table.rb +++ b/lib/scenes/turn_table.rb @@ -2,8 +2,14 @@ class IMICFPS class TurnTableScene < Scene def setup camera.field_of_view = 45 - lights << (Light.new(id: OpenGL::GL_LIGHT1, position: Vector.new(30, 10.0, 30), diffuse: Color.new(0, 0, 0), specular: Color.new(0, 0, 0))) - lights << (Light.new(id: OpenGL::GL_LIGHT2, position: Vector.new(0, 10, 5), diffuse: Color.new(1.0, 1.0, 1.0), specular: Color.new(0, 0, 0))) + lights << Light.new( + id: OpenGL::GL_LIGHT1, + type: Light::DIRECTIONAL, + direction: Vector.down, + position: Vector.new(0, 10, 5), + diffuse: Color.new(1.0, 1.0, 1.0), + specular: Color.new(0, 0, 0) + ) options = { # entity: scale @@ -14,7 +20,7 @@ class IMICFPS "ttank": 0.13, "alternate_tank": 0.065, "tree": 0.08, - # "evergreen_tree": 0.08, + "evergreen_tree": 0.08, "power_plant": 0.025, "war_factory": 0.03, "randomish_terrain": 0.004, diff --git a/lib/states/game_states/game.rb b/lib/states/game_states/game.rb index b6cc5c1..8a16385 100644 --- a/lib/states/game_states/game.rb +++ b/lib/states/game_states/game.rb @@ -8,7 +8,7 @@ class IMICFPS @player = @map.find_entity_by(name: "character") @camera = PerspectiveCamera.new( position: @player.position.clone, aspect_ratio: window.aspect_ratio ) - @camera_controller = CameraController.new(mode: :first_person, camera: @camera, entity: @player) + @camera_controller = CameraController.new(mode: :fpv, camera: @camera, entity: @player) @director = Networking::Director.new @director.load_map(map_parser: @options[:map_parser]) @@ -58,7 +58,7 @@ eos InputMapper.keys.each do |key, pressed| next unless pressed - actions = InputMapper.action(key) + actions = InputMapper.actions(key) next unless actions actions.each do |action| diff --git a/lib/tools/map_editor/lib/editor.rb b/lib/tools/map_editor/lib/editor.rb index 5716169..e482abe 100644 --- a/lib/tools/map_editor/lib/editor.rb +++ b/lib/tools/map_editor/lib/editor.rb @@ -38,7 +38,7 @@ class IMICFPS InputMapper.keys.each do |key, pressed| next unless pressed - actions = InputMapper.action(key) + actions = InputMapper.actions(key) next unless actions actions.each do |action| diff --git a/lib/ui/menus/main_menu.rb b/lib/ui/menus/main_menu.rb index 406ea26..32179d6 100644 --- a/lib/ui/menus/main_menu.rb +++ b/lib/ui/menus/main_menu.rb @@ -27,7 +27,7 @@ class IMICFPS major, minor = gl_version.split(" ").first.split(".").map { |v| v.to_i } unless (major == 3 && minor >= 3) || (major > 3) message = -"[Notice] Your computer is reporting support for OpenGL #{major}.#{minor}, +"[Notice] Your computer is reporting support for OpenGL #{major}.#{minor}, however OpenGL 3.3 or higher is required. Fallback immediate mode renderer will be used." @@ -38,7 +38,7 @@ linux_mesa_message = (Linux Only) For MESA based drivers append --mesa-override as a commandline argument to override reported version." message += linux_mesa_message if RUBY_PLATFORM =~ /linux/ && gl_version.downcase.include?(" mesa ") - @old_gl_warning = Gosu::Image.from_markup(message, 24, align: :center) + @old_gl_warning = Gosu::Image.from_markup(message, 24, align: :center, font: "") end end diff --git a/maps/islands_test_map.json b/maps/islands_test_map.json index 710e79e..0cb4b76 100644 --- a/maps/islands_test_map.json +++ b/maps/islands_test_map.json @@ -46,7 +46,7 @@ } }, { - "type":"directional", + "type":"point", "intensity": 1, "position": { "x":0, diff --git a/shaders/fragment/lighting.glsl b/shaders/fragment/lighting.glsl index 2559f4b..40f592f 100644 --- a/shaders/fragment/lighting.glsl +++ b/shaders/fragment/lighting.glsl @@ -37,20 +37,22 @@ vec4 spotLight(Light light) { vec4 calculateLighting(Light light) { vec4 result; - switch(light.type) { - case DIRECTIONAL: { - result = directionalLight(light); - } - case POINT: { - result = pointLight(light); - } - case SPOT: { - result = spotLight(light); - } - default: { - // result = vec4(1,1,1,1); - result = directionalLight(light); - } + // switch(light.type) { + // case DIRECTIONAL: { + // result = directionalLight(light); + // } + // case SPOT: { + // result = spotLight(light); + // } + // default: { + // result = pointLight(light); + // } + // } + + if (light.type == DIRECTIONAL) { + result = directionalLight(light); + } else { + result = pointLight(light); } return result; diff --git a/shaders/include/light_struct.glsl b/shaders/include/light_struct.glsl index 6a18d09..45e63ab 100644 --- a/shaders/include/light_struct.glsl +++ b/shaders/include/light_struct.glsl @@ -1,5 +1,5 @@ struct Light { - int type; + int type; vec3 direction; vec3 position;