(include raylib.h) (include raygui.h) (pub import pages) (import main) (import utils) (import audio) (pub import tmx) (define MAP-FILE "res/tilemaps/SaveAss.tmx") #-emscripten (define WIN-WIDTH 1440) #-emscripten (define WIN-HEIGHT 900) #+emscripten (define WIN-WIDTH 1440) #+emscripten (define WIN-HEIGHT 900) (define FISHING-POPUP-WIDTH 480.0f) (define FISHING-POPUP-HEIGHT 560.0f) (define FISHING-POPUP-PADDING 32.0f) (define BOBBER-WIDTH 28.0f) (define BOBBER-HEIGHT 36.0f) (define FISH-GAP 20.0f) (define FISH-SPEED 0.75f) ;; Physics is expressed in abstract units; this maps them to popup pixels (define BOBBER-SCALE 32.0f) (var bobber-impulse float 1.0f) (var bobber-g float 6.8f) (var bobber-mass float 1.0f) ;; 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 player-center ((state (* game-state))) Vector2) (pub record game-state ((app-state (* app-state)) (map (* tmx-map)) (cam camera) (player (* TmxObject)) (player-first-tile-gid int) (fishing-spots (* TmxObjectGroup)) (near-fishing-spot bool) (fishing-minigame-active bool) ;; Bobber height above the bottom of the popup and its vertical velocity (bobber-y float) (bobber-vy float) (catch-progress float) (fish-y float) (fish-vy float) (fish-next-y float) (fish-move-cooldown float) ;; Caught fish counter (caught-fish int) (fish-texture Texture2D))) (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 target x) 0.0f) (= (. self ray-cam target y) 0.0f) (= (. self ray-cam rotation) 0.0f) (= (. self ray-cam zoom) 2.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 player) NULL)) (return)) (var target _ (player-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 player-center ((state (* game-state))) Vector2 (return #(Vector2 : (+ (. state player x) (/ (. state player width) 2.0f)) (+ (. state player y) (/ (. state player height) 2.0f))))) (fn fishing-popup-rect () Rectangle (return #(Rectangle : (/ (- WIN-WIDTH FISHING-POPUP-WIDTH) 2.0f) (/ (- WIN-HEIGHT FISHING-POPUP-HEIGHT) 2.0f) FISHING-POPUP-WIDTH FISHING-POPUP-HEIGHT))) (fn fishing-bobber-range () float (return (- FISHING-POPUP-HEIGHT (* 4.0f FISHING-POPUP-PADDING) BOBBER-HEIGHT))) (fn random-float ((lo float) (hi float)) float (return (+ lo (* (- hi lo) (/ (cast (GetRandomValue 0 10000) float) 10000.0f))))) (fn pick-fish-target ((state (* game-state))) void (= (. state fish-next-y) (random-float 0.0f 1.0f)) (= (. state fish-move-cooldown) (random-float 0.5f 2.0f))) (pub fn mk-game ((app-state (* app-state))) page (var state _ (mem-alloc game-state)) (= (. state app-state) app-state) (= (. state fish-texture) (LoadTexture "res/img/small-fish.png")) (audio-play-game-music) (var map (* tmx-map) (load-tmx MAP-FILE)) (= (. state map) map) (if (== map NULL) (do (= (. 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) (= TILE-SIZE (. map tile-width)) (var object-count int (tmx-object-count map "objects")) (TraceLog LOG-INFO "Objects has %d object(s)" object-count) (if (== object-count 0) (do (= (. state cam ray-cam target x) (/ (cast (tmx-pixel-width map) float) 2.0)) (= (. state cam ray-cam target y) (/ (cast (tmx-pixel-height map) float) 2.0)) (return (page/game state)))) (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 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))) (= (. state fishing-spots) (tmx-object-group (. map handle) "fishing_spots")) (= (. state near-fishing-spot) false) (= (. state fishing-minigame-active) false) (= (. state bobber-y) 0.0f) (= (. state bobber-vy) 0.0f) (= (. state catch-progress) 0.5f) (= (. state fish-y) 0.5f) (= (. state fish-vy) 0.0f) (= (. state fish-next-y) 0.5f) (= (. state fish-move-cooldown) 0.0f) (= (. state caught-fish) 0) (.init (& (. state cam))) (return (page/game state))) (pub fn rm-game ((state (* game-state))) void (unload-tmx (. state map)) (if (IsTextureValid (. state fish-texture)) (UnloadTexture (. state fish-texture))) (free state) (return)) (fn render-fishing-minigame ((state (* game-state))) void (var popup Rectangle (fishing-popup-rect)) ;; Dim the game behind the popup (DrawRectangle 0 0 WIN-WIDTH WIN-HEIGHT (Fade BLACK 0.5f)) (GuiPanel popup "Fishing") (GuiLabel #(Rectangle : (+ (. popup x) FISHING-POPUP-PADDING) (+ (. popup y) 40.0f) (- (. popup width) (* 2.0f FISHING-POPUP-PADDING)) 40.0f) "X: pull! ESC: leave...") ;; The golden rectangle is the bobber, hanging at the bottom of the popup (var bobber-bottom float (- (+ (. popup y) (. popup height)) FISHING-POPUP-PADDING)) (var progress-bar-length int (- (. popup width) (* 2 FISHING-POPUP-PADDING))) (var progress-bar-bg _ #(Rectangle : (+ (. popup x) -1 FISHING-POPUP-PADDING) (+ (. popup y) -1 -10 (* 3 FISHING-POPUP-PADDING)) (+ progress-bar-length 2) 8)) (DrawRectangleLinesEx progress-bar-bg 1.0f DARKBROWN) (var progress-bar _ #(Rectangle : (+ (. popup x) FISHING-POPUP-PADDING) (+ (. popup y) -10 (* 3 FISHING-POPUP-PADDING)) (* progress-bar-length (. state catch-progress)) 6)) (DrawRectangleRec progress-bar BLUE) (var bobber-bg _ #(Rectangle : (+ (. popup x) -1.0f (/ (- (. popup width) BOBBER-WIDTH) 2.0f)) (+ (. popup y) -1.0f (* 3.0f FISHING-POPUP-PADDING)) (+ 2.0f BOBBER-WIDTH) (- (+ (. popup height) 2.0f) (* 4.0f FISHING-POPUP-PADDING)))) (DrawRectangleLinesEx bobber-bg 2.0f DARKBROWN) (var bobber-rect _ #(Rectangle : (+ (. popup x) (/ (- (. popup width) BOBBER-WIDTH) 2.0f)) (- bobber-bottom BOBBER-HEIGHT (. state bobber-y)) BOBBER-WIDTH BOBBER-HEIGHT)) (DrawRectangleRec bobber-rect GOLD) (DrawRectangleLinesEx bobber-rect 2.0f DARKBROWN) ;; Fish sprite sits beside the bobber and moves over the same range (if (IsTextureValid (. state fish-texture)) (do (var fish-range float (fishing-bobber-range)) (var fish-width float (cast (. state fish-texture width) float)) (var fish-height float (cast (. state fish-texture height) float)) (var fish-rect _ #(Rectangle : (+ (. popup x) (/ (- (. popup width) BOBBER-WIDTH) 2.0f) BOBBER-WIDTH FISH-GAP) (- bobber-bottom (* (. state fish-y) fish-range) fish-height) fish-width fish-height)) (DrawTexture (. state fish-texture) (cast (. fish-rect x) int) (cast (. fish-rect y) int) WHITE)))) (pub fn render-game ((state (* game-state))) void (BeginDrawing) (ClearBackground BLACK) (if (== (. state map) NULL) (do (DrawText "Failed to load map" 20 20 20 RED) (return))) (BeginMode2D (. state cam ray-cam)) (animate-tmx (. state map)) (draw-tmx-layer (. state map) "bg" (& (. state cam ray-cam))) (draw-tmx-layer (. state map) "objects" (& (. state cam ray-cam))) (EndMode2D) (DrawText (TextFormat "WASD/arrows: move X near fishing spot: fish ESC: back to menu Fish caught: %d" (. state caught-fish)) 10 10 20 RAYWHITE) ;; Show the interaction key above the player when near a fishing spot (if (&& (. state near-fishing-spot) (! (. state fishing-minigame-active))) (do (var player-pos Vector2 (player-center state)) (var screen-pos Vector2 (GetWorldToScreen2D #(Vector2 : (+ (. player-pos x) (* TILE-SIZE 0.5f)) (- (. player-pos y) (* TILE-SIZE 1.5f))) (. state cam ray-cam))) (var label-size float (cast (GuiGetStyle DEFAULT TEXT-SIZE) float)) (var old-align int (GuiGetStyle LABEL TEXT-ALIGNMENT)) (var old-color int (GuiGetStyle LABEL TEXT-COLOR-NORMAL)) (GuiSetStyle LABEL TEXT-ALIGNMENT TEXT-ALIGN-CENTER) (GuiSetStyle LABEL TEXT-COLOR-NORMAL (cast 0xffffffff i32)) (GuiLabel #(Rectangle : (- (. screen-pos x) (/ label-size 2.0f)) (- (. screen-pos y) (/ label-size 2.0f)) label-size label-size) "X") (GuiSetStyle LABEL TEXT-ALIGNMENT old-align) (GuiSetStyle LABEL TEXT-COLOR-NORMAL old-color))) (if (. state fishing-minigame-active) (render-fishing-minigame state)) (EndDrawing)) (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) "bg" (+ (* dx TILE-SIZE) (. state player x)) (+ (* dy TILE-SIZE) (. state player y)))) (return)) (+= (. state player x) (* dx TILE-SIZE)) (+= (. state player y) (* dy TILE-SIZE))) (fn update-fishing ((state (* game-state))) void (var dt float (GetFrameTime)) (clamp dt 0.0f 0.05f) ;; X gives the bobber an upward impulse (if (IsKeyDown KEY-X) (+= (. state bobber-vy) (/ (* bobber-impulse BOBBER-SCALE) bobber-mass))) ;; Gravity pulls it back down (var acceleration float (* bobber-g BOBBER-SCALE)) (-= (. state bobber-vy) (* acceleration dt)) (+= (. state bobber-y) (* (. state bobber-vy) dt)) ;; Keep the bobber inside the confines (var min-y float 0.0f) (var max-y float (fishing-bobber-range)) (if (< (. state bobber-y) min-y) (do (= (. state bobber-y) min-y) (= (. state bobber-vy) 0.0f))) (if (> (. state bobber-y) max-y) (do (= (. state bobber-y) max-y) (if (> (. state bobber-vy) 0.0f) (= (. state bobber-vy) 0.0f)))) ;; Fish waits for its cooldown, then moves to the next target height (if (> (. state fish-move-cooldown) 0.0f) (-= (. state fish-move-cooldown) dt)) (if (<= (. state fish-move-cooldown) 0.0f) (do (var dy float (- (. state fish-next-y) (. state fish-y))) (if (&& (< dy 0.01f) (> dy -0.01f)) (do (= (. state fish-y) (. state fish-next-y)) (= (. state fish-vy) 0.0f) (pick-fish-target state)) (do (= (. state fish-vy) (if (> dy 0.0f) FISH-SPEED (- FISH-SPEED))) (+= (. state fish-y) (* (. state fish-vy) dt)) (if (> dy 0.0f) (if (>= (. state fish-y) (. state fish-next-y)) (do (= (. state fish-y) (. state fish-next-y)) (= (. state fish-vy) 0.0f))) (if (<= (. state fish-y) (. state fish-next-y)) (do (= (. state fish-y) (. state fish-next-y)) (= (. state fish-vy) 0.0f)))))))) ;; Fill the catch meter while the bobber and fish are at the same ;; height, otherwise let it drain. (var bobber-y-normalized float (if (> max-y 0.0f) (/ (. state bobber-y) max-y) 0.0f)) (var fish-height-diff float (- bobber-y-normalized (. state fish-y))) (if (&& (<= fish-height-diff 0.10f) (>= fish-height-diff -0.10f)) (+= (. state catch-progress) (* dt 0.2)) (-= (. state catch-progress) (* dt 0.05))) ;; ESC closes the popup instead of leaving the game page, ;; also popup closes when catch-progress becomes negative (if (|| (IsKeyReleased KEY-ESCAPE) (> (. state catch-progress) 1.0f) (< (. state catch-progress) 0.0f)) (do (= (. state fishing-minigame-active) false) (= (. state bobber-y) 0.0f) (= (. state bobber-vy) 0.0f) (= (. state fish-vy) 0.0f) (if (> (. state catch-progress) 1.0f) (do (audio-play-gotcha) (++ (. state caught-fish)))) (audio-play-game-music)))) (pub fn update-game ((state (* game-state))) void (if (== (. state map) NULL) (return)) ;; The fishing minigame owns input while its popup is open (if (. state fishing-minigame-active) (do (update-fishing state) (return))) (var speed float (* 300.0 (GetFrameTime))) (if (|| (IsKeyPressed KEY-LEFT) (IsKeyPressed KEY-A)) (move-player state -1 0)) (if (|| (IsKeyPressed KEY-RIGHT) (IsKeyPressed KEY-D)) (move-player state 1 0)) (if (|| (IsKeyPressed KEY-UP) (IsKeyPressed KEY-W)) (move-player state 0 -1)) (if (|| (IsKeyPressed KEY-DOWN) (IsKeyPressed KEY-S)) (move-player state 0 1)) ;; TODO: we probably don't need zoom at all? ;; (var wheel float (GetMouseWheelMove)) ;; (if (!= wheel 0.0) ;; (do ;; (+= (. 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)))) (if (IsKeyReleased KEY-ESCAPE) (.change-page (. state app-state) (sum-type page menu))) (.update (& (. state cam)) state) ;; If we're near the fishing spot, show the tip and arm the fishing game (var was-near bool (. state near-fishing-spot)) (= (. state near-fishing-spot) false) (if (!= NULL (tmx-object-at-point (. state fishing-spots) (player-center state))) (do (= (. state near-fishing-spot) true) (if (! was-near) (audio-play-fishing-spot)) (if (IsKeyReleased KEY-X) (do (= (. state fishing-minigame-active) true) (= (. state bobber-y) 0.0f) (= (. state bobber-vy) 0.0f) (= (. state catch-progress) 0.5f) ;; Fish always starts in the middle of the bobber range (= (. state fish-y) 0.5f) (= (. state fish-vy) 0.0f) (pick-fish-target state) (audio-play-fishing-start) (audio-play-fishing-music) (TraceLog LOG-INFO "Starting fishing minigame!"))))))