susuwatari-game/src/lib.rs

183 lines
5.7 KiB
Rust
Raw Normal View History

extern crate ai_behavior;
extern crate find_folder;
extern crate gfx_device_gl;
2017-07-11 22:21:04 +00:00
extern crate graphics;
2017-07-13 22:53:30 +00:00
extern crate nalgebra;
extern crate ncollide;
extern crate piston_window;
2017-07-13 20:16:14 +00:00
extern crate rand;
extern crate sprite;
extern crate uuid;
2017-07-13 16:49:08 +00:00
mod mobs;
use gfx_device_gl::Resources;
2017-07-11 22:21:04 +00:00
use graphics::Image;
use graphics::rectangle::square;
use piston_window::*;
2017-07-13 20:16:14 +00:00
use mobs::{Hero, Star};
use sprite::*;
pub struct Game {
scene: Scene<Texture<Resources>>,
2017-07-13 16:49:08 +00:00
player: Hero,
2017-07-13 23:37:47 +00:00
stars: Vec<Star>,
2017-07-13 22:53:30 +00:00
diag: bool,
2017-08-08 16:51:29 +00:00
pause: bool,
2017-07-13 23:37:47 +00:00
victory: bool,
2017-08-08 16:13:15 +00:00
loss: bool,
}
impl Game {
pub fn new(w: &mut PistonWindow) -> Game {
let mut scene = Scene::new();
2017-07-13 23:37:47 +00:00
let mut stars: Vec<Star> = vec![];
2017-08-08 16:51:42 +00:00
for _ in 1..7 {
2017-07-13 23:37:47 +00:00
stars.push(Star::new(w, &mut scene));
}
2017-07-13 16:49:08 +00:00
let player = Hero::new(w, &mut scene);
Game {
scene: scene,
player: player,
2017-07-13 23:37:47 +00:00
stars: stars,
2017-07-13 22:53:30 +00:00
diag: false,
2017-08-08 16:51:29 +00:00
pause: false,
2017-07-13 23:37:47 +00:00
victory: false,
2017-08-08 16:13:15 +00:00
loss: false,
}
}
2017-07-13 20:16:14 +00:00
pub fn on_update(&mut self, e: &Input, upd: UpdateArgs, w: &PistonWindow) {
2017-08-08 16:51:29 +00:00
if self.pause || self.victory || self.loss {
2017-08-08 16:13:15 +00:00
return;
}
self.scene.event(e);
2017-08-08 16:13:15 +00:00
let mut grew = false;
2017-07-13 23:37:47 +00:00
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);
2017-08-08 16:13:15 +00:00
grew = true;
2017-07-13 23:37:47 +00:00
} else {
star.mov(w, &mut self.scene, upd.dt);
}
2017-07-13 22:53:30 +00:00
}
2017-07-13 23:37:47 +00:00
if self.stars.iter().all(|star| star.destroyed) {
self.victory = true;
2017-08-08 16:13:15 +00:00
self.loss = false;
2017-07-13 23:37:47 +00:00
return;
}
2017-07-13 20:16:14 +00:00
2017-08-08 16:13:15 +00:00
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) {
2017-07-11 22:21:04 +00:00
let assets = find_folder::Search::ParentsThenKids(3, 3)
.for_folder("assets")
.unwrap();
2017-07-12 00:25:54 +00:00
let ref font = assets.join("Fresca-Regular.ttf");
let factory = w.factory.clone();
let mut glyphs = Glyphs::new(font, factory).unwrap();
2017-07-11 22:21:04 +00:00
let image = Image::new().rect(square(0.0, 0.0, 640.0));
let bg = Texture::from_path(&mut w.factory,
assets.join("bg.png"),
Flip::None,
&TextureSettings::new()).unwrap();
w.draw_2d(e, |c, g| {
clear([1.0, 1.0, 1.0, 1.0], g);
2017-07-11 22:21:04 +00:00
for number in 0..100 {
let x: f64 = (number % 10 * 64).into();
let y: f64 = (number / 10 * 64).into();
image.draw(&bg, &Default::default(), c.transform.trans(x, y).zoom(0.1), g);
}
2017-07-13 20:16:14 +00:00
2017-07-13 23:37:47 +00:00
if self.victory {
text::Text::new_color([0.0, 1.0, 0.0, 1.0], 24).draw(
"Hurray! You win!",
&mut glyphs,
&c.draw_state,
c.transform.trans(200.0, 240.0), g
);
return;
2017-08-08 16:13:15 +00:00
} 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;
2017-07-13 23:37:47 +00:00
}
2017-07-13 22:53:30 +00:00
if self.diag {
text::Text::new_color([0.0, 1.0, 0.0, 1.0], 10).draw(
2017-07-13 23:37:47 +00:00
&format!("{}", self.player.diag()),
2017-07-13 22:53:30 +00:00
&mut glyphs,
&c.draw_state,
c.transform.trans(10.0, 10.0), g
);
}
2017-07-12 00:25:54 +00:00
self.scene.draw(c.transform, g);
});
}
2017-07-12 00:25:54 +00:00
// TODO use an enum to track requested movement direction
pub fn on_input(&mut self, inp: Input) {
match inp {
Input::Press(but) => {
match but {
Button::Keyboard(Key::Up) => {
2017-07-13 20:16:14 +00:00
self.player.dir = (self.player.dir.0, -1.0);
}
Button::Keyboard(Key::Down) => {
2017-07-13 20:16:14 +00:00
self.player.dir = (self.player.dir.0, 1.0);
}
Button::Keyboard(Key::Left) => {
2017-07-13 20:16:14 +00:00
self.player.dir = (-1.0, self.player.dir.1);
}
Button::Keyboard(Key::Right) => {
2017-07-13 20:16:14 +00:00
self.player.dir = (1.0, self.player.dir.1);
}
_ => {}
}
}
Input::Release(but) => {
match but {
Button::Keyboard(Key::Up) => {
2017-07-13 20:16:14 +00:00
self.player.dir = (self.player.dir.0, 0.0);
}
Button::Keyboard(Key::Down) => {
2017-07-13 20:16:14 +00:00
self.player.dir = (self.player.dir.0, 0.0);
}
Button::Keyboard(Key::Left) => {
2017-07-13 20:16:14 +00:00
self.player.dir = (0.0, self.player.dir.1);
}
Button::Keyboard(Key::Right) => {
2017-07-13 20:16:14 +00:00
self.player.dir = (0.0, self.player.dir.1);
}
2017-07-13 22:53:30 +00:00
Button::Keyboard(Key::H) => {
self.diag = !self.diag;
}
2017-08-08 16:51:29 +00:00
Button::Keyboard(Key::P) => {
self.pause = !self.pause;
}
_ => {}
}
}
_ => {}
}
2017-06-04 14:21:22 +00:00
}
}