Class: OR2D::Instance

Inherits:
Object
  • Object
show all
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.

Since:

  • 2023-04-26

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstance

Constructs a new OR2D instance.

Since:

  • 2023-04-26



29
30
31
32
# File 'lib/or2d/instance.rb', line 29

def initialize
  super()
  setup
end

Instance Attribute Details

#entitiesHash (readonly)

Returns a hash of all entities in the game instance.

Returns:

  • (Hash)

    a hash of all entities in the game instance

Since:

  • 2023-04-26



10
11
12
# File 'lib/or2d/instance.rb', line 10

def entities
  @entities
end

#modifiersArray (readonly)

Returns the currently held modifier keys.

Returns:

  • (Array)

    the currently held modifier keys

Since:

  • 2023-04-26



22
23
24
# File 'lib/or2d/instance.rb', line 22

def modifiers
  @modifiers
end

#mouseOR2D::Mouse (readonly)

Returns the mouse state.

Returns:

  • (OR2D::Mouse)

    the mouse state

Since:

  • 2023-04-26



26
27
28
# File 'lib/or2d/instance.rb', line 26

def mouse
  @mouse
end

#sceneOR2D::Scene (readonly)

Returns the current scene.

Returns:

Since:

  • 2023-04-26



14
15
16
# File 'lib/or2d/instance.rb', line 14

def scene
  @scene
end

#windowRuby2D::Window (readonly)

Returns the game window.

Returns:

Since:

  • 2023-04-26



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.

Parameters:

Since:

  • 2023-04-26



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.

Parameters:

Since:

  • 2023-04-26



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.

Since:

  • 2023-04-26



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?

Parameters:

  • frame (Integer) (defaults to: 60)

    the key frame interval

Returns:

  • (Boolean)

    whether or not the current frame is a key frame

Since:

  • 2023-04-26



82
83
84
# File 'lib/or2d/instance.rb', line 82

def key_frame?(frame = 60)
  (@window.get(:frames) % frame).zero?
end

#quitObject

Close the game instance.

Since:

  • 2023-04-26



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.

Parameters:

  • entity_id (String)

    the ID of the entity to remove

Returns:

Since:

  • 2023-04-26



75
76
77
# File 'lib/or2d/instance.rb', line 75

def remove_entity(entity_id)
  @entities.delete(entity_id)
end

#runObject

Run the game instance.

Since:

  • 2023-04-26



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.message}"
    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.message}"
    puts e.backtrace if OR2D.debug?
  end

  @window.show
end

#screen_heightInteger

The height of the game window.

Returns:

  • (Integer)

    the height of the game window

Since:

  • 2023-04-26



99
100
101
# File 'lib/or2d/instance.rb', line 99

def screen_height
  @window.get(:height)
end

#screen_widthInteger

The width of the game window.

Returns:

  • (Integer)

    the width of the game window

Since:

  • 2023-04-26



93
94
95
# File 'lib/or2d/instance.rb', line 93

def screen_width
  @window.get(:width)
end