bevy_hourglass

Crates.iobevy_hourglass
lib.rsbevy_hourglass
version0.3.1
created_at2025-05-17 16:42:30.475094+00
updated_at2025-08-21 23:15:09.168732+00
descriptionA flexible hourglass plugin for Bevy applications
homepage
repositoryhttps://github.com/edouardpoitras/bevy_hourglass
max_upload_size
id1677988
size3,770,277
Edouard Poitras (edouardpoitras)

documentation

README

Bevy Hourglass

Bevy Hourglass Latest version Documentation MIT Apache

A flexible hourglass plugin for Bevy applications.

Website: https://edouardpoitras.github.io/bevy_hourglass/

animation

Features

  • Customizable visual hourglass timer with detailed mesh geometry
  • Multiple hourglass styles: straight-sided, curved bulbs, various neck styles
  • Flexible builder pattern for easy configuration
  • Auto-flip functionality for continuous animation
  • Configurable flip animations with custom durations
  • Events for state changes (flipping, emptying)
  • WebAssembly (WASM) support

Examples

Basic 2D Mesh Hourglass

Run the basic 2D mesh hourglass example:

cargo run --example 2d_mesh_hourglass

Auto-Flip Mayhem

See multiple hourglasses with random configurations and auto-flipping:

cargo run --example auto_flip_mayhem

Interactive Flip Demo

Control hourglass flipping with keyboard input:

cargo run --example flip_demo

Curve Styles Demo

Explore different hourglass shapes and styles:

cargo run --example curve_styles_demo

WebAssembly Examples

This project includes WebAssembly support, allowing you to run hourglasses in a web browser.

Building for WASM

  1. Make the build script executable (if not already):

    chmod +x build_wasm.sh
    
  2. Run the build script:

    ./build_wasm.sh
    

This script will:

  • Install the necessary tools (wasm-bindgen-cli) if not already installed
  • Add the wasm32-unknown-unknown target if needed
  • Build examples for the WASM target (currently includes 2d_mesh_hourglass and auto_flip_mayhem)
  • Generate JavaScript bindings

Running the WASM Examples

After building, you can serve the WASM files with a local HTTP server:

Using Python's built-in HTTP server:

cd wasm && python -m http.server 8080

Then open http://localhost:8080 in your web browser to access the available examples.

Usage

use bevy::prelude::*;
use bevy_hourglass::{
    BulbStyle, HourglassMeshBodyConfig, HourglassMeshBuilder, HourglassMeshPlatesConfig,
    HourglassMeshSandConfig, HourglassPlugin, NeckStyle,
};
use std::time::Duration;

fn main() {
    App::new()
        .add_plugins((DefaultPlugins, HourglassPlugin))
        .add_systems(Startup, setup)
        .run();
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    commands.spawn(Camera2d::default());

    // Create a 2D mesh hourglass with detailed geometry using the new builder pattern
    HourglassMeshBuilder::new(Transform::from_xyz(0.0, 0.0, 0.0))
        .with_body(HourglassMeshBodyConfig {
            total_height: 200.0,
            bulb_style: BulbStyle::Circular {
                curvature: 1.0,
                width_factor: 0.75,
                curve_resolution: 20,
            },
            neck_style: NeckStyle::Curved {
                curvature: 0.2,
                width: 12.0,
                height: 8.0,
                curve_resolution: 5,
            },
            color: Color::srgba(0.85, 0.95, 1.0, 0.2),
        })
        .with_plates(HourglassMeshPlatesConfig {
            width: 165.0,
            height: 10.0,
            color: Color::srgb(0.6, 0.4, 0.2),
        })
        .with_sand(HourglassMeshSandConfig {
            color: Color::srgb(0.9, 0.8, 0.6),
            fill_percent: 1.0,  // Start with full top bulb
            wall_offset: 8.0,   // Distance from glass walls
        })
        .with_timing(Duration::from_secs(30)) // 30-second timer for automatic animation
        .with_auto_flip(true)                 // Enable auto-flipping when empty
        .with_flip_duration(0.5)              // 0.5 second flip animation
        .build(&mut commands, &mut meshes, &mut materials);
}

Bevy Compatibility

bevy bevy_hourglass
0.16 0.3

License

MIT OR Apache-2.0

Commit count: 60

cargo fmt