add walkability check

This commit is contained in:
2026-11-07 18:48:31 +03:00
parent df34b00aad
commit 4da1ff613b
3 changed files with 71 additions and 10 deletions

View File

@@ -13,7 +13,9 @@
#+emscripten (define WIN-WIDTH 960)
#+emscripten (define WIN-HEIGHT 600)
(var TILE-SIZE float 1.0f)
(var TILE-SIZE float 1.0f
;; 1.0f is fallback default
)
(defmacro (clamp-to-grid v)
`(* (/ (cast ,v i64) TILE-SIZE) TILE-SIZE))
@@ -90,7 +92,7 @@
(if (== (. state map) NULL)
(do
(DrawText "Failed to load res/tilemaps/SaveAss.tmx" 20 20 20 RED)
(DrawText "Failed to load map" 20 20 20 RED)
(return)))
(BeginMode2D (. state cam))
@@ -103,6 +105,21 @@
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 process-input-game ((state (* game-state))) void
(if (== (. state map) NULL)
(return))
@@ -110,13 +127,13 @@
(var speed float (* 300.0 (GetFrameTime)))
(if (|| (IsKeyPressed KEY-LEFT) (IsKeyPressed KEY-A))
(-= (. state fih x) TILE-SIZE))
(move-fih state -1 0))
(if (|| (IsKeyPressed KEY-RIGHT) (IsKeyPressed KEY-D))
(+= (. state fih x) TILE-SIZE))
(move-fih state 1 0))
(if (|| (IsKeyPressed KEY-UP) (IsKeyPressed KEY-W))
(-= (. state fih y) TILE-SIZE))
(move-fih state 0 -1))
(if (|| (IsKeyPressed KEY-DOWN) (IsKeyPressed KEY-S))
(+= (. state fih y) TILE-SIZE))
(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))