(local GraphicsEditView (require :editor.gfxedit)) (local style (require :core.style)) (local util (require :lib.util)) (local lume (require :lib.lume)) (local {: mouse-inside : activate : active? : checkbox : textbox} (util.require :editor.imstate)) (local MapEditView (GraphicsEditView:extend)) (local sprite-scale 3) (local mapw 20) (local maph 12) (local tilew (* sprite-scale 14)) (local tileh (* sprite-scale 16)) (fn MapEditView.new [self] (MapEditView.super.new self) (set self.sprite-scale sprite-scale) (set self.testtext "Hello") (self:reload)) ; 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.level.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.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))))) (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)) (local iobject (self:iobject-from-xy mx my)) (self:draw-sprite tilex tiley itile) (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)))))) (fn MapEditView.save [self] (util.writejson "game/map00001.json" (doto (lume.clone self.level) (tset :map (self.level.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)) (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)) (self:draw-tile-selector (+ self.position.x 10) (+ self.position.y 65 (* tileh maph)) (- self.size.x 20))) (fn MapEditView.get_name [self] "Map Editor") MapEditView