implemented game loop!
This commit is contained in:
@@ -1,30 +1,24 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
// Common
|
||||||
pub enum Item {
|
|
||||||
None,
|
|
||||||
Burger,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct Customer {
|
|
||||||
pub order: Item,
|
|
||||||
pub served: bool,
|
|
||||||
pub range: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct Fryer {
|
|
||||||
pub range: f32,
|
|
||||||
pub speed: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Unit {
|
pub struct Unit {
|
||||||
pub move_speed: f32,
|
pub move_speed: f32,
|
||||||
pub height_offset: f32,
|
pub height_offset: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct MoveTarget {
|
||||||
|
pub position: Vec3,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Player
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
pub enum Item {
|
||||||
|
None,
|
||||||
|
Burger,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
pub holding: Item,
|
pub holding: Item,
|
||||||
@@ -39,7 +33,34 @@ pub struct ProgressBar {
|
|||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct PlayerBurgerIndicator;
|
pub struct PlayerBurgerIndicator;
|
||||||
|
|
||||||
|
// Customer
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct MoveTarget {
|
pub struct Customer {
|
||||||
pub position: Vec3,
|
pub order: Item,
|
||||||
|
pub served: bool,
|
||||||
|
pub range: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct CustomerSpawner {
|
||||||
|
pub timer: Timer,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct CounterQueue {
|
||||||
|
pub positions: Vec<Vec3>,
|
||||||
|
pub occupied: Vec<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Fryer {
|
||||||
|
pub range: f32,
|
||||||
|
pub speed: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct QueueSlot(pub usize);
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct CustomerBurgerIndicator;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use bevy::prelude::*;
|
|||||||
mod components;
|
mod components;
|
||||||
mod systems;
|
mod systems;
|
||||||
|
|
||||||
use systems::{gameplay::*, movement::*, setup::*};
|
use systems::{customer::*, gameplay::*, movement::*, setup::*};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
@@ -17,7 +17,10 @@ fn main() {
|
|||||||
fryer_cook,
|
fryer_cook,
|
||||||
customer_serve,
|
customer_serve,
|
||||||
update_progress_bar,
|
update_progress_bar,
|
||||||
update_burger_indicators,
|
update_player_indicator,
|
||||||
|
spawn_customers,
|
||||||
|
despawn_customers,
|
||||||
|
update_customer_indicators,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.run();
|
.run();
|
||||||
|
|||||||
87
src/systems/customer.rs
Normal file
87
src/systems/customer.rs
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
use crate::components::*;
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
pub fn spawn_customers(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut spawner: ResMut<CustomerSpawner>,
|
||||||
|
mut queue: ResMut<CounterQueue>,
|
||||||
|
time: Res<Time>,
|
||||||
|
asset_server: Res<AssetServer>,
|
||||||
|
) {
|
||||||
|
spawner.timer.tick(time.delta());
|
||||||
|
|
||||||
|
// Guard clause
|
||||||
|
if !spawner.timer.just_finished() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check positions
|
||||||
|
let available_slot = queue.occupied.iter().position(|&occupied| !occupied);
|
||||||
|
|
||||||
|
let Some(slot_index) = available_slot else {
|
||||||
|
println!("FULL");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Take first location
|
||||||
|
let target_position = queue.positions[slot_index];
|
||||||
|
queue.occupied[slot_index] = true;
|
||||||
|
|
||||||
|
commands.spawn((
|
||||||
|
SceneRoot(asset_server.load("models/customer.glb#Scene0")),
|
||||||
|
Transform::from_xyz(20.0, 1.5, 0.0).with_scale(Vec3::splat(0.5)),
|
||||||
|
Unit {
|
||||||
|
move_speed: 5.0,
|
||||||
|
height_offset: 1.5,
|
||||||
|
},
|
||||||
|
Customer {
|
||||||
|
order: Item::Burger,
|
||||||
|
served: false,
|
||||||
|
range: 4.0,
|
||||||
|
},
|
||||||
|
MoveTarget {
|
||||||
|
position: target_position,
|
||||||
|
},
|
||||||
|
QueueSlot(slot_index),
|
||||||
|
children![(
|
||||||
|
SceneRoot(asset_server.load("models/burger.glb#Scene0")),
|
||||||
|
Transform::from_xyz(0.0, 7.1, 0.0).with_scale(Vec3::splat(0.9)),
|
||||||
|
Visibility::Hidden,
|
||||||
|
CustomerBurgerIndicator,
|
||||||
|
)],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn despawn_customers(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut queue: ResMut<CounterQueue>,
|
||||||
|
customers: Query<(Entity, &Transform, &Customer, &QueueSlot)>,
|
||||||
|
) {
|
||||||
|
for (entity, transform, customer, slot) in customers.iter() {
|
||||||
|
if customer.served && transform.translation.x > 25.0 {
|
||||||
|
// Customer reached border
|
||||||
|
queue.occupied[slot.0] = false;
|
||||||
|
|
||||||
|
commands.entity(entity).despawn();
|
||||||
|
|
||||||
|
println!("CUSTOMER DELETED!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_customer_indicators(
|
||||||
|
customers: Query<(&Customer, &Children)>,
|
||||||
|
mut burgers: Query<&mut Visibility, With<CustomerBurgerIndicator>>,
|
||||||
|
) {
|
||||||
|
for (customer, children) in customers.iter() {
|
||||||
|
for child in children.iter() {
|
||||||
|
if let Ok(mut visibility) = burgers.get_mut(child) {
|
||||||
|
*visibility = if customer.served {
|
||||||
|
Visibility::Visible
|
||||||
|
} else {
|
||||||
|
Visibility::Hidden
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
pub mod customer;
|
||||||
pub mod gameplay;
|
pub mod gameplay;
|
||||||
pub mod movement;
|
pub mod movement;
|
||||||
pub mod setup;
|
pub mod setup;
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ pub fn update_progress_bar(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_burger_indicators(
|
pub fn update_player_indicator(
|
||||||
player: Query<&Player>,
|
player: Query<&Player>,
|
||||||
mut burger: Query<(&mut Visibility, &mut Transform), With<PlayerBurgerIndicator>>,
|
mut burger: Query<(&mut Visibility, &mut Transform), With<PlayerBurgerIndicator>>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ pub fn setup(
|
|||||||
)),
|
)),
|
||||||
));
|
));
|
||||||
|
|
||||||
// Plane
|
// Floor plane
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(50.0)))),
|
Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(50.0)))),
|
||||||
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
|
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
|
||||||
@@ -94,21 +94,17 @@ pub fn setup(
|
|||||||
NotShadowCaster,
|
NotShadowCaster,
|
||||||
));
|
));
|
||||||
|
|
||||||
// Customer
|
// Customer spawner
|
||||||
commands.spawn((
|
commands.insert_resource(CounterQueue {
|
||||||
SceneRoot(asset_server.load("models/customer.glb#Scene0")),
|
positions: vec![
|
||||||
Transform::from_xyz(20.0, 1.5, 0.0).with_scale(Vec3::splat(0.5)),
|
Vec3::new(12.0, 0.0, -4.0),
|
||||||
Unit {
|
Vec3::new(12.0, 0.0, 0.0),
|
||||||
move_speed: 5.0,
|
Vec3::new(12.0, 0.0, 4.0),
|
||||||
height_offset: 1.5,
|
],
|
||||||
},
|
occupied: vec![false, false, false],
|
||||||
Customer {
|
});
|
||||||
order: Item::Burger,
|
|
||||||
served: false,
|
commands.insert_resource(CustomerSpawner {
|
||||||
range: 4.0,
|
timer: Timer::from_seconds(7.0, TimerMode::Repeating),
|
||||||
},
|
});
|
||||||
MoveTarget {
|
|
||||||
position: Vec3::new(12.0, 0.0, 0.0),
|
|
||||||
},
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user