Skip to content

Instantly share code, notes, and snippets.

@computermouth
Created November 28, 2023 16:43
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 computermouth/4f435a880d539c54fb101b269d4c017e to your computer and use it in GitHub Desktop.
Save computermouth/4f435a880d539c54fb101b269d4c017e to your computer and use it in GitHub Desktop.
#[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