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

@ -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())
}
}