diff options
author | equa <equaa@protonmail.com> | 2021-04-23 18:20:01 -0500 |
---|---|---|
committer | equa <equaa@protonmail.com> | 2021-04-23 18:20:01 -0500 |
commit | 5886071e3e05e6d75ffe0990776b5bd407f7ce3d (patch) | |
tree | 655d8f9cd3d724cfc39e10e6c410605ad012a5a8 | |
parent | 465b2e11a689526c7a830bb50483dc772d359f84 (diff) |
menu
-rw-r--r-- | lib/main.fnl | 9 | ||||
-rw-r--r-- | lib/state.fnl | 1 | ||||
-rw-r--r-- | lib/transition.fnl | 81 | ||||
-rw-r--r-- | lib/translation.fnl | 6 |
4 files changed, 88 insertions, 9 deletions
diff --git a/lib/main.fnl b/lib/main.fnl index e88f704..b657d94 100644 --- a/lib/main.fnl +++ b/lib/main.fnl @@ -77,6 +77,15 @@ (when profi? (profi:stopHooks))) +(fn love.mousepressed [x y button] + (match (pcall #(state.mousepressed the-state x y button)) + (true x) nil + (false x) (do + (print (.. "mousepressed: \n" x)) + (table.insert messages + {:ticks 5 + :msg (.. "mousepressed: \n" x)})))) + (fn love.keypressed [key scancode repeat] (when (and (= key "f") (not repeat)) (love.window.setFullscreen (not (love.window.getFullscreen)))) diff --git a/lib/state.fnl b/lib/state.fnl index 41dc312..a0ddd11 100644 --- a/lib/state.fnl +++ b/lib/state.fnl @@ -17,4 +17,5 @@ ;; i hope :draw (proto.meta-method-opt :state.draw) :keypressed (proto.meta-method-opt :state.keypressed) + :mousepressed (proto.meta-method-opt :state.mousepressed) } diff --git a/lib/transition.fnl b/lib/transition.fnl index e62dcb8..c1a81a0 100644 --- a/lib/transition.fnl +++ b/lib/transition.fnl @@ -3,6 +3,53 @@ (local translation (require :lib.translation)) (local music-state (require :lib.music-state)) +(fn get-scale [] + (if (> (select 2 (love.graphics.getDimensions)) 600) + 2 + 1)) + +(fn draw-boxes [boxes scale tab] + (let [(width height) (love.graphics.getDimensions) + (mouse-x mouse-y) (love.mouse.getPosition) + mouse-x (/ (- mouse-x (/ width 2)) scale) + mouse-y (/ (- mouse-y (/ height 2)) scale)] + (each [i box (ipairs boxes)] + (if (and (>= mouse-x box.x) + (< mouse-x (+ box.x box.width)) + (>= mouse-y box.y) + (< mouse-y (+ box.y box.height))) + (love.graphics.setColor 1 1 1 0.2) + (love.graphics.setColor 1 1 1 0.1)) + (love.graphics.rectangle :fill + (* scale box.x) + (* scale box.y) + (* scale box.width) + (* scale box.height)) + (when (= tab i) + (love.graphics.setColor 1 1 1 0.2) + (love.graphics.rectangle :line + (* scale box.x) + (* scale box.y) + (* scale box.width) + (* scale box.height))) + (love.graphics.setFont (. font (* 12 scale))) + (love.graphics.setColor 1 1 1 0.8) + (love.graphics.printf (box.text) + (* scale box.x) + (* scale (+ box.y (/ box.height 6))) + (* scale box.width) + :center) + ))) + +(local boxes + {:menu + [{:x -106 :y 94 :width 212 :height 24 :text #translation.text.start + :action (fn [self] (set self.transition true))} + {:x -106 :y 130 :width 100 :height 24 :text #"toki pona" + :action #(set translation.text translation.tp)} + {:x 6 :y 130 :width 100 :height 24 :text #"english" + :action #(set translation.text translation.en)}]}) + (fn draw [self] (local (width height) (love.graphics.getDimensions)) (state.draw self.present-state) @@ -10,9 +57,9 @@ (love.graphics.reset) (love.graphics.translate (/ width 2) (/ height 2)) ;; TODO: this scaling is pretty limited and should probably get bigger! - (local scale (if (> height 600) - 2 - 1)) + (local scale (get-scale)) + (when (. boxes self.type) + (draw-boxes (. boxes self.type) scale self.tab)) (love.graphics.setColor 0.8 0.8 0.8 (/ (math.max 0 (- self.age 30)) 10)) (love.graphics.setFont (. font (* 48 scale))) (love.graphics.printf (string.format @@ -43,19 +90,39 @@ nil)) (fn keypressed [self key scancode repeat] - (when (and (not repeat) (or (and (= self.type :pause) - (= key :escape)) - (= key :z))) + (when (and (not repeat) (= self.type :pause) (= key :escape)) (set self.transition true)) + (when (and (not repeat) (or (= key :z) (= key :return))) + (if (and (. boxes self.type) (> self.tab 0)) + ((. boxes self.type self.tab :action) self) + (set self.transition true))) + (when (and (= key :tab) (. boxes self.type)) + (set self.tab (+ (% self.tab (length (. boxes self.type))) 1))) ) +(fn mousepressed [self x y button] + (let [(width height) (love.graphics.getDimensions) + scale (get-scale) + x (/ (- x (/ width 2)) scale) + y (/ (- y (/ height 2)) scale)] + (print x y) + (when (and (= button 1) (. boxes self.type)) + (print :a) + (each [i box (ipairs (. boxes self.type))] + (when (and (>= x box.x) + (< x (+ box.x box.width)) + (>= y box.y) + (< y (+ box.y box.height))) + (box.action self)))))) + (fn init [self type present-state future-state level] (setmetatable {: type : present-state : level : future-state + :tab 0 :age (if (or (= type :pause) (= type :menu)) 40 0)} self)) -{state.draw draw state.init init state.update update state.keypressed keypressed} +{state.draw draw state.init init state.update update state.keypressed keypressed state.mousepressed mousepressed} diff --git a/lib/translation.fnl b/lib/translation.fnl index ee19879..e50a836 100644 --- a/lib/translation.fnl +++ b/lib/translation.fnl @@ -2,8 +2,9 @@ {:death ["ike a" "sina moli lon musi nanpa %s\n\nnena Z li open e musi sin"] :win ["pona a" "sina pini e musi nanpa %s\n\nnena Z li tawa musi kama!"] :pause ["awen" "nena Z li open sin e musi"] + :start "o musi" :menu ["soko" - "tan soweli nata\nhttps://equa.space/\n\nnena Z li open e musi\n\nmusi la\nnena ←↑→ li tawa e tomo\nnena Z li pana e kiwen seli"] + "tan soweli nata\nhttps://equa.space/\n\nmusi la\nnena ←↑→ li tawa e tomo\nnena Z li pana e kiwen seli"] :number (fn [x] (string.gsub (.. (string.rep "ale " (math.floor (/ x 100))) (string.rep "mute " (math.floor (/ (% x 100) 20))) @@ -18,8 +19,9 @@ {:death ["ike a" "you died on level %s\n\npress Z to restart"] :win ["pona a" "you beat level %s\n\npress Z to advance!"] :pause ["pause" "press Z to continue"] + :start "play" :menu ["soko" - "by soweli nata\nhttps://equa.space/\n\npress Z to start\n\ncontrols\nhold ←↑→ to move\npress Z to fire"] + "by soweli nata\nhttps://equa.space/\n\ncontrols\nhold ←↑→ to move\npress Z to fire"] :number tostring}) {: en : tp :text tp} |