diff --git a/i-mic-fps.rb b/i-mic-fps.rb index 399ae90..59dd30a 100644 --- a/i-mic-fps.rb +++ b/i-mic-fps.rb @@ -64,6 +64,12 @@ require_relative "lib/states/game_states/close" require_relative "lib/states/game_states/game" require_relative "lib/states/game_states/loading_state" +require_relative "lib/hud" +require_relative "lib/hud/widget" +require_relative "lib/hud/widgets/ammo" +require_relative "lib/hud/widgets/radar" +require_relative "lib/hud/widgets/health" + require_relative "lib/subscription" require_relative "lib/publisher" require_relative "lib/event" diff --git a/lib/hud.rb b/lib/hud.rb new file mode 100644 index 0000000..b39e7ab --- /dev/null +++ b/lib/hud.rb @@ -0,0 +1,23 @@ +class IMICFPS + class HUD + def initialize(player) + @ammo = AmmoWidget.new({ player: player }) + @radar = RadarWidget.new({ player: player }) + @health = HealthWidget.new({ player: player }) + + @hud_elements = [ + @ammo, + @radar, + @health, + ] + end + + def draw + @hud_elements.each(&:draw) + end + + def update + @hud_elements.each(&:update) + end + end +end \ No newline at end of file diff --git a/lib/hud/widget.rb b/lib/hud/widget.rb new file mode 100644 index 0000000..7e20844 --- /dev/null +++ b/lib/hud/widget.rb @@ -0,0 +1,25 @@ +class IMICFPS + class HUD + class Widget + include CommonMethods + attr_reader :options + + def initialize(options = {}) + @options = options + @player = options[:player] + @margin = 10 + + setup + end + + def setup + end + + def draw + end + + def update + end + end + end +end \ No newline at end of file diff --git a/lib/hud/widgets/ammo.rb b/lib/hud/widgets/ammo.rb new file mode 100644 index 0000000..0cef845 --- /dev/null +++ b/lib/hud/widgets/ammo.rb @@ -0,0 +1,29 @@ +class IMICFPS + class HUD + class AmmoWidget < HUD::Widget + def setup + @text = Text.new("") + @background = Gosu::Color.new(0x88222222) + end + + def draw + Gosu.draw_rect( + @text.x - @margin, @text.y - @margin, + @text.width + @margin * 2, @text.height + @margin * 2, + @background + ) + @text.draw + end + + def update + if (Gosu.milliseconds / 1000.0) % 1.0 >= 0.9 + random = "#{rand(0..999)}".rjust(3, "0") + @text.text = "Pistol\nAMMO: #{random}" + end + + @text.x = window.width - (@margin * 2 + @text.width) + @text.y = window.height - (@margin * 2 + @text.height) + end + end + end +end \ No newline at end of file diff --git a/lib/hud/widgets/health.rb b/lib/hud/widgets/health.rb new file mode 100644 index 0000000..842842c --- /dev/null +++ b/lib/hud/widgets/health.rb @@ -0,0 +1,48 @@ +class IMICFPS + class HUD + class HealthWidget < HUD::Widget + def setup + @spacer = 0 + @text = Text.new("") + @width = 512 + @height = 24 + @slant = 32 + + @color = Gosu::Color.rgba(100, 100, 200, 128) + @shield = Gosu::Color.rgba(200, 100, 50, 200) + + @health = 0.0 + end + + def draw + @text.draw + fill_quad( + window.width / 2 - @width / 2, @spacer, # TOP LEFT + window.width / 2 + @width / 2, @spacer, # TOP RIGHT + window.width / 2 + @width / 2 - @slant, @spacer + @height, # BOTTOM RIGHT + window.width / 2 - @width / 2 + @slant, @spacer + @height, # BOTTOM LEFT + @color + ) + + # Current Health + fill_quad( + window.width / 2 - @width / 2, @spacer, # TOP LEFT + (window.width / 2 - @width / 2) + @width * @health, @spacer, # TOP RIGHT + (window.width / 2 - @width / 2) + @width * @health - @slant, @spacer + @height, # BOTTOM RIGHT + window.width / 2 - @width / 2 + @slant, @spacer + @height, # BOTTOM LEFT + @shield + ) + end + + def update + percentage = "#{(@health * 100).round}".rjust(3, "0") + @text.text = "[Health #{percentage}%]" + @text.x = window.width / 2 - @text.width / 2 + @text.y = @spacer + @height / 2 - @text.height / 2 + + @health += 0.1 * window.dt + @health = 0 if @health > 1.0 + end + end + end +end \ No newline at end of file diff --git a/lib/hud/widgets/radar.rb b/lib/hud/widgets/radar.rb new file mode 100644 index 0000000..163c876 --- /dev/null +++ b/lib/hud/widgets/radar.rb @@ -0,0 +1,28 @@ +class IMICFPS + class HUD + class RadarWidget < HUD::Widget + def setup + @size = 256 + @color = Gosu::Color.new(0x88222222) + + @text = Text.new("RADAR") + end + + def draw + Gosu.draw_rect( + @margin, window.height - (@size + @margin), + @size, @size, + @color + ) + + @text.draw + end + + def update + @text.text = "RADAR: X #{@player.position.x.round(1)} Y #{@player.position.z.round(1)}" + @text.x = @margin + @size / 2 - @text.width / 2 + @text.y = window.height - (@margin + @size) + end + end + end +end \ No newline at end of file diff --git a/lib/states/game_states/boot.rb b/lib/states/game_states/boot.rb index 64138d4..ceb6638 100644 --- a/lib/states/game_states/boot.rb +++ b/lib/states/game_states/boot.rb @@ -57,7 +57,7 @@ class IMICFPS push_state(MainMenu) if Gosu.milliseconds - @start_time >= @time_to_live end - def button_down(id) + def button_up(id) if id == Gosu::KbEscape or (id >= Gosu::GP_LEFT and id >= Gosu::GP_BUTTON_15) or id == Gosu::MsLeft diff --git a/lib/states/game_states/close.rb b/lib/states/game_states/close.rb index 48dffbe..2cd1765 100644 --- a/lib/states/game_states/close.rb +++ b/lib/states/game_states/close.rb @@ -51,7 +51,7 @@ class IMICFPS @slope -= 25 * window.dt end - def button_down(id) + def button_up(id) if id == Gosu::KbEscape or (id >= Gosu::GP_LEFT and id >= Gosu::GP_BUTTON_15) or id == Gosu::MsLeft diff --git a/lib/states/game_states/game.rb b/lib/states/game_states/game.rb index a509569..9235d8f 100644 --- a/lib/states/game_states/game.rb +++ b/lib/states/game_states/game.rb @@ -12,6 +12,7 @@ class IMICFPS @director = Networking::Director.new(mode: :memory, hostname: "i-mic.rubyclan.org", port: 56789, interface: { server: Networking::MemoryServer, connection: Networking::MemoryConnection }, state: self) @crosshair = Crosshair.new + @hud = HUD.new(@player) @text = Text.new("Pending...", x: 10, y: 22, z: 1, size: 18, font: "DejaVu Sans", shadow_color: Gosu::Color::BLACK) @@ -27,6 +28,7 @@ class IMICFPS @map.render(@camera) @crosshair.draw + @hud.draw @text.draw end @@ -38,6 +40,7 @@ class IMICFPS @map.update control_player + @hud.update @camera.update @director.tick(window.dt)