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

@@ -9,7 +9,7 @@ RAYLIB_LIB := build/libraylib.a
RAYLIB_PLATFORM ?= PLATFORM_DESKTOP RAYLIB_PLATFORM ?= PLATFORM_DESKTOP
RAYLIB_BUILD_MODE ?= RELEASE RAYLIB_BUILD_MODE ?= RELEASE
CFLAGS ?= -O2 -g CFLAGS ?= -g
RAYLIB_SRC := third-party/raylib/src RAYLIB_SRC := third-party/raylib/src
RAYGUI_SRC := third-party/raygui/src RAYGUI_SRC := third-party/raygui/src

View File

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

50
tmx.sex
View File

@@ -203,9 +203,9 @@
(return (cast (. object height) float))) (return (cast (. object height) float)))
(pub fn tmx-find-object-by-name ((map (* TmxMap)) (pub fn tmx-find-object-by-name ((map (* TmxMap))
(layer-name (* const char)) (layer-name (* const char))
(obj-name (* const char))) (obj-name (* const char)))
(TmxObject *) (TmxObject *)
(if (== obj-name NULL) (if (== obj-name NULL)
(return NULL)) (return NULL))
@@ -226,3 +226,47 @@
(++ i)) (++ i))
(return NULL)) (return NULL))
(pub fn tmx-is-tile-walkable ((map (* tmx-map))
(layer-name (* const char))
(x float) (y float)) bool
;; x and y must be already in map coordinates (compensated for map origin)
(if (|| (== map NULL)
(== layer-name NULL))
(return false))
(var layer (* TmxLayer) (find-layer (. map handle layers) (. map handle layersLength) layer-name))
(if (== NULL layer)
(do
(TraceLog LOG-WARNING "TMX: layer \"%s\" not found" layer-name)
(return false)))
(var tx _ (cast (/ x (. map handle tileWidth)) int))
(var ty _ (cast (/ y (. map handle tileHeight)) int))
(if (|| (< tx 0) (< ty 0) (>= tx (. map width)) (>= ty (. map height)))
(do
;; Miss!
(TraceLog LOG-INFO "TMX: no tile at %d %d on %s" tx ty layer-name)
(return false)))
(var raw-gid u32 [(. layer exact tileLayer tiles) (+ tx (* ty (. map width)))])
(if (== raw-gid 0)
;; Empty tile
(do
(TraceLog LOG-INFO "TMX: tile at %d %d on %s is empty" tx ty layer-name)
(return false)))
;; strip flip/rotation bits
(var gid _ (& raw-gid 0x0FFFFFFF))
(if (>= gid (. map handle gidsToTilesLength))
(return false))
;; TODO: add "blocked" property
;;;(for (var i u32 0) (< i (. map handle tilesetsLength)) (++ i)
;;; (var tileset (* TmxTileset) (& [(. map handle tilesets) i]))
;;; (if (&& (>= gid (. tileset firstGid))
;;; (<= gid (. tileset lastGid)))
;;; (do
;;; (var local-gid _ (- gid (. tileset firstGid)))
;;; (return (& [(. tileset tiles) local-gid])))))
;; ???
(return true))