diff --git a/src/actors.rs b/src/actors.rs index 1d76f59..e46c068 100644 --- a/src/actors.rs +++ b/src/actors.rs @@ -17,7 +17,7 @@ pub fn fill_actors( let materials = materials.0.get(materials.1 .0.clone()).unwrap(); for (actor, transform) in actors.iter() { - let actor_pos = transform.world_pos().as_ivec2(); + let actor_pos = transform.world_pos(&actor).as_ivec2(); for x_off in 0..actor.width as i32 { for y_off in 0..actor.height as i32 { @@ -42,7 +42,7 @@ pub fn unfill_actors( let materials = materials.0.get(materials.1 .0.clone()).unwrap(); for (actor, transform) in actors.iter() { - let actor_pos = transform.world_pos().as_ivec2(); + let actor_pos = transform.world_pos(&actor).as_ivec2(); for x_off in 0..actor.width as i32 { for y_off in 0..actor.height as i32 { @@ -86,7 +86,7 @@ pub fn update_actors( let materials = materials.0.get(materials.1 .0.clone()).unwrap(); for (mut actor, mut transform) in actors.iter_mut() { - let mut actor_pos = transform.world_pos().as_ivec2(); + let mut actor_pos = transform.world_pos(&actor).as_ivec2(); let mut prev = actor_pos; for v in Line::new(actor_pos, actor.vel.as_ivec2()) { let move_hor = match (prev.x != v.x, prev.y != v.y) { @@ -181,7 +181,7 @@ pub fn update_actors( prev = v; } - transform.update_world_pos(&actor_pos.as_vec2()) + transform.update_world_pos(&actor, &actor_pos.as_vec2()) } } @@ -271,26 +271,28 @@ pub fn move_y( } pub trait WorldPos { - fn world_pos(&self) -> Vec2; + fn world_pos(&self, actor: &Actor) -> Vec2; } impl WorldPos for Transform { - fn world_pos(&self) -> Vec2 { + fn world_pos(&self, actor: &Actor) -> Vec2 { let mut pos = self.translation.xy(); pos.y *= -1.; + pos -= vec2(actor.width as f32, actor.height as f32) / 2.; + pos } } pub trait UpdateWorldPos { - fn update_world_pos(&mut self, world_pos: &Vec2); + fn update_world_pos(&mut self, actor: &Actor, world_pos: &Vec2); } impl UpdateWorldPos for Transform { - fn update_world_pos(&mut self, world_pos: &Vec2) { - self.translation.x = world_pos.x; - self.translation.y = -world_pos.y; + fn update_world_pos(&mut self, actor: &Actor, world_pos: &Vec2) { + self.translation.x = world_pos.x + actor.width as f32 / 2.; + self.translation.y = -world_pos.y - actor.height as f32 / 2.; } } diff --git a/src/chunk_manager.rs b/src/chunk_manager.rs index b061502..5b57f90 100644 --- a/src/chunk_manager.rs +++ b/src/chunk_manager.rs @@ -288,6 +288,7 @@ pub fn add_colliders( commands .entity(ent) .insert(collider) + .insert(CollisionGroups::new(WORLD_GROUP, RIGIDBODY_GROUP)) .insert(bevy_rapier2d::prelude::RigidBody::Fixed); } } @@ -509,7 +510,7 @@ pub fn update_manager_pos( mut commands: Commands, chunk_textures: Query>, image_entities: Query<(&Parent, Entity, &Handle)>, - player: Query<&Transform, With>, + player: Query<(&Actor, &Transform), With>, resources: ( ResMut, ResMut, @@ -519,7 +520,8 @@ pub fn update_manager_pos( ) { let (mut saving_task, mut chunk_manager, mut images) = resources; - let mut player_pos = player.single().world_pos().as_ivec2(); + let (actor, transform) = player.single(); + let mut player_pos = transform.world_pos(actor).as_ivec2(); if player_pos.x < 0 { player_pos.x -= CHUNK_LENGHT as i32 } diff --git a/src/consts.rs b/src/consts.rs index f2f9b7e..c161015 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -1,4 +1,5 @@ use bevy::prelude::Color; +use bevy_rapier2d::geometry::Group; // Chunk Length consts // Chunk length MUST be divisible by 4 @@ -52,6 +53,11 @@ pub const PLAYER_LAYER: f32 = 1.; pub const PARTICLE_LAYER: f32 = 10.; pub const AUTOMATA_LAYER: f32 = 100.; +//Rapier2d collision groups +pub const RIGIDBODY_GROUP: Group = Group::GROUP_1; +pub const WORLD_GROUP: Group = Group::GROUP_2; +pub const ACTOR_GROUP: Group = Group::GROUP_3; + //Buttons pub const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15); pub const HOVERED_BUTTON: Color = Color::rgb(0.25, 0.25, 0.25); diff --git a/src/debug.rs b/src/debug.rs index 790177f..8cfba39 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -130,6 +130,8 @@ pub fn render_dirty_rects(mut commands: Commands, dirty_rects: Res) fn render_actors(mut commands: Commands, actors: Query<(&Actor, &Transform)>) { for (actor, transform) in actors.iter() { + let pos = transform.world_pos(&actor); + commands .spawn(SpriteBundle { sprite: Sprite { @@ -138,7 +140,7 @@ fn render_actors(mut commands: Commands, actors: Query<(&Actor, &Transform)>) { anchor: Anchor::TopLeft, ..default() }, - transform: *transform, + transform: Transform::from_xyz(pos.x, -pos.y, 1.), ..default() }) .insert(DeleteImage); diff --git a/src/player.rs b/src/player.rs index a6e441c..12cae1d 100644 --- a/src/player.rs +++ b/src/player.rs @@ -118,6 +118,8 @@ pub fn player_setup( player_actor.width as f32 / 2., player_actor.height as f32 / 2., ), + SolverGroups::new(ACTOR_GROUP, RIGIDBODY_GROUP), + CollisionGroups::new(Group::NONE, RIGIDBODY_GROUP), )) .add_child(tool_ent); } @@ -152,7 +154,7 @@ pub fn update_player( let on_ground = on_ground( &chunk_manager, &actor, - &transform.world_pos().as_ivec2(), + &transform.world_pos(&actor).as_ivec2(), materials, ); diff --git a/src/rigidbody.rs b/src/rigidbody.rs index 46bb3ef..11aaed6 100644 --- a/src/rigidbody.rs +++ b/src/rigidbody.rs @@ -90,6 +90,10 @@ pub fn add_rigidbodies( .spawn(collider) .insert(rigidbody) .insert(bevy_rapier2d::prelude::RigidBody::Dynamic) + .insert(CollisionGroups::new( + RIGIDBODY_GROUP, + ACTOR_GROUP.union(WORLD_GROUP), + )) .insert(TransformBundle::default()); commands.entity(ent).insert(Hydrated);