143 lines
4.4 KiB
Scheme
143 lines
4.4 KiB
Scheme
(include raylib.h)
|
|
(include raygui.h)
|
|
|
|
(pub import pages)
|
|
(import main)
|
|
(import utils)
|
|
(pub import tmx)
|
|
|
|
(define MAP-FILE "res/tilemaps/SaveAss.tmx")
|
|
|
|
#-emscripten (define WIN-WIDTH 1440)
|
|
#-emscripten (define WIN-HEIGHT 900)
|
|
#+emscripten (define WIN-WIDTH 960)
|
|
#+emscripten (define WIN-HEIGHT 600)
|
|
|
|
(var TILE-SIZE float 1.0f)
|
|
|
|
(defmacro (clamp-to-grid v)
|
|
`(* (/ (cast ,v i64) TILE-SIZE) TILE-SIZE))
|
|
|
|
(pub record game-state
|
|
((app-state (* app-state))
|
|
(map (* tmx-map))
|
|
(cam Camera2D)
|
|
(fih (* TmxObject))))
|
|
|
|
(pub fn mk-game ((app-state (* app-state))) page
|
|
(var state _ (mem-alloc game-state))
|
|
(= (. state app-state) app-state)
|
|
|
|
(= (. state cam offset x) (/ WIN-WIDTH 2.0))
|
|
(= (. state cam offset y) (/ WIN-HEIGHT 2.0))
|
|
(= (. state cam rotation) 0.0f)
|
|
(= (. state cam zoom) 3.0)
|
|
|
|
(var map (* tmx-map) (load-tmx MAP-FILE))
|
|
(= (. state map) map)
|
|
|
|
(if (== map NULL)
|
|
(do
|
|
(= (. state cam target x) 0.0)
|
|
(= (. state cam target y) 0.0)
|
|
(return (page/game state))))
|
|
|
|
;; Assume width = height (true for our map)
|
|
(= TILE-SIZE (. map tile-width))
|
|
|
|
(var object-count int (tmx-object-count map "FIH"))
|
|
(TraceLog LOG-INFO "FIH has %d object(s)" object-count)
|
|
|
|
(if (== object-count 0)
|
|
(do
|
|
(= (. state cam target x)
|
|
(/ (cast (tmx-pixel-width map) float) 2.0))
|
|
(= (. state cam target y)
|
|
(/ (cast (tmx-pixel-height map) float) 2.0))
|
|
(return (page/game state))))
|
|
|
|
(var player-x float (tmx-object-x map "FIH" 0))
|
|
(var player-y float (tmx-object-y map "FIH" 0))
|
|
(var player-width float (tmx-object-width map "FIH" 0))
|
|
(var player-height float (tmx-object-height map "FIH" 0))
|
|
|
|
(var fih (* TmxObject)
|
|
(tmx-find-object-by-name (. state map handle) "FIH" "Player"))
|
|
(= (. state fih) fih)
|
|
(= (. state fih x) (clamp-to-grid (. state fih x)))
|
|
(= (. state fih y) (clamp-to-grid (. state fih y)))
|
|
|
|
(TraceLog LOG-INFO
|
|
"FIH[0]: \"%s\" type \"%s\" at %.1f, %.1f (%.1f x %.1f)"
|
|
(tmx-object-name map "FIH" 0)
|
|
(tmx-object-type map "FIH" 0)
|
|
player-x player-y player-width player-height)
|
|
|
|
(= (. state cam target x)
|
|
(+ player-x (/ player-width 2.0)))
|
|
(= (. state cam target y)
|
|
(+ player-y (/ player-height 2.0)))
|
|
(return (page/game state)))
|
|
|
|
(pub fn rm-game ((state (* game-state))) void
|
|
(unload-tmx (. state map))
|
|
(free state)
|
|
(return))
|
|
|
|
(pub fn render-game ((state (* game-state))) void
|
|
(BeginDrawing)
|
|
(ClearBackground BLACK)
|
|
|
|
(if (== (. state map) NULL)
|
|
(do
|
|
(DrawText "Failed to load res/tilemaps/SaveAss.tmx" 20 20 20 RED)
|
|
(return)))
|
|
|
|
(BeginMode2D (. state cam))
|
|
(animate-tmx (. state map))
|
|
(draw-tmx-layer (. state map) "Background" (& (. state cam)))
|
|
(draw-tmx-layer (. state map) "FIH" (& (. state cam)))
|
|
(EndMode2D)
|
|
|
|
(DrawText "WASD/arrows: move wheel: zoom click: back to menu"
|
|
10 10 20 RAYWHITE)
|
|
(DrawText (TextFormat "FIH objects: %d"
|
|
(tmx-object-count (. state map) "FIH"))
|
|
10 30 20 RAYWHITE)
|
|
|
|
(EndDrawing))
|
|
|
|
(pub fn process-input-game ((state (* game-state))) void
|
|
(if (!= (. state map) NULL)
|
|
(do
|
|
(var speed float (* 300.0 (GetFrameTime)))
|
|
|
|
(if (|| (IsKeyPressed KEY-LEFT) (IsKeyPressed KEY-A))
|
|
(-= (. state fih x) TILE-SIZE))
|
|
(if (|| (IsKeyPressed KEY-RIGHT) (IsKeyPressed KEY-D))
|
|
(+= (. state fih x) TILE-SIZE))
|
|
(if (|| (IsKeyPressed KEY-UP) (IsKeyPressed KEY-W))
|
|
(-= (. state fih y) TILE-SIZE))
|
|
(if (|| (IsKeyPressed KEY-DOWN) (IsKeyPressed KEY-S))
|
|
(+= (. state fih y) TILE-SIZE))
|
|
|
|
(var map-width float (cast (tmx-pixel-width (. state map)) float))
|
|
(var map-height float (cast (tmx-pixel-height (. state map)) float))
|
|
(if (< (. state cam target x) 0.0)
|
|
(= (. state cam target x) 0.0))
|
|
(if (> (. state cam target x) map-width)
|
|
(= (. state cam target x) map-width))
|
|
(if (< (. state cam target y) 0.0)
|
|
(= (. state cam target y) 0.0))
|
|
(if (> (. state cam target y) map-height)
|
|
(= (. state cam target y) map-height))
|
|
|
|
(var wheel float (GetMouseWheelMove))
|
|
(if (!= wheel 0.0)
|
|
(do
|
|
(+= (. state cam zoom) (* wheel 0.5))
|
|
(if (< (. state cam zoom) 0.5)
|
|
(= (. state cam zoom) 0.5))
|
|
(if (> (. state cam zoom) 8.0)
|
|
(= (. state cam zoom) 8.0)))))))
|