(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) ;; 1.0f is fallback default (var TILE-SIZE float 1.0f) (defmacro (clamp-to-grid v) `(* (/ (cast ,v i64) TILE-SIZE) TILE-SIZE)) (pub record camera ((follow-speed float) (ray-cam Camera2D))) (fn fih-center ((state (* game-state))) Vector2) (pub record game-state ((app-state (* app-state)) (map (* tmx-map)) (cam camera) (fih (* TmxObject)))) (ops camera (fn init ((self (* camera))) void (= (. self ray-cam offset x) (/ WIN-WIDTH 2.0f)) (= (. self ray-cam offset y) (/ WIN-HEIGHT 2.0f)) (= (. self ray-cam rotation) 0.0f) (= (. self ray-cam zoom) 3.0f) ;; More = faster. 4.0f will be smooth, 12.0f fast (= (. self follow-speed) 8.0f)) (fn clamp ((self (* camera)) (state (* game-state))) void (var w float (/ WIN-WIDTH (. self ray-cam zoom))) (var h float (/ WIN-HEIGHT (. self ray-cam zoom))) (var hw _ (/ w 2.0f)) (var hh _ (/ h 2.0f)) (var map-w _ (cast (tmx-pixel-width (. state map)) float)) (var map-h _ (cast (tmx-pixel-height (. state map)) float)) (if (<= map-w w) (= (. self ray-cam target x) (/ map-w 2.0f)) (clamp (. self ray-cam target x) hw (- map-w hw))) (if (<= map-h h) (= (. self ray-cam target y) (/ map-h 2.0f)) (clamp (. self ray-cam target y) hh (- map-h hh)))) (fn update ((self (* camera)) (state (* game-state))) void (if (|| (== (. state map) NULL) (== (. state fih) NULL)) (return)) (var target _ (fih-center state)) (var alpha _ (* (. self follow-speed) (GetFrameTime))) ;; Rubber band for camera (clamp alpha alpha 1.0f) (+= (. self ray-cam target x) (* (- (. target x) (. self ray-cam target x)) alpha)) (+= (. self ray-cam target y) (* (- (. target y) (. self ray-cam target y)) alpha)))) (fn fih-center ((state (* game-state))) Vector2 (return #(Vector2 : (+ (. state fih x) (/ (. state fih width) 2.0f)) (+ (. state fih y) (/ (. state fih height) 2.0f))))) (pub fn mk-game ((app-state (* app-state))) page (var state _ (mem-alloc game-state)) (= (. state app-state) app-state) (var map (* tmx-map) (load-tmx MAP-FILE)) (= (. state map) map) (if (== map NULL) (do (= (. state cam ray-cam target x) 0.0) (= (. state cam ray-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 ray-cam target x) (/ (cast (tmx-pixel-width map) float) 2.0)) (= (. state cam ray-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) (.init (& (. state cam))) (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 map" 20 20 20 RED) (return))) (BeginMode2D (. state cam ray-cam)) (animate-tmx (. state map)) (draw-tmx-layer (. state map) "Background" (& (. state cam ray-cam))) (draw-tmx-layer (. state map) "FIH" (& (. state cam ray-cam))) (EndMode2D) (DrawText "WASD/arrows: move wheel: zoom ESC: back to menu" 10 10 20 RAYWHITE) (EndDrawing)) (fn move-fih ((state (* game-state)) (dx i32) (dy i32)) void ;; dx and dy are number of tiles to move ;; no collision detection along path, i.e. can jump over obstacles ;; if the target can't be move to, do nothing (if (== (. state map) NULL) (return)) (if (! (tmx-is-tile-walkable (. state map) "Background" (+ (* dx TILE-SIZE) (. state fih x)) (+ (* dy TILE-SIZE) (. state fih y)))) (return)) (+= (. state fih x) (* dx TILE-SIZE)) (+= (. state fih y) (* dy TILE-SIZE))) (pub fn update-game ((state (* game-state))) void (if (== (. state map) NULL) (return)) (var speed float (* 300.0 (GetFrameTime))) (if (|| (IsKeyPressed KEY-LEFT) (IsKeyPressed KEY-A)) (move-fih state -1 0)) (if (|| (IsKeyPressed KEY-RIGHT) (IsKeyPressed KEY-D)) (move-fih state 1 0)) (if (|| (IsKeyPressed KEY-UP) (IsKeyPressed KEY-W)) (move-fih state 0 -1)) (if (|| (IsKeyPressed KEY-DOWN) (IsKeyPressed KEY-S)) (move-fih state 0 1)) (var wheel float (GetMouseWheelMove)) (if (!= wheel 0.0) (do (+= (. state cam ray-cam zoom) (* wheel 0.5)) (if (< (. state cam ray-cam zoom) 0.5) (= (. state cam ray-cam zoom) 0.5)) (if (> (. state cam ray-cam zoom) 8.0) (= (. state cam ray-cam zoom) 8.0)))) (.update (& (. state cam)) state) (if (IsKeyReleased KEY-ESCAPE) (.change-page (. state app-state) (sum-type page menu))))