Skip to content

Commit

Permalink
feat: 基于 World 和 Schedule 的原始框架构建
Browse files Browse the repository at this point in the history
  • Loading branch information
mjczz committed Dec 31, 2024
1 parent a9a8757 commit 6631d07
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ path = "src/bin/resource.rs"
name = "move1"
path = "src/bin/physics_in_fixed_timestep.rs"

[[bin]]
name = "world"
path = "src/bin/world.rs"

[workspace]
resolver = "2" # Important! wgpu/Bevy needs this!

Expand Down
77 changes: 77 additions & 0 deletions src/bin/world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use bevy::prelude::*;
use std::time::Duration;

#[derive(Component)]
struct Person;

#[derive(Component)]
struct Name(String);

// add people to our World using a "startup system".
// startup systems are just like normal systems, but they run exactly once, before all other systems, right when our app starts.
// use Commands to spawn some entities into our World:
fn add_people(mut commands: Commands) {
println!("adding people");
commands.spawn((Person, Name("Elaina Proctor".to_string())));
commands.spawn((Person, Name("Renzo Hume".to_string())));
commands.spawn((Person, Name("Zayna Nieves".to_string())));
}

// make query mutable, and use a mutable reference (&mut) to the components we want to change
fn update_people(mut query: Query<&mut Name, With<Person>>) {
println!("update people");
for mut name in &mut query {
if name.0 == "Elaina Proctor" {
name.0 = "New Name Elaina Hume".to_string();
break; // We don't need to change any other names.
}
}
}

#[derive(Resource)]
struct GreetTimer(Timer);

// customize some Resource
#[derive(Resource)]
struct CalledTimes(i32);

fn greet_people(
mut time: ResMut<Time>,
mut timer: ResMut<GreetTimer>,
mut demo: ResMut<CalledTimes>,
query: Query<&Name, With<Person>>,
) {
// Update the timer with the elapsed time since the last frame
time.advance_to(Duration::from_secs(3));
loop {
println!("{:?}", time.delta());
if timer.0.tick(time.delta()).just_finished() {
demo.0 += 1;
for name in query.iter() {
println!("hello {}! called {} times", name.0, demo.0);
}
}
}
}

#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
enum SimulationSet {
Spawn,
Age,
}

fn main() {
let mut world = World::new();
world.insert_resource(CalledTimes { 0: 0 });
world.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)));
world.init_resource::<Time>();

let mut schedule = Schedule::default();
schedule.add_systems((
add_people.in_set(SimulationSet::Spawn),
(update_people, greet_people)
.chain()
.after(SimulationSet::Spawn),
));
schedule.run(&mut world);
}

0 comments on commit 6631d07

Please sign in to comment.