add new player sprite and walking rotation

This commit is contained in:
2026-11-08 14:48:05 +03:00
parent 070e4e5552
commit 9e2979793f
6 changed files with 61 additions and 38 deletions

View File

@@ -23,20 +23,22 @@
((follow-speed float)
(ray-cam Camera2D)))
(fn fih-center ((state (* game-state))) Vector2)
(fn player-center ((state (* game-state))) Vector2)
(pub record game-state
((app-state (* app-state))
(map (* tmx-map))
(cam camera)
(fih (* TmxObject))))
(player (* TmxObject))
(player-first-tile-gid int)))
(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)
(= (. self ray-cam zoom) 2.0f)
;; More = faster. 4.0f will be smooth, 12.0f fast
(= (. self follow-speed) 8.0f))
@@ -59,9 +61,9 @@
(fn update ((self (* camera)) (state (* game-state))) void
(if (|| (== (. state map) NULL)
(== (. state fih) NULL))
(== (. state player) NULL))
(return))
(var target _ (fih-center state))
(var target _ (player-center state))
(var alpha _ (* (. self follow-speed) (GetFrameTime)))
;; Rubber band for camera
(clamp alpha alpha 1.0f)
@@ -70,10 +72,10 @@
(+= (. self ray-cam target y)
(* (- (. target y) (. self ray-cam target y)) alpha))))
(fn fih-center ((state (* game-state))) Vector2
(fn player-center ((state (* game-state))) Vector2
(return #(Vector2 :
(+ (. state fih x) (/ (. state fih width) 2.0f))
(+ (. state fih y) (/ (. state fih height) 2.0f)))))
(+ (. state player x) (/ (. state player width) 2.0f))
(+ (. state player y) (/ (. state player height) 2.0f)))))
(pub fn mk-game ((app-state (* app-state))) page
(var state _ (mem-alloc game-state))
@@ -91,8 +93,8 @@
;; 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)
(var object-count int (tmx-object-count map "Objects"))
(TraceLog LOG-INFO "Objects has %d object(s)" object-count)
(if (== object-count 0)
(do
@@ -102,21 +104,22 @@
(/ (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 player-x float (tmx-object-x map "Objects" 0))
(var player-y float (tmx-object-y map "Objects" 0))
(var player-width float (tmx-object-width map "Objects" 0))
(var player-height float (tmx-object-height map "Objects" 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)))
(var player (* TmxObject)
(tmx-find-object-by-name (. state map handle) "Objects" "Player"))
(= (. state player) player)
(= (. state player-first-tile-gid) (. player gid))
(= (. state player x) (clamp-to-grid (. state player x)))
(= (. state player y) (clamp-to-grid (. state player 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)
"Objects[0]: \"%s\" type \"%s\" at %.1f, %.1f (%.1f x %.1f)"
(tmx-object-name map "Objects" 0)
(tmx-object-type map "Objects" 0)
player-x player-y player-width player-height)
(.init (& (. state cam)))
@@ -139,27 +142,41 @@
(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)))
(draw-tmx-layer (. state map) "Objects" (& (. 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
(fn move-player ((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))
;; Now the somewhat fragile part
;; Ekko tileset goes: front (down), left, back (up), right,
;; so we just set the correct gid, counting from the start.
;; If the tileset changes, this breaks
(if (== dy 1)
(= (. state player gid) (. state player-first-tile-gid)))
(if (== dx -1)
(= (. state player gid) (+ 1 (. state player-first-tile-gid))))
(if (== dy -1)
(= (. state player gid) (+ 2 (. state player-first-tile-gid))))
(if (== dx 1)
(= (. state player gid) (+ 3 (. state player-first-tile-gid))))
;; Rotate in any case, but move only if can
(if (! (tmx-is-tile-walkable (. state map) "Background"
(+ (* dx TILE-SIZE) (. state fih x))
(+ (* dy TILE-SIZE) (. state fih y))))
(+ (* dx TILE-SIZE) (. state player x))
(+ (* dy TILE-SIZE) (. state player y))))
(return))
(+= (. state fih x) (* dx TILE-SIZE))
(+= (. state fih y) (* dy TILE-SIZE)))
(+= (. state player x) (* dx TILE-SIZE))
(+= (. state player y) (* dy TILE-SIZE)))
(pub fn update-game ((state (* game-state))) void
(if (== (. state map) NULL)
@@ -168,13 +185,13 @@
(var speed float (* 300.0 (GetFrameTime)))
(if (|| (IsKeyPressed KEY-LEFT) (IsKeyPressed KEY-A))
(move-fih state -1 0))
(move-player state -1 0))
(if (|| (IsKeyPressed KEY-RIGHT) (IsKeyPressed KEY-D))
(move-fih state 1 0))
(move-player state 1 0))
(if (|| (IsKeyPressed KEY-UP) (IsKeyPressed KEY-W))
(move-fih state 0 -1))
(move-player state 0 -1))
(if (|| (IsKeyPressed KEY-DOWN) (IsKeyPressed KEY-S))
(move-fih state 0 1))
(move-player state 0 1))
(var wheel float (GetMouseWheelMove))
(if (!= wheel 0.0)

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.12.2" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="4">
<map version="1.10" tiledversion="1.11.2" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="32" tileheight="32" infinite="0" nextlayerid="4" nextobjectid="7">
<tileset firstgid="1" source="WATAH 2.tsx"/>
<tileset firstgid="257" source="FIH 3.tsx"/>
<tileset firstgid="257" source="ekko.tsx"/>
<layer id="1" name="Background" width="30" height="20">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
@@ -26,7 +26,8 @@
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
<objectgroup color="#ffffff" id="3" name="FIH">
<object id="2" name="Player" gid="257" x="194.52" y="95.0905" width="16" height="16"/>
<objectgroup color="#ffffff" id="3" name="Objects">
<object id="5" name="Player" gid="257" x="321.16" y="161.145" width="32" height="32"/>
<object id="6" name="Spot 1" x="701.785" y="348.293" width="73.6441" height="37.2552"/>
</objectgroup>
</map>

View File

@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.12.2" name="path_tiles_rounded" tilewidth="16" tileheight="16" tilecount="256" columns="16">
<image source="path_tiles_rounded.png" width="256" height="256"/>
<tileset version="1.10" tiledversion="1.11.2" name="path_tiles_rounded" tilewidth="32" tileheight="32" tilecount="1024" columns="16">
<grid orientation="orthogonal" width="16" height="16"/>
<image source="path_tiles_rounded.png" width="512" height="512"/>
</tileset>

BIN
res/tilemaps/ekko.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

4
res/tilemaps/ekko.tsx Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.11.2" name="ekko" tilewidth="32" tileheight="32" tilecount="4" columns="4">
<image source="ekko.png" width="128" height="32"/>
</tileset>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 7.0 KiB