The position component is redundant with the built-in Bevy Transform. I've updated the velocity integrator to use the transform directly. Physics still works, but things still set their initial locations through the Position component. I'll need to fix all those call sites.
70 lines
1.9 KiB
Rust
70 lines
1.9 KiB
Rust
//! Custom physics items
|
|
//! TODO: Refactor in terms of Rapier2D, *or* implement colliders and remove it.
|
|
|
|
use crate::WorldSize;
|
|
|
|
use bevy::prelude::*;
|
|
|
|
#[derive(Component)]
|
|
pub(crate) struct Position(pub(crate) bevy::math::Vec2);
|
|
|
|
#[derive(Component)]
|
|
pub(crate) struct Velocity(pub(crate) bevy::math::Vec2);
|
|
|
|
#[derive(Component)]
|
|
pub(crate) struct Rotation(pub(crate) f32);
|
|
|
|
/// Marker for any entity that should wrap on screen edges
|
|
#[derive(Component)]
|
|
pub(crate) struct Wrapping;
|
|
|
|
// TODO: Combine movement integration steps into one function
|
|
// They need to be ordered so the physics is deterministic. Bevy can enforce
|
|
// order, but it makes more sense to cut out the extra machinery and have one
|
|
// single function. Probably better for cache locality or whatever, too.
|
|
/*
|
|
Add velocity to position
|
|
*/
|
|
pub(crate) fn integrate_velocity(
|
|
mut query: Query<(&mut Transform, &Velocity)>,
|
|
time: Res<Time>
|
|
) {
|
|
for (mut transform, velocity) in &mut query {
|
|
let delta = velocity.0 * time.delta_secs();
|
|
transform.translation += delta.extend(0.0);
|
|
}
|
|
}
|
|
|
|
/*
|
|
Assigns the rotation to the transform by copying it from the Rotation component.
|
|
*/
|
|
pub(crate) fn apply_rotation_to_mesh(mut query: Query<(&mut Transform, &Rotation)>) {
|
|
for (mut transform, rotation) in &mut query {
|
|
transform.rotation = Quat::from_rotation_z(rotation.0);
|
|
}
|
|
}
|
|
|
|
pub(crate) fn wrap_entities(
|
|
mut query: Query<&mut Position, With<Wrapping>>,
|
|
world_size: Res<WorldSize>,
|
|
) {
|
|
let right = world_size.width / 2.0;
|
|
let left = -right;
|
|
let top = world_size.height / 2.0;
|
|
let bottom = -top;
|
|
|
|
for mut pos in query.iter_mut() {
|
|
if pos.0.x > right {
|
|
pos.0.x = left;
|
|
} else if pos.0.x < left {
|
|
pos.0.x = right;
|
|
}
|
|
|
|
if pos.0.y > top {
|
|
pos.0.y = bottom;
|
|
} else if pos.0.y < bottom {
|
|
pos.0.y = top;
|
|
}
|
|
}
|
|
}
|