(local map-util (require "map-util.fnl")) (var player-sprite nil) ; 20x20 pixels (var dust-sprite nil) ; 35x35 pixels (local game-state { :player-pos [0 0] :level 1 :world [] }) (fn start-level [] ;; set player position to the start of the current level (tset game-state :player-pos (. map-util :levels (. game-state :level) :player-start)) ;; set fresh world (let [world []] (each [y walls (ipairs (. map-util :levels (. game-state :level) :walls))] (table.insert world []) (each [x wall (ipairs walls)] (table.insert (. world y) {:revealed false :type (if (= wall 1) :wall :floor) }) ) ) (tset game-state :world world) ) ) (fn love.load [] (map-util:load) (love.window.setMode 600 640) (set player-sprite (love.graphics.newImage "assets/player_001.png")) (set dust-sprite (love.graphics.newImage "assets/dust_001.png")) (start-level) (print (fennel.view game-state)) ;; start a thread listening on stdin (: (love.thread.newThread "require('love.event') while 1 do love.event.push('stdin', io.read('*line')) end") :start)) (fn love.handlers.stdin [line] ;; evaluate lines read from stdin as fennel code (let [(ok val) (pcall fennel.eval line)] (print (if ok (fennel.view val) val)))) ;; drawing (lambda color [full-r full-g full-b] (let [(r g b) (love.math.colorFromBytes full-r full-g full-b)] [r g b] )) (local off-white (color 237 230 200)) (local black [0 0 0]) (local black-half-tone [0 0 0 0.25]) (fn draw-game-outline [] (love.graphics.setColor (unpack black)) (love.graphics.rectangle "line" 50 50 500 500)) (fn draw-world [] (love.graphics.setColor (unpack black)) ; (love.graphics.rectangle "fill" 50 50 500 500)) (each [y cells (ipairs (. game-state :world))] (each [x cell (ipairs cells)] (case [(. cell :revealed) (. cell :type)] [false _] (do (love.graphics.setColor 1 1 1) ; reset color to white (no tinting) (love.graphics.draw dust-sprite (+ 45 (* 25 (- x 1))) (+ 45 (* 25 (- y 1))))) [true :wall] (love.graphics.rectangle "fill" (+ 50 (* 25 (- x 1))) (+ 50 (* 25 (- y 1))) 25 25) ) ) ) ) ; (print (fennel.view game-state)) (fn draw-player [] (love.graphics.setColor 1 1 1) ; reset color to white (no tinting) (let [[player-x player-y] (. game-state :player-pos)] (love.graphics.draw player-sprite (* 25 player-x) (* 25 player-y) 0 1.25 1.25))) (fn draw-ghost-grid [] (love.graphics.setColor (unpack black-half-tone)) (for [y 0 19 1] (for [x 0 19 1] (love.graphics.rectangle "line" (+ 50 (* x 25)) (+ 50 (* y 25)) 25 25)))) (fn love.draw [] ;; clear the screen and set bg to off white (love.graphics.clear) (love.graphics.setColor (unpack off-white)) (love.graphics.rectangle "fill" 0 0 600 640) (draw-game-outline) (draw-ghost-grid) (draw-world) (draw-player) ) ; (love.graphics.print "Hello from Fennel!\nPress any key to quit" 10 10)) (fn love.keypressed [key] (love.event.quit))