master
mitchellhansen 5 years ago
parent 4528299b7e
commit ac011ddb07

@ -25,7 +25,7 @@ use std::str::FromStr;
fn main() -> amethyst::Result<()> { fn main() -> amethyst::Result<()> {
//amethyst::start_logger(Default::default()); amethyst::start_logger(Default::default());
// Gets the root directory of the application // Gets the root directory of the application
let mut app_root = PathBuf::from_str("/home/mrh/source/flappy-bird-rust/")?; 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() let game_data = GameDataBuilder::default()
.with_bundle(TransformBundle::new())? .with_bundle(TransformBundle::new())?
.with_bundle(input_bundle)? .with_bundle(input_bundle)?
.with(ScrollScrollables, "scroll", &[]) // .with(System, "system", &["required_things"])
.with(BirbGravity{ fired: false }, "gravity", &["input_system"])
.with_bundle( .with_bundle(
RenderingBundle::<DefaultBackend>::new() RenderingBundle::<DefaultBackend>::new()
.with_plugin( .with_plugin(
@ -54,7 +53,7 @@ fn main() -> amethyst::Result<()> {
)?; )?;
// Creates the app with the startup state and bound game data // 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(); game.run();
Ok(()) Ok(())

@ -6,16 +6,21 @@ use amethyst::{
prelude::*, prelude::*,
renderer::{Camera, ImageFormat, SpriteRender, SpriteSheet, SpriteSheetFormat, Texture}, renderer::{Camera, ImageFormat, SpriteRender, SpriteSheet, SpriteSheetFormat, Texture},
window::ScreenDimensions, window::ScreenDimensions,
ecs::prelude::{Component, DenseVecStorage, Entity}, ecs::prelude::{Dispatcher, DispatcherBuilder, Component, DenseVecStorage, Entity},
}; };
use log::info; use log::info;
use crate::components::*; use crate::components::*;
use std::collections::HashMap; use std::collections::HashMap;
use crate::systems::BirbGravity; use crate::systems::{BirbGravity, ScrollScrollables};
pub struct MyState; #[derive(Default)]
impl SimpleState for MyState { pub struct PlayState<'a, 'b> {
/// The `State` specific `Dispatcher`, containing `System`s only relevant for this `State`.
dispatcher: Option<Dispatcher<'a, 'b>>,
}
impl<'a, 'b> SimpleState for PlayState<'a, 'b> {
// On start will run when this state is initialized. For more // On start will run when this state is initialized. For more
// state lifecycle hooks, see: // state lifecycle hooks, see:
@ -33,6 +38,19 @@ impl SimpleState for MyState {
/// function sets size of camera window /// function sets size of camera window
init_camera(world, &dimensions); 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 // Load our sprites and display them
let sprites = load_sprites(world); let sprites = load_sprites(world);
init_sprites(world, &sprites, &dimensions); init_sprites(world, &sprites, &dimensions);
@ -52,6 +70,8 @@ impl SimpleState for MyState {
} }
if is_key_down(&event, VirtualKeyCode::P) { 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)); return Trans::Push(Box::new(PausedState));
} }
} }
@ -61,6 +81,11 @@ impl SimpleState for MyState {
} }
fn update(&mut self, data: &mut StateData<'_, GameData<'_, '_>>) -> SimpleTrans { fn update(&mut self, data: &mut StateData<'_, GameData<'_, '_>>) -> SimpleTrans {
if let Some(dispatcher) = self.dispatcher.as_mut() {
dispatcher.dispatch(&data.world);
}
Trans::None Trans::None
} }
} }
@ -200,8 +225,6 @@ impl SimpleState for PausedState {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) { fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let world = data.world; let world = data.world;
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone(); let dimensions = (*world.read_resource::<ScreenDimensions>()).clone();
let storage = world.read_storage::<BirbGravity>();
} }
fn handle_event( fn handle_event(
@ -213,6 +236,7 @@ impl SimpleState for PausedState {
if let StateEvent::Window(event) = &event { if let StateEvent::Window(event) = &event {
// Check if the window should be closed // Check if the window should be closed
if is_key_down(&event, VirtualKeyCode::Space) { if is_key_down(&event, VirtualKeyCode::Space) {
return Trans::Pop; return Trans::Pop;
} }
} }

@ -6,7 +6,7 @@ use amethyst::{
ecs::prelude::{}, ecs::prelude::{},
ecs::prelude::{ ecs::prelude::{
Component, DenseVecStorage, Entity, Join, Read, Component, DenseVecStorage, Entity, Join, Read,
ReadStorage, System, SystemData, WriteStorage ReadStorage, System, SystemData, WriteStorage, Write
}, },
input::{InputHandler, StringBindings}, input::{InputHandler, StringBindings},
}; };
@ -16,6 +16,14 @@ use crate::components::*;
pub struct ScrollScrollables; 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 // This system iterates all the objects with transform (and falling object) component
impl<'a> System<'a> for ScrollScrollables { impl<'a> System<'a> for ScrollScrollables {
type SystemData = ( type SystemData = (
@ -52,6 +60,8 @@ impl<'a> System<'a> for BirbGravity {
fn run(&mut self, (mut transforms, mut scrolling, time, input): Self::SystemData) { fn run(&mut self, (mut transforms, mut scrolling, time, input): Self::SystemData) {
for (mut transform, mut object) in (&mut transforms, &mut scrolling).join() { for (mut transform, mut object) in (&mut transforms, &mut scrolling).join() {
//match game.current_state
if input.action_is_down("flap").expect("No action") { if input.action_is_down("flap").expect("No action") {
object.vertical_speed = 600.0; object.vertical_speed = 600.0;
} }

Loading…
Cancel
Save