Class: OR2D::Composite

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

Overview

A Composite object behaves like a collection of Entity objects.

Since:

  • 2023-04-26

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = "Composite_#{SecureRandom.uuid}") ⇒ Composite

Constructs a new Composite object.

Since:

  • 2023-04-26



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

def initialize(id = "Composite_#{SecureRandom.uuid}")
  @id = id
  @layers = {}
end

Instance Attribute Details

#idString (readonly)

Returns the identifier of the Composite object.

Returns:

  • (String)

    the identifier of the Composite object

Since:

  • 2023-04-26



11
12
13
# File 'lib/or2d/composite.rb', line 11

def id
  @id
end

#layersHash (readonly)

Returns the layers of the Composite object.

Returns:

  • (Hash)

    the layers of the Composite object

Since:

  • 2023-04-26



7
8
9
# File 'lib/or2d/composite.rb', line 7

def layers
  @layers
end

Instance Method Details

#add_layer(type, options, entity: nil) ⇒ Object

Add a layer to the Composite object.

Parameters:

  • type (Symbol)

    the type of layer to add

  • options (Hash)

    the options used to construct the layer

  • entity (OR2D::Entity) (defaults to: nil)

    an optional Entity object to add if the layer type is :entity

Since:

  • 2023-04-26



23
24
25
26
27
28
29
30
31
32
# File 'lib/or2d/composite.rb', line 23

def add_layer(type, options, entity: nil)
  entity = if type == :entity
             entity
           else
             OR2D::Entity.new(type, options)
           end

  @layers[entity.id] = { show: options[:show].nil? ? true : options[:show] }
  entity.id
end

#destroyObject

Destroys the Composite object and all of its Entity objects.

Since:

  • 2023-04-26



64
65
66
67
68
69
70
71
# File 'lib/or2d/composite.rb', line 64

def destroy
  @layers.each_key do |id|
    next if OR2D.game.entities[id].nil?

    OR2D.game.entities[id].destroy
    OR2D.game.remove_entity(id)
  end
end

#hideObject

Hide the Composite object.

Since:

  • 2023-04-26



50
51
52
53
54
# File 'lib/or2d/composite.rb', line 50

def hide
  @layers.each_key do |id|
    OR2D.game.entities[id].hide
  end
end

#remove_layer(id) ⇒ Object

Remove a layer from the Composite object.

Parameters:

  • id (String)

    the id of the Entity object to remove

Since:

  • 2023-04-26



36
37
38
39
40
# File 'lib/or2d/composite.rb', line 36

def remove_layer(id)
  @layers.delete(id)
  OR2D.game.entities[id]&.destroy
  OR2D.game.remove_entity(id)
end

#showObject

Show the Composite object.

Since:

  • 2023-04-26



43
44
45
46
47
# File 'lib/or2d/composite.rb', line 43

def show
  @layers.each do |id, properties|
    OR2D.game.entities[id].show if properties[:show]
  end
end

#toggle(id) ⇒ Object

Toggle the visibility of a specific layer.

Parameters:

  • id (String)

    the id of the layer to toggle

Since:

  • 2023-04-26



58
59
60
61
# File 'lib/or2d/composite.rb', line 58

def toggle(id)
  @layers[id][:show] = !@layers[id][:show]
  @layers[id][:show] ? OR2D.game.entities[id].show : OR2D.game.entities[id].hide
end