Skip to content

Commit

Permalink
Merge pull request #28 from jjant/wasm-game
Browse files Browse the repository at this point in the history
WebAssembly support
  • Loading branch information
jjant committed Dec 4, 2022
2 parents d77f31a + 37c2a17 commit 425efff
Show file tree
Hide file tree
Showing 64 changed files with 1,548 additions and 865 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,43 @@ jobs:
- beta
- nightly
- 1.63.0 # Current rustc on my machine
target:
- x86_64-unknown-linux-gnu
- wasm32-unknown-unknown

steps:
- uses: actions/checkout@v2
name: Checkout source

- uses: actions-rs/toolchain@v1
name: Install toolchain
with:
profile: minimal
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
override: true
components: rustfmt, clippy

- uses: actions-rs/cargo@v1
name: Build
with:
command: build
args: --workspace ${{ matrix.target == 'wasm32-unknown-unknown' && '--exclude runty8-editor' || '' }}

- uses: actions-rs/cargo@v1
name: Test
with:
command: test
args: --workspace ${{ matrix.target == 'wasm32-unknown-unknown' && '--exclude runty8-editor' || '' }}

- uses: actions-rs/cargo@v1
name: Format
with:
command: fmt
args: --all -- --check

- uses: actions-rs/cargo@v1
name: Clippy
with:
command: clippy
args: -- -D warnings
args: --workspace ${{ matrix.target == 'wasm32-unknown-unknown' && '--exclude runty8-editor' || '' }} -- -D warnings -A unknown-lints
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
/target
Cargo.lock
/sprite_sheet.txt
/sprite_sheet.txt
/generated
21 changes: 10 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
[package]
name = "runty8"
version = "0.1.0"
edition = "2021"
default-run = "runty8"
readme = "README.md"

[dependencies]
glium = "*"
itertools = "*"
rand = "*"
[workspace]
members = [
"src/runty8",
"src/runty8-core",
"src/runty8-runtime",
"src/runty8-editor",
"src/runty8-event-loop",
"src/runty8-winit",
"examples",
]
23 changes: 23 additions & 0 deletions build_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

set -e

if [[ -z $1 ]]; then
package="standalone-game"
else
package="$1"
fi

echo "Building: $package"

