Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::deref_as;
use bevy::math::Vec2;
use bevy::prelude::{Component, Query, Res, Time, Transform};
use std::ops::Mul;
#[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;
}
}