Skip to content

Instantly share code, notes, and snippets.

@killercup
Last active July 24, 2022 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killercup/3fe777f4a178aac4568c05dd621644b6 to your computer and use it in GitHub Desktop.
Save killercup/3fe777f4a178aac4568c05dd621644b6 to your computer and use it in GitHub Desktop.
(step 1 from post) Rebuilding Bevy system functions
// ------------------
// The game code
// ------------------
fn main() {
App::new().add_system(example_system).run();
}
fn example_system() {
println!("foo");
}
// ------------------
// App boilerplate
// ------------------
struct App {
systems: Vec<Box<dyn System>>,
}
impl App {
fn new() -> App {
App {
systems: Vec::new(),
}
}
fn add_system<S: System>(mut self, function: S) -> Self {
self.systems.push(Box::new(function));
self
}
fn run(&mut self) {
for system in &mut self.systems {
system.run();
}
}
}
// ------------------
// Systems magic
// ------------------
trait System: 'static {
fn run(&mut self);
}
impl<F> System for F
where
F: Fn() -> () + 'static,
{
fn run(&mut self) {
self();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment