implement ability to lose and win in fishing minigame

This commit is contained in:
2026-11-09 00:42:37 +03:00
parent 3e0f40c065
commit d9d06c978e
2 changed files with 149 additions and 12 deletions

View File

@@ -2,12 +2,16 @@
(define MENU-CLICK-FILE "res/music/01 - Menu.ogg")
(define FISHING-SPOT-FILE "res/music/02 - Fishing Spot.ogg")
(define FISHING-START-FILE "res/music/03 - Catch!.ogg")
(define GOTCHA-FILE "res/music/04 - Gotcha!.ogg")
(define MENU-MUSIC-FILE "res/music/Catch-Phase 01 - Fishing.ogg")
(define GAME-MUSIC-FILE "res/music/Catch-Phase 03 - Aqua World.ogg")
(define FISHING-MUSIC-FILE "res/music/Catch-Phase 05 - FIH.ogg")
(var menu-click Sound)
(var fishing-spot-sound Sound)
(var fishing-start-sound Sound)
(var gotcha-sound Sound)
(var menu-music Music)
(var game-music Music)
@@ -32,6 +36,8 @@
(pub fn audio-init () void
(= menu-click (load-sound MENU-CLICK-FILE))
(= fishing-spot-sound (load-sound FISHING-SPOT-FILE))
(= fishing-start-sound (load-sound FISHING-START-FILE))
(= gotcha-sound (load-sound GOTCHA-FILE))
(= menu-music (load-music MENU-MUSIC-FILE))
(= game-music (load-music GAME-MUSIC-FILE))
(= fishing-music (load-music FISHING-MUSIC-FILE)))
@@ -43,6 +49,8 @@
(UnloadSound menu-click)
(UnloadSound fishing-spot-sound)
(UnloadSound fishing-start-sound)
(UnloadSound gotcha-sound)
(UnloadMusicStream menu-music)
(UnloadMusicStream game-music)
(UnloadMusicStream fishing-music))
@@ -95,3 +103,11 @@
(pub fn audio-play-fishing-spot () void
(if (IsSoundValid fishing-spot-sound)
(PlaySound fishing-spot-sound)))
(pub fn audio-play-fishing-start () void
(if (IsSoundValid fishing-start-sound)
(PlaySound fishing-start-sound)))
(pub fn audio-play-gotcha () void
(if (IsSoundValid gotcha-sound)
(PlaySound gotcha-sound)))

145
game.sex
View File

@@ -19,11 +19,13 @@
(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 5.0f)
(var bobber-impulse float 1.0f)
(var bobber-g float 6.8f)
(var bobber-mass float 1.0f)
@@ -53,7 +55,17 @@
;; Bobber height above the bottom of the popup and its vertical velocity
(bobber-y float)
(bobber-vy 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
@@ -108,9 +120,24 @@
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))
@@ -154,11 +181,21 @@
(= (. 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))
@@ -180,6 +217,22 @@
(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)
@@ -198,7 +251,29 @@
BOBBER-WIDTH
BOBBER-HEIGHT))
(DrawRectangleRec bobber-rect GOLD)
(DrawRectangleLinesEx bobber-rect 2.0f DARKBROWN))
(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)
@@ -215,7 +290,7 @@
(draw-tmx-layer (. state map) "objects" (& (. state cam ray-cam)))
(EndMode2D)
(DrawText "WASD/arrows: move X near fishing spot: fish ESC: back to menu"
(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
@@ -283,7 +358,7 @@
(clamp dt 0.0f 0.05f)
;; X gives the bobber an upward impulse
(if (IsKeyPressed KEY-X)
(if (IsKeyDown KEY-X)
(+= (. state bobber-vy)
(/ (* bobber-impulse BOBBER-SCALE) bobber-mass)))
@@ -293,12 +368,8 @@
(+= (. state bobber-y) (* (. state bobber-vy) dt))
;; Keep the bobber inside the confines
(var popup Rectangle (fishing-popup-rect))
(var min-y float 0.0f)
(var max-y float
(- (. popup height)
(* 4.0f FISHING-POPUP-PADDING)
BOBBER-HEIGHT))
(var max-y float (fishing-bobber-range))
(if (< (. state bobber-y) min-y)
(do
(= (. state bobber-y) min-y)
@@ -309,12 +380,56 @@
(if (> (. state bobber-vy) 0.0f)
(= (. state bobber-vy) 0.0f))))
;; ESC closes the popup instead of leaving the game page
(if (IsKeyReleased KEY-ESCAPE)
;; 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
@@ -365,5 +480,11 @@
(= (. 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!"))))))