Conditional flag support

This commit is contained in:
Jeremy Penner 2021-05-05 21:09:40 -04:00
parent 8403e58857
commit a5dbc5fcfc
8 changed files with 66 additions and 11 deletions

Binary file not shown.

View file

@ -42,4 +42,22 @@
(actions.register-const :move-here :move-to-responder)
(actions.register-const :disappear :disappear)
(actions.register :set-flag
(fn [action view x y w i]
(let [y (+ y style.padding.y)
x (renderer.draw_text style.font "Set " x y style.text)
flag (or action.flag (. files.game.flags 1))
flag (dropdown view [:set-flag :flag i] flag files.game.flags x y 100)
x (renderer.draw_text style.font " to " (+ x 100) y style.text)
options (lume.concat
[{:label "<Yes>" :value 0xffff} {:label "<No>" :value 0}]
(icollect [_ flag (ipairs files.game.flags)] {:label flag :value (.. :cond- flag)}))
rhs (or action.rhs (. options 1))
(rhs y) (dropdown view [:set-flag :rhs i] rhs options x y 100)]
(set action.flag flag)
(set action.rhs rhs)
y))
(fn [action vm]
(values action.rhs.value (.. :cond-var- action.flag) :set)))
{}

View file

@ -86,9 +86,12 @@
(set view.imstate.active (make-tag tag))
true))
(fn set-cursor [view cursor]
(when (= view.cursor nil) (set view.cursor cursor)))
(fn active? [view tag] (= view.imstate.active (make-tag tag)))
(fn button [view tag x y w h]
(when (mouse-inside x y w h) (set view.cursor :hand))
(when (mouse-inside x y w h) (set-cursor view :hand))
(activate view tag x y w h)
(values (and (active? view tag) (= view.imstate.left :released) (mouse-inside x y w h)) (+ y h style.padding.y)))
@ -197,7 +200,7 @@
(set textNew (replace-selection view textNew "" iStartDel iLimDel)))))))
; handle mouse events
(when (mouse-inside x y w h) (set view.cursor :ibeam))
(when (mouse-inside x y w h) (set-cursor view :ibeam))
(when (and (focused? view tag) (active? view tag) (mouse-inside x y w h))
(local mouse-i (i-from-x textNew (love.mouse.getX) x style.font))
(when initial-press
@ -230,19 +233,23 @@
(renderer.draw_text style.font label x y style.text)
(textbox view label text (+ x wLabel) y wText))
(fn option-text [option]
(match (type option)
:string option
:table (or option.label (tostring option))
_ (tostring option)))
(fn dropdown [view tag selection options x y w]
(local row-h (+ (style.font:get_height) style.padding.y))
(var new-selection nil)
(renderer.draw_rect x y w row-h style.selection)
(renderer.draw_text style.font selection (+ x style.padding.x) (+ y (/ style.padding.y 2)) style.text)
(renderer.draw_text style.font (option-text selection) (+ x style.padding.x) (+ y (/ style.padding.y 2)) style.text)
(renderer.draw_text style.icon_font "-" (+ x w (- style.padding.x)) (+ y (/ style.padding.y 2)) style.text)
(when (focused? view tag)
(var row-y (+ y row-h))
(each [i option (ipairs options)]
(renderer.draw_rect x row-y w row-h style.selection)
(renderer.draw_text style.font option (+ x style.padding.x) (+ row-y (/ style.padding.y 2)) style.text)
(when (button view [(make-tag tag) i] x row-y w row-h)
(set new-selection option))
(set row-y (+ row-y row-h)))
@ -250,7 +257,7 @@
(var row-y (+ y row-h))
(each [i option (ipairs options)]
(renderer.draw_rect x row-y w row-h style.selection)
(renderer.draw_text style.font option (+ x style.padding.x) (+ row-y (/ style.padding.y 2)) style.text)
(renderer.draw_text style.font (option-text option) (+ x style.padding.x) (+ row-y (/ style.padding.y 2)) style.text)
(set row-y (+ row-y row-h))))))
(focus view tag x y w row-h)
(values (or new-selection selection) (+ y row-h)))

View file

