Add multiple candies

This commit is contained in:
Thomas Gideon 2017-07-13 19:37:47 -04:00
parent 1d859a3e7d
commit a8f313c8e8
2 changed files with 30 additions and 13 deletions

View file

@ -21,32 +21,44 @@ use sprite::*;
pub struct Game { pub struct Game {
scene: Scene<Texture<Resources>>, scene: Scene<Texture<Resources>>,
player: Hero, player: Hero,
star: Star, stars: Vec<Star>,
diag: bool, diag: bool,
victory: bool,
} }
impl Game { impl Game {
pub fn new(w: &mut PistonWindow) -> Game { pub fn new(w: &mut PistonWindow) -> Game {
let mut scene = Scene::new(); let mut scene = Scene::new();
let star = Star::new(w, &mut scene); let mut stars: Vec<Star> = vec![];
for _ in 1..5 {
stars.push(Star::new(w, &mut scene));
}
let player = Hero::new(w, &mut scene); let player = Hero::new(w, &mut scene);
Game { Game {
scene: scene, scene: scene,
player: player, player: player,
star: star, stars: stars,
diag: false, diag: false,
victory: false,
} }
} }
pub fn on_update(&mut self, e: &Input, upd: UpdateArgs, w: &PistonWindow) { pub fn on_update(&mut self, e: &Input, upd: UpdateArgs, w: &PistonWindow) {
self.scene.event(e); self.scene.event(e);
if !self.star.destroyed && self.player.collides(&self.star) { for mut star in &mut self.stars {
self.star.destroy(&mut self.scene, upd.dt); if !star.destroyed && self.player.collides(&star) {
self.player.grow(&mut self.scene, upd.dt); star.destroy(&mut self.scene, upd.dt);
self.player.grow(&mut self.scene, upd.dt);
} else {
star.mov(w, &mut self.scene, upd.dt);
}
} }
self.star.mov(w, &mut self.scene, upd.dt); if self.stars.iter().all(|star| star.destroyed) {
self.victory = true;
return;
}
self.player.mov(w, &mut self.scene, upd.dt); self.player.mov(w, &mut self.scene, upd.dt);
} }
@ -72,9 +84,19 @@ impl Game {
image.draw(&bg, &Default::default(), c.transform.trans(x, y).zoom(0.1), g); image.draw(&bg, &Default::default(), c.transform.trans(x, y).zoom(0.1), g);
} }
if self.victory {
text::Text::new_color([0.0, 1.0, 0.0, 1.0], 24).draw(
"Hurray! You win!",
&mut glyphs,
&c.draw_state,
c.transform.trans(200.0, 240.0), g
);
return;
}
if self.diag { if self.diag {
text::Text::new_color([0.0, 1.0, 0.0, 1.0], 10).draw( text::Text::new_color([0.0, 1.0, 0.0, 1.0], 10).draw(
&format!("{} {}", self.player.diag(), self.star.diag()), &format!("{}", self.player.diag()),
&mut glyphs, &mut glyphs,
&c.draw_state, &c.draw_state,
c.transform.trans(10.0, 10.0), g c.transform.trans(10.0, 10.0), g

View file

@ -76,11 +76,6 @@ impl Star {
scene.run(self.sprite_id, &Action(Ease(EaseFunction::CubicOut, Box::new(FadeOut(dt * 0.75))))); scene.run(self.sprite_id, &Action(Ease(EaseFunction::CubicOut, Box::new(FadeOut(dt * 0.75)))));
self.destroyed = true; self.destroyed = true;
} }
pub fn diag(&self) -> String {
format!("{}: x {} / y {}", self.sprite_id, self.x.trunc(), self.y.trunc())
}
} }
fn rand_dir() -> u32 { fn rand_dir() -> u32 {