Class: Reight::Map

Inherits:
Object
  • Object
show all
Includes:
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)


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

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



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

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



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

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

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



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

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



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

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

#cmp__(o) ⇒ Object



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

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

#drawSprite__(context) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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

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



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

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



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

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

#put(x, y, chip) ⇒ Object



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

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



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

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



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

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

#spritesObject



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

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

#to_hashObject



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

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

#to_spritesObject



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

def to_sprites()
  map(&:to_sprite)
end