#timer #bevy #gamedev #hourglass

bevy_hourglass

A flexible hourglass plugin for Bevy applications

1 unstable release

new 0.1.0 May 17, 2025

#626 in Game dev

MIT/Apache

44KB
333 lines

Bevy Hourglass

Bevy Hourglass Latest version Documentation MIT Apache

A flexible hourglass plugin for Bevy applications.

Features

  • Customizable visual hourglass timer
  • Sprite-based rendering
  • Configurable duration
  • Events for state changes (flipping, emptying)
  • WebAssembly (WASM) support

Examples

Simple Example

Run the simple example with:

cargo run --example simple

WebAssembly Example

This project includes WebAssembly support for the simple example, allowing you to run the hourglass 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 the example for the WASM target
  • Generate JavaScript bindings

Running the WASM Example

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.

Usage

Add the dependency to your Cargo.toml:

[dependencies]
bevy_hourglass = "0.2.0"

In your Bevy application:

use bevy::prelude::*;
use bevy_hourglass::*;
use std::time::Duration;

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

fn setup(mut commands: Commands) {
    // Spawn a camera
    commands.spawn(Camera2d::default());
    
    // Spawn a hourglass
    spawn_hourglass(
        &mut commands,
        Duration::from_secs(60),  // 60 second timer
        Vec2::ZERO,              // Position at center
        Vec2::new(100.0, 200.0), // Size
        Color::srgb(0.8, 0.8, 0.8), // Container color
        Color::srgb(0.9, 0.7, 0.2)  // Sand color
    );
}

License

MIT OR Apache-2.0

Dependencies

~18–27MB
~450K SLoC