From 2bf0129dfbee206394787fe211b4397da52cc409 Mon Sep 17 00:00:00 2001 From: Isaac Turci <78173025+Zac8668@users.noreply.github.com> Date: Sat, 27 Jan 2024 23:24:20 -0300 Subject: [PATCH] Removed actor.pos, using now bevy transform --- src/actors.rs | 102 +++++++++++++++++++++++++++++++++---------- src/chunk_group.rs | 2 + src/chunk_manager.rs | 4 +- src/debug.rs | 10 ++--- src/main.rs | 4 +- src/player.rs | 33 +++++++------- 6 files changed, 105 insertions(+), 50 deletions(-) diff --git a/src/actors.rs b/src/actors.rs index b2947b8..1d76f59 100644 --- a/src/actors.rs +++ b/src/actors.rs @@ -4,23 +4,24 @@ use crate::prelude::*; pub struct Actor { pub width: u8, pub height: u8, - pub pos: IVec2, pub vel: Vec2, } //Called before simulations pub fn fill_actors( mut chunk_manager: ResMut, - actors: Query<&Actor>, + actors: Query<(&Actor, &Transform)>, mut dirty_rects: ResMut, materials: (Res>, Res), ) { let materials = materials.0.get(materials.1 .0.clone()).unwrap(); - for actor in actors.iter() { + for (actor, transform) in actors.iter() { + let actor_pos = transform.world_pos().as_ivec2(); + for x_off in 0..actor.width as i32 { for y_off in 0..actor.height as i32 { - let pos = global_to_chunk(actor.pos + ivec2(x_off, y_off)); + let pos = global_to_chunk(actor_pos + ivec2(x_off, y_off)); if let Some(atom) = chunk_manager.get_mut_atom(pos) { if materials[atom.id].is_void() { *atom = Atom::object(); @@ -35,15 +36,17 @@ pub fn fill_actors( //Called after simulation, before actor update pub fn unfill_actors( mut chunk_manager: ResMut, - actors: Query<&Actor>, + actors: Query<(&Actor, &Transform)>, materials: (Res>, Res), ) { let materials = materials.0.get(materials.1 .0.clone()).unwrap(); - for actor in actors.iter() { + for (actor, transform) in actors.iter() { + let actor_pos = transform.world_pos().as_ivec2(); + for x_off in 0..actor.width as i32 { for y_off in 0..actor.height as i32 { - let pos = global_to_chunk(actor.pos + ivec2(x_off, y_off)); + let pos = global_to_chunk(actor_pos + ivec2(x_off, y_off)); if let Some(atom) = chunk_manager.get_mut_atom(pos) { if materials[atom.id].is_object() { *atom = Atom::default(); @@ -54,9 +57,14 @@ pub fn unfill_actors( } } -pub fn on_ground(chunk_manager: &ChunkManager, actor: &Actor, materials: &Materials) -> bool { +pub fn on_ground( + chunk_manager: &ChunkManager, + actor: &Actor, + pos: &IVec2, + materials: &Materials, +) -> bool { for x_off in 0..actor.width { - let chunk_pos = global_to_chunk(actor.pos + ivec2(x_off as i32, actor.height as i32)); + let chunk_pos = global_to_chunk(*pos + ivec2(x_off as i32, actor.height as i32)); if let Some(atom) = chunk_manager.get_atom(&chunk_pos) { if materials[atom].is_powder() || materials[atom].is_solid() { @@ -72,14 +80,15 @@ pub fn on_ground(chunk_manager: &ChunkManager, actor: &Actor, materials: &Materi pub fn update_actors( mut chunk_manager: ResMut, - mut actors: Query<&mut Actor>, + mut actors: Query<(&mut Actor, &mut Transform)>, materials: (Res>, Res), ) { let materials = materials.0.get(materials.1 .0.clone()).unwrap(); - for mut actor in actors.iter_mut() { - let mut prev = actor.pos; - for v in Line::new(actor.pos, actor.vel.as_ivec2()) { + for (mut actor, mut transform) in actors.iter_mut() { + let mut actor_pos = transform.world_pos().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) { (true, false) => true, (false, true) => false, @@ -91,23 +100,31 @@ pub fn update_actors( let moved_x = move_x( &mut chunk_manager, &mut actor, + &mut actor_pos, (v.x - prev.x).signum(), materials, ); - if on_ground(&chunk_manager, &actor, materials) { - let starting_y = actor.pos.y; + if on_ground(&chunk_manager, &actor, &actor_pos, materials) { + let starting_y = actor_pos.y; match moved_x { //If we can't move to the left or right //Check if we can get up a stair-like structure false => { for i in 1..=UP_WALK_HEIGHT { - let moved_y = move_y(&mut chunk_manager, &mut actor, -1, materials); + let moved_y = move_y( + &mut chunk_manager, + &mut actor, + &mut actor_pos, + -1, + materials, + ); //Abort if we couldn't move up, or if we moved up but couldn't move sideways on the last step if !moved_y || i == UP_WALK_HEIGHT && !move_x( &mut chunk_manager, &mut actor, + &mut actor_pos, (v.x - prev.x).signum(), materials, ) @@ -115,6 +132,7 @@ pub fn update_actors( abort_stair( &mut chunk_manager, &mut actor, + &mut actor_pos, starting_y, 1, materials, @@ -127,14 +145,20 @@ pub fn update_actors( //Check if we can snap back to the ground true => { for i in 1..=DOWN_WALK_HEIGHT { - if !move_y(&mut chunk_manager, &mut actor, 1, materials) - && on_ground(&chunk_manager, &actor, materials) + if !move_y( + &mut chunk_manager, + &mut actor, + &mut actor_pos, + 1, + materials, + ) && on_ground(&chunk_manager, &actor, &actor_pos, materials) { break; } else if i == DOWN_WALK_HEIGHT { abort_stair( &mut chunk_manager, &mut actor, + &mut actor_pos, starting_y, -1, materials, @@ -148,6 +172,7 @@ pub fn update_actors( move_y( &mut chunk_manager, &mut actor, + &mut actor_pos, (v.y - prev.y).signum(), materials, ); @@ -155,30 +180,34 @@ pub fn update_actors( prev = v; } + + transform.update_world_pos(&actor_pos.as_vec2()) } } pub fn abort_stair( chunk_manager: &mut ChunkManager, actor: &mut Actor, + actor_pos: &mut IVec2, starting_y: i32, dir: i32, materials: &Materials, ) { - for _ in 0..(starting_y - actor.pos.y) { - move_y(chunk_manager, actor, dir, materials); + for _ in 0..(starting_y - actor_pos.y) { + move_y(chunk_manager, actor, actor_pos, dir, materials); } } pub fn move_x( chunk_manager: &mut ChunkManager, actor: &mut Actor, + actor_pos: &mut IVec2, dir: i32, materials: &Materials, ) -> bool { //Check if we can move for y_off in 0..actor.height as i32 { - let pos = actor.pos + let pos = *actor_pos + if dir > 0 { // Moving right ivec2(actor.width as i32, y_off) @@ -200,7 +229,7 @@ pub fn move_x( } } - actor.pos.x += dir; + actor_pos.x += dir; true } @@ -208,12 +237,13 @@ pub fn move_x( pub fn move_y( chunk_manager: &mut ChunkManager, actor: &mut Actor, + actor_pos: &mut IVec2, dir: i32, materials: &Materials, ) -> bool { //Check if we can move for x_off in 0..actor.width as i32 { - let pos = actor.pos + let pos = *actor_pos + if dir > 0 { // Moving down ivec2(x_off, actor.height as i32) @@ -235,11 +265,35 @@ pub fn move_y( } } - actor.pos.y += dir; + actor_pos.y += dir; true } +pub trait WorldPos { + fn world_pos(&self) -> Vec2; +} + +impl WorldPos for Transform { + fn world_pos(&self) -> Vec2 { + let mut pos = self.translation.xy(); + pos.y *= -1.; + + pos + } +} + +pub trait UpdateWorldPos { + fn update_world_pos(&mut self, 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; + } +} + pub struct ActorsPlugin; impl Plugin for ActorsPlugin { fn build(&self, app: &mut App) { diff --git a/src/chunk_group.rs b/src/chunk_group.rs index 231c4e7..b657cc5 100644 --- a/src/chunk_group.rs +++ b/src/chunk_group.rs @@ -257,6 +257,8 @@ pub fn update_chunk_groups<'a>( } scope.spawn(async move { + puffin::profile_scope!("Get chunk group"); + //If not a center chunk in our current update step, or we don't have the chunk, continue let same_x = (chunk_pos.x + x_toff + manager_pos.x.abs() % 2) % 2 == 0; let same_y = (chunk_pos.y + y_toff + manager_pos.y.abs() % 2) % 2 == 0; diff --git a/src/chunk_manager.rs b/src/chunk_manager.rs index d4644a3..b061502 100644 --- a/src/chunk_manager.rs +++ b/src/chunk_manager.rs @@ -509,7 +509,7 @@ pub fn update_manager_pos( mut commands: Commands, chunk_textures: Query>, image_entities: Query<(&Parent, Entity, &Handle)>, - player: Query<&Actor, With>, + player: Query<&Transform, With>, resources: ( ResMut, ResMut, @@ -519,7 +519,7 @@ pub fn update_manager_pos( ) { let (mut saving_task, mut chunk_manager, mut images) = resources; - let mut player_pos = player.single().pos; + let mut player_pos = player.single().world_pos().as_ivec2(); if player_pos.x < 0 { player_pos.x -= CHUNK_LENGHT as i32 } diff --git a/src/debug.rs b/src/debug.rs index 837efb4..790177f 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -128,8 +128,8 @@ pub fn render_dirty_rects(mut commands: Commands, dirty_rects: Res) } } -fn render_actors(mut commands: Commands, actors: Query<&Actor>) { - for actor in actors.iter() { +fn render_actors(mut commands: Commands, actors: Query<(&Actor, &Transform)>) { + for (actor, transform) in actors.iter() { commands .spawn(SpriteBundle { sprite: Sprite { @@ -138,11 +138,7 @@ fn render_actors(mut commands: Commands, actors: Query<&Actor>) { anchor: Anchor::TopLeft, ..default() }, - transform: Transform::from_translation(Vec3::new( - actor.pos.x as f32, - -actor.pos.y as f32, - 1., - )), + transform: *transform, ..default() }) .insert(DeleteImage); diff --git a/src/main.rs b/src/main.rs index 849aea8..beca742 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,10 +69,10 @@ fn main() { ParticlesPlugin, MaterialsPlugin, CameraPlugin, - //RigidbodyPlugin, + RigidbodyPlugin, )) .add_plugins(( - RapierPhysicsPlugin::::pixels_per_meter(6.), + RapierPhysicsPlugin::::pixels_per_meter(1.), MenuPlugin, )) .add_systems(Startup, setup); diff --git a/src/player.rs b/src/player.rs index 2d35866..a6e441c 100644 --- a/src/player.rs +++ b/src/player.rs @@ -19,13 +19,14 @@ impl Default for Player { } } +/*TODO Find a way to redo this impl Drop for Actor { fn drop(&mut self) { let file = File::create("assets/world/player").unwrap(); let mut buffered = BufWriter::new(file); bincode::serialize_into(&mut buffered, &self.pos).unwrap(); } -} +}*/ #[derive(Default)] pub enum PlayerState { @@ -61,7 +62,6 @@ pub fn player_setup( let player_actor = Actor { height: 17, width: 10, - pos, vel: vec2(0., 0.), }; @@ -70,7 +70,11 @@ pub fn player_setup( TextureAtlas::from_grid(player_handle, Vec2::new(24.0, 24.0), 8, 5, None, None); let player_atlas_handle = texture_atlases.add(player_atlas); let animation_indices = AnimationIndices { first: 0, last: 1 }; - let player_transform = GlobalTransform::from_xyz(5. * 3., -8. * 3., PLAYER_LAYER); + let player_transform = GlobalTransform::from_xyz( + 5. * 3. + pos.x as f32, + -8. * 3. + pos.y as f32, + PLAYER_LAYER, + ); let tool_handle = asset_server.load("player/player_tool.png"); let tool_bundle = SpriteBundle { @@ -106,7 +110,9 @@ pub fn player_setup( }, animation_indices, AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)), - bevy_rapier2d::prelude::RigidBody::Fixed, + bevy_rapier2d::prelude::Velocity::zero(), + bevy_rapier2d::prelude::GravityScale(0.), + bevy_rapier2d::prelude::RigidBody::Dynamic, bevy_rapier2d::prelude::LockedAxes::ROTATION_LOCKED, bevy_rapier2d::prelude::Collider::cuboid( player_actor.width as f32 / 2., @@ -119,13 +125,13 @@ pub fn player_setup( /// Updates player pub fn update_player( input: (Res, EventReader), - mut player: Query<(&mut Actor, &mut Player, &mut AnimationIndices)>, + mut player: Query<(&mut Actor, &mut Player, &mut AnimationIndices, &Transform)>, chunk_manager: ResMut, materials: (Res>, Res), time: Res