Skip to content

Commit

Permalink
new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac8668 committed Feb 4, 2024
1 parent 2bf0129 commit 8774575
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
22 changes: 12 additions & 10 deletions src/actors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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.;
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/chunk_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -509,7 +510,7 @@ pub fn update_manager_pos(
mut commands: Commands,
chunk_textures: Query<Entity, With<ChunksParent>>,
image_entities: Query<(&Parent, Entity, &Handle<Image>)>,
player: Query<&Transform, With<Player>>,
player: Query<(&Actor, &Transform), With<Player>>,
resources: (
ResMut<SavingTask>,
ResMut<ChunkManager>,
Expand All @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::prelude::Color;
use bevy_rapier2d::geometry::Group;

// Chunk Length consts
// Chunk length MUST be divisible by 4
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ pub fn render_dirty_rects(mut commands: Commands, dirty_rects: Res<DirtyRects>)

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 {
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
);

Expand Down
4 changes: 4 additions & 0 deletions src/rigidbody.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8774575

Please sign in to comment.