Skip to content

Rewrite SpaceWar in Rust + WebAssembly for browser play - #1

Merged
lassault merged 2 commits into
masterfrom
copilot/refactor-code-to-rust-webassembly
Apr 8, 2026
Merged

Rewrite SpaceWar in Rust + WebAssembly for browser play#1
lassault merged 2 commits into
masterfrom
copilot/refactor-code-to-rust-webassembly

Conversation

Copilot AI commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

The original Java/Swing desktop game is inaccessible on the web. This adds a complete Rust + WebAssembly port, deployable as a static site (GitHub Pages), preserving all original gameplay mechanics.

What's new

spacewar-wasm/src/lib.rs — Rust game engine

  • SpaceWarGame struct exported via wasm-bindgen; three public entry points: tick(ts), on_key_down(key, code), on_mouse_move(x, y)
  • Full game state machine: Menu → Playing → GameOver
  • All original mechanics faithfully ported: AABB collision detection, score-based ship level progression (every 250 pts, 8 tiers), 3-bullet spread at high levels, enemy AI spawning/shooting with difficulty scaling
  • Movement uses Δt normalization (same physics feel as the original Java thread-sleep timings)
  • Canvas 2D rendering via web-sysdraw_image / draw_image_scaled; all 100 sprites accessed via a flat indexed array matching the JS manifest
// JS drives the loop; Rust owns all state and renders to the canvas
pub fn tick(&mut self, timestamp: f64) { /* Δt update + render */ }
pub fn on_key_down(&mut self, key: String, key_code: u32) { /* ship select / fire */ }
pub fn on_mouse_move(&mut self, x: f64, y: f64) { /* ship follows cursor */ }

spacewar-wasm/www/index.html — thin JS shell

  • Loads all 100 sprites in parallel with a progress bar, then hands the HTMLImageElement[] array to Rust
  • requestAnimationFrame loop calls game.tick(ts); keyboard/mouse/touch events forwarded to Rust
  • Canvas coordinates scaled from CSS pixels back to the logical 1300×720 game space, so the layout is responsive

spacewar-wasm/www/assets/ — sprite tree

  • 113 PNGs copied from src/lassa/net/imagenes/ and organized without path spaces (removes the "nave 1/" naming issue from the Java classpath)

.github/workflows/deploy-pages.yml — CI/CD

  • Builds WASM with wasm-pack on every push to master touching spacewar-wasm/**, uploads www/ as a Pages artifact, and deploys

Updated README.md

  • Documents both versions, play controls, architecture diagram, and local build steps (wasm-pack build --target web --out-dir www/pkg)

Why Rust + WASM over a JS framework

Rust compiles to a 54 KB WASM binary with direct Canvas 2D access via web-sys — no framework overhead, no GC, and a stronger portfolio signal than a React/Next.js port.

Copilot AI changed the title [WIP] Refactor game code to Rust with WebAssembly Rewrite SpaceWar in Rust + WebAssembly for browser play Apr 7, 2026
Copilot AI requested a review from lassault April 7, 2026 21:54
@lassault
lassault marked this pull request as ready for review April 8, 2026 08:28
@lassault
lassault merged commit f316849 into master Apr 8, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants