Get initial sprint rendering and controllable

This commit is contained in:
Thomas Gideon 2017-06-04 13:27:47 -04:00
parent f2c7e09339
commit 64ed7bde79
6 changed files with 166 additions and 4 deletions

View File

@ -4,3 +4,9 @@ version = "0.1.0"
authors = ["Thomas Gideon <cmdln@thecommandline.net>"]
[dependencies]
gfx_device_gl = "0.14.1"
piston-ai_behavior = "0.20.0"
piston_window = "0.66.0"
piston2d-sprite = "0.36.0"
find_folder = "0.3.0"
uuid = "0.1.17"

BIN
assets/soot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 606 B

BIN
assets/soot.xcf Normal file

Binary file not shown.

View File

@ -1,6 +1,101 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
extern crate ai_behavior;
extern crate find_folder;
extern crate gfx_device_gl;
extern crate piston_window;
extern crate sprite;
extern crate uuid;
mod object;
use gfx_device_gl::Resources;
use piston_window::*;
use object::Object;
use sprite::*;
pub struct Game {
scene: Scene<Texture<Resources>>,
player: Object,
up_d: bool,
down_d: bool,
left_d: bool,
right_d: bool,
}
impl Game {
pub fn new(w: &mut PistonWindow) -> Game {
let mut scene = Scene::new();
let player = Object::new(w, &mut scene);
Game {
scene: scene,
player: player,
up_d: false,
down_d: false,
left_d: false,
right_d: false,
}
}
pub fn on_update(&mut self, e: &Input, upd: UpdateArgs) {
self.scene.event(e);
if self.up_d {
self.player.mov(&mut self.scene, 0.0, -1500.0 * upd.dt);
}
if self.down_d {
self.player.mov(&mut self.scene, 0.0, 1500.0 * upd.dt);
}
if self.left_d {
self.player.mov(&mut self.scene, -1500.0 * upd.dt, 0.0);
}
if self.right_d {
self.player.mov(&mut self.scene, 1500.0 * upd.dt, 0.0);
}
}
pub fn on_draw(&mut self, e: &Input, _: RenderArgs, w: &mut PistonWindow) {
w.draw_2d(e, |c, g| {
clear([1.0, 1.0, 1.0, 1.0], g);
self.scene.draw(c.transform, g);
});
}
pub fn on_input(&mut self, inp: Input) {
match inp {
Input::Press(but) => {
match but {
Button::Keyboard(Key::Up) => {
self.up_d = true;
}
Button::Keyboard(Key::Down) => {
self.down_d = true;
}
Button::Keyboard(Key::Left) => {
self.left_d = true;
}
Button::Keyboard(Key::Right) => {
self.right_d = true;
}
_ => {}
}
}
Input::Release(but) => {
match but {
Button::Keyboard(Key::Up) => {
self.up_d = false;
}
Button::Keyboard(Key::Down) => {
self.down_d = false;
}
Button::Keyboard(Key::Left) => {
self.left_d = false;
}
Button::Keyboard(Key::Right) => {
self.right_d = false;
}
_ => {}
}
}
_ => {}
}
}
}

19
src/main.rs Normal file
View File

@ -0,0 +1,19 @@
extern crate ghostly;
extern crate piston_window;
use piston_window::*;
fn main() {
let mut window: PistonWindow = WindowSettings::new("Ghostly Adventure", [640, 480])
.exit_on_esc(true)
.build()
.unwrap();
let mut game = ghostly::Game::new(&mut window);
while let Some(e) = window.next() {
match e {
Input::Update(upd) => game.on_update(&e, upd),
Input::Render(ren) => game.on_draw(&e, ren, &mut window),
_ => game.on_input(e),
}
}
}

42
src/object.rs Normal file
View File

@ -0,0 +1,42 @@
use ai_behavior::Action;
use find_folder;
use gfx_device_gl::Resources;
use piston_window::*;
use sprite::*;
use uuid::Uuid;
use std::rc::Rc;
pub struct Object {
sprite_id: Uuid,
x: f64,
y: f64,
}
impl Object {
pub fn new(w: &mut PistonWindow, scene: &mut Scene<Texture<Resources>>) -> Object {
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 sprite_id = scene.add_child(sprite);
Object {
x: 0.0,
y: 0.0,
sprite_id: sprite_id,
}
}
pub fn mov(&mut self, scene: &mut Scene<Texture<Resources>>, x: f64, y: f64) {
self.x += x;
self.y += y;
let mov = Action(Ease(EaseFunction::CubicOut, Box::new(MoveBy(1.0, x, y))));
scene.run(self.sprite_id, &mov);
}
}