Class: Bomp::World
- Inherits:
-
Object
- Object
- Bomp::World
- Defined in:
- lib/bomp.rb
Constant Summary collapse
- DEFAULT_FILTER =
lambda { |a, b| :slide }
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#system_collision ⇒ Object
readonly
Returns the value of attribute system_collision.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Select item from world.
- #[]=(index, item) ⇒ Object
-
#add(item) ⇒ Object
Add item to world.
-
#add_response(name, &block) ⇒ Object
Add response.
-
#at(item) ⇒ Object
At item.
-
#check(item, &filter) ⇒ [Rect, Array]
Check.
-
#include?(item) ⇒ Boolean
Check if item include in world.
-
#initialize(width, height, **opts) ⇒ World
constructor
Create world for collisions processing.
- #items ⇒ Array?
-
#move(item, goal, &filter) ⇒ [Rect, Array]
Move item in the world.
-
#query_point(point, &filter) ⇒ Object
Query point.
-
#query_rect(rect, &filter) ⇒ Object
Query rect.
-
#query_segment(p0, p1, &filter) ⇒ Object
Query segment.
-
#remove(item) ⇒ Object
Remove item from world.
-
#to_a ⇒ Array
Cast to array.
Constructor Details
#initialize(width, height, **opts) ⇒ World
Create world for collisions processing
240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bomp.rb', line 240 def initialize(width, height, **opts) @opts = opts @system_collision = @opts[:system] || QuadTree.new(width, height, **opts) @response = { 'bounce': lambda { |item, other, goal| CollisionAABB.bounce(item, other, goal) }, 'cross': lambda { |item, other, goal| CollisionAABB.cross(item, other, goal) }, 'touch': lambda { |item, other, goal| CollisionAABB.touch(item, other, goal) }, 'slide': lambda { |item, other, goal| CollisionAABB.(item, other, goal) }, 'push': lambda { |item, other, goal| CollisionAABB.push(item, other, goal) }, 'nothing': lambda { |item, other, goal| item } } end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
235 236 237 |
# File 'lib/bomp.rb', line 235 def response @response end |
#system_collision ⇒ Object (readonly)
Returns the value of attribute system_collision.
235 236 237 |
# File 'lib/bomp.rb', line 235 def system_collision @system_collision end |
Instance Method Details
#[](index) ⇒ Object
Select item from world
265 266 267 |
# File 'lib/bomp.rb', line 265 def [](index) @system_collision&.items[index] end |
#[]=(index, item) ⇒ Object
271 272 273 |
# File 'lib/bomp.rb', line 271 def []=(index, item) @system_collision&.items[index] = item end |
#add(item) ⇒ Object
Add item to world
253 254 255 |
# File 'lib/bomp.rb', line 253 def add(item) @system_collision&.add item end |
#add_response(name, &block) ⇒ Object
Add response
323 324 325 |
# File 'lib/bomp.rb', line 323 def add_response(name, &block) @response[name.to_sym] = block end |
#at(item) ⇒ Object
At item
294 295 296 |
# File 'lib/bomp.rb', line 294 def at(item) add item unless include? item end |
#check(item, &filter) ⇒ [Rect, Array]
Check
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/bomp.rb', line 357 def check(item, &filter) filter = DEFAULT_FILTER if filter.nil? item = self[item] if item.is_a? Integer cols = [] all_sort_items = @system_collision&.sort all_sort_items.each do |others| next unless others.include? item others -= [item] cols += check_and_resolve item, [0, 0], others, filter end [item, cols] end |
#include?(item) ⇒ Boolean
Check if item include in world
288 289 290 |
# File 'lib/bomp.rb', line 288 def include?(item) items.include? item end |
#items ⇒ Array?
276 277 278 |
# File 'lib/bomp.rb', line 276 def items @system_collision&.items end |
#move(item, goal, &filter) ⇒ [Rect, Array]
Move item in the world
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/bomp.rb', line 332 def move(item, goal, &filter) filter = DEFAULT_FILTER if filter.nil? item = self[item] if item.is_a? Integer cols = [] all_sort_items = @system_collision&.sort [Vector2[goal[0], 0], Vector2[0, goal[1]]].each do |g| item.position += g all_sort_items.each do |others| next unless others.include? item others -= [item] cols += check_and_resolve item, g, others, filter end end [item, cols] end |
#query_point(point, &filter) ⇒ Object
Query point
301 302 303 304 |
# File 'lib/bomp.rb', line 301 def query_point(point, &filter) _, cols = check Rect[point[0], point[1], 1, 1], &filter cols end |
#query_rect(rect, &filter) ⇒ Object
Query rect
309 310 311 |
# File 'lib/bomp.rb', line 309 def query_rect(rect, &filter) raise NotImplementedError.new end |
#query_segment(p0, p1, &filter) ⇒ Object
Query segment
317 318 319 |
# File 'lib/bomp.rb', line 317 def query_segment(p0, p1, &filter) raise NotImplementedError.new end |
#remove(item) ⇒ Object
Remove item from world
259 260 261 |
# File 'lib/bomp.rb', line 259 def remove(item) @system_collision&.remove item end |
#to_a ⇒ Array
Cast to array
282 283 284 |
# File 'lib/bomp.rb', line 282 def to_a @system_collision&.sort || [] end |