59 lines
1.6 KiB
Rust
59 lines
1.6 KiB
Rust
use crate::constants::*;
|
|
use bevy::prelude::*;
|
|
use rand::prelude::*;
|
|
|
|
#[derive(Component)]
|
|
#[require(Transform)]
|
|
pub struct Ambulatory {
|
|
speed: f32,
|
|
}
|
|
|
|
#[derive(Bundle)]
|
|
pub struct Citizen {
|
|
walker: Ambulatory,
|
|
sprite: Sprite,
|
|
transform: Transform,
|
|
}
|
|
|
|
impl Citizen {
|
|
pub fn new(asset_server: &Res<AssetServer>, position: Vec3) -> Self {
|
|
Citizen {
|
|
walker: Ambulatory { speed: TILE_SIZE },
|
|
sprite: Sprite {
|
|
image: asset_server.load("character.png"),
|
|
..Default::default()
|
|
},
|
|
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn citizen_movement(mut query: Query<(&Ambulatory, &mut Transform), With<Ambulatory>>) {
|
|
for (citizen, mut transform) in query.iter_mut() {
|
|
if random::<f32>() < 0.66666 {
|
|
continue;
|
|
}
|
|
let mut direction = Vec2::ZERO;
|
|
|
|
direction.y += rand::random::<f32>();
|
|
direction.y -= rand::random::<f32>();
|
|
direction.x -= rand::random::<f32>();
|
|
direction.x += rand::random::<f32>();
|
|
|
|
if direction.length() > 0.0 {
|
|
direction.x = direction.x.round();
|
|
direction.y = direction.y.round();
|
|
|
|
let movement = direction * citizen.speed;
|
|
transform.translation.x += movement.x;
|
|
transform.translation.y += movement.y;
|
|
|
|
if direction.x > 0.0 {
|
|
transform.scale.x = PIXEL_RATIO;
|
|
} else if direction.x < 0.0 {
|
|
transform.scale.x = -PIXEL_RATIO;
|
|
}
|
|
}
|
|
}
|
|
}
|