use crate::deref_as; use bevy::math::Vec2; use bevy::prelude::{Component, Query, Res, Time, Transform}; use std::ops::Mul; pub const DIR_LEFT: Vec2 = Vec2::new(-1.0, 0.0); pub const DIR_RIGHT: Vec2 = Vec2::new(1.0, 0.0); #[derive(Clone, Copy, Debug, Component)] pub struct Velocity(Vec2); deref_as!(mut Velocity => Vec2); impl From<Vec2> for Velocity { fn from(value: Vec2) -> Self { Velocity(value) } } impl Default for Velocity { fn default() -> Self { Self(Vec2::new(100.0, 100.0)) } } impl<T> Mul<T> for Velocity where Vec2: Mul<T, Output = Vec2>, { type Output = Velocity; fn mul(self, rhs: T) -> Self::Output { Velocity::from(self.0.mul(rhs)) } } impl Velocity { pub const ZERO: Velocity = Velocity(Vec2::ZERO); } pub fn apply_velocity(delta: Res<Time>, mut entity_query: Query<(&Velocity, &mut Transform)>) { let dt = delta.delta_seconds(); for (velocity, mut transform) in &mut entity_query { transform.translation += velocity.extend(0.0) * dt; } }