@ -3,7 +3,7 @@
(local util (require :lib.util))
(local lume (require :lib.lume))
(local files (require :game.files))
(local {: mouse-inside : activate : active? : checkbox : textfield : textbutton : dropdown} (util.require :editor.imstate))
(local {: mouse-inside : activate : active? : checkbox : textfield : textbutton : textbox : dropdown} (util.require :editor.imstate))
(local {: tilestrip-to-sprite} (util.require :editor.tiledraw))
(local {: encode-yx : encode-itile : decode-itile} (util.require :game.tiles))
(local actions (require :editor.actions))
@ -156,6 +156,15 @@
(if (= self.imstate.left :released) (set self.iobject-linking nil)
(self:draw-link-line x y self.iobject-linking [0 1 0] true)))))
(fn condition-label [flag]
(if flag {:label flag : flag} {:label "<always>"}))
(fn condition-options []
(let [options [(condition-label nil)]]
(each [_ flag (ipairs (or files.game.flags []))]
(table.insert options (condition-label flag)))
options))
(fn MapEditView.draw-object-code-editor [self object x y]
(var y y)
(var istep-to-delete nil)
@ -163,6 +172,9 @@
(each [istep step (ipairs object.steps)]
(when (textbutton self "X" (+ x 280) y)
(set istep-to-delete istep))
(set step.condition (. (dropdown self [:code-condition istep] (condition-label step.condition) (condition-options)
(+ x 100 style.padding.x) y 100)
:flag))
(set (step.action y) (dropdown self [:code-action istep] (or step.action (. actions.actionlist 1)) actions.actionlist x y 100))
(set y (actions.edit step self x y 300 istep))
(set y (+ y style.padding.y)))
@ -186,8 +198,16 @@
y (if object.advanced
(self:draw-object-advanced-editor object x y)
(self:draw-object-code-editor object x y))
new-flag-name (textbox self :new-flag-name self.new-flag-name x (+ y style.padding.y) 200)
(mk-new-flag y) (textbutton self "+ New Flag" (+ x 200 style.padding.x) (+ y style.padding.y))
do-delete (textbutton self "Delete" x (+ y 40))
(do-advanced y) (textbutton self (if object.advanced "Simple" "Advanced") (+ x 150) (+ y 40))]
(set self.new-flag-name new-flag-name)
(when mk-new-flag
(when (= files.game.flags nil)
(set files.game.flags []))
(table.insert files.game.flags new-flag-name)
(set self.new-flag-name ""))
(when do-delete
(move-object self.level.objects (+ self.iobject 1) self.iobject)
(set self.iobject nil))

View file

@ -146,7 +146,8 @@
(when (not entity.advanced)
(let [code []]
(each [iaction action (ipairs (or entity.steps []))]
(lume.push code (actions.generate action vm iaction)))
(if action.condition (lume.push code (.. :cond- action.condition) (vm:when (actions.generate action vm iaction)))
(lume.push code (actions.generate action vm iaction))))
(vm:word (.. prefix ientity) :drop (table.unpack code))))))
(fn deflevel [ilevel label]

View file

@ -37,12 +37,17 @@
:levels (do (set value.map (value.map:tohex)) value)
_ value))
(fn clone [v]
(match (type v)
:table (lume.clone v)
_ v))
(fn files.load []
(set files.game
(if (util.file-exists filename)
(let [game (util.readjson filename)]
(each [k v (pairs game)]
(tset game k (lume.map v #(deserialize k (lume.clone $1)))))
(tset game k (lume.map v #(deserialize k (clone $1)))))
game)
{:tiles [] :portraits [] :font [] :levels []}))
files.game)
@ -50,7 +55,7 @@
(fn files.save []
(let [game {}]
(each [k v (pairs files.game)]
(tset game k (lume.map v #(serialize k (lume.clone $1)))))
(tset game k (lume.map v #(serialize k (clone $1)))))
(util.writejson filename game)))
(fn new-cache [game key]

File diff suppressed because one or more lines are too long

View file

@ -32,6 +32,10 @@
:map-specific-load
:full-redraw)
(each [_ flag (ipairs (or files.game.flags []))]
(vm:var (.. :cond-var- flag) vm.false)
(vm:word (.. :cond- flag) (.. :cond-var- flag) :get))
(each [imap _ (ipairs files.game.levels)]
(deflevel imap (.. :map imap)))