Class: Reight::Map::SpriteArray

Inherits:
Array
  • Object
show all
Defined in:
lib/reight/map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(world: nil, sprites: [], &each_chunk) ⇒ SpriteArray

Returns a new instance of SpriteArray.



352
353
354
355
# File 'lib/reight/map.rb', line 352

def initialize(world: nil, sprites: [], &each_chunk)
  @world, @each_chunk = world, each_chunk
  super(sprites)
end

Instance Attribute Details

#worldObject (readonly)

Returns the value of attribute world.



357
358
359
# File 'lib/reight/map.rb', line 357

def world
  @world
end

Instance Method Details

#activate(x, y, w, h, &activated) ⇒ Object

Raises:

  • (ArgumentError)


359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/reight/map.rb', line 359

def activate(x, y, w, h, &activated)
  raise ArgumentError, "missing 'activated' block" if !@world && !activated

  bounds, old_bounds = [x, y, w, h], @bounds
  return if bounds == old_bounds

  chunks, old_chunks = @each_chunk.call(x, y, w, h).to_a, @chunks || []
  return if chunks == old_chunks

  activateds, deactivateds = [chunks - old_chunks, old_chunks - chunks]
    .map {|chunks| chunks.map(&:sprites).flatten.compact}
  if activated
    activated.call activateds, deactivateds
  elsif @world
      activateds.each {@world   .add_sprite _1}
    deactivateds.each {@world.remove_sprite _1}
  end

  @bounds, @chunks = bounds, chunks
  clear.concat @chunks.map(&:sprites).flatten.compact
end

#delete(sprite) ⇒ Object



381
382
383
384
# File 'lib/reight/map.rb', line 381

def delete(sprite)
  sprite.map_chunk&.delete_sprite__ sprite
  super
end

#drawSprite__(context) ⇒ Object



390
391
392
# File 'lib/reight/map.rb', line 390

def drawSprite__(context)
  (@chunks&.each || each).each {_1.drawSprite__ context}
end

#inspectObject



386
387
388
# File 'lib/reight/map.rb', line 386

def inspect()
  "#<#{self.class.name}:0x#{object_id}>"
end