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



74
75
76
77
78
79
80
81
# File 'lib/or2d/composite.rb', line 74

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

#each_layer(&block) ⇒ Object

Since:

  • 2023-04-26



42
43
44
# File 'lib/or2d/composite.rb', line 42

def each_layer(&block)
  @layers.each(&block)
end

#hideObject

Hide the Composite object.

Since:

  • 2023-04-26



60
61
62
63
64
# File 'lib/or2d/composite.rb', line 60

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



53
54
55
56
57
# File 'lib/or2d/composite.rb', line 53

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



68
69
70
71
# File 'lib/or2d/composite.rb', line 68

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

#toggle_boundaryObject

Since:

  • 2023-04-26



46
47
48
49
50
# File 'lib/or2d/composite.rb', line 46

def toggle_boundary
  @layers.each_key do |key|
    OR2D.game.entities[key].toggle_boundary
  end
end