You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
usecrate::components::*;use bevy::prelude::*;use bevy_ecs_ldtk::prelude::*;use std::collections::{HashMap,HashSet};use bevy_rapier2d::prelude::*;usecrate::*;pubfnspawn_player(mutcommands:Commands,asset_server:Res<AssetServer>,muttexture_atlases:ResMut<Assets<TextureAtlas>>,){let texture_handle = asset_server.load("gabe-idle-run.png");let texture_atlas =
TextureAtlas::from_grid(texture_handle,Vec2::new(24.0,24.0),7,1,None,None);let texture_atlas_handle = texture_atlases.add(texture_atlas);let animation_indices = AnimationIndices{first:1,last:6};// Use only the subset of sprites in the sheet that make up the running animation
commands.spawn((SpriteSheetBundle{texture_atlas: texture_atlas_handle,sprite:TextureAtlasSprite::new(3),transform:Transform{translation:Vec3{x:0.0,y:0.0,z:4.5,},scale:Vec3::new(3.0,3.0,0.0),
..default()},
..default()},Name::new("Player"),Player,
animation_indices,AnimationTimer(Timer::from_seconds(0.1,TimerMode::Repeating)),));}pubfnplayer_movement(keyboard_input:Res<Input<KeyCode>>,mutplayer_query:Query<&mutTransform,With<Player>>,time:Res<Time>,){ifletOk(mut transform) = player_query.get_single_mut(){letmut direction = Vec3::ZERO;if keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A){
direction += Vec3::new(-1.0,0.0,0.0);
transform.scale = Vec3::new(-3.0,3.0,0.0);}if keyboard_input.pressed(KeyCode::Right) || keyboard_input.pressed(KeyCode::D){
direction += Vec3::new(1.0,0.0,0.0);
transform.scale = Vec3::new(3.0,3.0,0.0);}if keyboard_input.pressed(KeyCode::Up) || keyboard_input.pressed(KeyCode::W){
direction += Vec3::new(0.0,1.0,0.0);}if keyboard_input.pressed(KeyCode::Down) || keyboard_input.pressed(KeyCode::S){
direction += Vec3::new(0.0, -1.0,0.0);}if direction.length() > 0.0{
direction = direction.normalize();}
transform.translation += direction *PLAYER_SPEED* time.delta_seconds();}}pubfncamera_with_player_movement(keyboard_input:Res<Input<KeyCode>>,mutcamera_query:Query<&mutTransform,With<Camera>>,time:Res<Time>,){ifletOk(mut transform) = camera_query.get_single_mut(){letmut direction = Vec3::ZERO;if keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A){
direction += Vec3::new(-1.0,0.0,0.0);}if keyboard_input.pressed(KeyCode::Right) || keyboard_input.pressed(KeyCode::D){
direction += Vec3::new(1.0,0.0,0.0);}if keyboard_input.pressed(KeyCode::Up) || keyboard_input.pressed(KeyCode::W){
direction += Vec3::new(0.0,1.0,0.0);}if keyboard_input.pressed(KeyCode::Down) || keyboard_input.pressed(KeyCode::S){
direction += Vec3::new(0.0, -1.0,0.0);}if direction.length() > 0.0{
direction = direction.normalize();}
transform.translation += direction *PLAYER_SPEED* time.delta_seconds();}}pubfnconfine_player_movement(mutplayer_query:Query<&mutTransform,With<Player>>,window_query:Query<&Window,With<PrimaryWindow>>,){ifletOk(mut player_transform) = player_query.get_single_mut(){let window = window_query.get_single().unwrap();let half_player_size = PLAYER_SIZE;let x_min = -window.width() / 2.0 + half_player_size - 2.8;let x_max = window.width() / 2.0 - half_player_size;letmut translation = player_transform.translation;// Bound the player X positionif translation.x < x_min {
translation.x = x_min
}elseif translation.x > x_max {
translation.x = x_max
}
player_transform.translation = translation;}}pubfnanimate_sprite(keyboard_input:Res<Input<KeyCode>>,time:Res<Time>,mutquery:Query<(&AnimationIndices,&mutAnimationTimer,&mutTextureAtlasSprite,)>,){for(indices,mut timer,mut sprite)in&mut query {
timer.tick(time.delta());if keyboard_input.pressed(KeyCode::Right)
|| keyboard_input.pressed(KeyCode::D)
|| keyboard_input.pressed(KeyCode::Left)
|| keyboard_input.pressed(KeyCode::A){if timer.just_finished(){
sprite.index = if sprite.index == indices.last{
indices.first}else{
sprite.index + 1};}}else{
sprite.index = 0}}}pubfnplayer_jump(mutcommands:Commands,keyboard_input:Res<Input<KeyCode>>,mutplayer:Query<(Entity,&mutTransform,&mutJump),With<Player>>,time:Res<Time>,){letOk((player,mut transform,mut jump)) = player.get_single_mut()else{return;};let jump_power = (time.delta_seconds()*FALL_SPEED*2.).min(jump.0);
jump.0 -= jump_power;
transform.translation.y += jump_power;if jump.0 == 0.{
commands.entity(player).remove::<Jump>();}}
components.rs:
use bevy::prelude::*;use bevy_inspector_egui::prelude::*;use bevy_inspector_egui::quick::WorldInspectorPlugin;#[derive(Component,InspectorOptions)]pubstructPlayer;#[derive(Copy,Clone,Eq,PartialEq,Debug,Default,Component)]pubstructWall;#[derive(Component,Deref,DerefMut)]pubstructAnimationTimer(pubTimer);#[derive(Component,InspectorOptions)]pubstructAnimationIndices{pubfirst:usize,publast:usize,}#[derive(Component)]pubstructJump(pubf32);
The text was updated successfully, but these errors were encountered:
Abubakar1122331
changed the title
Adding collision to your ldtk map
How to add collision to your ldtk map
Aug 15, 2023
There's a system in the platformer example that does this sort of thing, but fyi the platformer example uses rapier for physics/collision. Naively, you could spawn a collider for every Wall tile, and even do so through LdtkIntCell registration, but this leaves you with a lot of colliders in a level which slows things down a lot.
Though, it doesn't look like you're spawning these components in any way. Be sure to do so either through the registering LdtkEntity/LdtkIntCell bundles to the app, or by fleshing them out in a system that queries for Added<EntityInstance> or Added<IntGridCell>, or some combination of the two: https://docs.rs/bevy_ecs_ldtk/latest/bevy_ecs_ldtk/#entity-and-intgrid-layers
I want to add collision for any these ldtk.
Tell me how do you separate tiles for collision and add colliders to it.
Here's my main.rs:
This is my systems.rs:
components.rs:
The text was updated successfully, but these errors were encountered: