From 46cffa293e570aed74b5c9df1ace1f78c76758e4 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Tue, 28 Jan 2020 19:59:42 -0600 Subject: [PATCH] Added more menus; stub for settings, basic pause menu, fixes for multiple Game inits --- README.md | 1 + i-mic-fps.rb | 2 ++ lib/component.rb | 3 ++- lib/event_handler.rb | 6 ++++-- lib/states/game_states/game.rb | 6 +++++- lib/ui/menu.rb | 35 ++++++++++++++++++--------------- lib/ui/menus/game_pause_menu.rb | 23 ++++++++++++++++++++++ lib/ui/menus/main_menu.rb | 2 +- lib/ui/menus/settings_menu.rb | 14 +++++++++++++ lib/window.rb | 12 +++++++++++ 10 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 lib/ui/menus/game_pause_menu.rb create mode 100644 lib/ui/menus/settings_menu.rb diff --git a/README.md b/README.md index 5f3acb9..47a84e9 100644 --- a/README.md +++ b/README.md @@ -10,5 +10,6 @@ Requires a Ruby runtime that supports the gosu and opengl-bindings C-extensions ### Options * `--native` - Launch in fullscreen using primary displays resolution * `--profile` - Run ruby-prof profiler +* `--mesa-override` - (Linux) Force MESA to use OpenGL/GLSL version 3.30 * `--savedemo` - Record camera movement and key events to playback later *(alpha-quality feature)* * `--playdemo` - Plays the previously recorded demo *(alpha-quality feature)* \ No newline at end of file diff --git a/i-mic-fps.rb b/i-mic-fps.rb index 282781a..aaba46f 100644 --- a/i-mic-fps.rb +++ b/i-mic-fps.rb @@ -52,6 +52,8 @@ Dir.glob("#{IMICFPS::GAME_ROOT_PATH}/lib/ui/commands/*.rb").each do |cmd| end require_relative "lib/ui/console" require_relative "lib/ui/menus/main_menu" +require_relative "lib/ui/menus/settings_menu" +require_relative "lib/ui/menus/game_pause_menu" require_relative "lib/states/game_states/game" require_relative "lib/states/game_states/loading_state" diff --git a/lib/component.rb b/lib/component.rb index f83d125..527a579 100644 --- a/lib/component.rb +++ b/lib/component.rb @@ -7,12 +7,13 @@ class IMICFPS end def self.inherited(subclass) - pp subclass COMPONENTS["__pending"] ||= [] COMPONENTS["__pending"] << subclass end def self.initiate + return unless COMPONENTS.dig("__pending") # Already setup + COMPONENTS["__pending"].each do |klass| component = klass.new COMPONENTS[component.name] = component diff --git a/lib/event_handler.rb b/lib/event_handler.rb index 59ff36f..7bb5fa0 100644 --- a/lib/event_handler.rb +++ b/lib/event_handler.rb @@ -9,14 +9,16 @@ class IMICFPS end def self.initiate + preserve = @@handlers["__pending"] + @@handlers.clear + @@handlers["__pending"] = preserve + @@handlers["__pending"].each do |handler| instance = handler.new instance.handles.each do |event| @@handlers[event] = instance end end - - @@handlers.delete("__pending") end def self.get(event) diff --git a/lib/states/game_states/game.rb b/lib/states/game_states/game.rb index 07834de..e36498e 100644 --- a/lib/states/game_states/game.rb +++ b/lib/states/game_states/game.rb @@ -50,7 +50,6 @@ class IMICFPS @demo.update if @demo - window.close if window.button_down?(Gosu::KbEscape) window.number_of_vertices = 0 end @@ -86,6 +85,11 @@ eos end def button_down(id) + if id == Gosu::KB_ESCAPE + push_state(GamePauseMenu) + + return + end @demo.button_down(id) if @demo InputMapper.keydown(id) diff --git a/lib/ui/menu.rb b/lib/ui/menu.rb index 822d7be..bb205c1 100644 --- a/lib/ui/menu.rb +++ b/lib/ui/menu.rb @@ -12,14 +12,27 @@ class IMICFPS def title(text, color = @base_color) @elements << Text.new(text, color: color, size: 100, x: 0, y: 15, alignment: :center) + @_title = @elements.last + end + + def subtitle(text, color = Gosu::Color::WHITE) + @elements << Text.new(text, color: color, size: 50, x: 0, y: 100, alignment: :center) + @_subtitle = @elements.last 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) + 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 + draw_background + draw_menu_box + draw_menu + window.draw_cursor + end + + def draw_background @background ||= Gosu.record(Gosu.screen_width, Gosu.screen_height) do ((Gosu.screen_height+@slope)/@size).times do |i| fill_quad( @@ -41,30 +54,21 @@ class IMICFPS end @background.draw(0, 0, 0) + end - # Box + def draw_menu_box draw_rect( window.width/4, 0, window.width/2, window.height, Gosu::Color.rgba(0, 0, 0, 150) # Gosu::Color.rgba(@base_color.red+@color_step, @base_color.green+@color_step, @base_color.blue+@color_step, 200) ) + end - # Texts + def draw_menu @elements.each do |e| e.draw end - - # Cursor - if window.needs_cursor - fill_quad( - mouse_x, mouse_y, - mouse_x+16, mouse_y, - mouse_x, mouse_y+16, - mouse_x, mouse_y+16, - Gosu::Color::WHITE, Float::INFINITY - ) - end end def update @@ -84,7 +88,6 @@ class IMICFPS end def button_up(id) - window.close if id == Gosu::KbEscape if id == Gosu::MsLeft @elements.each do |e| next unless e.is_a?(Link) @@ -126,7 +129,7 @@ class IMICFPS def width; text.width; end def height; text.height; end def draw; text.draw; end - def clicked; @block.call; end + def clicked; @block.call if @block; end end end end diff --git a/lib/ui/menus/game_pause_menu.rb b/lib/ui/menus/game_pause_menu.rb new file mode 100644 index 0000000..72237d4 --- /dev/null +++ b/lib/ui/menus/game_pause_menu.rb @@ -0,0 +1,23 @@ +class IMICFPS + class GamePauseMenu < Menu + def setup + title "I-MIC FPS" + subtitle "Paused" + + link "Resume" do + pop_state + end + + link "Disconnect" do + push_state(MainMenu) + end + end + + def draw + previous_state.draw + Gosu.flush + + super + end + end +end \ No newline at end of file diff --git a/lib/ui/menus/main_menu.rb b/lib/ui/menus/main_menu.rb index 61dc937..3e5016e 100644 --- a/lib/ui/menus/main_menu.rb +++ b/lib/ui/menus/main_menu.rb @@ -6,7 +6,7 @@ class IMICFPS push_state(LoadingState.new(forward: Game, map_file: GAME_ROOT_PATH + "/maps/test_map.json")) end link "Settings" do - # push_game_state(SettingsMenu) + push_state(SettingsMenu) end link "Exit" do window.close diff --git a/lib/ui/menus/settings_menu.rb b/lib/ui/menus/settings_menu.rb new file mode 100644 index 0000000..ab0925b --- /dev/null +++ b/lib/ui/menus/settings_menu.rb @@ -0,0 +1,14 @@ +class IMICFPS + class SettingsMenu < Menu + def setup + title "I-MIC FPS" + subtitle "Settings" + + link "\"There is no spoon.\"" + + link "Back" do + pop_state + end + end + end +end \ No newline at end of file diff --git a/lib/window.rb b/lib/window.rb index 9cd2ac0..8eef29f 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -36,6 +36,18 @@ class IMICFPS @console.draw if @show_console end + def draw_cursor + if needs_cursor + draw_quad( + mouse_x, mouse_y, Gosu::Color::WHITE, + mouse_x+16, mouse_y, Gosu::Color::WHITE, + mouse_x, mouse_y+16, Gosu::Color::WHITE, + mouse_x, mouse_y+16, Gosu::Color::WHITE, + Float::INFINITY + ) + end + end + def update super