Skip to content

Instantly share code, notes, and snippets.

@copygirl
Created July 9, 2019 16:51
Show Gist options
  • Save copygirl/a2620b60b5fd08c06711f75eae8f0d6d to your computer and use it in GitHub Desktop.
Save copygirl/a2620b60b5fd08c06711f75eae8f0d6d to your computer and use it in GitHub Desktop.
type SystemData = (
WriteStorage<'s, Ball>,
WriteStorage<'s, Transform>,
Read<'s, EventChannel<ScoreEvent>>,
Read<'s, InputHandler<StringBindings>>,
Read<'s, Time>,
);
fn setup(&mut self, res: &mut Resources) {
Self::SystemData::setup(res);
self.waiting_for_input = true;
self.reader = Some(res.fetch_mut::<EventChannel<ScoreEvent>>().register_reader());
}
fn run(&mut self, (
mut balls,
mut transforms,
score_channel,
input,
time
): Self::SystemData) {
// If all balls are gone, create a new one.
for event in score_channel.read(self.reader.as_mut().unwrap()) {
if balls.count() == 0 {
self.waiting_for_input = true;
}
}
impl<'s> System<'s> for ScoreSystem {
type SystemData = (
Entities<'s>,
WriteStorage<'s, Ball>,
WriteStorage<'s, Transform>,
Write<'s, EventChannel<ScoreEvent>>,
);
fn run(&mut self, (entities, mut balls, mut transforms, mut channel): Self::SystemData) {
for (entity, ball, transform) in (&*entities, &mut balls, &mut transforms).join() {
let ball_x = transform.translation().x;
let score_side = if ball_x.as_f32() <= ball.radius {
Option::Some(Side::Right)
} else if ball_x.as_f32() >= ARENA_WIDTH - ball.radius {
Option::Some(Side::Left)
} else {
Option::None
};
match score_side {
Some(side) => {
entities.delete(entity).unwrap();
channel.single_write(ScoreEvent::new(side));
}
None => { }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment