2020-10-19 00:13:26 +00:00
|
|
|
(local GraphicsEditView (require :editor.gfxedit))
|
|
|
|
(local style (require :core.style))
|
|
|
|
(local util (require :lib.util))
|
2020-10-29 02:59:47 +00:00
|
|
|
(local lume (require :lib.lume))
|
|
|
|
(local {: mouse-inside : activate : active? : checkbox : textbox} (util.require :editor.imstate))
|
2020-10-19 00:13:26 +00:00
|
|
|
|
|
|
|
(local MapEditView (GraphicsEditView:extend))
|
2020-10-29 02:59:47 +00:00
|
|
|
(local sprite-scale 3)
|
2020-10-19 00:13:26 +00:00
|
|
|
(local mapw 20)
|
|
|
|
(local maph 12)
|
2020-10-29 02:59:47 +00:00
|
|
|
(local tilew (* sprite-scale 14))
|
|
|
|
(local tileh (* sprite-scale 16))
|
2020-10-19 00:13:26 +00:00
|
|
|
|
|
|
|
(fn MapEditView.new [self]
|
|
|
|
(MapEditView.super.new self)
|
2020-10-29 02:59:47 +00:00
|
|
|
(set self.sprite-scale sprite-scale)
|
|
|
|
(set self.testtext "Hello")
|
|
|
|
(self:reload))
|
2020-10-19 00:13:26 +00:00
|
|
|
|
|
|
|
; 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))
|
2020-10-29 02:59:47 +00:00
|
|
|
(local enctile (string.byte (self.level.map:sub imap imap)))
|
2020-10-19 00:13:26 +00:00
|
|
|
(+ 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]
|
2020-10-29 02:59:47 +00:00
|
|
|
(set self.level.map (update-map self.level.map mx my itile)))
|
|
|
|
|
|
|
|
(fn MapEditView.iobject-from-xy [self mx my ?iobj]
|
|
|
|
(local iobj (or ?iobj 1))
|
|
|
|
(local obj (. self.level.objects iobj))
|
|
|
|
(when obj
|
|
|
|
(if (and (= obj.x mx) (= obj.y my))
|
|
|
|
iobj
|
|
|
|
(self:iobject-from-xy mx my (+ iobj 1)))))
|
2020-10-19 00:13:26 +00:00
|
|
|
|
|
|
|
(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))
|
2020-10-29 02:59:47 +00:00
|
|
|
(local iobject (self:iobject-from-xy mx my))
|
2020-10-19 00:13:26 +00:00
|
|
|
(self:draw-sprite tilex tiley itile)
|
2020-10-29 02:59:47 +00:00
|
|
|
(when (not= iobject nil)
|
|
|
|
(love.graphics.setColor 1 0 (if (and (= self.itile nil) (= iobject self.iobject)) 1 0))
|
|
|
|
(love.graphics.setLineWidth 3)
|
|
|
|
(love.graphics.rectangle :line tilex tiley tilew tileh)
|
|
|
|
(love.graphics.setColor 1 1 1))
|
|
|
|
(when (and self.itile (active? self :map) (mouse-inside tilex tiley tilew tileh) (not= itile self.itile))
|
|
|
|
(self:set-tile mx my self.itile))
|
|
|
|
(when (and (= self.itile nil) (active? self :map) (mouse-inside tilex tiley tilew tileh) (= self.imstate.left :released))
|
|
|
|
(if (= iobject nil)
|
|
|
|
(do (table.insert self.level.objects {:x mx :y my :func ""})
|
|
|
|
(set self.iobject (length self.level.objects)))
|
|
|
|
(set self.iobject iobject))))))
|
|
|
|
|
|
|
|
(fn MapEditView.reload [self]
|
|
|
|
(MapEditView.super.reload self)
|
|
|
|
(local level (util.readjson "game/map00001.json"))
|
|
|
|
(set self.level
|
|
|
|
(match (type level)
|
|
|
|
:string {:map (level:fromhex) :objects []}
|
|
|
|
:table (doto level (tset :map (level.map:fromhex))))))
|
2020-10-19 00:13:26 +00:00
|
|
|
|
|
|
|
(fn MapEditView.save [self]
|
2020-10-29 02:59:47 +00:00
|
|
|
(util.writejson "game/map00001.json"
|
|
|
|
(doto (lume.clone self.level)
|
|
|
|
(tset :map (self.level.map:tohex)))))
|
2020-10-19 00:13:26 +00:00
|
|
|
|
|
|
|
(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))
|
2020-10-29 02:59:47 +00:00
|
|
|
(when (checkbox self "Edit objects" (= self.itile nil) (+ self.position.x 10) (+ self.position.y 20 (* tileh maph)))
|
|
|
|
(set self.itile nil))
|
|
|
|
(set self.testtext (textbox self :test self.testtext (+ self.position.x 10) (+ self.position.y 40 (* tileh maph)) 200))
|
2020-10-19 00:13:26 +00:00
|
|
|
(self:draw-tile-selector
|
|
|
|
(+ self.position.x 10)
|
2020-10-29 02:59:47 +00:00
|
|
|
(+ self.position.y 65 (* tileh maph))
|
2020-10-19 00:13:26 +00:00
|
|
|
(- self.size.x 20)))
|
|
|
|
|
|
|
|
(fn MapEditView.get_name [self] "Map Editor")
|
|
|
|
|
|
|
|
MapEditView
|