Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions .github/workflows/deploy-pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Deploy SpaceWar to GitHub Pages

on:
push:
branches: [master]
paths:
- 'spacewar-wasm/**'
workflow_dispatch:

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: pages
cancel-in-progress: true

jobs:
build:
name: Build WASM + package site
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Rust (stable + wasm32 target)
uses: dtolnay/rust-toolchain@stable
with:
targets: wasm32-unknown-unknown

- name: Cache Cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
key: ${{ runner.os }}-cargo-${{ hashFiles('spacewar-wasm/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-

- name: Install wasm-pack
run: cargo install wasm-pack --locked

- name: Build WebAssembly
working-directory: spacewar-wasm
run: wasm-pack build --target web --out-dir www/pkg

- name: Upload Pages artifact
uses: actions/upload-pages-artifact@v3
with:
path: spacewar-wasm/www

deploy:
name: Deploy to GitHub Pages
needs: build
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}

steps:
- name: Deploy
id: deployment
uses: actions/deploy-pages@v4
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# Rust build artifacts
spacewar-wasm/target/

# wasm-pack output (generated during CI/CD build; not stored in Git)
spacewar-wasm/www/pkg/
104 changes: 103 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,112 @@

![Gameplay](spacewar.gif)

### 🚀 A space odyssey written in Java!
### 🚀 A space odyssey — originally written in Java, now playable in the browser via Rust + WebAssembly!

---

<img src="java.svg" alt="Java" height=20px> <img src="beer.svg" alt="Beer">

</div>

---

## 🌐 Play Online

The game is deployed to **GitHub Pages** and is playable directly in your browser — no install required.

> **[▶ Play Space War](https://lassault.github.io/SpaceWar/)**

---

## 🎮 How to Play

| Action | Control |
|---|---|
| Move ship | Mouse (or touch-drag on mobile) |
| Fire | Spacebar (or tap on mobile) |
| Select ship | Keys **1 – 8** |

The ship upgrades automatically as your score increases every 250 points (up to level 8). Destroy enemies to earn points; survive with your 3 lives!

---

## 🗂️ Repository Structure

```
SpaceWar/
├── src/lassa/net/ # Original Java source (Swing/AWT desktop app)
│ └── imagenes/ # All game sprites (shared with web version)
├── spacewar-wasm/ # Rust + WebAssembly rewrite
│ ├── src/lib.rs # Complete game logic in Rust
│ ├── Cargo.toml
│ └── www/ # Web-deployable folder
│ ├── index.html # Game page (single file, no bundler needed)
│ └── assets/ # Sprites served as static files
└── .github/workflows/
└── deploy-pages.yml # CI/CD: builds WASM → deploys to GitHub Pages
```

---

## 🦀 Web Version — Rust + WebAssembly

### Why Rust + WebAssembly?

| Concern | Answer |
|---|---|
| **Performance** | Rust compiles to near-native speed WASM; no GC pauses |
| **Safety** | Memory-safe by default — no null-pointer crashes |
| **Portability** | Runs in any modern browser without plugins |
| **Portfolio fit** | Demonstrates both systems programming and web skills |

### Architecture

```
┌─────────────────────────────────────┐
│ Browser │
│ ┌─────────────┐ ┌───────────────┐ │
│ │ index.html │ │ spacewar_wasm │ │
│ │ (JS glue) │◄─│ .wasm (54 KB)│ │
│ │ │ │ │ │
│ │ • load imgs │ │ • game state │ │
│ │ • RAF loop │ │ • physics │ │
│ │ • events │ │ • collisions │ │
│ └──────┬──────┘ │ • rendering │ │
│ │ Canvas │ (web-sys) │ │
│ └────────►└───────────────┘ │
└─────────────────────────────────────┘
```

- **`SpaceWarGame::tick(timestamp)`** — called each animation frame; runs all physics, AI, and renders to the HTML5 Canvas.
- **`SpaceWarGame::on_key_down(key, code)`** — keyboard input forwarded from JS.
- **`SpaceWarGame::on_mouse_move(x, y)`** — mouse/touch position forwarded from JS.

### Building Locally

```bash
# Prerequisites: Rust stable, wasm-pack
rustup target add wasm32-unknown-unknown
cargo install wasm-pack

# Build
cd spacewar-wasm
wasm-pack build --target web --out-dir www/pkg

# Serve (any static file server works)
cd www
python3 -m http.server 8080
# Open http://localhost:8080
```

---

## ☕ Original Java Version

The original desktop game (`src/`) is a pure **Java Swing/AWT** application using multi-threading for movement, collision detection, enemy generation, and rendering.

```bash
# Run the pre-built JAR
java -jar SpaceWar.jar
```

5 changes: 5 additions & 0 deletions spacewar-wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Rust build artifacts
target/

# wasm-pack generated package (rebuilt on deploy)
www/pkg/
136 changes: 136 additions & 0 deletions spacewar-wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions spacewar-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "spacewar-wasm"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm-bindgen = "0.2"
js-sys = "0.3"

[dependencies.web-sys]
version = "0.3"
features = [
"CanvasRenderingContext2d",
"Document",
"Element",
"HtmlCanvasElement",
"HtmlImageElement",
"Window",
"console",
]

[profile.release]
opt-level = "s"
lto = true

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
Loading
Loading