Overview
The Food Catch main script (main.gd) manages gameplay for a minigame where players catch falling food items while avoiding trash. It features a lives system, dynamic difficulty scaling, touch controls, and visual effects.
Script Path: minigames/food_catch/main.gd
Extends: Node2D
Exported Variables
Item Scenes
Scene resource for edible food items that increase score when caught.
Scene resource for trash items that decrease lives when caught.
Scene resource for special bonus items that award extra points.
Scene resource for floating text effects displayed when items are caught.
Spawn Configuration
Base time interval between spawns in seconds. Decreases as difficulty increases.
Probability (0-1) of spawning a bonus item instead of regular food/trash.
Probability (0-1) of spawning trash when not spawning bonus.
Difficulty Scaling
Time in seconds between difficulty increases.
Factor by which falling speed increases with each difficulty stage.
Factor by which spawn interval decreases with each difficulty stage (makes spawning faster).
Node References
UI Nodes
Touch Controls
Game Objects
Audio
Particle Effects
State Variables
Current player score. Increases by 10 for food, varies for bonuses.
Remaining lives. Game ends when reaches 0.
Current difficulty level. Increments periodically to increase challenge.
Whether the game is currently paused.
Coins earned in current game session for display on game over.
Core Functions
Initialization
Sets up the game environment.Key Actions:
- Connects touch input handlers
- Configures UI follow viewport
- Sets up button connections
- Initializes spawn timer (wait_time = spawn_every)
- Resets global speed multiplier for FallingItem class
- Shows intro panel and disables player until game starts
Spawning System
Spawns a random falling item based on probability distribution.Spawn Logic:
- Roll random number (0-1)
- If < bonus_probability: spawn bonus
- Else if < (bonus_probability + trash_probability): spawn trash
- Else: spawn food
- Sets z_index to 20
- Connects “resolved” or “resolved_bonus” signal
- Adds to scene tree
Item Resolution
Called when food or trash item is caught or missed.Parameters:
is_trash(bool): True if item was trash, false if food
- Decreases lives by 1
- Plays bad sound with pitch variation (0.9-1.0)
- Flashes lives label red
- Flashes damage overlay
- Triggers player damage animation
- Increases score by 10
- Plays eat sound with pitch variation (1.0-1.15)
- Flashes score label green
- Shows “+10” floating text
- Triggers player catch animation
- Updates HUD
- Checks for game over (if lives <= 0)
Called when bonus item is caught.Parameters:
points(int): Points awarded by the bonus
- Increases score by points
- Plays bonus sound with pitch variation (1.05-1.2)
- Shows floating text with point value
- Triggers player catch animation
HUD Management
Updates score and lives display labels.Format:
- Score: “Puntos: ”
- Lives: “Vidas: ”
Flashes the lives label red to indicate damage taken.Animation: 0.06s fade to red, 0.22s fade back to original color
Flashes the score label green to indicate points gained.Animation: 0.06s fade to green (#60e55a), 0.22s fade back
Visual Effects
Flashes a red damage overlay using shader material.Animation:
- 0.08s: intensity 0.0 → 0.5
- 0.25s: intensity 0.5 → 0.0
Creates floating text near the player position.Parameters:
text(String): Text to display (e.g., “+10”, “+50”)color(Color): Text color
Audio
Plays audio with random pitch variation for variety.Parameters:
player(AudioStreamPlayer): The audio player to usepmin(float): Minimum pitch scale (default: 0.95)pmax(float): Maximum pitch scale (default: 1.05)
Difficulty System
Increases game difficulty periodically.Changes:
- Increments difficulty_stage by 1
- Multiplies falling speed:
FallingItem.global_speed_multiplier = pow(speed_multiplier, difficulty_stage) - Reduces spawn interval:
spawn_timer.wait_time *= spawn_multiplier - Shows “¡Más rápido!” message
Displays an animated difficulty message.Parameters:
text(String): Message to display
- Fade in + scale down (1.4 → 1.0) over 0.2s
- Hold for 1.0s
- Fade out over 0.4s
Game Over
Checks if lives have reached 0 and triggers game over sequence.Flow:
- Stop spawn timer
- Play player hide/damage animation
- Wait 0.8 seconds
- Save high score to ScoreManager (“food_catch” key)
- Calculate and award coins (score * 0.1)
- Show game over panel
Displays the game over panel with results.Actions:
- Sets panel title to “¡Fin del Juego!”
- Shows final score and coins earned
- Hides touch controls and back button
Pause System
Pauses all game activity.Pauses:
- Spawn timer and difficulty timer
- Player processing and animation
- All falling items and their animations
- All particle systems
Resumes game from paused state.Resumes:
- All timers
- Player processing and animation
- All falling items
- All particles (with custom speed scales for fish particles: 0.4)
Pauses or resumes all particle effects.Parameters:
pause(bool): True to pause, false to resume
Pauses or resumes all falling items in the scene.Parameters:
pause(bool): True to pause, false to resume
Touch Controls
Handles touch/mouse input for player movement.Parameters:
event(InputEvent): Input eventdir(float): Direction (-1 for left, 1 for right)
- On press: Sets player.touch_dir to dir
- On release: Sets player.touch_dir to 0.0
Navigation
Returns to main menu, resetting screen orientation to portrait.
Reloads the current scene to restart the game.
Toggles pause state.
ScoreManager Integration
"food_catch"
Coin Conversion: Final score × 0.1
Common Patterns
Global Speed Control
Falling items use a class-level static multiplier:Probability-Based Spawning
HUD Flash Animation
Label flashes provide immediate visual feedback:The game uses landscape orientation during play but switches to portrait when returning to menu.