mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Added event handler for :entity_moved, added door script, various tweaks
This commit is contained in:
@@ -2,3 +2,6 @@
|
|||||||
name: "door"
|
name: "door"
|
||||||
model: "door.obj"
|
model: "door.obj"
|
||||||
collision: "boundingbox"
|
collision: "boundingbox"
|
||||||
|
scripts: [
|
||||||
|
"door"
|
||||||
|
]
|
||||||
|
|||||||
9
assets/base/door/scripts/door.rb
Normal file
9
assets/base/door/scripts/door.rb
Normal file
@@ -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
|
||||||
@@ -62,6 +62,7 @@ require_relative "lib/publisher"
|
|||||||
require_relative "lib/event"
|
require_relative "lib/event"
|
||||||
require_relative "lib/event_handler"
|
require_relative "lib/event_handler"
|
||||||
require_relative "lib/event_handlers/input"
|
require_relative "lib/event_handlers/input"
|
||||||
|
require_relative "lib/event_handlers/entity_moved"
|
||||||
require_relative "lib/event_handlers/entity_lifecycle"
|
require_relative "lib/event_handlers/entity_lifecycle"
|
||||||
|
|
||||||
require_relative "lib/component"
|
require_relative "lib/component"
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class IMICFPS
|
|||||||
|
|
||||||
def window; $window; end
|
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 button_down?(id); window.button_down?(id); end
|
||||||
|
|
||||||
def mouse_x; window.mouse_x; end
|
def mouse_x; window.mouse_x; end
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ class IMICFPS
|
|||||||
class EventHandler
|
class EventHandler
|
||||||
class EntityLifeCycle < EventHandler
|
class EntityLifeCycle < EventHandler
|
||||||
def handles
|
def handles
|
||||||
[:create, :entity_move, :destroy]
|
[:create, :move, :destroy]
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle(subscriber, context, *args)
|
def handle(subscriber, context, *args)
|
||||||
|
|||||||
15
lib/event_handlers/entity_moved.rb
Normal file
15
lib/event_handlers/entity_moved.rb
Normal file
@@ -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
|
||||||
@@ -36,7 +36,6 @@ class IMICFPS
|
|||||||
@physics = @manifest.physics # Entity affected by gravity and what not
|
@physics = @manifest.physics # Entity affected by gravity and what not
|
||||||
@mass = 100 # kg
|
@mass = 100 # kg
|
||||||
|
|
||||||
@delta_time = Gosu.milliseconds
|
|
||||||
@last_position = Vector.new(@position.x, @position.y, @position.z)
|
@last_position = Vector.new(@position.x, @position.y, @position.z)
|
||||||
|
|
||||||
load_scripts
|
load_scripts
|
||||||
@@ -110,11 +109,13 @@ class IMICFPS
|
|||||||
|
|
||||||
def update
|
def update
|
||||||
model.update
|
model.update
|
||||||
@delta_time = Gosu.milliseconds
|
|
||||||
|
|
||||||
unless at_same_position?
|
unless at_same_position?
|
||||||
|
Publisher.instance.publish(:entity_moved, self, self)
|
||||||
@bounding_box = normalize_bounding_box_with_offset if model
|
@bounding_box = normalize_bounding_box_with_offset if model
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@last_position = Vector.new(@position.x, @position.y, @position.z)
|
||||||
end
|
end
|
||||||
|
|
||||||
def debug_color=(color)
|
def debug_color=(color)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class IMICFPS
|
|||||||
unless load_model_from_cache
|
unless load_model_from_cache
|
||||||
case @type
|
case @type
|
||||||
when :obj
|
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
|
else
|
||||||
raise "Unsupported model type, supported models are: #{@supported_models.join(', ')}"
|
raise "Unsupported model type, supported models are: #{@supported_models.join(', ')}"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
class IMICFPS
|
class IMICFPS
|
||||||
class Game < GameState
|
class Game < GameState
|
||||||
|
|
||||||
attr_reader :collision_manager
|
attr_reader :collision_manager, :delta_time
|
||||||
def setup
|
def setup
|
||||||
@collision_manager = CollisionManager.new(game_state: self)
|
@collision_manager = CollisionManager.new(game_state: self)
|
||||||
@renderer = Renderer.new(game_state: self)
|
@renderer = Renderer.new(game_state: self)
|
||||||
@@ -84,10 +84,9 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
@last_frame_time = Gosu.milliseconds
|
|
||||||
update_text
|
update_text
|
||||||
|
|
||||||
@publisher.publish(:tick, Gosu.milliseconds - @delta_time)
|
@publisher.publish(:tick, Gosu.milliseconds - window.delta_time)
|
||||||
|
|
||||||
@collision_manager.update
|
@collision_manager.update
|
||||||
@entities.each(&:update)
|
@entities.each(&:update)
|
||||||
@@ -161,7 +160,6 @@ class IMICFPS
|
|||||||
|
|
||||||
window.close if window.button_down?(Gosu::KbEscape)
|
window.close if window.button_down?(Gosu::KbEscape)
|
||||||
window.number_of_vertices = 0
|
window.number_of_vertices = 0
|
||||||
@delta_time = Gosu.milliseconds
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_text
|
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}
|
#{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}
|
Field Of View: #{@camera.field_of_view}
|
||||||
Mouse Sesitivity: #{@camera.mouse_sensitivity}
|
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)}
|
Vertices: #{formatted_number(window.number_of_vertices)}
|
||||||
Faces: #{formatted_number(window.number_of_vertices/3)}
|
Faces: #{formatted_number(window.number_of_vertices/3)}
|
||||||
|
|||||||
@@ -25,11 +25,10 @@ class IMICFPS
|
|||||||
private def subscribable_events
|
private def subscribable_events
|
||||||
[
|
[
|
||||||
:tick,
|
:tick,
|
||||||
:create,
|
:create, :move, :destroy,
|
||||||
:destroy,
|
:entity_moved,
|
||||||
:button_down, :button_up,
|
:button_down, :button_up,
|
||||||
:mouse_move,
|
:mouse_move,
|
||||||
:entity_move,
|
|
||||||
:interact,
|
:interact,
|
||||||
:player_join, :player_leave, :player_die,
|
:player_join, :player_leave, :player_die,
|
||||||
:pickup_item, :use_item, :drop_item,
|
:pickup_item, :use_item, :drop_item,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ class IMICFPS
|
|||||||
attr_accessor :number_of_vertices, :needs_cursor
|
attr_accessor :number_of_vertices, :needs_cursor
|
||||||
attr_reader :camera
|
attr_reader :camera
|
||||||
|
|
||||||
attr_reader :console
|
attr_reader :console, :delta_time
|
||||||
def initialize(window_width = 1280, window_height = 800, fullscreen = false)
|
def initialize(window_width = 1280, window_height = 800, fullscreen = false)
|
||||||
fps_target = (ARGV.first.to_i != 0) ? ARGV.first.to_i : 60
|
fps_target = (ARGV.first.to_i != 0) ? ARGV.first.to_i : 60
|
||||||
if ARGV.join.include?("--native")
|
if ARGV.join.include?("--native")
|
||||||
@@ -22,6 +22,8 @@ class IMICFPS
|
|||||||
Commands::Command.setup
|
Commands::Command.setup
|
||||||
|
|
||||||
push_state(MainMenu)
|
push_state(MainMenu)
|
||||||
|
|
||||||
|
@delta_time = Gosu.milliseconds
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
@@ -34,6 +36,7 @@ class IMICFPS
|
|||||||
super
|
super
|
||||||
|
|
||||||
@console.update if @show_console
|
@console.update if @show_console
|
||||||
|
@delta_time = Gosu.milliseconds
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_down(id)
|
def button_down(id)
|
||||||
|
|||||||
Reference in New Issue
Block a user