Class: Bomp::World

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

Constant Summary collapse

DEFAULT_FILTER =
lambda { |a, b| :slide }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, **opts) ⇒ World

Create world for collisions processing

Parameters:

  • width (Integer)

    The first number

  • height (Integer)

    The second number



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.slide(item, other, goal) },
                'push': lambda { |item, other, goal| CollisionAABB.push(item, other, goal) },
                'nothing': lambda { |item, other, goal| item } }
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



235
236
237
# File 'lib/bomp.rb', line 235

def response
  @response
end

#system_collisionObject (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

Parameters:

  • index (Rect)

    Index



265
266
267
# File 'lib/bomp.rb', line 265

def [](index)
  @system_collision&.items[index]
end

#[]=(index, item) ⇒ Object

Parameters:

  • index (Integer)
  • item (Rect)


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

Parameters:

  • item (Rect)

    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

Parameters:

  • name (String)


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

Parameters:



294
295
296
# File 'lib/bomp.rb', line 294

def at(item)
  add item unless include? item
end

#check(item, &filter) ⇒ [Rect, Array]

Check

Parameters:

  • item (Rect)
  • filter (Proc)

Returns:



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

Parameters:

  • item (Rect)

    Check if include item

Returns:

  • (Boolean)


288
289
290
# File 'lib/bomp.rb', line 288

def include?(item)
  items.include? item
end

#itemsArray?

Returns:

  • (Array, nil)


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

Parameters:

Returns:



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

Parameters:



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

Parameters:

  • rect (Rect)
  • filter (Proc)

Raises:

  • (NotImplementedError)


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

Parameters:

Raises:

  • (NotImplementedError)


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

Parameters:

  • item (Rect)

    Remove item from world



259
260
261
# File 'lib/bomp.rb', line 259

def remove(item)
  @system_collision&.remove item
end

#to_aArray

Cast to array

Returns:

  • (Array)


282
283
284
# File 'lib/bomp.rb', line 282

def to_a
  @system_collision&.sort || []
end