diff --git a/src/main.rs b/src/main.rs index 98c9433..cf91232 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ use std::str::FromStr; fn main() -> amethyst::Result<()> { - //amethyst::start_logger(Default::default()); + amethyst::start_logger(Default::default()); // Gets the root directory of the application let mut app_root = PathBuf::from_str("/home/mrh/source/flappy-bird-rust/")?; @@ -42,8 +42,7 @@ fn main() -> amethyst::Result<()> { let game_data = GameDataBuilder::default() .with_bundle(TransformBundle::new())? .with_bundle(input_bundle)? - .with(ScrollScrollables, "scroll", &[]) - .with(BirbGravity{ fired: false }, "gravity", &["input_system"]) + // .with(System, "system", &["required_things"]) .with_bundle( RenderingBundle::::new() .with_plugin( @@ -54,7 +53,7 @@ fn main() -> amethyst::Result<()> { )?; // Creates the app with the startup state and bound game data - let mut game = Application::new(resources, state::MyState, game_data)?; + let mut game = Application::new(resources, state::PlayState::default(), game_data)?; game.run(); Ok(()) diff --git a/src/state.rs b/src/state.rs index faf627d..ebb20fd 100644 --- a/src/state.rs +++ b/src/state.rs @@ -6,16 +6,21 @@ use amethyst::{ prelude::*, renderer::{Camera, ImageFormat, SpriteRender, SpriteSheet, SpriteSheetFormat, Texture}, window::ScreenDimensions, - ecs::prelude::{Component, DenseVecStorage, Entity}, + ecs::prelude::{Dispatcher, DispatcherBuilder, Component, DenseVecStorage, Entity}, }; use log::info; use crate::components::*; use std::collections::HashMap; -use crate::systems::BirbGravity; +use crate::systems::{BirbGravity, ScrollScrollables}; -pub struct MyState; -impl SimpleState for MyState { +#[derive(Default)] +pub struct PlayState<'a, 'b> { + /// The `State` specific `Dispatcher`, containing `System`s only relevant for this `State`. + dispatcher: Option>, +} + +impl<'a, 'b> SimpleState for PlayState<'a, 'b> { // On start will run when this state is initialized. For more // state lifecycle hooks, see: @@ -33,6 +38,19 @@ impl SimpleState for MyState { /// function sets size of camera window init_camera(world, &dimensions); + + + // 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); + + self.dispatcher = Some(dispatcher); + // Load our sprites and display them let sprites = load_sprites(world); init_sprites(world, &sprites, &dimensions); @@ -52,6 +70,8 @@ impl SimpleState for MyState { } if is_key_down(&event, VirtualKeyCode::P) { + // So I need to set the scrolling and gravity systems to pause + return Trans::Push(Box::new(PausedState)); } } @@ -61,6 +81,11 @@ impl SimpleState for MyState { } fn update(&mut self, data: &mut StateData<'_, GameData<'_, '_>>) -> SimpleTrans { + + if let Some(dispatcher) = self.dispatcher.as_mut() { + dispatcher.dispatch(&data.world); + } + Trans::None } } @@ -200,8 +225,6 @@ impl SimpleState for PausedState { fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) { let world = data.world; let dimensions = (*world.read_resource::()).clone(); - - let storage = world.read_storage::(); } fn handle_event( @@ -213,6 +236,7 @@ impl SimpleState for PausedState { if let StateEvent::Window(event) = &event { // Check if the window should be closed if is_key_down(&event, VirtualKeyCode::Space) { + return Trans::Pop; } } diff --git a/src/systems.rs b/src/systems.rs index bec589c..2aa1f50 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -6,7 +6,7 @@ use amethyst::{ ecs::prelude::{}, ecs::prelude::{ Component, DenseVecStorage, Entity, Join, Read, - ReadStorage, System, SystemData, WriteStorage + ReadStorage, System, SystemData, WriteStorage, Write }, input::{InputHandler, StringBindings}, }; @@ -16,6 +16,14 @@ use crate::components::*; pub struct ScrollScrollables; +/* +Pausable systems +https://book.amethyst.rs/stable/controlling_system_execution/pausable_systems.html + + + +*/ + // This system iterates all the objects with transform (and falling object) component impl<'a> System<'a> for ScrollScrollables { type SystemData = ( @@ -52,6 +60,8 @@ impl<'a> System<'a> for BirbGravity { fn run(&mut self, (mut transforms, mut scrolling, time, input): Self::SystemData) { 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; }