diff --git a/game.sex b/game.sex index 9d8d219..7150f99 100644 --- a/game.sex +++ b/game.sex @@ -13,35 +13,79 @@ #+emscripten (define WIN-WIDTH 960) #+emscripten (define WIN-HEIGHT 600) -(var TILE-SIZE float 1.0f - ;; 1.0f is fallback default - ) +;; 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 Camera2D) + (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) - (= (. 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) + (= (. 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) @@ -52,9 +96,9 @@ (if (== object-count 0) (do - (= (. state cam target x) + (= (. state cam ray-cam target x) (/ (cast (tmx-pixel-width map) float) 2.0)) - (= (. state cam target y) + (= (. state cam ray-cam target y) (/ (cast (tmx-pixel-height map) float) 2.0)) (return (page/game state)))) @@ -75,10 +119,7 @@ (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))) + (.init (& (. state cam))) (return (page/game state))) (pub fn rm-game ((state (* game-state))) void @@ -95,10 +136,10 @@ (DrawText "Failed to load map" 20 20 20 RED) (return))) - (BeginMode2D (. state cam)) + (BeginMode2D (. state cam ray-cam)) (animate-tmx (. state map)) - (draw-tmx-layer (. state map) "Background" (& (. state cam))) - (draw-tmx-layer (. state map) "FIH" (& (. state cam))) + (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" @@ -120,7 +161,7 @@ (+= (. state fih x) (* dx TILE-SIZE)) (+= (. state fih y) (* dy TILE-SIZE))) -(pub fn process-input-game ((state (* game-state))) void +(pub fn update-game ((state (* game-state))) void (if (== (. state map) NULL) (return)) @@ -135,25 +176,16 @@ (if (|| (IsKeyPressed KEY-DOWN) (IsKeyPressed KEY-S)) (move-fih state 0 1)) - (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)))) + (+= (. 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)))) diff --git a/main.sex b/main.sex index c98c562..0bc41ec 100644 --- a/main.sex +++ b/main.sex @@ -58,10 +58,10 @@ (.change-page- state) (match (. state page) ((menu (state)) - (process-input-menu state) + (update-menu state) (render-menu state)) ((game (state)) - (process-input-game state) + (update-game state) (render-game state)))) (fn init () void diff --git a/menu.sex b/menu.sex index 8923982..2c00e56 100644 --- a/menu.sex +++ b/menu.sex @@ -51,7 +51,7 @@ (EndDrawing)) -(pub fn process-input-menu ((state (* menu-state))) void +(pub fn update-menu ((state (* menu-state))) void (if (== (. state btn-to-game) 1) (do (= (. state btn-to-game) 0) diff --git a/utils.sex b/utils.sex index ab447ce..04865ba 100644 --- a/utils.sex +++ b/utils.sex @@ -8,3 +8,10 @@ (pub defmacro (tag-type sum-type) `(enum ,(cat sum-type 'ǂ))) + + +(pub defmacro (clamp val lo hi) + `(do (if (< ,val ,lo) + (= ,val ,lo)) + (if (> ,val ,hi) + (= ,val ,hi))))