;; deps (local levels (. (require "levels.fnl") :levels)) (local bump (require "bump")) ;; colors (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 color-pallet { :cream (color 255 238 204) :dark-purple (color 70 66 94) :dark-pink (color 255 105 115) :dark-blue (color 21 120 140) :light-blue (color 0 185 190) :light-pink (color 255 176 163) :black [0 0 0] :black-half-tone [0 0 0 0.25] }) (fn reset-color [] "reset color to white (no tinting)" (love.graphics.setColor 1 1 1)) (lambda set-color [color] "set color to the given color from the color pallet" (love.graphics.setColor (unpack (. color-pallet color)))) ;; global vars (var player-art nil) ; 25x50 pixels each player is 25x25 (var dust-sprite nil) ; 35x35 pixels (var ui-bg nil) (var walls-sprite nil) (var walls-batch nil) (var wall-quads nil) (local screen (let [scale 2 canvas-w 800 canvas-h 450] { :screen-w (* canvas-w scale) :screen-h (* canvas-h scale) :canvas-w canvas-w :canvas-h canvas-h :scale scale :canvas nil})) (local camera {:x 0 :y 0}) (local debug false) (local collider-debug-boxes []) (local bump-world (bump.newWorld 25)) (local player { :x 50 :y 50 :w 25 :h 25 :speed 80 :battery 100 :rot 0 }) ; 16:9 (Modern Widescreen - Recommended)** ; - **640x360** ← Best middle ground ; - 800x450 ; - 1024x576 ; - 1280x720 ; **4:3 (Classic/Arcade Feel)** ; - **320x240** (very retro, but small) ; - **800x600** (spacious, classic 4:3) ; - 1024x768 (fn love.load [] (love.window.setMode screen.screen-w screen.screen-h) (tset screen :canvas (love.graphics.newCanvas screen.canvas-w screen.canvas-h)) (bump-world:add player player.x player.y player.w player.h) ;; load world images (set walls-sprite (love.graphics.newImage "assets/walls.png")) (set walls-batch (love.graphics.newSpriteBatch walls-sprite 2500)) (set ui-bg (love.graphics.newImage "assets/ui.png")) ;; load wall quads (set wall-quads []) (let [(w h) (: walls-sprite :getDimensions)] (for [i 0 19 1] (table.insert wall-quads (love.graphics.newQuad (* i 25) 0 25 25 w h)))) ;; load tiles (each [_ row (pairs (. levels :level01 :tiles))] (each [_ tile (pairs row)] (let [ x (. tile :x) y (. tile :y) id (. tile :tile-id) colliders (or (. levels.level01.wall-colliders (- id 1)) [])] (walls-batch:add (. wall-quads id) x y) ; (print (fennel.view colliders)) (each [_ collider (pairs colliders)] (bump-world:add {: x : y :type "wall"} (+ x collider.x) (+ y collider.y) collider.width collider.height) (table.insert collider-debug-boxes {:x (+ x collider.x) :y (+ y collider.y) :width collider.width :height collider.height}) )))) (set player-art (let [ player-sprite (love.graphics.newImage "assets/player.png") (w h) (player-sprite:getDimensions)] { :player-sprite player-sprite :player1 { :quads { :n (love.graphics.newQuad 0 0 25 25 w h) :s (love.graphics.newQuad 25 0 25 25 w h) :ne (love.graphics.newQuad 50 0 25 25 w h) :e (love.graphics.newQuad 75 0 25 25 w h) :se (love.graphics.newQuad 100 0 25 25 w h) :sw (love.graphics.newQuad 125 0 25 25 w h) :w (love.graphics.newQuad 150 0 25 25 w h) :nw (love.graphics.newQuad 175 0 25 25 w h) } } } )) (set dust-sprite (love.graphics.newImage "assets/dust_001.png")) ; (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 (fn draw-world [] (reset-color) (love.graphics.draw walls-batch) ;; draw collider debug boxes (when debug (set-color :black) (each [_ collider (pairs collider-debug-boxes)] (love.graphics.rectangle "line" collider.x collider.y collider.width collider.height)))) (fn love.update [dt] ; move / rotate player (let [ d (love.keyboard.isDown :d) a (love.keyboard.isDown :a) e (love.keyboard.isDown :e) q (love.keyboard.isDown :q)] (case [d a e q] [true false false false] (tset player :rot (+ player.rot (* dt 2))) [false true false false] (tset player :rot (- player.rot (* dt 2))) [false false true false] (tset player :rot (- player.rot (* dt 2))) [false false false true] (tset player :rot (+ player.rot (* dt 2))) ) (if (or d a e q) (let [ dir-fn (if (or d a) #(+ $1 $2) #(- $1 $2)) new-x (dir-fn player.x (* player.speed dt (math.cos player.rot))) new-y (dir-fn player.y (* player.speed dt (math.sin player.rot))) (x y) (bump-world:move player new-x new-y)] (tset player :x x) (tset player :y y)))) ;; Update camera to follow player (keep player centered on screen) (tset camera :x (- player.x (/ screen.canvas-w 2))) (tset camera :y (- player.y (/ screen.canvas-h 2))) ) (fn draw-ui [] (love.graphics.push) (love.graphics.translate 0 400) (let [font (love.graphics.newFont 10) font-small (love.graphics.newFont 6)] (set-color :cream) (love.graphics.rectangle "fill" 0 0 screen.canvas-w 100) (reset-color) (love.graphics.draw ui-bg 0 0) (set-color :dark-purple) (love.graphics.setFont font) (love.graphics.print "Battery:" 20 6) (set-color :light-blue) (love.graphics.rectangle "fill" 72 12 (* 152 (/ player.battery 100)) 4) (love.graphics.setFont font-small) (set-color :dark-purple) (love.graphics.print "100%" 229 2) (set-color :light-pink) (love.graphics.print "125%" 267 2) (love.graphics.print "125%" 306 2) (love.graphics.print "150%" 345 2) (love.graphics.print "200%" 382 2) (set-color :dark-purple) (love.graphics.line 0 0 screen.canvas-w 0) ) (love.graphics.pop) ) (lambda angle-to-direction [angle] "Convert angle (radians) to compass direction keyword" (local tau (* 2 math.pi)) ; Normalize angle to 0-2π range (local normalized (% (+ angle tau) tau)) ; Offset by 22.5° (π/8) so section boundaries align with directions (local offset (+ normalized (/ math.pi 8))) ; Find which 45° section (π/4) it falls into (local section (math.floor (/ offset (/ math.pi 4)))) ; Map to compass directions (local directions [:e :se :s :sw :w :nw :n :ne]) (. directions (+ (% section 8) 1))) (fn draw-player [] "draw player sprite and hitbox" (reset-color) (love.graphics.draw player-art.player-sprite (. player-art.player1.quads (angle-to-direction player.rot)) player.x player.y) ;; draw player hitbox and direction line (when debug (set-color :black) (love.graphics.rectangle "line" player.x player.y player.w player.h) (love.graphics.push) (let [ox (+ player.x (/ 25 2)) oy (+ player.y (/ 25 2))] (love.graphics.translate ox oy) (love.graphics.rotate player.rot) (love.graphics.line 0 0 35 0)) (love.graphics.pop) )) (fn love.draw [] (love.graphics.setCanvas screen.canvas) (love.graphics.clear) (set-color :cream) (love.graphics.rectangle "fill" 0 0 screen.canvas-w screen.canvas-h) (love.graphics.push) ; stores the default coordinate system (love.graphics.translate (* -1 camera.x) (* -1 camera.y)) (draw-world) (draw-player) (love.graphics.pop) (draw-ui) (love.graphics.setCanvas) (reset-color) (love.graphics.draw screen.canvas 0 0 0 screen.scale screen.scale)) ; (love.graphics.print "Hello from Fennel!\nPress any key to quit" 10 10)) (fn love.keypressed [key] nil )