Make everything scale with window size.

This commit is contained in:
Thomas Gideon 2017-08-08 13:45:02 -04:00
parent 0984bc62e3
commit c507233bf4
4 changed files with 61 additions and 39 deletions

View file

@ -28,6 +28,8 @@ pub struct Game {
loss: bool, loss: bool,
} }
const TILE_SIZE: u32 = 64;
impl Game { impl Game {
pub fn new(w: &mut PistonWindow) -> Game { pub fn new(w: &mut PistonWindow) -> Game {
let mut scene = Scene::new(); let mut scene = Scene::new();
@ -73,7 +75,7 @@ impl Game {
if !grew { if !grew {
self.player.shrink(&mut self.scene, upd.dt); self.player.shrink(&mut self.scene, upd.dt);
} }
if self.player.size > 0 { if self.player.size > 0.0 {
self.player.mov(w, &mut self.scene, upd.dt); self.player.mov(w, &mut self.scene, upd.dt);
} else { } else {
self.loss = true; self.loss = true;
@ -89,33 +91,37 @@ impl Game {
let factory = w.factory.clone(); let factory = w.factory.clone();
let mut glyphs = Glyphs::new(font, factory).unwrap(); let mut glyphs = Glyphs::new(font, factory).unwrap();
let image = Image::new().rect(square(0.0, 0.0, 640.0)); let Size { height, width } = w.size();
let image = Image::new().rect(square(0.0, 0.0, width as f64));
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| {
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 { let cols = width / TILE_SIZE;
let x: f64 = (number % 10 * 64).into(); // 4:3 means rows will be fractional so add one to cover completely
let y: f64 = (number / 10 * 64).into(); let tile_count = cols * (height / TILE_SIZE + 1);
image.draw(&bg, &Default::default(), c.transform.trans(x, y).zoom(0.1), g); for number in 0..tile_count {
let x: f64 = (number % cols * TILE_SIZE).into();
let y: f64 = (number / cols * TILE_SIZE).into();
image.draw(&bg, &Default::default(), c.transform.trans(x, y).zoom(1.0 / cols as f64), g);
} }
if self.victory { if self.victory {
text::Text::new_color([0.0, 1.0, 0.0, 1.0], 24).draw( text::Text::new_color([0.0, 1.0, 0.0, 1.0], height / 10).draw(
"Hurray! You win!", "Hurray! You win!",
&mut glyphs, &mut glyphs,
&c.draw_state, &c.draw_state,
c.transform.trans(200.0, 240.0), g c.transform.trans(width as f64 / 2.0 - width as f64 / 4.5, height as f64 / 2.0), g
); );
return; return;
} else if self.loss { } else if self.loss {
text::Text::new_color([0.0, 1.0, 0.0, 1.0], 24).draw( text::Text::new_color([0.0, 1.0, 0.0, 1.0], height / 10).draw(
"Aw! You lose!", "Aw! You lose!",
&mut glyphs, &mut glyphs,
&c.draw_state, &c.draw_state,
c.transform.trans(200.0, 240.0), g c.transform.trans(width as f64 / 2.0 - width as f64 / 4.5, height as f64 / 2.0), g
); );
return; return;
} }

View file

@ -4,7 +4,7 @@ extern crate piston_window;
use piston_window::*; use piston_window::*;
fn main() { fn main() {
let mut window: PistonWindow = WindowSettings::new("Susuwatari Game", [640, 480]) let mut window: PistonWindow = WindowSettings::new("Susuwatari Game", [1024, 768])
.exit_on_esc(true) .exit_on_esc(true)
.resizable(false) .resizable(false)
.build() .build()

View file

@ -21,9 +21,11 @@ pub struct Hero {
collider: Cuboid2<f64>, collider: Cuboid2<f64>,
} }
const DESIGNED_FOR_WIDTH: f64 = 640.0;
const BASE_SIZE: f64 = 500.0; const BASE_SIZE: f64 = 500.0;
const GROWTH_FACTOR: f64 = 50.0; const GROWTH_FACTOR: f64 = 50.0;
const SHRINK_FACTOR: f64 = 1.0; const SHRINK_FACTOR: f64 = 0.5;
const GROW_SHRINK_DUR: f64 = 5.0; const GROW_SHRINK_DUR: f64 = 5.0;
const MOVE_DUR: f64 = 0.75; const MOVE_DUR: f64 = 0.75;
@ -41,12 +43,17 @@ impl Hero {
&TextureSettings::new()) &TextureSettings::new())
.unwrap()); .unwrap());
let mut sprite = Sprite::from_texture(tex); let mut sprite = Sprite::from_texture(tex);
sprite.set_position(320.0, 240.0); let Size { width, height } = w.size();
let x = width as f64 / 2.0;
let y = height as f64 / 2.0;
let scale = width as f64 / DESIGNED_FOR_WIDTH;
sprite.set_position(x, y);
sprite.set_scale(scale, scale);
let bounds = sprite.bounding_box(); let bounds = sprite.bounding_box();
let sprite_id = scene.add_child(sprite); let sprite_id = scene.add_child(sprite);
Hero { Hero {
x: 320.0, x: x,
y: 240.0, y: y,
w: bounds[2], w: bounds[2],
h: bounds[3], h: bounds[3],
dir: (0.0, 0.0), dir: (0.0, 0.0),

View file

@ -21,6 +21,14 @@ pub struct Star {
pub destroyed: bool, pub destroyed: bool,
} }
const DESIGNED_FOR_WIDTH: f64 = 640.0;
const SCALE_FACTOR: f64 = 0.5;
const ACCEL: f64 = 2.0;
const MOVE_DUR: f64 = 0.75;
const DESTROY_DUR: f64 = 0.75;
impl Star { impl Star {
pub fn new(w: &mut PistonWindow, scene: &mut Scene<Texture<Resources>>) -> Star { pub fn new(w: &mut PistonWindow, scene: &mut Scene<Texture<Resources>>) -> Star {
let assets = find_folder::Search::ParentsThenKids(3, 3) let assets = find_folder::Search::ParentsThenKids(3, 3)
@ -31,14 +39,18 @@ impl Star {
Flip::None, Flip::None,
&TextureSettings::new()) &TextureSettings::new())
.unwrap()); .unwrap());
let Size { width, height } = w.size();
let x = rand_pos(width as f64);
let y = rand_pos(height as f64);
let scale = width as f64 / DESIGNED_FOR_WIDTH * SCALE_FACTOR;
let mut sprite = Sprite::from_texture(tex); let mut sprite = Sprite::from_texture(tex);
sprite.set_scale(0.5, 0.5); sprite.set_scale(scale, scale);
sprite.set_position(30.0, 32.0); sprite.set_position(x, y);
let bounds = sprite.bounding_box(); let bounds = sprite.bounding_box();
let sprite_id = scene.add_child(sprite); let sprite_id = scene.add_child(sprite);
Star { Star {
x: 32.0, x: x,
y: 32.0, y: y,
w: bounds[2], w: bounds[2],
h: bounds[3], h: bounds[3],
sprite_id: sprite_id, sprite_id: sprite_id,
@ -57,7 +69,9 @@ impl Star {
self.x = sprite_x; self.x = sprite_x;
self.y = sprite_y; 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)); 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 wrapped {
if let Some(ref mut sprite) = scene.child_mut(self.sprite_id) { if let Some(ref mut sprite) = scene.child_mut(self.sprite_id) {
self.x = new_x; self.x = new_x;
@ -67,13 +81,16 @@ impl Star {
} }
self.dir = rand_turn(self.dir); self.dir = rand_turn(self.dir);
let dir = lookup_dir(self.dir); let dir = lookup_dir(self.dir);
let mov_x = 2.0 * dir.0; let mov_x = ACCEL * dir.0;
let mov_y = 2.0 * dir.1; let mov_y = ACCEL * dir.1;
scene.run(self.sprite_id, &Action(Ease(EaseFunction::CubicOut, Box::new(MoveBy(dt * 0.75, mov_x, mov_y))))); scene.run(self.sprite_id,
&Action(Ease(EaseFunction::CubicOut,
Box::new(MoveBy(dt * MOVE_DUR, mov_x, mov_y)))));
} }
pub fn destroy(&mut self, scene: &mut Scene<Texture<Resources>>, dt: f64) { 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))))); scene.run(self.sprite_id,
&Action(Ease(EaseFunction::CubicOut, Box::new(FadeOut(dt * DESTROY_DUR)))));
self.destroyed = true; self.destroyed = true;
} }
} }
@ -82,23 +99,15 @@ fn rand_dir() -> u32 {
rand::random::<u32>() % 8 rand::random::<u32>() % 8
} }
fn rand_pos(dim: f64) -> f64 {
(rand::random::<f64>() % dim).abs()
}
fn rand_turn(dir: u32) -> u32 { fn rand_turn(dir: u32) -> u32 {
let coin = rand::random::<i32>() % 10; let coin = rand::random::<i32>() % 10;
match coin { match coin {
-9 => { -9 => if dir == 0 { 7 } else { dir - 1 },
if dir == 0 { 9 => if dir == 7 { 0 } else { dir + 1 },
7
} else {
dir - 1
}
}
9 => {
if dir == 7 {
0
} else {
dir + 1
}
}
_ => dir, _ => dir,
} }
} }