Get wrapping working fully.

This commit is contained in:
Thomas Gideon 2017-07-11 20:25:54 -04:00
parent 06fe046d6f
commit 388ead607f
3 changed files with 60 additions and 14 deletions

BIN
assets/Fresca-Regular.ttf Normal file

Binary file not shown.

View file

@ -15,6 +15,9 @@ use piston_window::*;
use object::Object; use object::Object;
use sprite::*; use sprite::*;
// TODO move this into the player object
const MV_FACT: f64 = 2500.0;
pub struct Game { pub struct Game {
scene: Scene<Texture<Resources>>, scene: Scene<Texture<Resources>>,
player: Object, player: Object,
@ -42,16 +45,16 @@ impl Game {
self.scene.event(e); self.scene.event(e);
if self.up_d { if self.up_d {
self.player.mov(&mut self.scene, 0.0, -1500.0 * upd.dt); self.player.mov(&mut self.scene, 0.0, -MV_FACT * upd.dt);
} }
if self.down_d { if self.down_d {
self.player.mov(&mut self.scene, 0.0, 1500.0 * upd.dt); self.player.mov(&mut self.scene, 0.0, MV_FACT * upd.dt);
} }
if self.left_d { if self.left_d {
self.player.mov(&mut self.scene, -1500.0 * upd.dt, 0.0); self.player.mov(&mut self.scene, -MV_FACT * upd.dt, 0.0);
} }
if self.right_d { if self.right_d {
self.player.mov(&mut self.scene, 1500.0 * upd.dt, 0.0); self.player.mov(&mut self.scene, MV_FACT * upd.dt, 0.0);
} }
} }
@ -59,22 +62,39 @@ impl Game {
let assets = find_folder::Search::ParentsThenKids(3, 3) let assets = find_folder::Search::ParentsThenKids(3, 3)
.for_folder("assets") .for_folder("assets")
.unwrap(); .unwrap();
let ref font = assets.join("Fresca-Regular.ttf");
let factory = w.factory.clone();
let mut glyphs = Glyphs::new(font, factory).unwrap();
let image = Image::new().rect(square(0.0, 0.0, 640.0)); let image = Image::new().rect(square(0.0, 0.0, 640.0));
let bg = Texture::from_path(&mut w.factory, let bg = Texture::from_path(&mut w.factory,
assets.join("bg.png"), assets.join("bg.png"),
Flip::None, Flip::None,
&TextureSettings::new()).unwrap(); &TextureSettings::new()).unwrap();
w.draw_2d(e, |c, g| { w.draw_2d(e, |c, g| {
let transform = c.transform.trans(10.0, 100.0);
clear([1.0, 1.0, 1.0, 1.0], g); clear([1.0, 1.0, 1.0, 1.0], g);
for number in 0..100 { for number in 0..100 {
let x: f64 = (number % 10 * 64).into(); let x: f64 = (number % 10 * 64).into();
let y: 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); image.draw(&bg, &Default::default(), c.transform.trans(x, y).zoom(0.1), g);
} }
if let Some(player_sprite) = self.scene.child(self.player.sprite_id) {
let (x, y) = player_sprite.get_position();
text::Text::new_color([0.0, 1.0, 0.0, 1.0], 32).draw(
&format!("x: {}, y: {}", x.trunc(), y.trunc()),
&mut glyphs,
&c.draw_state,
transform, g
);
}
self.scene.draw(c.transform, g); self.scene.draw(c.transform, g);
}); });
} }
// TODO use an enum to track requested movement direction
pub fn on_input(&mut self, inp: Input) { pub fn on_input(&mut self, inp: Input) {
match inp { match inp {
Input::Press(but) => { Input::Press(but) => {

View file

@ -7,10 +7,11 @@ use uuid::Uuid;
use std::rc::Rc; use std::rc::Rc;
// TODO add size of player sprite for boundary checking
pub struct Object { pub struct Object {
sprite_id: Uuid, pub sprite_id: Uuid,
x: f64, pub x: f64,
y: f64, pub y: f64,
} }
impl Object { impl Object {
@ -27,16 +28,41 @@ impl Object {
sprite.set_position(320.0, 240.0); sprite.set_position(320.0, 240.0);
let sprite_id = scene.add_child(sprite); let sprite_id = scene.add_child(sprite);
Object { Object {
x: 0.0, x: 320.0,
y: 0.0, y: 240.0,
sprite_id: sprite_id, sprite_id: sprite_id,
} }
} }
pub fn mov(&mut self, scene: &mut Scene<Texture<Resources>>, x: f64, y: f64) { pub fn mov(&mut self, scene: &mut Scene<Texture<Resources>>, input_x: f64, input_y: f64) {
self.x += x; if let Some(sprite) = scene.child(self.sprite_id) {
self.y += y; let (sprite_x, sprite_y) = sprite.get_position();
let mov = Action(Ease(EaseFunction::CubicOut, Box::new(MoveBy(1.0, x, y)))); self.x = sprite_x;
scene.run(self.sprite_id, &mov); self.y = sprite_y;
}
let mut wrapped = false;
// TODO pass in window size
if self.x > 640.0 + 32.0 {
self.x = -32.0;
wrapped = true;
} else if self.x < -32.0 {
self.x = 640.0 + 32.0;
wrapped = true;
}
if self.y > 480.0 + 32.0 {
self.y = -32.0;
wrapped = true;
} else if self.y < -32.0 {
self.y = 480.0 + 32.0;
wrapped = true;
}
self.y += input_y;
if wrapped {
scene.stop_all(self.sprite_id);
if let Some(ref mut sprite) = scene.child_mut(self.sprite_id) {
sprite.set_position(self.x, self.y);
}
}
scene.run(self.sprite_id, &Action(Ease(EaseFunction::CubicOut, Box::new(MoveBy(1.0, input_x, input_y)))));
} }
} }