diff --git a/i-mic-fps.rb b/i-mic-fps.rb index 63a40fc..23d4bb5 100644 --- a/i-mic-fps.rb +++ b/i-mic-fps.rb @@ -46,6 +46,7 @@ require_relative "lib/managers/object_manager" require_relative "lib/managers/light_manager" require_relative "lib/states/game_state" +require_relative "lib/states/game_states/loading_state" require_relative "lib/states/game_states/game" require_relative "lib/states/menu" require_relative "lib/states/menus/main_menu" diff --git a/lib/common_methods.rb b/lib/common_methods.rb index 3f4d93e..c9302cb 100644 --- a/lib/common_methods.rb +++ b/lib/common_methods.rb @@ -23,5 +23,8 @@ class IMICFPS def draw_quad(*args) $window.draw_quad(*args) end + def fill(color = Gosu::Color::WHITE) + draw_rect(0, 0, $window.width, $window.height, color) + end end end diff --git a/lib/objects/game_object.rb b/lib/objects/game_object.rb index 25f989b..af49ee6 100644 --- a/lib/objects/game_object.rb +++ b/lib/objects/game_object.rb @@ -26,6 +26,9 @@ class IMICFPS setup if @bound_model + @bound_model.model.game_object = self + @bound_model.model.objects.each {|o| o.parent = self} + box = normalize_bounding_box(@bound_model.model.bounding_box) @width = box.max_x-box.min_x @height = box.max_y-box.min_y @@ -38,6 +41,8 @@ class IMICFPS def bind_model(model) raise "model isn't a model!" unless model.is_a?(ModelLoader) @bound_model = model + @bound_model.model.game_object = self + @bound_model.model.objects.each {|o| o.parent = self} box = normalize_bounding_box(@bound_model.model.bounding_box) @width = box.max_x-box.min_x @height = box.max_y-box.min_y diff --git a/lib/objects/model_loader.rb b/lib/objects/model_loader.rb index bcd46b7..d4028b7 100644 --- a/lib/objects/model_loader.rb +++ b/lib/objects/model_loader.rb @@ -8,20 +8,21 @@ class IMICFPS attr_reader :model, :name, :debug_color - def initialize(type:, file_path:, game_object:) + def initialize(type:, file_path:, game_object: nil) @type = type @file_path = file_path @name = file_path.split("/").last.split(".").first @debug_color = Color.new(0.0, 1.0, 0.0) @model = nil + @supported_models = ["OBJ"] unless load_model_from_cache - case type + case @type when :obj @model = Wavefront::Model.new(file_path: @file_path, game_object: game_object) else - raise "Unsupported model type, supported models are: #{Model.supported_models.join(', ')}" + raise "Unsupported model type, supported models are: #{@supported_models.join(', ')}" end cache_model diff --git a/lib/states/game_state.rb b/lib/states/game_state.rb index 6804913..24e47f1 100644 --- a/lib/states/game_state.rb +++ b/lib/states/game_state.rb @@ -1,12 +1,17 @@ class IMICFPS class GameState include CommonMethods - - def initialize + attr_reader :options + def initialize(options = {}) + @options = options @delta_time = Gosu.milliseconds setup end + def push_game_state(klass_or_instance) + $window.push_game_state(klass_or_instance) + end + def setup end diff --git a/lib/states/game_states/game.rb b/lib/states/game_states/game.rb index 61437e2..60ff31c 100644 --- a/lib/states/game_states/game.rb +++ b/lib/states/game_states/game.rb @@ -77,7 +77,7 @@ Camera X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)} #{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} -Faces: #{@number_of_faces} +Faces: #{$window.number_of_faces} Last Frame: #{delta_time*1000.0}ms (#{Gosu.fps} fps) Draw Skydome: #{@draw_skydome} @@ -117,7 +117,7 @@ eos @camera.update $window.close if $window.button_down?(Gosu::KbEscape) - @number_of_faces = 0 + $window.number_of_faces = 0 @delta_time = Gosu.milliseconds end diff --git a/lib/states/menu.rb b/lib/states/menu.rb index 69d8ba6..6297d9f 100644 --- a/lib/states/menu.rb +++ b/lib/states/menu.rb @@ -1,9 +1,126 @@ class IMICFPS class Menu < GameState - def title(text, color = Gosu::Color.rgba(127, 64, 0, 200)) + def initialize + @elements = [] + @size = 50 + @slope = 250 + @color_step = 10 + @base_color = Gosu::Color.rgb(255, 127, 0) + super end - def link(text, color = Gosu::Color::BLUE, &block) + def title(text, color = @base_color) + @elements << Text.new(text, color: color, size: 100, x: 0, y: 15, alignment: :center) + end + + def link(text, color = Gosu::Color.rgb(0,127,127), &block) + text = Text.new(text, color: color, size: 50, x: 0, y: 100+(60*@elements.count), alignment: :center) + @elements << Link.new(text, self, block) + end + + def draw + @background ||= Gosu.record(Gosu.screen_width, Gosu.screen_height) do + ((Gosu.screen_height+@slope)/@size).times do |i| + fill_quad( + 0, i*@size, + 0, @slope+(i*@size), + $window.width/2, (-@slope)+(i*@size), + $window.width/2, i*@size, + Gosu::Color.rgba(@base_color.red-i*@color_step, @base_color.green-i*@color_step, @base_color.blue-i*@color_step, 200) + ) + fill_quad( + $window.width, i*@size, + $window.width, @slope+(i*@size), + $window.width/2, (-@slope)+(i*@size), + $window.width/2, i*@size, + Gosu::Color.rgba(@base_color.red-i*@color_step, @base_color.green-i*@color_step, @base_color.blue-i*@color_step, 200) + ) + end + end + + @background.draw(0, 0, 0) + + # Box + draw_rect( + $window.width/4, 0, + $window.width/2, $window.height, + Gosu::Color.rgba(100, 100, 100, 150) + # Gosu::Color.rgba(@base_color.red+@color_step, @base_color.green+@color_step, @base_color.blue+@color_step, 200) + ) + + # Texts + @elements.each do |e| + e.draw + end + + # Cursor + fill_quad( + mouse_x, mouse_y, + mouse_x+16, mouse_y+16, + mouse_x, mouse_y+16, + mouse_x, mouse_y+16, + Gosu::Color::RED, Float::INFINITY + ) + end + + def update + @elements.each do |e| + e.update + end + end + + def fill_quad(x1, y1, x2, y2, x3, y3, x4, y4, color = Gosu::Color::WHITE, z = 0, mode = :default) + draw_quad( + x1,y1, color, + x2,y2, color, + x3,y3, color, + x4,y4, color, + z, mode + ) + end + + def button_up(id) + close if id == Gosu::KbEscape + if Gosu::MsLeft + @elements.each do |e| + next unless e.is_a?(Link) + if mouse_over?(e) + e.clicked + end + end + end + end + + def mouse_over?(object) + if mouse_x.between?(object.x, object.x+object.width) + if mouse_y.between?(object.y, object.y+object.height) + true + end + end + end + + class Link + attr_reader :text, :block + def initialize(text, host, block) + @text, @host, @block = text, host, block + @color = @text.color + @hover_color = Gosu::Color.rgb(255, 127, 0) + end + + def update + if @host.mouse_over?(self) + @text.color = @hover_color + else + @text.color = @color + end + end + + def x; text.x; end + def y; text.y; end + def width; text.width; end + def height; text.height; end + def draw; text.draw; end + def clicked; @block.call; end end end end \ No newline at end of file diff --git a/lib/states/menus/main_menu.rb b/lib/states/menus/main_menu.rb index 86e8d8e..1973b90 100644 --- a/lib/states/menus/main_menu.rb +++ b/lib/states/menus/main_menu.rb @@ -2,6 +2,12 @@ class IMICFPS class MainMenu < Menu def setup title "I-MIC FPS" + link "Single Player" do + push_game_state(LoadingState.new(forward: Game)) + end + link "Exit" do + $window.close + end end end end \ No newline at end of file diff --git a/lib/wavefront/model.rb b/lib/wavefront/model.rb index 857a458..8570b07 100644 --- a/lib/wavefront/model.rb +++ b/lib/wavefront/model.rb @@ -11,12 +11,12 @@ class IMICFPS include Parser attr_accessor :objects, :materials, :vertices, :texures, :normals, :faces - attr_accessor :x, :y, :z, :scale + attr_accessor :x, :y, :z, :scale, :game_object attr_reader :bounding_box - def initialize(file_path:, game_object:) + def initialize(file_path:, game_object: nil) @game_object = game_object - update + update if @game_object @file_path = file_path @file = File.open(file_path, 'r') @material_file = nil @@ -103,6 +103,10 @@ class IMICFPS def update @x, @y, @z = @game_object.x, @game_object.y, @game_object.z @scale = @game_object.scale + # if @scale != @game_object.scale + # puts "oops for #{self}: #{@scale} != #{@game_object.scale}" + # self.objects.each(&:reflatten) if self.objects && self.objects.count > 0 + # end end end end diff --git a/lib/wavefront/object.rb b/lib/wavefront/object.rb index 635ce35..de203d5 100644 --- a/lib/wavefront/object.rb +++ b/lib/wavefront/object.rb @@ -22,6 +22,20 @@ class IMICFPS # material = index[3] end + def parent=(game_object) + @parent = game_object + end + + def reflatten + @vertices_list = nil + @textures_list = nil + @normals_list = nil + + flattened_vertices + flattened_textures + flattened_normals + end + def at_same_position? if @x == @parent.x if @x == @parent.x diff --git a/lib/window.rb b/lib/window.rb index c56d7f1..b86b7f3 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -15,19 +15,29 @@ class IMICFPS @needs_cursor = false @number_of_faces = 0 - @active_state = Game.new + @active_state = nil + + push_game_state(MainMenu) + end + + def push_game_state(klass_or_instance) + if klass_or_instance.respond_to?(:draw) + @active_state = klass_or_instance + else + @active_state = klass_or_instance.new + end end def draw - @active_state.draw + @active_state.draw if @active_state end def update - @active_state.update + @active_state.update if @active_state end def button_up(id) - @active_state.button_up(id) + @active_state.button_up(id) if @active_state end end end