Implement ability to lose.

This commit is contained in:
Thomas Gideon 2017-08-08 12:13:15 -04:00
parent c18cfe9c5c
commit a5314ca0c0
2 changed files with 51 additions and 6 deletions

View file

@ -24,6 +24,7 @@ pub struct Game {
stars: Vec<Star>, stars: Vec<Star>,
diag: bool, diag: bool,
victory: bool, victory: bool,
loss: bool,
} }
impl Game { impl Game {
@ -40,16 +41,22 @@ impl Game {
stars: stars, stars: stars,
diag: false, diag: false,
victory: false, victory: false,
loss: 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) {
if self.victory || self.loss {
return;
}
self.scene.event(e); self.scene.event(e);
let mut grew = false;
for mut star in &mut self.stars { for mut star in &mut self.stars {
if !star.destroyed && self.player.collides(&star) { if !star.destroyed && self.player.collides(&star) {
star.destroy(&mut self.scene, upd.dt); star.destroy(&mut self.scene, upd.dt);
self.player.grow(&mut self.scene, upd.dt); self.player.grow(&mut self.scene, upd.dt);
grew = true;
} else { } else {
star.mov(w, &mut self.scene, upd.dt); star.mov(w, &mut self.scene, upd.dt);
} }
@ -57,10 +64,19 @@ impl Game {
if self.stars.iter().all(|star| star.destroyed) { if self.stars.iter().all(|star| star.destroyed) {
self.victory = true; self.victory = true;
self.loss = false;
return; return;
} }
self.player.mov(w, &mut self.scene, upd.dt); if !grew {
self.player.shrink(&mut self.scene, upd.dt);
}
if self.player.size > 0 {
self.player.mov(w, &mut self.scene, upd.dt);
} else {
self.loss = true;
self.victory = false;
}
} }
pub fn on_draw(&mut self, e: &Input, _: RenderArgs, w: &mut PistonWindow) { pub fn on_draw(&mut self, e: &Input, _: RenderArgs, w: &mut PistonWindow) {
@ -92,6 +108,14 @@ impl Game {
c.transform.trans(200.0, 240.0), g c.transform.trans(200.0, 240.0), g
); );
return; return;
} else if self.loss {
text::Text::new_color([0.0, 1.0, 0.0, 1.0], 24).draw(
"Aw! You lose!",
&mut glyphs,
&c.draw_state,
c.transform.trans(200.0, 240.0), g
);
return;
} }
if self.diag { if self.diag {

View file

@ -15,6 +15,7 @@ pub struct Hero {
pub dir: (f64, f64), pub dir: (f64, f64),
pub x: f64, pub x: f64,
pub y: f64, pub y: f64,
pub size: u32,
w: f64, w: f64,
h: f64, h: f64,
collider: Cuboid2<f64>, collider: Cuboid2<f64>,
@ -29,7 +30,7 @@ impl Hero {
assets.join("soot.png"), assets.join("soot.png"),
Flip::None, Flip::None,
&TextureSettings::new()) &TextureSettings::new())
.unwrap()); .unwrap());
let mut sprite = Sprite::from_texture(tex); let mut sprite = Sprite::from_texture(tex);
sprite.set_position(320.0, 240.0); sprite.set_position(320.0, 240.0);
let bounds = sprite.bounding_box(); let bounds = sprite.bounding_box();
@ -41,6 +42,7 @@ impl Hero {
h: bounds[3], h: bounds[3],
dir: (0.0, 0.0), dir: (0.0, 0.0),
sprite_id: sprite_id, sprite_id: sprite_id,
size: 2000,
collider: Cuboid2::new(Vector2::new(bounds[2], bounds[3])), collider: Cuboid2::new(Vector2::new(bounds[2], bounds[3])),
} }
} }
@ -51,7 +53,9 @@ impl Hero {
self.x = sprite_x; self.x = sprite_x;
self.y = sprite_y; self.y = sprite_y;
} }
let (wrapped, new_x, new_y) = wrap((w.size().width.into(), w.size().height.into()), (self.w, self.h), (self.x, self.y)); let (wrapped, new_x, new_y) = wrap((w.size().width.into(), w.size().height.into()),
(self.w, self.h),
(self.x, self.y));
if wrapped { if wrapped {
self.x = new_x; self.x = new_x;
self.y = new_y; self.y = new_y;
@ -61,11 +65,25 @@ impl Hero {
} }
let mov_x = self.dir.0 * 2.0; let mov_x = self.dir.0 * 2.0;
let mov_y = self.dir.1 * 2.0; let mov_y = self.dir.1 * 2.0;
scene.run(self.sprite_id, &Action(Ease(EaseFunction::CubicOut, Box::new(MoveBy(dt * 0.75, mov_x, mov_y))))); scene.run(self.sprite_id,
&Action(Ease(EaseFunction::CubicOut,
Box::new(MoveBy(dt * 0.75, mov_x, mov_y)))));
} }
pub fn grow(&mut self, scene: &mut Scene<Texture<Resources>>, dt: f64) { pub fn grow(&mut self, scene: &mut Scene<Texture<Resources>>, dt: f64) {
scene.run(self.sprite_id, &Action(Ease(EaseFunction::ElasticInOut, Box::new(ScaleBy(dt * 5.0, 0.3, 0.3))))); self.size += 1;
scene.run(self.sprite_id,
&Action(Ease(EaseFunction::ElasticInOut,
Box::new(ScaleBy(dt * 5.0, 0.3, 0.3)))));
}
pub fn shrink(&mut self, scene: &mut Scene<Texture<Resources>>, dt: f64) {
if self.size > 0 {
self.size -= 1;
scene.run(self.sprite_id,
&Action(Ease(EaseFunction::ElasticInOut,
Box::new(ScaleBy(dt * 5.0, -0.005, -0.005)))));
}
} }
pub fn collides(&mut self, star: &Star) -> bool { pub fn collides(&mut self, star: &Star) -> bool {
@ -75,6 +93,9 @@ impl Hero {
} }
pub fn diag(&self) -> String { pub fn diag(&self) -> String {
format!("{}: x {} / y {}", self.sprite_id, self.x.trunc(), self.y.trunc()) format!("{}: x {} / y {}",
self.sprite_id,
self.x.trunc(),
self.y.trunc())
} }
} }