Last active
July 24, 2022 19:57
-
-
Save killercup/3fe777f4a178aac4568c05dd621644b6 to your computer and use it in GitHub Desktop.
(step 1 from post) Rebuilding Bevy system functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ------------------ | |
// 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