Skip to content

Commit

Permalink
Merge pull request #180 from janhohenheim/fix-wasm
Browse files Browse the repository at this point in the history
Former-commit-id: d09231c
  • Loading branch information
janhohenheim authored Feb 21, 2023
2 parents 9f9fcd2 + ed3a299 commit 3d3c0a4
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ blender_assets.cats.txt*

installer/
Trunk.toml

trace-*.json
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ native-dev = [

native = [
"bevy_rapier3d/parallel",
"bevy_rapier3d/simd-nightly",
"bevy_hanabi",
]

wasm = [
"bevy_rapier3d/wasm-bindgen"
]

tracing = [
"bevy/trace_chrome"
]

[dependencies]
bevy = { version = "^0.9.1", default-features = false }
bevy_kira_audio = "0.13"
Expand All @@ -67,7 +70,7 @@ iyes_progress = "0.7.1"
unicode-segmentation = "1.10.1"
bevy_hanabi = { version = "0.5", optional = true }
anyhow = "1.0.69"
bevy_rapier3d = { version = "0.20", features = ["serde-serialize"] }
bevy_rapier3d = { version = "0.20", features = ["serde-serialize", "simd-nightly"] }

# keep the following in sync with Bevy's dependencies
winit = { version = "0.27", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Native:
```bash
cargo run
```
WASM (not fully functional yet, see [#11](https://github.com/janhohenheim/foxtrot/issues/11)):
WASM (runs best on Chromium based browsers):
```bash
trunk serve --no-default-features --features core,dev,wasm
```
Expand Down
4 changes: 4 additions & 0 deletions src/level_instantiation/spawning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ impl Plugin for SpawningPlugin {

impl<'w, 's, 'a> PrimedGameObjectSpawner<'w, 's, 'a> {
pub fn spawn<'c: 'a>(&'c mut self, object: GameObject, transform: Transform) -> Result<Entity> {
#[cfg(feature = "tracing")]
let _span = info_span!("spawn").entered();
self.outer_spawner.implementors[&object].spawn(self, object, transform)
}
}

fn load_assets_for_spawner(mut commands: Commands, mut mesh_assets: ResMut<Assets<Mesh>>) {
#[cfg(feature = "tracing")]
let _span = info_span!("load_assets_for_spawner").entered();
let mut implementors = HashMap::new();

for game_object in GameObject::iter() {
Expand Down
2 changes: 2 additions & 0 deletions src/level_instantiation/spawning/animation_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub fn link_animations(
animations_entity_link_query: Query<&AnimationEntityLink>,
mut commands: Commands,
) {
#[cfg(feature = "tracing")]
let _span = info_span!("link_animations").entered();
for entity in player_query.iter() {
let top_entity = get_top_parent(entity, &parent_query);
if animations_entity_link_query.get(top_entity).is_ok() {
Expand Down
8 changes: 8 additions & 0 deletions src/level_instantiation/spawning/post_spawn_modification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use regex::Regex;
use std::sync::LazyLock;

pub fn set_hidden(mut added_name: Query<(&Name, &mut Visibility), Added<Name>>) {
#[cfg(feature = "tracing")]
let _span = info_span!("set_hidden").entered();
for (name, mut visibility) in added_name.iter_mut() {
if name.to_lowercase().contains("[hidden]") {
visibility.is_visible = false;
Expand All @@ -16,6 +18,8 @@ pub fn despawn_removed(
mut commands: Commands,
mut added_name: Query<(Entity, &Name), Added<Name>>,
) {
#[cfg(feature = "tracing")]
let _span = info_span!("despawn_removed").entered();
for (entity, name) in added_name.iter_mut() {
if name.to_lowercase().contains("[remove]") {
commands.entity(entity).insert(Despawn { recursive: true });
Expand All @@ -29,6 +33,8 @@ pub fn generate_tangents(
mut mesh_asset_events: EventReader<AssetEvent<Mesh>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
#[cfg(feature = "tracing")]
let _span = info_span!("generate_tangents").entered();
for event in mesh_asset_events.iter() {
if let AssetEvent::Created { handle } = event {
// Guaranteed to work because we just created the mesh
Expand All @@ -52,6 +58,8 @@ pub fn set_color(
material_handles: Query<&Handle<StandardMaterial>>,
mut standard_materials: ResMut<Assets<StandardMaterial>>,
) -> Result<()> {
#[cfg(feature = "tracing")]
let _span = info_span!("set_color").entered();
for (name, children) in added_name.iter() {
if let Some(captures) = COLOR_REGEX.captures(&name.to_lowercase()) {
let color = Color::rgba_u8(
Expand Down
12 changes: 12 additions & 0 deletions src/movement/general_movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ fn update_grounded(
mut query: Query<(Entity, &Transform, &Collider, &mut Grounded)>,
rapier_context: Res<RapierContext>,
) {
#[cfg(feature = "tracing")]
let _span = info_span!("update_grounded").entered();
for (entity, transform, collider, mut grounded) in &mut query {
let height = collider.raw.compute_local_aabb().maxs.y;
grounded.0 = rapier_context
Expand All @@ -79,6 +81,8 @@ pub fn reset_movement_components(
mut walking: Query<&mut Walking>,
mut jumpers: Query<&mut Jumping>,
) {
#[cfg(feature = "tracing")]
let _span = info_span!("reset_movement_components").entered();
for mut force in &mut forces {
*force = default();
}
Expand All @@ -103,6 +107,8 @@ pub fn apply_jumping(
&Transform,
)>,
) {
#[cfg(feature = "tracing")]
let _span = info_span!("apply_jumping").entered();
for (grounded, mut impulse, mut velocity, mass, jump, transform) in &mut character_query {
if jump.requested && grounded.0 {
let up = transform.up();
Expand All @@ -117,6 +123,8 @@ pub fn apply_jumping(
}

fn rotate_characters(time: Res<Time>, mut player_query: Query<(&Velocity, &mut Transform)>) {
#[cfg(feature = "tracing")]
let _span = info_span!("rotate_characters").entered();
let dt = time.delta_seconds();
for (velocity, mut transform) in player_query.iter_mut() {
let up = transform.up();
Expand Down Expand Up @@ -144,6 +152,8 @@ fn play_animations(
&CharacterAnimations,
)>,
) -> Result<()> {
#[cfg(feature = "tracing")]
let _span = info_span!("play_animations").entered();
for (velocity, transform, grounded, animation_entity_link, animations) in characters.iter() {
let mut animation_player = animation_player
.get_mut(animation_entity_link.0)
Expand Down Expand Up @@ -178,6 +188,8 @@ pub fn apply_walking(
&Transform,
)>,
) {
#[cfg(feature = "tracing")]
let _span = info_span!("apply_walking").entered();
for (mut force, walking, mut velocity, grounded, mass, transform) in &mut character_query {
let mass = mass.0.mass;
if let Some(acceleration) = walking.get_acceleration(grounded.0) {
Expand Down
13 changes: 9 additions & 4 deletions src/movement/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ use crate::player_control::player_embodiment::Player;
use crate::util::log_error::log_errors;
use crate::util::trait_extension::{F32Ext, Vec3Ext};
use crate::GameState;
use anyhow::{Context, Result};
#[cfg(feature = "dev")]
use anyhow::Context;
use anyhow::Result;
use bevy::prelude::*;
#[cfg(feature = "dev")]
use bevy_prototype_debug_lines::DebugLines;
use oxidized_navigation::{
query::{find_path, perform_string_pulling_on_path},
Expand All @@ -19,7 +22,7 @@ use serde::{Deserialize, Serialize};
/// Currently only one navmesh is supported. It is loaded automagically from any entity whose name contains `"[navmesh]"`.
pub struct NavigationPlugin;

const CELL_WIDTH: f32 = 0.5 * npc::RADIUS * 2.;
const CELL_WIDTH: f32 = 0.5 * npc::RADIUS;

impl Plugin for NavigationPlugin {
fn build(&self, app: &mut App) {
Expand All @@ -34,7 +37,7 @@ impl Plugin for NavigationPlugin {
world_bottom_bound: -100.0,
max_traversable_slope_radians: (40.0_f32 - 0.1).to_radians(),
walkable_height: 25,
walkable_radius: 2,
walkable_radius: 3,
step_height: 3,
min_region_area: 50,
merge_region_area: 500,
Expand Down Expand Up @@ -62,9 +65,11 @@ fn query_mesh(
with_player: Query<&Transform, (With<Player>, Without<Follower>)>,
nav_mesh_settings: Res<NavMeshSettings>,
nav_mesh: Res<NavMesh>,
mut lines: ResMut<DebugLines>,
#[cfg(feature = "dev")] mut lines: ResMut<DebugLines>,
#[cfg(feature = "dev")] editor_state: Res<bevy_editor_pls::Editor>,
) -> Result<()> {
#[cfg(feature = "tracing")]
let _span = info_span!("query_mesh").entered();
if let Ok(nav_mesh) = nav_mesh.get().read() {
for (follower_transform, mut walking) in &mut with_follower {
for player_transform in &with_player {
Expand Down
2 changes: 2 additions & 0 deletions src/movement/navigation/navmesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub fn read_navmesh(
mesh_handles: Query<&Handle<Mesh>>,
mut path_meshes: ResMut<Assets<PathMesh>>,
) {
#[cfg(feature = "tracing")]
let _span = info_span!("read_navmesh").entered();
for (parent, name, global_transform) in &added_name {
if name.to_lowercase().contains("[navmesh]") {
let transform = global_transform.compute_transform();
Expand Down
11 changes: 10 additions & 1 deletion src/movement/physics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ pub struct PhysicsPlugin;
impl Plugin for PhysicsPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
.insert_resource(RapierConfiguration::default())
.insert_resource(RapierConfiguration {
timestep_mode: TimestepMode::Variable {
max_dt: 1.0 / 20.0,
time_scale: 1.0,
substeps: 1,
},
..default()
})
.add_system_set(
SystemSet::on_update(GameState::Playing)
.with_system(read_colliders.pipe(log_errors)),
Expand All @@ -28,6 +35,8 @@ pub fn read_colliders(
meshes: Res<Assets<Mesh>>,
mesh_handles: Query<&Handle<Mesh>>,
) -> Result<()> {
#[cfg(feature = "tracing")]
let _span = info_span!("read_colliders").entered();
for (entity, name) in &added_name {
if name.to_lowercase().contains("[collider]") {
for (collider_entity, collider_mesh) in
Expand Down
10 changes: 8 additions & 2 deletions src/player_control/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ pub fn set_actions(
mut mouse_wheel: EventReader<MouseWheel>,
actions_frozen: Res<ActionsFrozen>,
) {
#[cfg(feature = "tracing")]
let _span = info_span!("set_actions").entered();
*actions = default();
actions.ui.toggle_editor = GameControl::ToggleEditor.just_pressed(&keyboard_input);
#[cfg(feature = "dev")]
{
actions.ui.toggle_editor = GameControl::ToggleEditor.just_pressed(&keyboard_input);
}
actions.ui.toggle_pause = GameControl::TogglePause.just_pressed(&keyboard_input);
actions.ui.speed_up_dialog = GameControl::SpeedUpDialog.pressed(&keyboard_input);
for i in 0..=9 {
Expand All @@ -105,7 +110,8 @@ pub fn set_actions(
- get_movement(GameControl::Down, &keyboard_input),
);

actions.player.movement = (player_movement != Vec2::ZERO).then(|| player_movement.normalize());
actions.player.movement =
(!player_movement.is_approx_zero()).then(|| player_movement.normalize());
actions.player.jump = get_movement(GameControl::Jump, &keyboard_input) > 0.5;
actions.player.sprint = get_movement(GameControl::Sprint, &keyboard_input) > 0.5;
actions.player.interact = GameControl::Interact.just_pressed(&keyboard_input);
Expand Down
1 change: 1 addition & 0 deletions src/player_control/actions/game_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub enum GameControl {
Right,
Sprint,
Jump,
#[cfg_attr(not(feature = "dev"), allow(dead_code))]
ToggleEditor,
TogglePause,
Interact,
Expand Down
Loading

0 comments on commit 3d3c0a4

Please sign in to comment.