add fhishing minigame base

(can't win or lose yet)
This commit is contained in:
2026-11-08 20:05:41 +03:00
parent 2e1aa1bb71
commit 360d9b0fbe
2 changed files with 120 additions and 10 deletions

128
game.sex
View File

@@ -13,6 +13,19 @@
#+emscripten (define WIN-WIDTH 960)
#+emscripten (define WIN-HEIGHT 600)
(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)
;; 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-g float 6.8f)
(var bobber-mass float 1.0f)
;; 1.0f is fallback default
(var TILE-SIZE float 1.0f)
@@ -35,7 +48,11 @@
(fishing-spots (* TmxObjectGroup))
(near-fishing-spot bool)
(fishing-minigame-active bool)))
(fishing-minigame-active bool)
;; Bobber height above the bottom of the popup and its vertical velocity
(bobber-y float)
(bobber-vy float)))
(ops camera
(fn init ((self (* camera))) void
@@ -81,6 +98,13 @@
(+ (. 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)))
(pub fn mk-game ((app-state (* app-state))) page
(var state _ (mem-alloc game-state))
(= (. state app-state) app-state)
@@ -121,7 +145,10 @@
(= (. 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)
(.init (& (. state cam)))
(return (page/game state)))
@@ -131,6 +158,44 @@
(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 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))
(pub fn render-game ((state (* game-state))) void
(BeginDrawing)
(ClearBackground BLACK)
@@ -146,11 +211,12 @@
(draw-tmx-layer (. state map) "objects" (& (. state cam ray-cam)))
(EndMode2D)
(DrawText "WASD/arrows: move wheel: zoom ESC: back to menu"
(DrawText "WASD/arrows: move X near fishing spot: fish ESC: back to menu"
10 10 20 RAYWHITE)
;; Show the interaction key above the player when near a fishing spot
(if (. state near-fishing-spot)
(if (&& (. state near-fishing-spot)
(! (. state fishing-minigame-active)))
(do
(var player-pos Vector2 (player-center state))
(var screen-pos Vector2
@@ -174,6 +240,9 @@
(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
@@ -205,10 +274,53 @@
(+= (. 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 (IsKeyPressed 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 popup Rectangle (fishing-popup-rect))
(var min-y float 0.0f)
(var max-y float
(- (. popup height)
(* 4.0f FISHING-POPUP-PADDING)
BOBBER-HEIGHT))
(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))))
;; ESC closes the popup instead of leaving the game page
(if (IsKeyReleased KEY-ESCAPE)
(do
(= (. state fishing-minigame-active) false)
(= (. state bobber-y) 0.0f)
(= (. state bobber-vy) 0.0f))))
(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))
@@ -220,7 +332,7 @@
(if (|| (IsKeyPressed KEY-DOWN) (IsKeyPressed KEY-S))
(move-player state 0 1))
;; TODO: we probably don't need zoom at all
;; TODO: we probably don't need zoom at all?
;; (var wheel float (GetMouseWheelMove))
;; (if (!= wheel 0.0)
;; (do
@@ -243,8 +355,6 @@
(if (IsKeyReleased KEY-X)
(do
(= (. state fishing-minigame-active) true)
(TraceLog LOG-INFO "Trying to activate minigame near fishing spot!")
;; TODO: show fishing minigame popup: indicator that rises
;; and falls and we need to click X to keep indicator in
;; the fish zone that moves randomly
)))))
(= (. state bobber-y) 0.0f)
(= (. state bobber-vy) 0.0f)
(TraceLog LOG-INFO "Starting fishing minigame!"))))))

View File

@@ -71,7 +71,7 @@
(call-and-check IsAudioDeviceReady "failed to init audio" 2
InitAudioDevice)
(SetExitKey -1)
(GuiSetStyle DEFAULT TEXT-SIZE 40)
(GuiSetStyle DEFAULT TEXT-SIZE 30)
(SetTargetFPS 60))
(fn deinit () void