Class: OR2D::Instance
- Inherits:
-
Object
- Object
- OR2D::Instance
- Includes:
- Singleton
- Defined in:
- lib/or2d/instance.rb
Overview
The Instance object represents a single running instance of OR2D. It maintains a list of all entities in the current instance, and a list of all scenes in the current instance. It also maintains a reference to the current scene. There should only be one instance of OR2D::Instance at any given time.
Instance Attribute Summary collapse
-
#entities ⇒ Hash
readonly
A hash of all entities in the game instance.
-
#modifiers ⇒ Array
readonly
The currently held modifier keys.
-
#mouse ⇒ OR2D::Mouse
readonly
The mouse state.
-
#scene ⇒ OR2D::Scene
readonly
The current scene.
-
#window ⇒ Ruby2D::Window
readonly
The game window.
Instance Method Summary collapse
-
#add_entity(entity) ⇒ Object
Adds an entity to the game instance.
-
#add_scene(scene) ⇒ Object
Adds a scene to the game instance.
-
#animate(entity_id, &_) ⇒ Object
Schedule an animation for an entity.
-
#initialize ⇒ Instance
constructor
Constructs a new OR2D instance.
-
#key_frame?(frame = 60) ⇒ Boolean
Is the current frame a key frame?.
-
#quit ⇒ Object
Close the game instance.
-
#remove_entity(entity_id) ⇒ OR2D::Entity
Removes an entity from the game instance.
-
#run ⇒ Object
Run the game instance.
Constructor Details
#initialize ⇒ Instance
Constructs a new OR2D instance.
29 30 31 32 |
# File 'lib/or2d/instance.rb', line 29 def initialize super() setup end |
Instance Attribute Details
#entities ⇒ Hash (readonly)
Returns a hash of all entities in the game instance.
10 11 12 |
# File 'lib/or2d/instance.rb', line 10 def entities @entities end |
#modifiers ⇒ Array (readonly)
Returns the currently held modifier keys.
22 23 24 |
# File 'lib/or2d/instance.rb', line 22 def modifiers @modifiers end |
#mouse ⇒ OR2D::Mouse (readonly)
Returns the mouse state.
26 27 28 |
# File 'lib/or2d/instance.rb', line 26 def mouse @mouse end |
#scene ⇒ OR2D::Scene (readonly)
Returns the current scene.
14 15 16 |
# File 'lib/or2d/instance.rb', line 14 def scene @scene end |
#window ⇒ Ruby2D::Window (readonly)
Returns the game window.
18 19 20 |
# File 'lib/or2d/instance.rb', line 18 def window @window end |
Instance Method Details
#add_entity(entity) ⇒ Object
Adds an entity to the game instance.
68 69 70 |
# File 'lib/or2d/instance.rb', line 68 def add_entity(entity) @entities[entity.id] = entity end |
#add_scene(scene) ⇒ Object
Adds a scene to the game instance.
62 63 64 |
# File 'lib/or2d/instance.rb', line 62 def add_scene(scene) @scenes.add(scene) end |
#animate(entity_id, &_) ⇒ Object
Schedule an animation for an entity.
55 56 57 58 |
# File 'lib/or2d/instance.rb', line 55 def animate(entity_id, &_) @animations[entity_id] ||= Set.new @animations[entity_id].add(OR2D::Animation.new(entity_id, &_)) end |
#key_frame?(frame = 60) ⇒ Boolean
Is the current frame a key frame?
82 83 84 |
# File 'lib/or2d/instance.rb', line 82 def key_frame?(frame = 60) (@window.get(:frames) % frame).zero? end |
#quit ⇒ Object
Close the game instance.
87 88 89 |
# File 'lib/or2d/instance.rb', line 87 def quit @window.close end |
#remove_entity(entity_id) ⇒ OR2D::Entity
Removes an entity from the game instance.
75 76 77 |
# File 'lib/or2d/instance.rb', line 75 def remove_entity(entity_id) @entities.delete(entity_id) end |
#run ⇒ Object
Run the game instance.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/or2d/instance.rb', line 35 def run @window.render do @scene.render unless @scene.nil? || @scene.rendered? || @scene.finished? rescue StandardError => e puts "An error occurred while rendering the scene: #{e.}" puts e.backtrace if OR2D.debug? end @window.update do process_animations process_scene rescue StandardError => e puts "An error occurred while updating the scene: #{e.}" puts e.backtrace if OR2D.debug? end @window.show end |