From 8df05eda133aef62cfea773f05e4db0749a414d4 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Thu, 26 Sep 2019 16:19:01 -0500 Subject: [PATCH] Added event handler for :entity_moved, added door script, various tweaks --- assets/base/door/manifest.yaml | 3 +++ assets/base/door/scripts/door.rb | 9 +++++++++ i-mic-fps.rb | 1 + lib/common_methods.rb | 2 +- lib/event_handlers/entity_lifecycle.rb | 2 +- lib/event_handlers/entity_moved.rb | 15 +++++++++++++++ lib/game_objects/entity.rb | 5 +++-- lib/game_objects/model_loader.rb | 2 +- lib/states/game_states/game.rb | 8 +++----- lib/subscription.rb | 5 ++--- lib/window.rb | 5 ++++- 11 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 assets/base/door/scripts/door.rb create mode 100644 lib/event_handlers/entity_moved.rb diff --git a/assets/base/door/manifest.yaml b/assets/base/door/manifest.yaml index 10d2792..1738a81 100644 --- a/assets/base/door/manifest.yaml +++ b/assets/base/door/manifest.yaml @@ -2,3 +2,6 @@ name: "door" model: "door.obj" collision: "boundingbox" +scripts: [ + "door" +] diff --git a/assets/base/door/scripts/door.rb b/assets/base/door/scripts/door.rb new file mode 100644 index 0000000..225be42 --- /dev/null +++ b/assets/base/door/scripts/door.rb @@ -0,0 +1,9 @@ +origin = self.position + +on.entity_moved do |event| + if origin.distance3d(event.entity.position) <= 3.0 + self.position = origin + Vector.up * 2.4 + else + self.position = origin + end +end \ No newline at end of file diff --git a/i-mic-fps.rb b/i-mic-fps.rb index 8376c04..1cd8dcb 100644 --- a/i-mic-fps.rb +++ b/i-mic-fps.rb @@ -62,6 +62,7 @@ require_relative "lib/publisher" require_relative "lib/event" require_relative "lib/event_handler" require_relative "lib/event_handlers/input" +require_relative "lib/event_handlers/entity_moved" require_relative "lib/event_handlers/entity_lifecycle" require_relative "lib/component" diff --git a/lib/common_methods.rb b/lib/common_methods.rb index aafef05..2ab0945 100644 --- a/lib/common_methods.rb +++ b/lib/common_methods.rb @@ -7,7 +7,7 @@ class IMICFPS def window; $window; end - def delta_time; (Gosu.milliseconds - @delta_time) / 1000.0; end + def delta_time; (Gosu.milliseconds - window.delta_time) / 1000.0; end def button_down?(id); window.button_down?(id); end def mouse_x; window.mouse_x; end diff --git a/lib/event_handlers/entity_lifecycle.rb b/lib/event_handlers/entity_lifecycle.rb index b16b348..0346696 100644 --- a/lib/event_handlers/entity_lifecycle.rb +++ b/lib/event_handlers/entity_lifecycle.rb @@ -2,7 +2,7 @@ class IMICFPS class EventHandler class EntityLifeCycle < EventHandler def handles - [:create, :entity_move, :destroy] + [:create, :move, :destroy] end def handle(subscriber, context, *args) diff --git a/lib/event_handlers/entity_moved.rb b/lib/event_handlers/entity_moved.rb new file mode 100644 index 0000000..2853770 --- /dev/null +++ b/lib/event_handlers/entity_moved.rb @@ -0,0 +1,15 @@ +class IMICFPS + class EventHandler + class EntityMoved < EventHandler + def handles + [:entity_moved] + end + + def handle(subscriber, context, *args) + event = EventHandler::Event.new(entity: context) + + subscriber.trigger(event) + end + end + end +end \ No newline at end of file diff --git a/lib/game_objects/entity.rb b/lib/game_objects/entity.rb index 479aff9..0cbf4ea 100644 --- a/lib/game_objects/entity.rb +++ b/lib/game_objects/entity.rb @@ -36,7 +36,6 @@ class IMICFPS @physics = @manifest.physics # Entity affected by gravity and what not @mass = 100 # kg - @delta_time = Gosu.milliseconds @last_position = Vector.new(@position.x, @position.y, @position.z) load_scripts @@ -110,11 +109,13 @@ class IMICFPS def update model.update - @delta_time = Gosu.milliseconds unless at_same_position? + Publisher.instance.publish(:entity_moved, self, self) @bounding_box = normalize_bounding_box_with_offset if model end + + @last_position = Vector.new(@position.x, @position.y, @position.z) end def debug_color=(color) diff --git a/lib/game_objects/model_loader.rb b/lib/game_objects/model_loader.rb index 6336f9a..b2961a8 100644 --- a/lib/game_objects/model_loader.rb +++ b/lib/game_objects/model_loader.rb @@ -21,7 +21,7 @@ class IMICFPS unless load_model_from_cache case @type when :obj - @model = IMICFPS::Model.new(file_path: @model_file, entity: entity, parser: Wavefront::Parser) + @model = IMICFPS::Model.new(file_path: @model_file, parser: Wavefront::Parser) else raise "Unsupported model type, supported models are: #{@supported_models.join(', ')}" end diff --git a/lib/states/game_states/game.rb b/lib/states/game_states/game.rb index 93ca3f4..c24b120 100644 --- a/lib/states/game_states/game.rb +++ b/lib/states/game_states/game.rb @@ -1,7 +1,7 @@ class IMICFPS class Game < GameState - attr_reader :collision_manager + attr_reader :collision_manager, :delta_time def setup @collision_manager = CollisionManager.new(game_state: self) @renderer = Renderer.new(game_state: self) @@ -84,10 +84,9 @@ class IMICFPS end def update - @last_frame_time = Gosu.milliseconds update_text - @publisher.publish(:tick, Gosu.milliseconds - @delta_time) + @publisher.publish(:tick, Gosu.milliseconds - window.delta_time) @collision_manager.update @entities.each(&:update) @@ -161,7 +160,6 @@ class IMICFPS window.close if window.button_down?(Gosu::KbEscape) window.number_of_vertices = 0 - @delta_time = Gosu.milliseconds end def update_text @@ -176,7 +174,7 @@ Camera X:#{@camera.position.x.round(2)} Y:#{@camera.position.y.round(2)} Z:#{@ca #{if @camera.entity then "Actor X:#{@camera.entity.position.x.round(2)} Y:#{@camera.entity.position.y.round(2)} Z:#{@camera.entity.position.z.round(2)}";end} Field Of View: #{@camera.field_of_view} Mouse Sesitivity: #{@camera.mouse_sensitivity} -Last Frame: #{delta_time * 1000.0}ms (#{Gosu.fps} fps) +Last Frame: #{Gosu.milliseconds - window.delta_time}ms (#{Gosu.fps} fps) Vertices: #{formatted_number(window.number_of_vertices)} Faces: #{formatted_number(window.number_of_vertices/3)} diff --git a/lib/subscription.rb b/lib/subscription.rb index 3876807..ff03b26 100644 --- a/lib/subscription.rb +++ b/lib/subscription.rb @@ -25,11 +25,10 @@ class IMICFPS private def subscribable_events [ :tick, - :create, - :destroy, + :create, :move, :destroy, + :entity_moved, :button_down, :button_up, :mouse_move, - :entity_move, :interact, :player_join, :player_leave, :player_die, :pickup_item, :use_item, :drop_item, diff --git a/lib/window.rb b/lib/window.rb index d48d941..dfcf3fb 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -3,7 +3,7 @@ class IMICFPS attr_accessor :number_of_vertices, :needs_cursor attr_reader :camera - attr_reader :console + attr_reader :console, :delta_time def initialize(window_width = 1280, window_height = 800, fullscreen = false) fps_target = (ARGV.first.to_i != 0) ? ARGV.first.to_i : 60 if ARGV.join.include?("--native") @@ -22,6 +22,8 @@ class IMICFPS Commands::Command.setup push_state(MainMenu) + + @delta_time = Gosu.milliseconds end def draw @@ -34,6 +36,7 @@ class IMICFPS super @console.update if @show_console + @delta_time = Gosu.milliseconds end def button_down(id)