Add wasm stuff

This commit is contained in:
Daniel Flanagan 2021-11-12 12:04:27 -06:00
parent e23ede279f
commit b9ac77f1aa
5 changed files with 38 additions and 5 deletions

3
Cargo.lock generated
View file

@ -563,8 +563,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"js-sys",
"libc", "libc",
"wasi", "wasi",
"wasm-bindgen",
] ]
[[package]] [[package]]
@ -1267,6 +1269,7 @@ name = "rust-game-1"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bracket-lib", "bracket-lib",
"getrandom",
] ]
[[package]] [[package]]

View file

@ -7,3 +7,4 @@ edition = "2021"
[dependencies] [dependencies]
bracket-lib = "0.8.1" # Meta-crate holding the entirety of bracket-lib (and exposing it). Use this for the full r... bracket-lib = "0.8.1" # Meta-crate holding the entirety of bracket-lib (and exposing it). Use this for the full r...
getrandom = { version = "0.2", features = ["js"] }

11
makefile Normal file
View file

@ -0,0 +1,11 @@
build:
cargo build
build-release:
cargo build --release
build-wasm:
cargo build --release --target wasm32-unknown-unknown
wasm-bindgen target/wasm32-unknown-unknown/release/rust-game-1.wasm --out-dir target/rust-flappy-dragon --no-modules --no-typescript
cp src/index.html target/rust-flappy-dragon
upload target/rust-flappy-dragon

18
src/index.html Normal file
View file

@ -0,0 +1,18 @@
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
</head>
<body>
<canvas id="canvas" width="640" height="480"></canvas>
<script src="./rust-game-1.js"></script>
<script>
window.addEventListener("load", async () => {
await wasm_bindgen("./rust-game-1_bg.wasm");
});
</script>
<style>
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
body, html { background-color: #111; display: flex; height: 100vh; width: 100vw; justify-content: center; align-items: center; }
</style>
</body>
</html>

View file

@ -6,7 +6,7 @@ use std::collections::VecDeque;
const SCREEN_WIDTH: u16 = 80; const SCREEN_WIDTH: u16 = 80;
const SCREEN_HEIGHT: u16 = 50; const SCREEN_HEIGHT: u16 = 50;
const FRAME_DURATION: f32 = 20.0; const FRAME_DURATION: f32 = 35.0;
const X_OFFSET: i32 = 10; const X_OFFSET: i32 = 10;
const Y_START: i32 = 25; const Y_START: i32 = 25;
@ -139,10 +139,10 @@ impl State {
self.time += ctx.frame_time_ms; self.time += ctx.frame_time_ms;
self.obstacleSpawnTime -= ctx.frame_time_ms; self.obstacleSpawnTime -= ctx.frame_time_ms;
ctx.print(1, 1, "Press Space to flag!"); ctx.print(1, 1, "Press Space to flap!");
ctx.print(1, 2, &format!("Score: {}", self.score)); ctx.print(1, 2, &format!("Score: {}", self.score));
ctx.print(1, 3, &format!("Pos: {}, {}", self.player.x, self.player.y)); // ctx.print(1, 3, &format!("Pos: {}, {}", self.player.x, self.player.y));
ctx.print(1, 4, &format!("Obstables: (Spawn: {}) #{} {:#?}", self.obstacleSpawnTime, self.obstacles.len(), self.obstacles)); // ctx.print(1, 4, &format!("Obstables: (Spawn: {}) #{} {:#?}", self.obstacleSpawnTime, self.obstacles.len(), self.obstacles));
// update // update
if self.time > FRAME_DURATION { if self.time > FRAME_DURATION {
@ -152,7 +152,7 @@ impl State {
if self.obstacleSpawnTime < 0.0 { if self.obstacleSpawnTime < 0.0 {
let mut random = RandomNumberGenerator::new(); let mut random = RandomNumberGenerator::new();
self.obstacleSpawnTime += random.range(400.0, 1200.0); self.obstacleSpawnTime += random.range(400.0, 1200.0) - self.score as f32;
let o = Obstacle::new(self.player.x + SCREEN_WIDTH as i32, self.score); let o = Obstacle::new(self.player.x + SCREEN_WIDTH as i32, self.score);
self.obstacles.push_back(o); self.obstacles.push_back(o);
} }