rm -rf generated/*
cargo build --target wasm32-unknown-unknown -p "examples" --bin "$package" --release
wasm-bindgen target/wasm32-unknown-unknown/release/$package.wasm --out-dir generated --target web

cp index.html generated/index.html
placeholder="__PACKAGE_NAME__"
echo "Replacing placeholder: $placeholder -> $package"
sed -i.bkp "s/$placeholder/$package/g" generated/index.html

cd generated
serve
34 changes: 34 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "examples"
version = "0.1.0"
edition = "2021"

[dependencies]
runty8 = { path = "../src/runty8" }
runty8-core = { path = "../src/runty8-core" }
log = "0.4"
include_dir= "0.7.3"

[[bin]]
name = "celeste"
path = "./celeste/main.rs"

[[bin]]
name = "confetti"
path = "./confetti/main.rs"

[[bin]]
name = "moving-box"
path = "./moving-box/main.rs"

[[bin]]
name = "bresenham"
path = "./bresenham/main.rs"

[[bin]]
name = "stress-lines"
path = "./stress-lines/main.rs"

[[bin]]
name = "standalone-game"
path = "./standalone-game/main.rs"
8 changes: 5 additions & 3 deletions examples/bresenham/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use runty8::{self, App, Button, Pico8};
use runty8::{App, Button, Pico8};

fn main() {
runty8::run_app::<MyThing>("examples/bresenham".to_owned()).unwrap();
let resources = runty8::load_assets!("bresenham").unwrap();

runty8::debug_run::<MyThing>(resources).unwrap();
}

struct MyThing {
Expand Down Expand Up @@ -84,7 +86,7 @@ fn midpoint(cx: i32, cy: i32, r: i32) -> Vec<(i32, i32)> {
points.push((cx - y as i32, cy - x as i32));

x = (x.powi(2) - 2.0 * y - 1.0).sqrt();
y = y + 1.0;
y += 1.0;
}
points
}
38 changes: 18 additions & 20 deletions examples/celeste/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::f32::consts::{FRAC_1_SQRT_2, PI};

use rand::Rng;
use runty8::{App, Button, Pico8};
use runty8::{rnd, App, Button, Pico8};

use std::iter::{Chain, Map};
use std::slice;

fn main() {
runty8::run_app::<GameState>("examples/celeste".to_owned()).unwrap();
let resources = runty8::load_assets!("celeste").unwrap();

runty8::debug_run::<GameState>(resources).unwrap();
}

struct GameState {
Expand Down Expand Up @@ -465,10 +466,6 @@ struct Vec2<T> {
y: T,
}

fn rnd(max: f32) -> f32 {
rand::thread_rng().gen_range(0.0..max)
}

impl GameState {
fn begin_game(&mut self, pico8: &Pico8) {
self.frames = 0;
Expand Down Expand Up @@ -733,6 +730,7 @@ impl Player {
));
} else {
// -- wall jump
#[allow(clippy::bool_to_int_with_if)]
let wall_dir = if this.is_solid(state, objects, room, -3, 0) {
-1
} else if this.is_solid(state, objects, room, 3, 0) {
Expand Down Expand Up @@ -1159,7 +1157,7 @@ impl RoomTitle {
} else {
let level = (1 + level_index(room)) * 100;
let x = 52 + (if level < 1000 { 2 } else { 0 });
draw.print(&format!("{} M", level), x, 62, 7);
draw.print(&format!("{level} M"), x, 62, 7);
}

draw_time(seconds, minutes, draw, 4, 4);
Expand All @@ -1186,9 +1184,7 @@ struct Object {
}

fn got_fruit_for_room(got_fruit: &[bool], room: Vec2<i32>) -> bool {
*got_fruit
.get(1 + level_index(room) as usize)
.unwrap_or(&false)
*got_fruit.get(1 + level_index(room)).unwrap_or(&false)
}

impl Object {
Expand Down Expand Up @@ -1354,13 +1350,13 @@ impl Object {

// [x] get move amount
self.base_object.rem.x += ox;
let amount_x = (self.base_object.rem.x as f32 + 0.5).floor();
let amount_x = (self.base_object.rem.x + 0.5).floor();
self.base_object.rem.x -= amount_x;
self.move_x(state, objects, room, amount_x as i32, 0);

// [y] get move amount
self.base_object.rem.y += oy;
let amount_y = (self.base_object.rem.y as f32 + 0.5).floor();
let amount_y = (self.base_object.rem.y + 0.5).floor();
self.base_object.rem.y -= amount_y;
self.move_y(state, objects, room, amount_y as i32);
}
Expand Down Expand Up @@ -1748,21 +1744,21 @@ fn draw_time(seconds: i32, minutes: i32, draw: &mut Pico8, x: i32, y: i32) {
let h = minutes / 60;

let h_str = if h < 10 {
format!("0{}", h)
format!("0{h}")
} else {
h.to_string()
};
let m_str = if m < 10 {
format!("0{}", m)
format!("0{m}")
} else {
m.to_string()
};
let s_str = if s < 10 {
format!("0{}", s)
format!("0{s}")
} else {
s.to_string()
};
let time_str = format!("{}:{}:{}", h_str, m_str, s_str);
let time_str = format!("{h_str}:{m_str}:{s_str}");

draw.rectfill(x, y, x + 32, y + 6, 0);
draw.print(&time_str, x + 1, y + 1, 7);
Expand All @@ -1784,8 +1780,9 @@ fn appr(val: f32, target: f32, amount: f32) -> f32 {
}

fn maybe() -> bool {
rand::thread_rng().gen()
rnd(1.0) > 0.5
}

fn ice_at(state: &Pico8, room: Vec2<i32>, x: i32, y: i32, w: i32, h: i32) -> bool {
tile_flag_at(state, room, x, y, w, h, 4)
}
Expand Down Expand Up @@ -1935,7 +1932,7 @@ impl Fruit {
player.djump = max_djump;
// sfx_timer=20
// sfx(13)
got_fruit[level_index(room) as usize] = true;
got_fruit[level_index(room)] = true;

UpdateAction::noop().destroy().push(Object::init(
got_fruit,
Expand Down Expand Up @@ -2386,6 +2383,7 @@ fn horizontal_input(state: &Pico8) -> i32 {
}

fn vertical_input(state: &Pico8) -> i32 {
#[allow(clippy::bool_to_int_with_if)]
if state.btn(K_UP) {
-1
} else if state.btn(K_DOWN) {
Expand Down Expand Up @@ -3170,7 +3168,7 @@ impl Flag {
draw.spr(26, 55, 6);
draw.print(&format!("X{}", self.score), 64, 9, 7);
draw_time(seconds, minutes, draw, 49, 16);
draw.print(&format!("DEATHS:{}", deaths), 48, 24, 7);
draw.print(&format!("DEATHS:{deaths}"), 48, 24, 7);
} else if this.check(objects.into_iter(), &ObjectKind::Player, 0, 0) {
// sfx(55);
// sfx_timer = 30;
Expand Down
20 changes: 12 additions & 8 deletions examples/confetti/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rand::Rng;
use runty8::{App, Button, Pico8};

fn main() {
runty8::run_app::<Confetti>("examples/confetti".to_owned()).unwrap();
let resources = runty8::load_assets!("confetti").unwrap();
runty8::debug_run::<Confetti>(resources).unwrap();
}

struct Confetti {
Expand Down Expand Up @@ -80,11 +80,11 @@ struct Particle {

impl Particle {
fn new(x: f32, y: f32) -> Self {
let x = rand::thread_rng().gen_range(-2.0..2.0) + x;
let y = rand::thread_rng().gen_range(-2.0..2.0) + y;
let vx = rand::thread_rng().gen_range(-15.0..15.0) / 50.0;
let vy = rand::thread_rng().gen_range(-50.0..10.0) / 50.0;
let ttl = rand::thread_rng().gen_range(10..70);
let x = rand_between(-2.0, 2.0) + x;
let y = rand_between(-2.0, 2.0) + y;
let vx = rand_between(-15.0, 15.0) / 50.0;
let vy = rand_between(-50.0, 10.0) / 50.0;
let ttl = rand_between(10.0, 70.0) as i32;

Self {
x,
Expand All @@ -93,7 +93,7 @@ impl Particle {
vy,
ay: 0.05,
ttl,
color: rand::thread_rng().gen_range(1..16),
color: rand_between(1.0, 16.0) as u8,
}
}

Expand All @@ -108,3 +108,7 @@ impl Particle {
draw_context.pset(self.x as i32, self.y as i32, self.color);
}
}

fn rand_between(min: f32, max: f32) -> f32 {
min + runty8::rnd(max - min)
}
3 changes: 2 additions & 1 deletion examples/moving_box/main.rs → examples/moving-box/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use runty8::{App, Button, Pico8};

fn main() {
runty8::run_app::<ExampleApp>("examples/moving_box".to_owned()).unwrap();
let resources = runty8::load_assets!("moving-box").unwrap();
runty8::debug_run::<ExampleApp>(resources).unwrap();
}

pub struct ExampleApp {
Expand Down

0 comments on commit 425efff

Please sign in to comment.