Class: Reight::Map::Chunk

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, w, h, chip_size: 8) ⇒ Chunk

Returns a new instance of Chunk.

Raises:

  • (ArgumentError)


182
183
184
185
186
187
188
189
# File 'lib/reight/map.rb', line 182

def initialize(x, y, w, h, chip_size: 8)
  raise ArgumentError, "Invalid chip_size: #{chip_size}" if chip_size.to_i != chip_size
  raise ArgumentError, "Invalid w: #{w}"                 if w % chip_size != 0
  raise ArgumentError, "Invalid h: #{h}"                 if h % chip_size != 0

  @x, @y, @w, @h, @chip_size = [x, y, w, h, chip_size].map &:to_i
  @chips, @ncolumn           = [], @w / @chip_size
end

Instance Attribute Details

#hObject (readonly)

Returns the value of attribute h.



191
192
193
# File 'lib/reight/map.rb', line 191

def h
  @h
end

#wObject (readonly)

Returns the value of attribute w.



191
192
193
# File 'lib/reight/map.rb', line 191

def w
  @w
end

#xObject (readonly)

Returns the value of attribute x.



191
192
193
# File 'lib/reight/map.rb', line 191

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



191
192
193
# File 'lib/reight/map.rb', line 191

def y
  @y
end

Class Method Details

.restore(hash, source_chips) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/reight/map.rb', line 306

def self.restore(hash, source_chips)
  x, y, w, h, chip_size, chip_ids = hash.values_at :x, :y, :w, :h, :chip_size, :chips
  #hash      => {x:, y:, w:, h:, chip_size: chip_size, chips: chip_ids}
  tmp_chips = {}
  get_chip  = -> id, x, y {
    tmp_chips[[id, x, y]] ||= source_chips[id].with(pos: create_vector(x, y))
  }
  new(x, y, w, h, chip_size: chip_size).tap do |obj|
    obj.instance_eval do
      @chips = chip_ids.map {|id, x, y| id ? get_chip.call(id, x, y) : nil}
    end
  end
end

Instance Method Details

#[](x, y) ⇒ Object



265
266
267
268
269
# File 'lib/reight/map.rb', line 265

def [](x, y)
  index = pos2index x, y
  return nil if index < 0 || (@w * @h) <= index
  @chips[index]
end

#clear_spritesObject



197
198
199
# File 'lib/reight/map.rb', line 197

def clear_sprites()
  @sprites = nil
end

#cmp__(o) ⇒ Object



276
277
278
279
280
# File 'lib/reight/map.rb', line 276

def cmp__(o)
  a =                  [@x, @y, @w, @h, @chip_size, @chips]
  b = o.instance_eval {[@x, @y, @w, @h, @chip_size, @chips]}
  a <=> b
end

#delete_sprite__(sprite) ⇒ Object



301
302
303
304
# File 'lib/reight/map.rb', line 301

def delete_sprite__(sprite)
  @sprites.delete sprite
  invalidate_cache__
end

#drawSprite__(context) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
# File 'lib/reight/map.rb', line 288

def drawSprite__(context)
  @cached ||= true.tap do
    @cache ||= create_graphics @w, @h
    @cache.begin_draw do |g|
      g.background 0, 0
      g.translate -@x, -@y
      sprites.each {_1.drawSprite__ g}
    end
  end
  context.image @cache, @x, @y
end

#each(&block) ⇒ Object



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

def each(&block) = each_chip {block.call _1}

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



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/reight/map.rb', line 226

def each_chip(x = nil, y = nil, w = nil, h = nil, include_hidden: false, &block)
  return enum_for(:each_chip, x, y, w, h, include_hidden: include_hidden) unless block
  x, w = x + w, -w if x && w && w < 0
  y, h = y + h, -h if y && h && h < 0
  @chips.each.with_index do |chip, index|
    next unless chip
    xx, yy = index2pos index
    pos    = chip.pos
    next if x && !intersect?(x, y, w, h, pos.x, pos.y, chip.w, chip.h)
    block.call chip, xx, yy if include_hidden || (xx == pos.x && yy == pos.y)
  end
end

#each_chip_pos(x, y, w, h, &block) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/reight/map.rb', line 239

def each_chip_pos(x, y, w, h, &block)
  return enum_for :each_chip_pos, x, y, w, h unless block
  x, w   = x + w, -w if w < 0
  y, h   = y + h, -h if h < 0
  x1, y1 = align_chip_pos x, y
  x2, y2 = align_chip_pos x + w + @chip_size - 1, y + h + @chip_size - 1
  x1, x2 = [x1, x2].map {_1.clamp @x, @x + @w}
  y1, y2 = [y1, y2].map {_1.clamp @y, @y + @h}
  (y1...y2).step @chip_size do |yy|
    (x1...x2).step @chip_size do |xx|
      block.call xx, yy
    end
  end
end

#frameObject



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

def frame = [@x, @y, @w, @h]

#inspectObject



271
272
273
# File 'lib/reight/map.rb', line 271

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

#invalidate_cache__Object



283
284
285
# File 'lib/reight/map.rb', line 283

def invalidate_cache__()
  @cached = false
end

#put(x, y, chip) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/reight/map.rb', line 201

def put(x, y, chip)
  x, y = align_chip_pos x, y
  raise "Invalid chip size" if
    chip.w % @chip_size != 0 || chip.h % @chip_size != 0
  raise "Conflicts with other chips" if
    each_chip_pos(x, y, chip.w, chip.h).any? {|xx, yy| self[xx, yy]}

  new_chip = nil
  get_chip = -> {new_chip ||= chip.with pos: create_vector(x, y)}
  each_chip_pos x, y, chip.w, chip.h do |xx, yy|
    @chips[pos2index xx, yy] = get_chip.call
  end
  invalidate_cache__
end

#remove(x, y) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/reight/map.rb', line 216

def remove(x, y)
  chip = self[x, y] or return
  each_chip_pos chip.pos.x, chip.pos.y, chip.w, chip.h do |xx, yy|
    index         = pos2index xx, yy
    @chips[index] = nil if @chips[index]&.id == chip.id
  end
  delete_last_nils
  invalidate_cache__
end

#spritesObject



193
194
195
# File 'lib/reight/map.rb', line 193

def sprites()
  @sprites ||= map(&:sprite).each {_1.map_chunk = self}
end

#to_hashObject



258
259
260
261
262
263
# File 'lib/reight/map.rb', line 258

def to_hash()
  {
    x: @x, y: @y, w: @w, h: @h, chip_size: @chip_size,
    chips: @chips.map {|chip| chip ? [chip.id, chip.pos.x, chip.pos.y] : nil}
  }
end