From 76ac1d553a8991ec655fd7cd14df16dadc602ff5 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Wed, 30 Jan 2019 13:37:22 -0600 Subject: [PATCH] Fixed crash due to old code from initial import, various clean up. --- lib/cyberarm_engine.rb | 2 + lib/cyberarm_engine/common.rb | 27 +++++++++ lib/cyberarm_engine/engine.rb | 37 ++++++------ lib/cyberarm_engine/game_state.rb | 91 +++++++++++++---------------- lib/cyberarm_engine/ui/container.rb | 28 +++++---- 5 files changed, 103 insertions(+), 82 deletions(-) create mode 100644 lib/cyberarm_engine/common.rb diff --git a/lib/cyberarm_engine.rb b/lib/cyberarm_engine.rb index 56d1320..1f2fe53 100644 --- a/lib/cyberarm_engine.rb +++ b/lib/cyberarm_engine.rb @@ -2,6 +2,8 @@ require "gosu" require_relative "cyberarm_engine/version" +require_relative "cyberarm_engine/common" + require_relative "cyberarm_engine/game_object" require_relative "cyberarm_engine/game_state" require_relative "cyberarm_engine/engine" diff --git a/lib/cyberarm_engine/common.rb b/lib/cyberarm_engine/common.rb new file mode 100644 index 0000000..37a8265 --- /dev/null +++ b/lib/cyberarm_engine/common.rb @@ -0,0 +1,27 @@ +module CyberarmEngine + module Common + def push_state(klass, options={}) + $window.push_state(klass, options) + end + + def current_state + $window.current_state + end + + def pop_state + $window.pop_state + end + + def show_cursor + $window.show_cursor + end + + def show_cursor=boolean + $window.show_cursor = boolean + end + + def draw_rect(x, y, width, height, color, z = 0) + $window.draw_rect(x,y,width,height,color,z) + end + end +end \ No newline at end of file diff --git a/lib/cyberarm_engine/engine.rb b/lib/cyberarm_engine/engine.rb index 372975b..9c6a8a4 100644 --- a/lib/cyberarm_engine/engine.rb +++ b/lib/cyberarm_engine/engine.rb @@ -1,7 +1,7 @@ module CyberarmEngine class Engine < Gosu::Window attr_accessor :show_cursor - attr_reader :current_game_state, :last_game_state, :last_frame_time + attr_reader :current_state, :last_frame_time def self.now Gosu.milliseconds @@ -20,19 +20,17 @@ module CyberarmEngine @current_frame_time = Gosu.milliseconds self.caption = "CyberarmEngine #{CyberarmEngine::VERSION} #{Gosu.language}" + @states = [] + setup if defined?(setup) end def draw - if @current_game_state.is_a?(GameState) - @current_game_state.draw - end + current_state.draw if current_state end def update - if @current_game_state.is_a?(GameState) - @current_game_state.update - end + current_state.update if current_state @last_frame_time = Gosu.milliseconds-@current_frame_time @current_frame_time = Gosu.milliseconds end @@ -46,27 +44,28 @@ module CyberarmEngine end def button_up(id) - @current_game_state.button_up(id) if @current_game_state + current_state.button_up(id) if current_state end - def push_game_state(klass, options={}) - @last_game_state = @current_game_state if @current_game_state + def push_state(klass, options={}) if klass.instance_of?(klass.class) && defined?(klass.options) - @current_game_state = klass + @states << klass else - klass.new(options) + @states << klass.new(options) if child_of?(klass, GameState) + @states << klass.new if child_of?(klass, Container) end end - def set_game_state(klass_instance) - @current_game_state = klass_instance + private def child_of?(input, klass) + input.ancestors.detect {|c| c == klass} end - def previous_game_state - # current_game_state = @current_game_state - # @current_game_state = @last_frame_time - # @last_game_state = current_game_state - @last_game_state + def current_state + @states.last + end + + def previous_state + @states.pop end # Sourced from https://gist.github.com/ippa/662583 diff --git a/lib/cyberarm_engine/game_state.rb b/lib/cyberarm_engine/game_state.rb index e01436f..b6d31d4 100644 --- a/lib/cyberarm_engine/game_state.rb +++ b/lib/cyberarm_engine/game_state.rb @@ -1,64 +1,51 @@ -class GameState - SCALE_X_BASE = 1920.0 - SCALE_Y_BASE = 1080.0 - attr_accessor :options, :global_pause - attr_reader :game_objects +module CyberarmEngine + class GameState + include Common - def initialize(options={}) - $window.set_game_state(self) - @options = options unless @options - @game_objects = [] - @global_pause = false + SCALE_X_BASE = 1920.0 + SCALE_Y_BASE = 1080.0 + attr_accessor :options, :global_pause + attr_reader :game_objects - setup - @_4ship = Ship.all.first if Ship.all.is_a?(Array) - end + def initialize(options={}) + @options = options unless @options + @game_objects = [] + @global_pause = false - def setup - end - - def draw - # count = 0 - @game_objects.each do |o| - o.draw if o.visible - # p o.class if o.visible - # count+=1 if o.visible + setup end - # puts "Num visible objects: #{count} of #{@game_objects.count}" - end - def update - @game_objects.each do |o| - unless o.paused || @global_pause - o.world_center_point.x = @_4ship.x - o.world_center_point.y = @_4ship.y + def setup + end - o.update - o.update_debug_text if $debug + def draw + @game_objects.each do |o| + o.draw if o.visible end end - end - def destroy - @options = nil - @game_objects = nil - end + def update + @game_objects.each do |o| + unless o.paused || @global_pause + o.update + o.update_debug_text if $debug + end + end + end - def button_up(id) - @game_objects.each do |o| - o.button_up(id) unless o.paused + def destroy + @options = nil + @game_objects = nil + end + + def button_up(id) + @game_objects.each do |o| + o.button_up(id) unless o.paused + end + end + + def add_game_object(object) + @game_objects << object end end - - def push_game_state(klass, options={}) - $window.push_game_state(klass, options) - end - - def draw_rect(x, y, width, height, color, z = 0) - $window.draw_rect(x,y,width,height,color,z) - end - - def add_game_object(object) - @game_objects << object - end -end +end \ No newline at end of file diff --git a/lib/cyberarm_engine/ui/container.rb b/lib/cyberarm_engine/ui/container.rb index 8be8a86..7b19d8b 100644 --- a/lib/cyberarm_engine/ui/container.rb +++ b/lib/cyberarm_engine/ui/container.rb @@ -1,14 +1,20 @@ module CyberarmEngine class Container + include Common + attr_accessor :text_color attr_reader :elements, :x, :y, :width, :height, :options attr_reader :scroll_x, :scroll_y, :internal_width, :internal_height - def initialize(x = 0, y = 100, width = $window.width, height = $window.height, options = {}) + def initialize(x = 0, y = 0, width = $window.width, height = $window.height, options = {}) + raise unless x.is_a?(Numeric) + raise unless y.is_a?(Numeric) + raise unless width.is_a?(Numeric) + raise unless height.is_a?(Numeric) + raise unless options.is_a?(Hash) @x, @y, @width, @height, @internal_width, @internal_height = x, y, width, height-y, width, height-y @scroll_x, @scroll_y = 0, 0 @scroll_speed = 10 - puts "#{self.class}: width #{width}, height #{@height}" @options = {} @allow_recreation_on_resize = true @@ -60,8 +66,8 @@ module CyberarmEngine end def text(text, x, y, size = 18, color = self.text_color, alignment = nil, font = nil) - relative_x = @x+x - relative_y = @y+y + relative_x(x) + relative_y(y) _text = Text.new(text, x: relative_x, y: relative_y, size: size, color: color, alignment: alignment, font: font) @elements.push(_text) if _text.y-(_text.height*2) > @internal_height @@ -72,8 +78,8 @@ module CyberarmEngine end def button(text, x, y, tooltip = "", &block) - relative_x = @x+x - relative_y = @y+y + relative_x(x) + relative_y(y) _button = Button.new(text, relative_x, relative_y, false, tooltip) { if block.is_a?(Proc); block.call; end } @elements.push(_button) if _button.y-(_button.height*2) > @internal_height @@ -84,8 +90,8 @@ module CyberarmEngine end def input(text, x, y, width = Input::WIDTH, size = 18, color = Gosu::Color::BLACK, tooltip = "") - relative_x = @x+x - relative_y = @y+y + relative_x(x) + relative_y(y) _input = Input.new(text, relative_x, relative_y, width, size, color) @elements.push(_input) if _input.y-(_input.height*2) > @internal_height @@ -96,8 +102,8 @@ module CyberarmEngine end def check_box(x, y, checked = false, size = CheckBox::SIZE) - relative_x = @x+x - relative_y = @y+y + relative_x(x) + relative_y(y) _check_box = CheckBox.new(relative_x, relative_y, checked, size) @elements.push(_check_box) if _check_box.y-(_check_box.height*2) > @internal_height @@ -115,7 +121,7 @@ module CyberarmEngine # Fills container background with color def fill(color = Gosu::Color::BLACK, z = -1) - $window.draw_rect(@x, @y, @width, @height, color, z) + Gosu.draw_rect(@x, @y, @width, @height, color, z) end def set_layout_y(start, spacing)