Add star mob.
This commit is contained in:
parent
ce783a3609
commit
d62e585d9f
10 changed files with 213 additions and 67 deletions
61
src/mobs/hero.rs
Normal file
61
src/mobs/hero.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
use ai_behavior::Action;
|
||||
use find_folder;
|
||||
use gfx_device_gl::Resources;
|
||||
use piston_window::*;
|
||||
use sprite::*;
|
||||
use uuid::Uuid;
|
||||
use std::rc::Rc;
|
||||
use mobs::wrap;
|
||||
|
||||
pub struct Hero {
|
||||
pub sprite_id: Uuid,
|
||||
pub dir: (f64, f64),
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
w: f64,
|
||||
h: f64,
|
||||
}
|
||||
|
||||
impl Hero {
|
||||
pub fn new(w: &mut PistonWindow, scene: &mut Scene<Texture<Resources>>) -> Hero {
|
||||
let assets = find_folder::Search::ParentsThenKids(3, 3)
|
||||
.for_folder("assets")
|
||||
.unwrap();
|
||||
let tex = Rc::new(Texture::from_path(&mut w.factory,
|
||||
assets.join("soot.png"),
|
||||
Flip::None,
|
||||
&TextureSettings::new())
|
||||
.unwrap());
|
||||
let mut sprite = Sprite::from_texture(tex);
|
||||
sprite.set_position(320.0, 240.0);
|
||||
let bounds = sprite.bounding_box();
|
||||
let sprite_id = scene.add_child(sprite);
|
||||
Hero {
|
||||
x: 320.0,
|
||||
y: 240.0,
|
||||
w: bounds[2],
|
||||
h: bounds[3],
|
||||
dir: (0.0, 0.0),
|
||||
sprite_id: sprite_id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mov(&mut self, w: &PistonWindow, scene: &mut Scene<Texture<Resources>>, dt: f64) {
|
||||
if let Some(sprite) = scene.child(self.sprite_id) {
|
||||
let (sprite_x, sprite_y) = sprite.get_position();
|
||||
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));
|
||||
if wrapped {
|
||||
self.x = new_x;
|
||||
self.y = new_y;
|
||||
if let Some(ref mut sprite) = scene.child_mut(self.sprite_id) {
|
||||
sprite.set_position(self.x, self.y);
|
||||
}
|
||||
}
|
||||
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)))));
|
||||
}
|
||||
}
|
22
src/mobs/mod.rs
Normal file
22
src/mobs/mod.rs
Normal file
|
@ -0,0 +1,22 @@
|
|||
pub use self::hero::Hero;
|
||||
pub use self::star::Star;
|
||||
|
||||
mod hero;
|
||||
mod star;
|
||||
|
||||
// TODO use window::size
|
||||
pub fn wrap(win: (f64, f64), bounds: (f64, f64), pos: (f64, f64)) -> (bool, f64, f64) {
|
||||
let new_x = wrap_dim(win.0, bounds.0, pos.0);
|
||||
let new_y = wrap_dim(win.1, bounds.1, pos.1);
|
||||
(new_x != pos.0 || new_y != pos.1, new_x, new_y)
|
||||
}
|
||||
|
||||
fn wrap_dim(win_dim: f64, sprite_dim: f64, pos_dim: f64) -> f64 {
|
||||
if pos_dim > win_dim + sprite_dim / 2.0 {
|
||||
-sprite_dim / 2.0
|
||||
} else if pos_dim < -sprite_dim / 2.0 {
|
||||
win_dim + sprite_dim / 2.0
|
||||
} else {
|
||||
pos_dim
|
||||
}
|
||||
}
|
62
src/mobs/star.rs
Normal file
62
src/mobs/star.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
use ai_behavior::Action;
|
||||
use find_folder;
|
||||
use gfx_device_gl::Resources;
|
||||
use piston_window::*;
|
||||
use sprite::*;
|
||||
use uuid::Uuid;
|
||||
use rand;
|
||||
use std::rc::Rc;
|
||||
use super::wrap;
|
||||
|
||||
|
||||
pub struct Star {
|
||||
pub sprite_id: Uuid,
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
w: f64,
|
||||
h: f64,
|
||||
}
|
||||
|
||||
impl Star {
|
||||
pub fn new(w: &mut PistonWindow, scene: &mut Scene<Texture<Resources>>) -> Star {
|
||||
let assets = find_folder::Search::ParentsThenKids(3, 3)
|
||||
.for_folder("assets")
|
||||
.unwrap();
|
||||
let tex = Rc::new(Texture::from_path(&mut w.factory,
|
||||
assets.join("star.png"),
|
||||
Flip::None,
|
||||
&TextureSettings::new())
|
||||
.unwrap());
|
||||
let mut sprite = Sprite::from_texture(tex);
|
||||
sprite.set_scale(0.5, 0.5);
|
||||
sprite.set_position(30.0, 32.0);
|
||||
let bounds = sprite.bounding_box();
|
||||
let sprite_id = scene.add_child(sprite);
|
||||
Star {
|
||||
x: 32.0,
|
||||
y: 32.0,
|
||||
w: bounds[2],
|
||||
h: bounds[3],
|
||||
sprite_id: sprite_id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mov(&mut self, w: &PistonWindow, scene: &mut Scene<Texture<Resources>>, dt: f64) {
|
||||
if let Some(sprite) = scene.child(self.sprite_id) {
|
||||
let (sprite_x, sprite_y) = sprite.get_position();
|
||||
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));
|
||||
if wrapped {
|
||||
if let Some(ref mut sprite) = scene.child_mut(self.sprite_id) {
|
||||
self.x = new_x;
|
||||
self.y = new_y;
|
||||
sprite.set_position(self.x, self.y);
|
||||
}
|
||||
}
|
||||
let mov_x = rand::random::<f64>() * 0.5;
|
||||
let mov_y = rand::random::<f64>() * 0.5;
|
||||
scene.run(self.sprite_id, &Action(Ease(EaseFunction::CubicOut, Box::new(MoveBy(dt * 0.75, mov_x, mov_y)))));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue