| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- -- ================
- -- Private helpers
- -- ================
- local setmetatable = setmetatable
- -- Internal class constructor
- local class = function(...)
- local klass = {}
- klass.__index = klass
- klass.__call = function(_,...) return klass:new(...) end
- function klass:new(...)
- local instance = setmetatable({}, klass)
- klass.__init(instance, ...)
- return instance
- end
- return setmetatable(klass,{__call = klass.__call})
- end
- local Map = class()
- Map.__tostring = function(m) return ('Map %dx%d'):format(m.width, m.height) end
- function Map:__init(w, h)
- self.width, self.height = w, h
- self.tiles = {}
- for y=0,h-1 do
- self.tiles[y] = {}
- for x=0,w-1 do
- self.tiles[y][x] = 1
- end
- end
- end
- function Map:print()
- for y=0,self.height-1 do
- line = ''
- for x=0,self.width-1 do
- if self.tiles[y][x] == 1 then
- line = line..'#'
- elseif self.tiles[y][x] == 2 then
- line = line..'.'
- else
- line = line..' '
- end
- end
- print(line)
- end
- end
- function Map:carve(room)
- local rx, ry = math.floor(room.x), math.floor(room.y)
- if rx <= 0 or ry <= 0 or rx >= self.width-room.width-1 or ry >= self.height-room.height-1 then return end
- self.tiles[ry][rx] = 2
- for y=ry, ry+room.height-1 do
- for x=rx, rx+room.width-1 do
- self.tiles[y][x] = 0
- end
- end
- end
- function Map:toArray()
- local a = {}
- for y=0,self.height do
- for x=0,self.width do
- table.insert(a, self.tiles[y][x])
- end
- end
- return a
- end
- return Map
|