Make everything scale with window size.
This commit is contained in:
parent
0984bc62e3
commit
c507233bf4
4 changed files with 61 additions and 39 deletions
26
src/lib.rs
26
src/lib.rs
|
@ -28,6 +28,8 @@ pub struct Game {
|
|||
loss: bool,
|
||||
}
|
||||
|
||||
const TILE_SIZE: u32 = 64;
|
||||
|
||||
impl Game {
|
||||
pub fn new(w: &mut PistonWindow) -> Game {
|
||||
let mut scene = Scene::new();
|
||||
|
@ -73,7 +75,7 @@ impl Game {
|
|||
if !grew {
|
||||
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);
|
||||
} else {
|
||||
self.loss = true;
|
||||
|
@ -89,33 +91,37 @@ impl Game {
|
|||
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 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,
|
||||
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);
|
||||
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);
|
||||
let cols = width / TILE_SIZE;
|
||||
// 4:3 means rows will be fractional so add one to cover completely
|
||||
let tile_count = cols * (height / TILE_SIZE + 1);
|
||||
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 {
|
||||
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!",
|
||||
&mut glyphs,
|
||||
&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;
|
||||
} 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!",
|
||||
&mut glyphs,
|
||||
&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;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ extern crate piston_window;
|
|||
use piston_window::*;
|
||||
|
||||
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)
|
||||
.resizable(false)
|
||||
.build()
|
||||
|
|
|
@ -21,9 +21,11 @@ pub struct Hero {
|
|||
collider: Cuboid2<f64>,
|
||||
}
|
||||
|
||||
const DESIGNED_FOR_WIDTH: f64 = 640.0;
|
||||
|
||||
const BASE_SIZE: f64 = 500.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 MOVE_DUR: f64 = 0.75;
|
||||
|
@ -41,12 +43,17 @@ impl Hero {
|
|||
&TextureSettings::new())
|
||||
.unwrap());
|
||||
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 sprite_id = scene.add_child(sprite);
|
||||
Hero {
|
||||
x: 320.0,
|
||||
y: 240.0,
|
||||
x: x,
|
||||
y: y,
|
||||
w: bounds[2],
|
||||
h: bounds[3],
|
||||
dir: (0.0, 0.0),
|
||||
|
|
|
@ -21,6 +21,14 @@ pub struct Star {
|
|||
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 {
|
||||
pub fn new(w: &mut PistonWindow, scene: &mut Scene<Texture<Resources>>) -> Star {
|
||||
let assets = find_folder::Search::ParentsThenKids(3, 3)
|
||||
|
@ -31,14 +39,18 @@ impl Star {
|
|||
Flip::None,
|
||||
&TextureSettings::new())
|
||||
.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);
|
||||
sprite.set_scale(0.5, 0.5);
|
||||
sprite.set_position(30.0, 32.0);
|
||||
sprite.set_scale(scale, scale);
|
||||
sprite.set_position(x, y);
|
||||
let bounds = sprite.bounding_box();
|
||||
let sprite_id = scene.add_child(sprite);
|
||||
Star {
|
||||
x: 32.0,
|
||||
y: 32.0,
|
||||
x: x,
|
||||
y: y,
|
||||
w: bounds[2],
|
||||
h: bounds[3],
|
||||
sprite_id: sprite_id,
|
||||
|
@ -57,7 +69,9 @@ impl Star {
|
|||
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));
|
||||
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;
|
||||
|
@ -67,13 +81,16 @@ impl Star {
|
|||
}
|
||||
self.dir = rand_turn(self.dir);
|
||||
let dir = lookup_dir(self.dir);
|
||||
let mov_x = 2.0 * dir.0;
|
||||
let mov_y = 2.0 * dir.1;
|
||||
scene.run(self.sprite_id, &Action(Ease(EaseFunction::CubicOut, Box::new(MoveBy(dt * 0.75, mov_x, mov_y)))));
|
||||
let mov_x = ACCEL * dir.0;
|
||||
let mov_y = ACCEL * dir.1;
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -82,23 +99,15 @@ fn rand_dir() -> u32 {
|
|||
rand::random::<u32>() % 8
|
||||
}
|
||||
|
||||
fn rand_pos(dim: f64) -> f64 {
|
||||
(rand::random::<f64>() % dim).abs()
|
||||
}
|
||||
|
||||
fn rand_turn(dir: u32) -> u32 {
|
||||
let coin = rand::random::<i32>() % 10;
|
||||
match coin {
|
||||
-9 => {
|
||||
if dir == 0 {
|
||||
7
|
||||
} else {
|
||||
dir - 1
|
||||
}
|
||||
}
|
||||
9 => {
|
||||
if dir == 7 {
|
||||
0
|
||||
} else {
|
||||
dir + 1
|
||||
}
|
||||
}
|
||||
-9 => if dir == 0 { 7 } else { dir - 1 },
|
||||
9 => if dir == 7 { 0 } else { dir + 1 },
|
||||
_ => dir,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue