Class: Reight::Map

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/reight/map.rb

Defined Under Namespace

Classes: Chunk, SpriteArray

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chip_size: 8, chunk_size: 128) ⇒ Map

Returns a new instance of Map.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
# File 'lib/reight/map.rb', line 9

def initialize(chip_size: 8, chunk_size: 128)
  raise ArgumentError, "Invalid chip_size: #{chip_size}" if
    chip_size.to_i != chip_size
  raise ArgumentError, "Invalid chunk_size: #{chunk_size}" if
    chunk_size.to_i != chunk_size || chunk_size % chip_size != 0

  @chip_size, @chunk_size = [chip_size, chunk_size].map(&:to_i)
  @chunks                 = {}
end

Class Method Details

.restore(hash, source_chips) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/reight/map.rb', line 107

def self.restore(hash, source_chips)
  chip_size, chunk_size, chunks = hash.values_at :chip_size, :chunk_size, :chunks
  #hash => {chip_size:, chunk_size:, chunks:}
  new(chip_size: chip_size, chunk_size: chunk_size).tap do |obj|
    obj.instance_eval do
      @chunks = chunks.each.with_object({}) do |chunk_hash, result|
        x, y = chunk_hash.values_at :x, :y
        #chunk_hash => {x:, y:}
        result[[x, y]] = Chunk.restore chunk_hash, source_chips
      end
    end
  end
end

Instance Method Details

#[](x, y) ⇒ Object



83
84
85
# File 'lib/reight/map.rb', line 83

def [](x, y)
  chunk_at(x, y)&.[](x, y)
end

#activate(x, y, w, h, world = nil, &activated) ⇒ Object Also known as: sprites_at



19
20
21
22
23
24
# File 'lib/reight/map.rb', line 19

def activate(x, y, w, h, world = nil, &activated)
  @sprites   = nil if !activated && @sprites && @sprites.world != world
  @sprites ||= SpriteArray.new(world: world) {|*a, &b| each_chunk(*a, &b)}
  @sprites.activate(x, y, w, h, &activated)
  @sprites
end

#clear_spritesObject



36
37
38
39
# File 'lib/reight/map.rb', line 36

def clear_sprites()
  @chunks.each_value {_1&.clear_sprites}
  @sprites = nil
end

#cmp__(o) ⇒ Object



92
93
94
95
96
# File 'lib/reight/map.rb', line 92

def cmp__(o)
  a =                  [@chip_size, @chunk_size, @chunks]
  b = o.instance_eval {[@chip_size, @chunk_size, @chunks]}
  a <=> b
end

#drawSprite__(context) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/reight/map.rb', line 99

def drawSprite__(context)
  if @sprites
    @sprites.drawSprite__ context
  else
    @chunks.each_value {_1.drawSprite__ context}
  end
end

#each(&block) ⇒ Object



74
# File 'lib/reight/map.rb', line 74

def each(&block) = each_chip(&block)

#each_chip(x = nil, y = nil, w = nil, h = nil, clip_by_chunk: false, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/reight/map.rb', line 60

def each_chip(x = nil, y = nil, w = nil, h = nil, clip_by_chunk: false, &block)
  return enum_for :each_chip, x, y, w, h, clip_by_chunk: clip_by_chunk unless block
  enum =
    case [x, y, w, h]
    in [nil,     nil,     nil,     nil]     then @chunks.values.each
    in [Numeric, Numeric, Numeric, Numeric] then each_chunk x, y, w, h
    else raise ArgumentError, "Invalid bounds"
    end
  x = y = w = h = nil if clip_by_chunk
  enum.each do |chunk|
    chunk.each_chip(x, y, w, h) {|chip, _, _| block.call chip}
  end
end

#inspectObject



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

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

#put(x, y, chip) ⇒ Object



41
42
43
44
45
46
# File 'lib/reight/map.rb', line 41

def put(x, y, chip)
  return unless chip
  each_chunk x, y, chip.w, chip.h, create: true do |chunk|
    chunk.put x, y, chip
  end
end

#remove(x, y) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/reight/map.rb', line 48

def remove(x, y)
  chip           = self[x, y] or return
  cx, cy, cw, ch = chip.then {[_1.pos.x, _1.pos.y, _1.w, _1.h]}
  each_chunk cx, cy, cw, ch, create: false do |chunk|
    each_chip_pos(cx, cy, cw, ch) {|xx, yy| chunk.remove xx, yy}
  end
end

#remove_chip(chip) ⇒ Object



56
57
58
# File 'lib/reight/map.rb', line 56

def remove_chip(chip)
  remove chip.pos.x, chip.pos.y
end

#spritesObject



30
31
32
# File 'lib/reight/map.rb', line 30

def sprites()
  @sprites ||= SpriteArray.new(sprites: to_sprites)
end

#to_hashObject



76
77
78
79
80
81
# File 'lib/reight/map.rb', line 76

def to_hash()
  {
    chip_size: @chip_size, chunk_size: @chunk_size,
    chunks: @chunks.values.map(&:to_hash)
  }
end

#to_spritesObject



26
27
28
# File 'lib/reight/map.rb', line 26

def to_sprites()
  map(&:to_sprite)
end