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

@ -12,3 +12,5 @@ piston2d-graphics = "0.21.1"
find_folder = "0.3.0"
uuid = "0.1.17"
rand = "0.3"
nalgebra = "0.12.0"
ncollide = "0.12.0"

View file

@ -2,6 +2,8 @@ extern crate ai_behavior;
extern crate find_folder;
extern crate gfx_device_gl;
extern crate graphics;
extern crate nalgebra;
extern crate ncollide;
extern crate piston_window;
extern crate rand;
extern crate sprite;
@ -20,6 +22,7 @@ pub struct Game {
scene: Scene<Texture<Resources>>,
player: Hero,
star: Star,
diag: bool,
}
impl Game {
@ -31,12 +34,18 @@ impl Game {
scene: scene,
player: player,
star: star,
diag: false,
}
}
pub fn on_update(&mut self, e: &Input, upd: UpdateArgs, w: &PistonWindow) {
self.scene.event(e);
if !self.star.destroyed && self.player.collides(&self.star) {
self.star.destroy(&mut self.scene, upd.dt);
self.player.grow(&mut self.scene, upd.dt);
}
self.star.mov(w, &mut self.scene, upd.dt);
self.player.mov(w, &mut self.scene, upd.dt);
@ -63,19 +72,14 @@ impl Game {
image.draw(&bg, &Default::default(), c.transform.trans(x, y).zoom(0.1), g);
}
if self.diag {
text::Text::new_color([0.0, 1.0, 0.0, 1.0], 10).draw(
&format!("{} x: {}, y: {}", self.player.sprite_id, self.player.x.trunc(), self.player.y.trunc()),
&format!("{} {}", self.player.diag(), self.star.diag()),
&mut glyphs,
&c.draw_state,
c.transform.trans(10.0, 100.0), g
);
text::Text::new_color([0.0, 1.0, 0.0, 1.0], 10).draw(
&format!("{} x: {}, y: {}", self.star.sprite_id, self.star.x.trunc(), self.star.y.trunc()),
&mut glyphs,
&c.draw_state,
c.transform.trans(10.0, 110.0), g
c.transform.trans(10.0, 10.0), g
);
}
self.scene.draw(c.transform, g);
});
@ -115,6 +119,9 @@ impl Game {
Button::Keyboard(Key::Right) => {
self.player.dir = (0.0, self.player.dir.1);
}
Button::Keyboard(Key::H) => {
self.diag = !self.diag;
}
_ => {}
}
}

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 {