honeylisp/editor/mapedit.fnl
2020-10-18 20:13:26 -04:00

67 lines
2 KiB
Fennel

(local GraphicsEditView (require :editor.gfxedit))
(local style (require :core.style))
(local util (require :lib.util))
(local {: mouse-inside : activate : active?} (util.require :editor.imstate))
(local MapEditView (GraphicsEditView:extend))
(local mapw 20)
(local maph 12)
(local tilew (* GraphicsEditView.sprite-scale 14))
(local tileh (* GraphicsEditView.sprite-scale 16))
(fn MapEditView.new [self]
(MapEditView.super.new self)
(set self.map (string.rep "\0" (* mapw maph))))
; map is stored bottom-to-top
(fn imap-from-xy [mx my]
(+ mx -1 (* mapw (- maph my))))
(fn update-map [map mx my itile]
(local imap (imap-from-xy mx my))
(local enctile
(bit.bor
(bit.lshift (bit.band (- itile 1) 0x07) 5)
(bit.rshift (bit.band (- itile 1) 0xf8) 3)))
(..
(map:sub 1 imap)
(string.char enctile)
(map:sub (+ imap 2))))
(fn MapEditView.itile-from-xy [self mx my]
(local imap (+ (imap-from-xy mx my) 1))
(local enctile (string.byte (self.map:sub imap imap)))
(+ 1 (bit.bor
(bit.lshift (bit.band enctile 0x1f) 3)
(bit.rshift (bit.band enctile 0xe0) 5))))
(fn MapEditView.set-tile [self mx my itile]
(set self.map (update-map self.map mx my itile)))
(fn MapEditView.draw-map-editor [self x y]
(activate self :map x y (* tilew mapw) (* tileh maph))
(for [mx 1 mapw] (for [my 1 maph]
(local tilex (+ x (* (- mx 1) tilew)))
(local tiley (+ y (* (- my 1) tileh)))
(local itile (self:itile-from-xy mx my))
(self:draw-sprite tilex tiley itile)
(when (and (active? self :map) (mouse-inside tilex tiley tilew tileh) (not= itile self.itile))
(self:set-tile mx my self.itile)))))
(fn MapEditView.save [self]
(util.writejson "game/map00001.json" (self.map:tohex)))
(fn MapEditView.draw [self]
(self:draw_background style.background)
(love.graphics.setColor 1 1 1 1)
(self:draw-map-editor (+ self.position.x 10) (+ self.position.y 10))
(self:draw-tile-selector
(+ self.position.x 10)
(+ self.position.y 20 (* tileh maph))
(- self.size.x 20)))
(fn MapEditView.get_name [self] "Map Editor")
MapEditView