Implement ability to lose.
This commit is contained in:
parent
c18cfe9c5c
commit
a5314ca0c0
2 changed files with 51 additions and 6 deletions
26
src/lib.rs
26
src/lib.rs
|
@ -24,6 +24,7 @@ pub struct Game {
|
|||
stars: Vec<Star>,
|
||||
diag: bool,
|
||||
victory: bool,
|
||||
loss: bool,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
|
@ -40,16 +41,22 @@ impl Game {
|
|||
stars: stars,
|
||||
diag: false,
|
||||
victory: false,
|
||||
loss: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_update(&mut self, e: &Input, upd: UpdateArgs, w: &PistonWindow) {
|
||||
if self.victory || self.loss {
|
||||
return;
|
||||
}
|
||||
self.scene.event(e);
|
||||
|
||||
let mut grew = false;
|
||||
for mut star in &mut self.stars {
|
||||
if !star.destroyed && self.player.collides(&star) {
|
||||
star.destroy(&mut self.scene, upd.dt);
|
||||
self.player.grow(&mut self.scene, upd.dt);
|
||||
grew = true;
|
||||
} else {
|
||||
star.mov(w, &mut self.scene, upd.dt);
|
||||
}
|
||||
|
@ -57,10 +64,19 @@ impl Game {
|
|||
|
||||
if self.stars.iter().all(|star| star.destroyed) {
|
||||
self.victory = true;
|
||||
self.loss = false;
|
||||
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) {
|
||||
|
@ -92,6 +108,14 @@ impl Game {
|
|||
c.transform.trans(200.0, 240.0), g
|
||||
);
|
||||
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 {
|
||||
|
|
|
@ -15,6 +15,7 @@ pub struct Hero {
|
|||
pub dir: (f64, f64),
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
pub size: u32,
|
||||
w: f64,
|
||||
h: f64,
|
||||
collider: Cuboid2<f64>,
|
||||
|
@ -29,7 +30,7 @@ impl Hero {
|
|||
assets.join("soot.png"),
|
||||
Flip::None,
|
||||
&TextureSettings::new())
|
||||
.unwrap());
|
||||
.unwrap());
|
||||
let mut sprite = Sprite::from_texture(tex);
|
||||
sprite.set_position(320.0, 240.0);
|
||||
let bounds = sprite.bounding_box();
|
||||
|
@ -41,6 +42,7 @@ impl Hero {
|
|||
h: bounds[3],
|
||||
dir: (0.0, 0.0),
|
||||
sprite_id: sprite_id,
|
||||
size: 2000,
|
||||
collider: Cuboid2::new(Vector2::new(bounds[2], bounds[3])),
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +53,9 @@ impl Hero {
|
|||
self.x = sprite_x;
|
||||
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 {
|
||||
self.x = new_x;
|
||||
self.y = new_y;
|
||||
|
@ -61,11 +65,25 @@ impl Hero {
|
|||
}
|
||||
let mov_x = self.dir.0 * 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) {
|
||||
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 {
|
||||
|
@ -75,6 +93,9 @@ impl Hero {
|
|||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue