Make it possible to eat star candy

This commit is contained in:
Thomas Gideon 2017-07-13 18:53:30 -04:00
parent 69b8c5ee0c
commit 1d859a3e7d
4 changed files with 61 additions and 15 deletions

View file

@ -2,10 +2,13 @@ use ai_behavior::Action;
use find_folder;
use gfx_device_gl::Resources;
use piston_window::*;
use ncollide::shape::Cuboid2;
use ncollide::query::{proximity, Proximity};
use nalgebra::{self, Isometry2, Vector2};
use sprite::*;
use uuid::Uuid;
use std::rc::Rc;
use mobs::wrap;
use mobs::{wrap, Star};
pub struct Hero {
pub sprite_id: Uuid,
@ -14,6 +17,7 @@ pub struct Hero {
pub y: f64,
w: f64,
h: f64,
collider: Cuboid2<f64>,
}
impl Hero {
@ -37,6 +41,7 @@ impl Hero {
h: bounds[3],
dir: (0.0, 0.0),
sprite_id: sprite_id,
collider: Cuboid2::new(Vector2::new(bounds[2], bounds[3])),
}
}
@ -58,4 +63,18 @@ impl Hero {
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)))));
}
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)))));
}
pub fn collides(&mut self, star: &Star) -> bool {
let star_pos = Isometry2::new(Vector2::new(star.x, star.y), nalgebra::zero());
let pos = Isometry2::new(Vector2::new(self.x, self.y), nalgebra::zero());
proximity(&star_pos, &star.collider, &pos, &self.collider, 0.0) == Proximity::Intersecting
}
pub fn diag(&self) -> String {
format!("{}: x {} / y {}", self.sprite_id, self.x.trunc(), self.y.trunc())
}
}

View file

@ -2,13 +2,14 @@ use ai_behavior::Action;
use find_folder;
use gfx_device_gl::Resources;
use piston_window::*;
use ncollide::shape::Cuboid2;
use nalgebra::Vector2;
use sprite::*;
use uuid::Uuid;
use rand;
use std::rc::Rc;
use super::wrap;
pub struct Star {
pub sprite_id: Uuid,
pub x: f64,
@ -16,6 +17,8 @@ pub struct Star {
w: f64,
h: f64,
dir: u32,
pub collider: Cuboid2<f64>,
pub destroyed: bool,
}
impl Star {
@ -40,10 +43,15 @@ impl Star {
h: bounds[3],
sprite_id: sprite_id,
dir: rand_dir(),
collider: Cuboid2::new(Vector2::new(bounds[2], bounds[3])),
destroyed: false,
}
}
pub fn mov(&mut self, w: &PistonWindow, scene: &mut Scene<Texture<Resources>>, dt: f64) {
if self.destroyed {
return;
}
if let Some(sprite) = scene.child(self.sprite_id) {
let (sprite_x, sprite_y) = sprite.get_position();
self.x = sprite_x;
@ -63,6 +71,16 @@ impl Star {
let mov_y = 2.0 * dir.1;
scene.run(self.sprite_id, &Action(Ease(EaseFunction::CubicOut, Box::new(MoveBy(dt * 0.75, mov_x, mov_y)))));
}
pub fn destroy(&mut self, scene: &mut Scene<Texture<Resources>>, dt: f64) {
scene.run(self.sprite_id, &Action(Ease(EaseFunction::CubicOut, Box::new(FadeOut(dt * 0.75)))));
self.destroyed = true;
}
pub fn diag(&self) -> String {
format!("{}: x {} / y {}", self.sprite_id, self.x.trunc(), self.y.trunc())
}
}
fn rand_dir() -> u32 {