Skip to content

Instantly share code, notes, and snippets.

@Rizary
Last active February 28, 2023 08:46
Show Gist options
  • Save Rizary/9d355d9620b4120391f4db1deabb9a4d to your computer and use it in GitHub Desktop.
Save Rizary/9d355d9620b4120391f4db1deabb9a4d to your computer and use it in GitHub Desktop.
Error in bevy
panicked at 'Resource requested by bevy_html5canvas_wasm::player::spawn_player does not exist: bevy_html5canvas_wasm::models::ModelsAssets', /home/rizary/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.9.1/src/system/system_param.rs:412:17
Stack:
Error
at imports.wbg.__wbg_new_abda76e883ba8a5f (http://localhost:8189/static/index.js:392:21)
at console_error_panic_hook::hook::h1b81b264559f9967 (http://localhost:8189/static/assets/bevy_html5canvas_wasm.wasm:wasm-function[6891]:0x9be946)
at core::ops::function::Fn::call::h5cf039a186059757 (http://localhost:8189/static/assets/bevy_html5canvas_wasm.wasm:wasm-function[43238]:0xeff323)
at std::panicking::rust_panic_with_hook::hd000e9fb43b5781d (http://localhost:8189/static/assets/bevy_html5canvas_wasm.wasm:wasm-function[16487]:0xd1cf5e)
at std::panicking::begin_panic_handler::{{closure}}::he16e52e9a7dddeb1 (http://localhost:8189/static/assets/bevy_html5canvas_wasm.wasm:wasm-function[19118]:0xd9c357)
at std::sys_common::backtrace::__rust_end_short_backtrace::h227361e053771d9e (http://localhost:8189/static/assets/bevy_html5canvas_wasm.wasm:wasm-function[29178]:0xeb618b)
at rust_begin_unwind (http://localhost:8189/static/assets/bevy_html5canvas_wasm.wasm:wasm-function[25609]:0xe77ef5)
at core::panicking::panic_fmt::h9d972fcdb087ce21 (http://localhost:8189/static/assets/bevy_html5canvas_wasm.wasm:wasm-function[27835]:0xea3711)
at <bevy_ecs::system::function_system::FunctionSystem<In,Out,Param,Marker,F> as bevy_ecs::system::system::System>::run_unsafe::h40981eb487bc7e89 (http://localhost:8189/static/assets/bevy_html5canvas_wasm.wasm:wasm-function[7579]:0xa1d6ad)
at async_task::raw::RawTask<F,T,S>::run::h66bbdf7bd0f65463 (http://localhost:8189/static/assets/bevy_html5canvas_wasm.wasm:wasm-function[1209]:0x412d3b)
#[wasm_bindgen(start)]
pub async fn main_js() {
logwithdiv("load dominator app");
let dominator_app = Arc::new(DominatorApp::new());
dominator::append_dom(&dominator::body(), DominatorApp::render(DominatorApp::new()));
logwithdiv("creating bevy app");
let mut app = App::new();
let window = WindowDescriptor {
title: "Lobby Game".to_string(),
resizable: false,
canvas: Some(format!("#game")),
height: 800.0,
width: 800.0,
decorations: false,
fit_canvas_to_parent: true,
mode: WindowMode::BorderlessFullscreen,
..default()
};
app.insert_resource(ClearColor(Color::WHITE))
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
window,
..default()
})
.set(AssetPlugin {
asset_folder: "./static/assets".to_string(),
..default()
})
.set(LogPlugin {
level: Level::DEBUG,
filter: "wgpu=error".to_string(),
}),
)
.add_plugin(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(16.))
.add_plugin(RapierDebugRenderPlugin::default())
.add_startup_system(spawn_camera)
.add_startup_system(spawn_map)
.add_plugin(ModelsPlugin)
.add_plugin(WallPlugin)
.add_plugin(PlayerPlugin)
.run();
std::mem::forget(Box::new(dominator_app));
}
fn spawn_camera(mut commands: Commands) {
logwithdiv("camera spawned");
// camera
commands.spawn(Camera2dBundle::default());
}
fn spawn_map(mut commands: Commands, asset_server: Res<AssetServer>) {
logwithdiv("map spawned");
commands.spawn(SpriteBundle {
texture: asset_server.load("background.png"),
transform: Transform {
translation: Vec3::new(0.0,0.0,0.0001),
..default()
},
..default()
});
commands.spawn(SpriteBundle {
texture: asset_server.load("block_green_large.png"),
transform: Transform {
translation: Vec3::new(X_GREEN_BLOCK,Y_GREEN_BLOCK,0.01),
..default()
},
..default()
});
commands.spawn(SpriteBundle {
texture: asset_server.load("block_yellow_large.png"),
transform: Transform {
translation: Vec3::new(X_YELLOW_BLOCK,Y_YELLOW_BLOCK,0.01),
..default()
},
..default()
});
}
use bevy::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(SystemLabel)]
pub enum ModelsLabel {
Loaded
}
pub struct ModelsPlugin;
#[derive(Resource)]
pub struct ModelsAssets {
pub default_player: Handle<Scene>,
}
impl Plugin for ModelsPlugin {
fn build(&self, app: &mut App) {
app.add_startup_system(setup_resources.label(ModelsLabel::Loaded));
}
}
pub fn setup_resources(mut commands: Commands, asset_server: ResMut<AssetServer>) {
let scene_assets = ModelsAssets {
default_player: asset_server.load("./static/assets/robot/idle.png"),
};
commands.insert_resource(scene_assets);
}
use crate::user_input::PlayerInput;
use bevy::prelude::*;
use tracing::*;
use bevy_rapier2d::prelude::*;
use leafwing_input_manager::prelude::*;
use crate::constants::*;
use crate::models::{ModelsAssets, setup_resources};
use crate::logging::logwithdiv;
#[derive(SystemLabel)]
pub enum PlayerStages {
Move,
}
#[derive(Component)]
pub struct DefaultPlayer;
pub struct PlayerPlugin;
impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) {
app.add_startup_system(spawn_player.after(ModelsLabel::Loaded))
.add_system(move_player);
}
}
fn spawn_player(mut commands: Commands, existing_player: Query<&DefaultPlayer>, models: Res<ModelsAssets>) {
if !existing_player.is_empty() {
logwithdiv("Player is not existed");
return;
}
commands.spawn((
SpriteBundle {
texture: models.default_player.clone(),
transform: Transform {
translation: Vec3::new(0.0,0.0,1.0),
..default()
},
..default()
},
InputManagerBundle {
input_map: PlayerInput::player_one(),
..default()
},
DefaultPlayer,
RigidBody::KinematicPositionBased,
Velocity::default(),
Collider::cuboid(9., 15.95),
LockedAxes::ROTATION_LOCKED_Z,
Friction {
coefficient: 5.,
combine_rule: CoefficientCombineRule::Multiply,
},
Damping {
linear_damping: 1.,
angular_damping: 1.,
},
Name::new("Player")
));
}
fn move_player(
mut player: Query<(
&mut Velocity,
&ActionState<PlayerInput>,
&mut Transform,
With<DefaultPlayer>
)>,
rapier_context: Res<RapierContext>,
) {
for (mut velocity, input, mut pos, _) in &mut player {
let mut direction_x: f32 = 0.0;
let mut direction_y = 0.0;
if input.pressed(PlayerInput::Up) {
error_span!("Up");
direction_y += PLAYER_ACCELERATION;
// velocity.linvel.y += PLAYER_ACCELERATION;
} else if input.pressed(PlayerInput::Down) {
direction_y -= PLAYER_ACCELERATION;
// velocity.linvel.y -= PLAYER_ACCELERATION;
} else if input.pressed(PlayerInput::Left) {
// direction_x += PLAYER_ACCELERATION;
direction_x -= PLAYER_ACCELERATION;
} else if input.pressed(PlayerInput::Right) {
// direction_x -= PLAYER_ACCELERATION;
direction_x += PLAYER_ACCELERATION;
};
pos.translation.y = pos.translation.y + direction_y;
pos.translation.x = pos.translation.x + direction_x;
dbg!(pos.translation);
dbg!(direction_x, direction_y);
}
}
use bevy::prelude::KeyCode;
use leafwing_input_manager::prelude::*;
#[derive(Debug, Actionlike, Clone)]
pub enum PlayerInput {
Left,
Right,
Up,
Down,
}
impl PlayerInput {
pub fn player_one() -> InputMap<PlayerInput> {
let mut map = InputMap::default();
map.insert_multiple([
(KeyCode::A, PlayerInput::Left),
(KeyCode::Left, PlayerInput::Left),
(KeyCode::D, PlayerInput::Right),
(KeyCode::Right, PlayerInput::Right),
(KeyCode::W, PlayerInput::Up),
(KeyCode::Up, PlayerInput::Up),
(KeyCode::S, PlayerInput::Down),
(KeyCode::Down, PlayerInput::Down),
]);
map
}
}
use bevy::prelude::*;
use bevy::sprite::{MaterialMesh2dBundle, Mesh2dHandle};
use crate::constants::*;
pub struct WallPlugin;
impl Plugin for WallPlugin {
fn build(&self, app: &mut App) {
app.add_system(setup_walls);
}
}
fn setup_walls(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn(WallBundle::new(
WallLocation::Left,
&mut meshes,
&mut materials,
));
commands.spawn(WallBundle::new(
WallLocation::Right,
&mut meshes,
&mut materials,
));
commands.spawn(WallBundle::new(
WallLocation::Bottom,
&mut meshes,
&mut materials,
));
commands.spawn(WallBundle::new(
WallLocation::Top,
&mut meshes,
&mut materials,
));
}
#[derive(Component)]
pub struct Wall;
#[derive(Bundle)]
struct WallBundle<ColorMaterial: 'static + bevy::sprite::Material2d> {
#[bundle]
material_bundle: MaterialMesh2dBundle<ColorMaterial>,
// wall: Wall,
}
impl WallBundle<ColorMaterial> {
fn new(
location: WallLocation,
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
) -> WallBundle<ColorMaterial> {
let mut mesh = Mesh::from(shape::Quad::default());
let vertex_colors: Vec<[f32; 4]> = vec![
Color::RED.as_rgba_f32(),
Color::GREEN.as_rgba_f32(),
Color::BLUE.as_rgba_f32(),
Color::WHITE.as_rgba_f32(),
];
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, vertex_colors);
let mesh_handle: Mesh2dHandle = meshes.add(mesh).into();
WallBundle {
material_bundle: MaterialMesh2dBundle {
mesh: mesh_handle.clone(),
transform: Transform {
translation: location.position().extend(0.01),
scale: location.size().extend(1.0),
..default()
},
material: materials.add(ColorMaterial::from(WALL_COLOR)),
// sprite: Sprite {
// color: WALL_COLOR,
// ..default()
// },
..default()
},
// wall: Wall,
}
}
}
enum WallLocation {
Left,
Right,
Bottom,
Top
}
impl WallLocation {
fn position(&self) -> Vec2 {
match self {
WallLocation::Left => Vec2::new(LEFT_WALL, 0.),
WallLocation::Right => Vec2::new(RIGHT_WALL, 0.),
WallLocation::Bottom => Vec2::new(0., BOTTOM_WALL),
WallLocation::Top => Vec2::new(0., TOP_WALL),
}
}
fn size(&self) -> Vec2 {
let arena_height = TOP_WALL - BOTTOM_WALL;
let arena_width = RIGHT_WALL - LEFT_WALL;
// Make sure we haven't messed up our constants
assert!(arena_height > 0.0);
assert!(arena_width > 0.0);
match self {
WallLocation::Left | WallLocation::Right => {
Vec2::new(WALL_THICKNESS, arena_height + WALL_THICKNESS)
}
WallLocation::Bottom | WallLocation::Top => {
Vec2::new(arena_width + WALL_THICKNESS, WALL_THICKNESS)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment