2018-11-03 16:57:20 +01:00
|
|
|
|
|
|
|
require "json"
|
|
|
|
|
|
|
|
class Naka::Map
|
|
|
|
@save_directory : String
|
2018-11-04 00:49:40 +01:00
|
|
|
getter default_reference : ObjectReference
|
2018-11-03 16:57:20 +01:00
|
|
|
getter generator
|
2018-11-04 00:49:40 +01:00
|
|
|
|
|
|
|
def initialize(@default_reference, @save_directory, &block : Proc(Tile, Int32, Int32, Tile?))
|
2018-11-03 16:57:20 +01:00
|
|
|
Dir.mkdir_p @save_directory
|
|
|
|
|
|
|
|
@generator = block
|
|
|
|
end
|
|
|
|
|
2018-11-04 00:49:40 +01:00
|
|
|
class ObjectReference
|
|
|
|
getter name : String
|
|
|
|
getter walkable : Bool
|
|
|
|
getter tiles : Array(String)
|
|
|
|
getter drawer : Proc(Map, Int32, Int32, Int32, Tile, Object, Int32, Int32, Float64, Nil)?
|
|
|
|
|
|
|
|
def initialize(@name, @walkable = false, @tiles = [] of String, @drawer = nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-03 16:57:20 +01:00
|
|
|
class Object
|
2018-11-04 00:49:40 +01:00
|
|
|
getter reference : ObjectReference
|
|
|
|
def initialize(@reference)
|
2018-11-03 16:57:20 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_json(builder)
|
|
|
|
builder.object do
|
2018-11-04 00:49:40 +01:00
|
|
|
builder.field "type", @reference.name
|
2018-11-03 16:57:20 +01:00
|
|
|
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
|
|
|
|
|
2018-11-04 00:49:40 +01:00
|
|
|
Tile.new(Object.new map.default_reference).tap do |tile|
|
2018-11-03 16:57:20 +01:00
|
|
|
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)
|
2021-04-19 22:10:53 +02:00
|
|
|
x_value = x / CHUNK_SIZE
|
|
|
|
y_value = y / CHUNK_SIZE
|
|
|
|
get_chunk(x_value.to_i, y_value.to_i).get(x % CHUNK_SIZE, y % CHUNK_SIZE)
|
2018-11-03 16:57:20 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|