opengame-engine
v0.1.0-beta.1
Published
WebAssembly bindings for the OpenGame 2D game engine
Downloads
169
Maintainers
Readme
opengame-engine
WebAssembly bindings for the OpenGame 2D game engine — build browser-based 2D games with a full ECS, physics, and rendering pipeline.
Installation
npm install opengame-engineQuick Start
import init, { Engine } from 'opengame-engine';
async function main() {
await init();
const engine = new Engine(800, 600);
engine.set_clear_color(0.1, 0.1, 0.15, 1.0);
engine.set_gravity(0, -400);
// Spawn a dynamic entity
const player = engine.spawn();
engine.add_transform(player, 100, 300);
engine.add_rigid_body(player, 'dynamic');
engine.add_collider_rect(player, 32, 32);
// Game loop
let lastTime = performance.now();
function loop() {
const now = performance.now();
const dt = (now - lastTime) / 1000;
lastTime = now;
engine.update(dt);
const pos = engine.get_position(player);
console.log(`Player: ${pos.x}, ${pos.y}`);
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
}
main();API
Engine
| Method | Description |
|--------|-------------|
| new(width, height) | Create engine with viewport dimensions |
| set_clear_color(r, g, b, a) | Set background color (0.0–1.0) |
| set_gravity(x, y) | Set gravity vector |
| set_camera_position(x, y) | Set camera world position |
| set_camera_zoom(zoom) | Set camera zoom level |
| update(dt) | Advance physics simulation by dt seconds |
| entity_count() | Get current entity count |
Entity Management
| Method | Description |
|--------|-------------|
| spawn() | Spawn a new entity, returns EntityHandle |
| despawn(handle) | Remove an entity |
| add_transform(handle, x, y) | Add position component |
| add_rigid_body(handle, type) | Add rigid body ("dynamic", "kinematic", "static") |
| add_collider_rect(handle, w, h) | Add rectangle collider |
| add_collider_circle(handle, r) | Add circle collider |
Physics
| Method | Description |
|--------|-------------|
| apply_force(handle, fx, fy) | Apply continuous force |
| apply_impulse(handle, ix, iy) | Apply instant impulse |
| set_velocity(handle, vx, vy) | Set velocity directly |
| get_position(handle) | Get entity position ({x, y}) |
| set_position(handle, x, y) | Set entity position |
Math Utilities
| Function | Description |
|----------|-------------|
| lerp(a, b, t) | Linear interpolation |
| clamp(value, min, max) | Clamp value to range |
| smoothstep(edge0, edge1, x) | Hermite interpolation |
| deg_to_rad(degrees) | Degrees to radians |
| rad_to_deg(radians) | Radians to degrees |
| version() | Engine version string |
License
MIT
