Class: Cukunity::Unity::GameObject

Inherits:
JSONContainer show all
Defined in:
lib/cukunity/unity/gameobject.rb

Instance Attribute Summary collapse

Attributes inherited from JSONContainer

#json

Instance Method Summary collapse

Methods inherited from JSONContainer

#has_key?, #keys, #method_missing, #to_s, #value_of

Methods included from Cukunity::Utils

#check_timeout, #merge_options, #restrict_options, #to_options, #wait_connectivity

Constructor Details

#initialize(json, scene, parent = nil) ⇒ GameObject

Returns a new instance of GameObject.



7
8
9
10
11
# File 'lib/cukunity/unity/gameobject.rb', line 7

def initialize(json, scene, parent = nil)
  super(json)
  @scene = scene
  @parent = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cukunity::Unity::JSONContainer

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/cukunity/unity/gameobject.rb', line 5

def parent
  @parent
end

#sceneObject (readonly)

Returns the value of attribute scene.



4
5
6
# File 'lib/cukunity/unity/gameobject.rb', line 4

def scene
  @scene
end

Instance Method Details

#childrenObject



13
14
15
16
17
# File 'lib/cukunity/unity/gameobject.rb', line 13

def children
  @children ||= (value_of('children') || []).map do |json|
    Unity::GameObject.new(json, @scene, self)
  end
end

#component(*args) ⇒ Object



19
20
21
# File 'lib/cukunity/unity/gameobject.rb', line 19

def component(*args)
  components(*args).first
end

#components(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cukunity/unity/gameobject.rb', line 27

def components(options = {})
  # memoize all components
  if @components.nil?
    @components = (value_of('components') || []).map do |json|
      Unity::Component.new(json, self)
    end
    @components += children.inject([]) do |components, child|
      components + child.components(:recursive => true)
    end
  end
  options = merge_options(options, { :recursive => false })
  # select desired components
  @components.select do |component|
    acceptable_type = (options[:type].nil? or options[:type] == component.type)
    acceptable_type and (options[:recursive] or component.gameobject == self)
  end
end

#has_component?(*args) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/cukunity/unity/gameobject.rb', line 23

def has_component?(*args)
  !!component(*args)
end

#hints(options = {}) ⇒ Object



45
46
47
48
49
50
# File 'lib/cukunity/unity/gameobject.rb', line 45

def hints(options = {})
  options = merge_options(options, { :recursive => false })
  components(restrict_options(options, :recursive)).inject([]) do |hints, component|
    hints + component.hints(options)
  end
end