add player walking on map

This commit is contained in:
2026-11-06 22:41:29 +03:00
parent 759ef2a903
commit e792e53690

View File

@@ -13,6 +13,11 @@
#+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))
@@ -37,6 +42,9 @@
(= (. 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)
@@ -53,9 +61,11 @@
(var player-width float (tmx-object-width map "FIH" 0))
(var player-height float (tmx-object-height map "FIH" 0))
(var player (* TmxObject)
(var fih (* TmxObject)
(tmx-find-object-by-name (. state map handle) "FIH" "Player"))
(= (. state 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)"
@@ -102,14 +112,14 @@
(do
(var speed float (* 300.0 (GetFrameTime)))
(if (|| (IsKeyDown KEY-LEFT) (IsKeyDown KEY-A))
(-= (. state cam target x) speed))
(if (|| (IsKeyDown KEY-RIGHT) (IsKeyDown KEY-D))
(+= (. state cam target x) speed))
(if (|| (IsKeyDown KEY-UP) (IsKeyDown KEY-W))
(-= (. state cam target y) speed))
(if (|| (IsKeyDown KEY-DOWN) (IsKeyDown KEY-S))
(+= (. state cam target y) speed))
(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))