Skip to content

Commit

Permalink
Allow changing position in wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Nov 25, 2023
1 parent 21d584f commit 51aaf9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ surf = { version = "2.3.2", default-features = false, features = [
] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = {version = "0.3.22", default-features = false}
js-sys = {version = "0.3", default-features = false}
wasm-bindgen = {version = "0.2", default-features = false}
web-sys = { version = "0.3.22", default-features = false, features = [
"Location",
] }
js-sys = { version = "0.3", default-features = false }
wasm-bindgen = { version = "0.2", default-features = false }
wasm-bindgen-futures = "0.4"

[target.'cfg(not(any(target_os="macos", target_arch = "wasm32")))'.dependencies]
Expand Down
25 changes: 21 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,32 @@ pub fn main() {

fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
// Just for testing:
const X0: i32 = 17437;
const Y0: i32 = 11371;
#[allow(unused_mut)]
let mut x: i32 = 17437;
#[allow(unused_mut)]
let mut y: i32 = 11371;

#[cfg(target_arch = "wasm32")]
{
let window = web_sys::window().expect("no window exists");
let document = window.document().expect("no global document exist");
let location = document.location().expect("no location exists");
let raw_search = location.search().expect("no search exists");
info!(?location);
if let Some(addr) = raw_search.strip_prefix('?') {
let (l, r) = addr.split_once(',').unwrap();
x = l.parse().unwrap();
y = r.parse().unwrap();
}
}

commands.spawn((
TileMap::new(&mut meshes),
SpatialBundle {
transform: Transform::from_xyz(
-X0 as f32 * TileMap::TILE_SIZE,
-x as f32 * TileMap::TILE_SIZE,
0.,
-Y0 as f32 * TileMap::TILE_SIZE,
-y as f32 * TileMap::TILE_SIZE,
),
..default()
},
Expand Down

0 comments on commit 51aaf9d

Please sign in to comment.