Add wasm stuff
This commit is contained in:
parent
e23ede279f
commit
b9ac77f1aa
3
Cargo.lock
generated
3
Cargo.lock
generated
|
@ -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]]
|
||||||
|
|
|
@ -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
11
makefile
Normal 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
18
src/index.html
Normal 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>
|
10
src/main.rs
10
src/main.rs
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue