Crate wasm_rgame

Source
Expand description

§wasm-rgame

A Rust framework for building browser games with WASM.

This framework was inspired by the Game of Life Rust + WASM Tutorial. You can dive right in and start writing a wasm-rgame application with the [tutorial](TODO link tutorial)!

§How It Works

This framework abstracts away writing custom HTML/Javascript and provides Rust types that interface with the Javascript bundled in the wasm-rgame-js respository.

The Rust API provides types for:

  • Keyboard events
  • Mouse position and events
  • Rendering API to a 2D canvas
  • Spawning new objects in the Application

Also, a build tool (wasm-rgame-tools) is provided that builds the project (targeting wasm32-unknown-unknown), runs wasm-bindgen, and bundles together the generated WASM binaries and Javascript/HTML.

§Goals

This project is mainly experimental, but has been used to make non-trivial applications (see wrg-snake).

  • Users only need to write Rust.
  • Minimal calls between Rust / Javascript.

§Example API Usage

These examples are taken from wrg-snake. Also note that these examples can’t be run stand-alone as they require the Javascript/HTML framework.

Rendering a transparent overlay:

use wasm_rgame::{Delegate, Graphics, Canvas};

impl Delegate for MyObject {
    fn tick(..) {}

    fn render(&self, graphics: &mut Graphics) {
        let canvas = Canvas::instance();
        // draw a transparent overlay over the game
        graphics.draw_rect(
            0.0,
            0.0,
            canvas.width() as f32,
            canvas.height() as f32,
            [255, 255, 255, 150]
        );
    }
}

Checking keyboard input:

use wasm_rgame::{KeyManager, key_codes};

pub fn store_direction_change(&mut self, key_manager: &KeyManager) {
    // Don't let direction change if already going opposite direction
    let wanted_direction = if key_manager.key_down(key_codes::W) {
        Direction::Up
    } else if key_manager.key_down(key_codes::D) {
        Direction::Right
    } else if key_manager.key_down(key_codes::S) {
        Direction::Down
    } else if key_manager.key_down(key_codes::A) {
        Direction::Left
    } else {
        return;
    };

    ...
}

Modules§

delegate_prelude
The required types when implementing a delegate. You can glob import this to save time with: use wasm_rgame::delegate_prelude::*;
key_codes
A list of KeyCodes (taken from the internet), does not cover all keycodes. All KeyCodes reported from Javascript are tracked in KeyManager so you can look up KeyCodes not listed here and define your own constants if necessary.

Structs§

Application
ApplicationContext
Canvas
DelegateSpawner
Type used to spawn / spawn_root new delegates onto the Application
Graphics
KeyManager
The type managing the state of the keys. Use the key_code module for a list of KeyCodes to query for.
MouseState
The type that holds the state of the Mouse.
SpawnHandle
SpawnHandles
Helper type to compact holding onto multiple SpawnHandle<_>s with a single reference (at the cost of boxing). Given that spawning should be relatively not often, the allocation cost should be fine for the ergonomical trade-off.

Enums§

MouseButton

Traits§

Delegate
Trait for defining an interface for objects receiving updates from the application.
SpawnableDelegate
Trait for delegates that can be “spawned” on an DelegateManager. This trait defines the method of communciation with the spawned object as ownership is passed to the DelegateManager.

Type Aliases§

Color