Created
November 28, 2023 16:43
-
-
Save computermouth/4f435a880d539c54fb101b269d4c017e to your computer and use it in GitHub Desktop.
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
#[derive(Debug)] | |
struct Entity { | |
x: u16, | |
y: u16, | |
} | |
impl Entity { | |
fn update(&mut self) -> Option<Entity> { | |
if self.x == u16::MAX { | |
self.x = 0; | |
} else { | |
self.x += 1; | |
} | |
if self.y == u16::MAX { | |
self.y = 0; | |
} else { | |
self.y += 1; | |
} | |
if self.x == 0 || self.y == 0 { | |
return Some(self.spawn()); | |
} | |
None | |
} | |
fn spawn(&self) -> Entity { | |
Entity { | |
x: self.x + 1, | |
y: self.y + 1, | |
} | |
} | |
} | |
fn main() { | |
let mut evec = vec![ | |
Entity { x: 1, y: 1 }, | |
Entity { x: 2, y: 2 }, | |
Entity { | |
x: u16::MAX - 1, | |
y: 3, | |
}, | |
]; | |
for _ in 0..10 { | |
let mut new_entities_this_frame = Vec::new(); | |
for i in &mut evec { | |
if let Some(e) = i.update() { | |
new_entities_this_frame.push(e); | |
} | |
} | |
evec.append(&mut new_entities_this_frame); | |
} | |
println!("Hello, {:?}!", evec); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment