initial commit - shuttle template

This commit is contained in:
Ponj 2024-11-14 19:27:48 -05:00
commit 9cf667a145
Signed by: p6nj
GPG key ID: 6FED68D87C479A59
12 changed files with 5936 additions and 0 deletions

7
.gitignore vendored Normal file
View file

@ -0,0 +1,7 @@
target/
Secrets*.toml
dist/*
!dist/index.html
!dist/Roboto-Black.ttf
/target
.shuttle*

5781
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

3
Cargo.toml Normal file
View file

@ -0,0 +1,3 @@
[workspace]
resolver = "2"
members = ["game", "server"]

5
Makefile Normal file
View file

@ -0,0 +1,5 @@
build:
cargo build --package game --release --target wasm32-unknown-unknown && \
wasm-bindgen --out-dir dist --target web target/wasm32-unknown-unknown/release/game.wasm
up: build
shuttle run

20
README.md Normal file
View file

@ -0,0 +1,20 @@
Example of how you can compile Bevy to WASM and run it on Shuttle.
## Instructions
Make sure you have `wasm-bindgen-cli` and the rustup `wasm32-unknown-unknown` compilation target added. If you don't, you can get them by running the following:
```bash
cargo install wasm-bindgen-cli
rustup target add wasm32-unknown-unknown
```
You can try the example out by simply running `cargo run`, or go straight to compilation if you want to get your game straight to the browser.
In the Makefile, there is a command for compiling the game to WASM.
If you don't have `make` installed, you can run the command manually.
```bash
make build
```
Go to the project workspace root or the `server` folder, run `shuttle run`, then visit `http://localhost:8000/game`, you should see your Bevy program in action!

10
Shuttle.toml Normal file
View file

@ -0,0 +1,10 @@
name = "typonomy"
[deploy]
include = [
"dist/*",
]
[build]
assets = [
"dist/*",
]

BIN
dist/Roboto-Black.ttf vendored Normal file

Binary file not shown.

31
dist/index.html vendored Normal file
View file

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html,
body,
canvas {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
overflow: hidden;
position: absolute;
background: black;
z-index: 0;
}
</style>
<title>My Game</title>
</head>
<body>
<canvas id="canvas" width="1280" height="720" cursor="auto"></canvas>
<script type="module">
import init from "./game.js"
init()
</script>
</body>
</html>

7
game/Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "game"
version = "0.1.0"
edition = "2021"
[dependencies]
bevy = { version = "0.13", features = ["webgl2"] }

50
game/src/main.rs Normal file
View file

@ -0,0 +1,50 @@
use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
use bevy::diagnostic::LogDiagnosticsPlugin;
use bevy::prelude::*;
use bevy::window::{MonitorSelection, WindowPosition};
fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
prevent_default_event_handling: false,
canvas: Some("#canvas".to_string()),
position: WindowPosition::Centered(MonitorSelection::Current),
..default()
}),
..default()
}),
LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin,
))
.add_systems(Startup, setup)
.add_systems(Update, main_menu)
.run();
}
fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}
fn main_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.load("/Roboto-Black.ttf");
let title_font = title_text_style(60.0, font.clone());
commands.spawn({
Text2dBundle {
text: Text::from_section("Hello world!", title_font.clone())
.with_justify(JustifyText::Center),
..default()
}
});
}
fn title_text_style(font_size: f32, font: Handle<Font>) -> TextStyle {
TextStyle {
font,
font_size,
color: Color::WHITE,
}
}

11
server/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "shuttle-bevy-ex"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = "0.7.4"
shuttle-axum = "0.49.0"
shuttle-runtime = "0.49.0"
tokio = "1.28.2"
tower-http = { version = "0.5.0", features = ["fs"] }

11
server/src/main.rs Normal file
View file

@ -0,0 +1,11 @@
use axum::Router;
use tower_http::services::{ServeDir, ServeFile};
#[shuttle_runtime::main]
async fn main() -> shuttle_axum::ShuttleAxum {
let router = Router::new().nest_service(
"/",
ServeDir::new("dist").not_found_service(ServeFile::new("dist/index.html")),
);
Ok(router.into())
}