Class: OR2D::Scene

Inherits:
Object
  • Object
show all
Defined in:
lib/or2d/scene.rb

Overview

A Scene object represents a single scene in an OR2D instance. Each Scene maintains two internal lists to keep track of the visibility state of Entities in the scene. It provides 3 functions to control how the Scene is updated, rendered, and whether or not it should be redrawn. It is recommended to use inheritance to create new scenes.

Examples:

Inheritance

class MyScene < OR2D::Scene
  def initialize
    super('my_scene')
  end

  private

  def setup_render
   super do
     # Render code here
    end
  end

  def setup_update
    super do
      # Update code here
    end
  end

  def setup_redraw
    super do
      # Redraw code here
    end
  end
end

Since:

  • 2023-04-12

Direct Known Subclasses

OR2D::Scenes::PlaceholderScene

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, procs = {}) ⇒ Scene

Constructs a new Scene object.

Parameters:

  • id (String)

    the ID of the scene

  • procs (Hash) (defaults to: {})

    a hash of procs to be used for the scene

Since:

  • 2023-04-12



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/or2d/scene.rb', line 43

def initialize(id, procs = {})
  @id = id
  @descriptors = {}
  @workers = {}
  @visible = Set.new
  @hidden = Set.new
  @rendered = false
  @finish = false

  procs[:redraw?].nil? ? setup_redraw : setup_redraw(&procs[:redraw?])
  procs[:update].nil? ? setup_update : setup_update(&procs[:update])
  procs[:render].nil? ? setup_render : setup_render(&procs[:render])

  self
end

Instance Attribute Details

#idString (readonly)

Returns the ID of the scene.

Returns:

  • (String)

    the ID of the scene

Since:

  • 2023-04-12



38
39
40
# File 'lib/or2d/scene.rb', line 38

def id
  @id
end

Instance Method Details

#finishObject

Finishes the scene.

Since:

  • 2023-04-12



76
77
78
79
# File 'lib/or2d/scene.rb', line 76

def finish
  @descriptors.each_value { |descriptor| OR2D.game.window.off(descriptor) }
  @finish = true
end

#finished?Boolean

Has the scene finished?

Returns:

  • (Boolean)

    true if the scene has finished, false otherwise

Since:

  • 2023-04-12



83
84
85
# File 'lib/or2d/scene.rb', line 83

def finished?
  @finish
end

#redraw?Boolean

Should the scene be redrawn?

Returns:

  • (Boolean)

    true if the scene should be redrawn, false otherwise

Since:

  • 2023-04-12



71
72
73
# File 'lib/or2d/scene.rb', line 71

def redraw?
  @workers[:redraw?].resume
end

#renderObject

Render the scene.

Since:

  • 2023-04-12



65
66
67
# File 'lib/or2d/scene.rb', line 65

def render
  @workers[:render].resume
end

#rendered?Boolean

Has the scene been rendered?

Returns:

  • (Boolean)

Since:

  • 2023-04-12



88
89
90
# File 'lib/or2d/scene.rb', line 88

def rendered?
  @rendered
end

#updateObject

Updates the scene.

Since:

  • 2023-04-12



60
61
62
# File 'lib/or2d/scene.rb', line 60

def update
  @workers[:update].resume(self)
end