Class: Bomp::QuadTree::QuadNode

Inherits:
Rect
  • Object
show all
Defined in:
lib/bomp.rb

Instance Attribute Summary collapse

Attributes inherited from Rect

#position, #size

Instance Method Summary collapse

Methods inherited from Rect

[], #bottom, #bottom=, #clone, #height, #height=, #left, #left=, #right, #right=, #to_s, #top, #top=, #width, #width=, #x, #y

Constructor Details

#initialize(x, y, w, h, args = {}) ⇒ QuadNode

Returns a new instance of QuadNode.



76
77
78
79
80
81
82
83
84
85
# File 'lib/bomp.rb', line 76

def initialize(x, y, w, h, args = {})
  super(Vector2[x, y], Vector2[w, h])
  limit = args[:limit_w] || 64, args[:limit_h] || 64
  @limit_w = (limit[0]).to_f
  @limit_h = (limit[1]).to_f
  @limit = (args[:limit]) || 16

  @items = []
  @children = []
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



74
75
76
# File 'lib/bomp.rb', line 74

def items
  @items
end

Instance Method Details

#clearObject



104
105
106
107
# File 'lib/bomp.rb', line 104

def clear
  @children.each { |child| child&.clear }
  @items.clear
end

#each(&block) ⇒ Object



122
123
124
125
# File 'lib/bomp.rb', line 122

def each(&block)
  @children.each { |c| c&.each(&block) }
  block.call @items.clone unless @items.empty?
end

#each_children(&block) ⇒ Object



127
128
129
130
# File 'lib/bomp.rb', line 127

def each_children(&block)
  @children.each { |c| c&.each_children(&block) }
  block.call self unless @items.empty?
end

#each_children_by(item, &block) ⇒ Object



132
133
134
135
# File 'lib/bomp.rb', line 132

def each_children_by(item, &block)
  @children.each { |c| c&.each_children_by(item, &block) }
  block.call self if CollisionAABB.is_overlaps?(self, item) and not @items.empty?
end

#empty?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/bomp.rb', line 87

def empty?
  @items.empty?
end

#group_by(item) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/bomp.rb', line 145

def group_by(item)
  children = []

  self.each_children_by(item) { |c| children.push c.items }

  children
end

#insert(item) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/bomp.rb', line 95

def insert(item)
  return unless CollisionAABB.is_overlaps? self, item

  subdivided unless subdivided?
  @children.each { |child| child&.insert item }

  @items << item unless subdivided? && @items.size <= @limit
end

#limit_sizeObject



114
115
116
# File 'lib/bomp.rb', line 114

def limit_size
  [@limit_w, @limit_h]
end

#limit_size=(limit) ⇒ Object



118
119
120
# File 'lib/bomp.rb', line 118

def limit_size=(limit)
  self.each { |c| c.limit_size = limit }
end

#releaseObject



109
110
111
112
# File 'lib/bomp.rb', line 109

def release
  @children.each { |child| child&.release }
  @children.clear
end

#subdivided?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/bomp.rb', line 91

def subdivided?
  !@children.empty?
end

#to_aObject



137
138
139
140
141
142
143
# File 'lib/bomp.rb', line 137

def to_a
  items = []

  self.each { |item| items.push item }

  items
end