;; corridor ;; maybe make this generic enough to have multiple ;; corridors with the same code, eventually (local player (require :src.player)) (local bump (require :vendor.bump)) (fn clamp [x min max] (if (< x min) min (> x max) max x)) (fn draw-rectangle [mode x1 y1 x2 y2] ; instead of width and height (love.graphics.polygon mode x1 y1 x2 y1 x2 y2 x1 y2)) (local WIDTH 640) (local HALFWIDTH (/ WIDTH 2)) (local border-left -30) (local border-right 1230) (fn effective-ppos-x [player-x] (let [threshold-left (+ border-left HALFWIDTH) threshold-right (- border-right HALFWIDTH)] (clamp player-x threshold-left threshold-right))) (fn trans-amt-x [player-x] (let [eff (effective-ppos-x player-x)] (- HALFWIDTH eff))) (fn draw-stars [cpos] (love.graphics.setColor 0.05 0.05 0.2) (love.graphics.rectangle :fill border-left 0 (- border-right border-left) 480) (math.randomseed 62869420) (love.graphics.setColor 1 1 1) (for [i 1 200] (let [sz (math.random 3 10) x (math.random border-left border-right) y (math.random 0 480) c (- 0.7 (* 0.3 (math.random))) d (+ 0.5 (math.random))] (love.graphics.setColor 1 1 c) (love.graphics.rectangle :fill (+ x (* 0.1 d cpos)) y sz sz)))) (fn add-rect-bounds [world x1 y1 x2 y2 ?inset ?prefix ?thickness] (let [w (- x2 x1) h (- y2 y1) prefix (or ?prefix "") thickness (or ?thickness 10) inset (or ?inset 0) Lu (.. prefix :-Wu) Ld (.. prefix :-Wd) Ll (.. prefix :-Wl) Lr (.. prefix :-Wr)] (print thickness) (world:add Lu x1 (- y1 thickness) w (+ thickness inset)) (world:add Ld x1 (- y2 inset) w (+ thickness inset)) (world:add Ll (- x1 thickness) y1 (+ thickness inset) h) (world:add Lr (- x2 inset) y1 (+ thickness inset) h))) (fn dbg-world [world] (love.graphics.setLineWidth 1) (love.graphics.setColor 1 0 0) (let [items (world:getItems)] (each [_ item (pairs items)] (let [(x y w h) (world:getRect item)] (love.graphics.rectangle :line x y w h) (if (= (type item) :string) (love.graphics.print item x y)))))) (fn make [font] (local title (love.graphics.newText font "Corridor A2")) (local player (player.make 100 340)) (local windows [[10 100 400 80] [600 100 400 80] [1050 50 800 300]]) (local world (bump.newWorld)) (world:add :player player.x player.y 30 30) (add-rect-bounds world 0 250 1000 450 5) (fn draw-windows [] (love.graphics.stencil (fn [] (each [_ [x y w h] (pairs windows)] (love.graphics.rectangle :fill x y w h)) :replace 1)) (love.graphics.setStencilTest :greater 0) (love.graphics.push) (let [cpos (trans-amt-x player.x)] (love.graphics.origin) (love.graphics.translate (* 0.25 cpos) 0) (draw-stars cpos)) (love.graphics.pop) (love.graphics.setStencilTest) (love.graphics.setColor 0 0 0) (love.graphics.setLineWidth 7) (each [_ [x y w h] (pairs windows)] (love.graphics.rectangle :line x y w h))) (fn draw [] (love.graphics.setColor 0 0 0) (fn dbg-print [...] ; bad (var out {}) (for [i 1 (select :# ...) 2] (table.insert out (.. (select i ...) " " (select (+ i 1) ...)))) (let [txt (table.concat out "\n")] (love.graphics.print txt 6 28) (love.graphics.print txt 500 400))) ;(dbg-print ; :x player.x ; :y player.y) (love.graphics.translate (trans-amt-x player.x) 0) (love.graphics.setColor 0.8 0.8 0.8) (love.graphics.setLineWidth 10) (draw-rectangle :line 0 250 1000 450) (draw-windows) (love.graphics.draw title 0 10) (player:draw) ;(dbg-world world) ) (fn update [dt] (player:update dt world)) {: draw : update}) {: make}