From 3d34224fe6ba9cc0fd69a45d08fd46ba4c43f52b Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Thu, 9 Jan 2020 23:34:50 -0800 Subject: [PATCH] sprites aligned --- resources/sprites/flappy.ron | 12 +-- src/main.rs | 1 + src/state.rs | 168 +++++++++++++---------------------- src/systems.rs | 7 +- 4 files changed, 75 insertions(+), 113 deletions(-) diff --git a/resources/sprites/flappy.ron b/resources/sprites/flappy.ron index bab8129..7a600d8 100644 --- a/resources/sprites/flappy.ron +++ b/resources/sprites/flappy.ron @@ -47,14 +47,14 @@ ( // Play Button x: 354, y: 118, - width: 50, - height: 28, + width: 52, + height: 29, ), ( // Leaderboard button x: 414, y: 118, - width: 42, - height: 28, + width: 52, + height: 29, ), ( // Get Ready x: 295, @@ -65,8 +65,8 @@ ( // Flappy Bird Text x: 351, y: 91, - width: 88, + width: 90, height: 25, - ), + ) ] ) \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7c95d0f..1be0baf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,7 @@ fn main() -> amethyst::Result<()> { .with_bundle(TransformBundle::new())? .with_bundle(input_bundle)? // .with(System, "system", &["required_things"]) + .with(ScrollScrollables, "scroll", &[]) .with_bundle( RenderingBundle::::new() .with_plugin( diff --git a/src/state.rs b/src/state.rs index 915c81b..e365598 100644 --- a/src/state.rs +++ b/src/state.rs @@ -30,57 +30,11 @@ impl<'a, 'b> PlayState<'a, 'b> { let sprites = world.try_fetch_mut::>().unwrap().clone(); let dimensions = (*world.read_resource::()).clone(); - - let background_sprite = sprites.get("day-background").unwrap().clone(); - - let background_object = TiledScroller { - speed: -75.0, - position: 1.0, - width: 144.0 * 3.0, - height: 256.0 * 3.0, - }; + let birb_sprite = sprites + .get("floppy").unwrap(); let mut transform = Transform::default(); transform.set_scale(Vector3::new(3.0, 3.0, 3.0)); - transform.set_translation_xyz(background_object.width/2.0, background_object.height/2.0, 0.0); - - self.sprites.push(world - .create_entity() - .with(background_sprite.clone()) // Sprite Render - .with(background_object.clone()) - .with(transform.clone()) - .build()); - - transform.set_translation_xyz(3.0*144.0/2.0*3.0, 3.0*256.0/2.0, 0.0); - - self.sprites.push(world - .create_entity() - .with(background_sprite.clone()) // Sprite Render - .with(TiledScroller { - speed: -75.0, - position: 2.0, - width: 0.0, - height: 0.0 - }) - .with(transform.clone()) - .build()); - - let ground_sprite = sprites.get("ground").unwrap(); - transform.set_translation_xyz(3.0*168.0/2.0*3.0, 3.0*56.0/2.0, 0.1); - - self.sprites.push(world - .create_entity() - .with(ground_sprite.clone()) // Sprite Render - .with(TiledScroller { - speed: -100.0, - position: 2.0, - width: 0.0, - height: 0.0, - }) - .with(transform.clone()) - .build()); - - let birb_sprite = sprites.get("floppy").unwrap(); transform.set_translation_xyz(dimensions.width()/2.0, dimensions.height()/2.0, 0.2); self.sprites.push(world @@ -101,23 +55,12 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> { fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) { let world = data.world; - - // Get the screen dimensions so we can initialize the camera and - // place our sprites correctly later. We'll clone this since we'll - // pass the world mutably to the following functions. let dimensions = (*world.read_resource::()).clone(); - - // Place the camera - /// function sets size of camera window - - // Create the `DispatcherBuilder` and register some `System`s that should only run for this `State`. let mut dispatcher_builder = DispatcherBuilder::new(); - dispatcher_builder.add(ScrollScrollables, "scroll", &[]); dispatcher_builder.add(BirbGravity { fired: false }, "gravity", &[]); - // Build and setup the `Dispatcher`. let mut dispatcher = dispatcher_builder.build(); dispatcher.setup(world); @@ -129,7 +72,7 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> { fn handle_event( &mut self, - mut _data: StateData<'_, GameData<'_, '_>>, + mut data: StateData<'_, GameData<'_, '_>>, event: StateEvent, ) -> SimpleTrans { @@ -140,6 +83,11 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> { } if is_key_down(&event, VirtualKeyCode::P) { + let world = data.world; + for i in &self.sprites { + world.delete_entity(*i); + } + self.sprites.clear(); return Trans::Pop; } } @@ -158,26 +106,11 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> { #[derive(Default)] pub struct SplashState { sprites: Vec, + persistent_sprites: Vec, } impl SplashState { - fn init_camera(world: &mut World) { - - let dimensions = (*world.read_resource::()).clone(); - - // Center the camera in the middle of the screen, and let it cover - // the entire screen - let mut transform = Transform::default(); - transform.set_translation_xyz(dimensions.width() * 0.5, dimensions.height() * 0.5, 1.); - - world - .create_entity() - .with(Camera::standard_2d(dimensions.width(), dimensions.height())) - .with(transform) - .build(); - } - fn init_sprites(&mut self, world: &mut World) { let sprites = world.try_fetch_mut::>().unwrap().clone(); @@ -191,56 +124,71 @@ impl SplashState { .get("leaderboard-button").unwrap().clone(); let background_sprite = sprites .get("day-background").unwrap().clone(); + let night_background_sprite = sprites + .get("night-background").unwrap().clone(); let ground_sprite = sprites .get("ground").unwrap().clone(); - let background_object = TiledScroller { - speed: -75.0, - position: 1.0, - width: 144.0 * 3.0, - height: 256.0 * 3.0, - }; let mut transform = Transform::default(); transform.set_scale(Vector3::new(3.0, 3.0, 3.0)); - transform.set_translation_xyz(dimensions.width()/2.0 - 100.0, dimensions.height() - 100.0, 0.1); + transform.set_translation_xyz(3.0*144.0/2.0, 3.0*256.0/2.0, 0.0); - self.sprites.push(world + self.persistent_sprites.push(world .create_entity() .with(background_sprite.clone()) // Sprite Render - .with(background_object.clone()) + .with(TiledScroller { + speed: -75.0, + position: 1.0, + width: 144.0 * 3.0, + height: 256.0 * 3.0, + }) .with(transform.clone()) .build()); transform.set_translation_xyz(3.0*144.0/2.0*3.0, 3.0*256.0/2.0, 0.0); - self.sprites.push(world + self.persistent_sprites.push(world .create_entity() .with(background_sprite.clone()) // Sprite Render .with(TiledScroller { speed: -75.0, position: 2.0, - width: 0.0, - height: 0.0 + width: 144.0 * 3.0, + height: 256.0 * 3.0, + }) + .with(transform.clone()) + .build()); + + transform.set_translation_xyz(3.0*168.0/2.0, 3.0*56.0/2.0, 0.1); + + self.persistent_sprites.push(world + .create_entity() + .with(ground_sprite.clone()) // Sprite Render + .with(TiledScroller { + speed: -100.0, + position: 2.0, + width: 168.0 * 3.0, + height: 56.0 * 3.0, }) .with(transform.clone()) .build()); transform.set_translation_xyz(3.0*168.0/2.0*3.0, 3.0*56.0/2.0, 0.1); - self.sprites.push(world + self.persistent_sprites.push(world .create_entity() .with(ground_sprite.clone()) // Sprite Render .with(TiledScroller { speed: -100.0, position: 2.0, - width: 0.0, - height: 0.0, + width: 168.0 * 3.0, + height: 56.0 * 3.0, }) .with(transform.clone()) .build()); - transform.set_translation_xyz(background_object.width/2.0, background_object.height/2.0, 0.0); + transform.set_translation_xyz(dimensions.width()*0.5, dimensions.height()*0.8, 0.2); self.sprites.push(world .create_entity() @@ -248,7 +196,7 @@ impl SplashState { .with(transform.clone()) .build()); - transform.set_translation_xyz(100.0/2.0, 100.0/2.0, 0.1); + transform.set_translation_xyz(dimensions.width()*0.25, dimensions.height()*0.4, 0.2); self.sprites.push(world .create_entity() @@ -256,7 +204,7 @@ impl SplashState { .with(transform.clone()) .build()); - transform.set_translation_xyz(500.0/2.0, 100.0/2.0, 0.1); + transform.set_translation_xyz(dimensions.width()*0.75, dimensions.height()*0.4, 0.2); self.sprites.push(world .create_entity() @@ -265,6 +213,22 @@ impl SplashState { .build()); } + fn init_camera(world: &mut World) { + + let dimensions = (*world.read_resource::()).clone(); + + // Center the camera in the middle of the screen, and let it cover + // the entire screen + let mut transform = Transform::default(); + transform.set_translation_xyz(dimensions.width() * 0.5, dimensions.height() * 0.5, 1.); + + world + .create_entity() + .with(Camera::standard_2d(dimensions.width(), dimensions.height())) + .with(transform) + .build(); + } + fn load_sprites(world: &mut World) -> HashMap { // Load the texture for our sprites. We'll later need to // add a handle to this texture to our `SpriteRender`s, so @@ -354,8 +318,6 @@ impl SimpleState for SplashState { return Trans::Push(Box::new(ReadyState::default())); } } - - // Keep going Trans::None } @@ -370,7 +332,8 @@ pub struct ReadyState { } impl ReadyState { - fn init_sprites(&mut self, world: &mut World, dimensions: &ScreenDimensions) { + + fn init_sprites(&mut self, world: &mut World) { let dimensions = (*world.read_resource::()).clone(); let sprites = world.try_fetch_mut::>().unwrap().clone(); @@ -383,7 +346,8 @@ impl ReadyState { let mut transform = Transform::default(); transform.set_scale(Vector3::new(3.0, 3.0, 3.0)); - transform.set_translation_xyz(dimensions.width()/2.0 - 100.0, dimensions.height() - 100.0, 0.1); + + transform.set_translation_xyz(dimensions.width()*0.5, dimensions.height()*0.8, 0.2); self.sprites.push(world .create_entity() @@ -391,7 +355,7 @@ impl ReadyState { .with(transform.clone()) .build()); - transform.set_translation_xyz(500.0/2.0, 500.0/2.0, 0.1); + transform.set_translation_xyz(dimensions.width()*0.5, dimensions.height()*0.5, 0.2); self.sprites.push(world .create_entity() @@ -402,11 +366,9 @@ impl ReadyState { } impl SimpleState for ReadyState { - fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) { let world = data.world; - - + ReadyState::init_sprites(self, world); } fn handle_event( @@ -432,8 +394,6 @@ impl SimpleState for ReadyState { return Trans::Push(Box::new(PlayState::default())); } } - - // Keep going Trans::None } diff --git a/src/systems.rs b/src/systems.rs index 2aa1f50..eb656f7 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -35,15 +35,17 @@ impl<'a> System<'a> for ScrollScrollables { fn run(&mut self, (mut transforms, mut scrolling, time): Self::SystemData) { for (mut transform, mut object) in (&mut transforms, &mut scrolling).join() { + // I need to tile it by it's width transform.prepend_translation_x(object.speed * time.delta_seconds()); - if transform.translation().x+144.0*1.5 < 0.0 { - transform.set_translation_x(144.0*3.0/2.0*3.0); + if transform.translation().x+object.width/2.0 < 0.0 { + transform.set_translation_x(object.width/2.0*3.0); } } } } +#[derive(Default)] pub struct BirbGravity { pub fired: bool, } @@ -61,7 +63,6 @@ impl<'a> System<'a> for BirbGravity { for (mut transform, mut object) in (&mut transforms, &mut scrolling).join() { //match game.current_state - if input.action_is_down("flap").expect("No action") { object.vertical_speed = 600.0; }