naka-chan-dayo/src/naka/map.cr

97 lines
1.5 KiB
Crystal

require "json"
class Naka::Map
@save_directory : String
getter generator
def initialize(@save_directory, &block : Proc(Tile, Int32, Int32, Tile?))
Dir.mkdir_p @save_directory
@generator = block
end
class Object
getter type : String
def initialize(@type)
end
def to_json(builder)
builder.object do
builder.field "type", @type
end
end
end
class Tile < Array(Object)
def initialize(element)
initialize
self << element
end
end
CHUNK_SIZE = 32
class Chunk
property tiles : Array(Array(Tile))
getter x : Int32
getter y : Int32
def initialize(@x, @y, map)
p "Creating Map::Chunk"
@tiles = Array.new(CHUNK_SIZE) do |x|
x += @x * CHUNK_SIZE
Array.new(CHUNK_SIZE) do |y|
y += @y * CHUNK_SIZE
Tile.new(Object.new "stone").tap do |tile|
map.generator.call tile, x, y
end
end
end
end
def to_json
{
:x => @x,
:y => @y,
:tiles => @tiles
}.to_json
end
def get(x, y)
@tiles[x][y]
end
end
@chunks = Hash(Int32, Hash(Int32, Chunk)).new
def get_chunk(x, y)
chunks_list = @chunks[x]?
if chunks_list.nil?
chunks_list = Hash(Int32, Chunk).new
@chunks[x] = chunks_list
end
chunk = chunks_list[y]?
if chunk.nil?
chunk = Chunk.new x, y, self
chunks_list[y] = chunk
save chunk
end
chunk
end
def save(chunk : Chunk)
File.write "#{@save_directory}/chunk-#{chunk.x}-#{chunk.y}.json", chunk.to_json
end
def get(x, y)
get_chunk(x / CHUNK_SIZE, y / CHUNK_SIZE).get(x % CHUNK_SIZE, y % CHUNK_SIZE)
end
end