Skip to content

Commit

Permalink
refactor: Breaking Dispatcher out from the core Application, and maki…
Browse files Browse the repository at this point in the history
…ng the game data pluggable
  • Loading branch information
Rhuagh committed May 14, 2018
1 parent a0d7bae commit 5bacee4
Show file tree
Hide file tree
Showing 28 changed files with 806 additions and 526 deletions.
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ path = "examples/window/main.rs"
name = "sphere"
path = "examples/sphere/main.rs"

[[example]]
name = "sphere_multisample"
path = "examples/sphere_multisample/main.rs"

[[example]]
name = "renderable"
path = "examples/renderable/main.rs"
Expand Down Expand Up @@ -120,6 +124,14 @@ path = "examples/ui/main.rs"
name = "animation"
path = "examples/animation/main.rs"

[[example]]
name = "fly_camera"
path = "examples/fly_camera/main.rs"

[[example]]
name = "sprites"
path = "examples/sprites/main.rs"

[[example]]
name = "pong_tutorial_01"
path = "examples/pong_tutorial_01/main.rs"
Expand Down
19 changes: 13 additions & 6 deletions examples/animation/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ impl Default for Example {
}
}

impl State for Example {
fn on_start(&mut self, world: &mut World) {
impl<'a, 'b> State<GameData<'a, 'b>> for Example {
fn on_start(&mut self, data: StateData<GameData>) {
let StateData { world, .. } = data;
// Initialise the scene with an object, a light and a camera.
let sphere_entity = initialise_sphere(world);
self.sphere = Some(sphere_entity);
Expand All @@ -62,7 +63,8 @@ impl State for Example {
initialise_camera(world);
}

fn handle_event(&mut self, world: &mut World, event: Event) -> Trans {
fn handle_event(&mut self, data: StateData<GameData>, event: Event) -> Trans<GameData<'a, 'b>> {
let StateData { world, .. } = data;
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
Expand Down Expand Up @@ -181,6 +183,11 @@ impl State for Example {
_ => Trans::None,
}
}

fn update(&mut self, data: StateData<GameData>) -> Trans<GameData<'a, 'b>> {
data.data.update(&data.world);
Trans::None
}
}

fn run() -> Result<(), amethyst::Error> {
Expand All @@ -199,14 +206,14 @@ fn run() -> Result<(), amethyst::Error> {

let config = DisplayConfig::load(&display_config_path);

let mut game = Application::build(resources, Example::default())?
let game_data = GameDataBuilder::default()
.with_bundle(AnimationBundle::<AnimationId, Transform>::new(
"animation_control_system",
"sampler_interpolation_system",
))?
.with_bundle(TransformBundle::new().with_dep(&["sampler_interpolation_system"]))?
.with_bundle(RenderBundle::new(pipe, Some(config)))?
.build()?;
.with_bundle(RenderBundle::new(pipe, Some(config)))?;
let mut game = Application::new(resources, Example::default(), game_data)?;
game.run();
Ok(())
}
Expand Down
22 changes: 13 additions & 9 deletions examples/appendix_a/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,27 @@ fn run() -> Result<()> {
.with_pass(DrawUi::new()),
);
let pong_config = PongConfig::load(&config);

let game_data = GameDataBuilder::default()
.with_bundle(
InputBundle::<String, String>::new().with_bindings_from_file(&key_bindings_path),
)?
.with_bundle(PongBundle::default())?
.with_bundle(TransformBundle::new().with_dep(&["ball_system", "paddle_system"]))?
.with_bundle(AudioBundle::new(|music: &mut Music| music.music.next()))?
.with_bundle(UiBundle::<String, String>::new())?
.with_bundle(RenderBundle::new(pipe, Some(display_config)))?;

let mut game = Application::build(assets_dir, Pong)?
.with_frame_limit(
FrameRateLimitStrategy::SleepAndYield(Duration::from_millis(2)),
144,
)
.with_bundle(
InputBundle::<String, String>::new().with_bindings_from_file(&key_bindings_path),
)?
.with_resource(pong_config.arena)
.with_resource(pong_config.ball)
.with_resource(pong_config.paddles)
.with_bundle(PongBundle::default())?
.with_bundle(TransformBundle::new().with_dep(&["ball_system", "paddle_system"]))?
.with_bundle(AudioBundle::new(|music: &mut Music| music.music.next()))?
.with_bundle(UiBundle::<String, String>::new())?
.with_bundle(RenderBundle::new(pipe, Some(display_config)))?
.build()?;
.build(game_data)?;

game.run();
Ok(())
}
Expand Down
12 changes: 9 additions & 3 deletions examples/appendix_a/pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use systems::ScoreText;

pub struct Pong;

impl State for Pong {
fn on_start(&mut self, world: &mut World) {
impl<'a, 'b> State<GameData<'a, 'b>> for Pong {
fn on_start(&mut self, data: StateData<GameData>) {
let StateData { world, .. } = data;
use audio::initialise_audio;

// Setup our game.
Expand All @@ -25,7 +26,7 @@ impl State for Pong {
hide_cursor(world);
}

fn handle_event(&mut self, _: &mut World, event: Event) -> Trans {
fn handle_event(&mut self, _: StateData<GameData>, event: Event) -> Trans<GameData<'a, 'b>> {
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
Expand All @@ -41,6 +42,11 @@ impl State for Pong {
_ => Trans::None,
}
}

fn update(&mut self, data: StateData<GameData>) -> Trans<GameData<'a, 'b>> {
data.data.update(&data.world);
Trans::None
}
}

/// Initialise the camera.
Expand Down
21 changes: 13 additions & 8 deletions examples/asset_loading/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern crate amethyst;
extern crate rayon;

use amethyst::{Application, Error, State, Trans};
use amethyst::{Application, Error, GameData, GameDataBuilder, State, StateData, Trans};
use amethyst::assets::{Loader, Result as AssetResult, SimpleFormat};
use amethyst::config::Config;
use amethyst::core::cgmath::{Array, Vector3};
Expand Down Expand Up @@ -58,8 +58,9 @@ impl SimpleFormat<Mesh> for Custom {

struct AssetsExample;

impl State for AssetsExample {
fn on_start(&mut self, world: &mut World) {
impl<'a, 'b> State<GameData<'a, 'b>> for AssetsExample {
fn on_start(&mut self, data: StateData<GameData>) {
let StateData { world, .. } = data;
world.add_resource(0usize);

initialise_camera(world);
Expand Down Expand Up @@ -95,7 +96,7 @@ impl State for AssetsExample {
.build();
}

fn handle_event(&mut self, _: &mut World, event: Event) -> Trans {
fn handle_event(&mut self, _: StateData<GameData>, event: Event) -> Trans<GameData<'a, 'b>> {
match event {
Event::WindowEvent { event, .. } => {
match event {
Expand All @@ -117,6 +118,11 @@ impl State for AssetsExample {
_ => Trans::None,
}
}

fn update(&mut self, data: StateData<GameData>) -> Trans<GameData<'a, 'b>> {
data.data.update(&data.world);
Trans::None
}
}

fn main() {
Expand Down Expand Up @@ -144,13 +150,12 @@ fn run() -> Result<(), Error> {
.with_pass(DrawShaded::<PosNormTex>::new()),
);

let mut game = Application::build(resources_directory, AssetsExample)
.expect("Failed to build ApplicationBuilder for an unknown reason.")
let game_data = GameDataBuilder::default()
.with_bundle(InputBundle::<String, String>::new())?
.with_bundle(TransformBundle::new())?
.with_bundle(RenderBundle::new(pipeline_builder, Some(display_config)))?
.build()?;
.with_bundle(RenderBundle::new(pipeline_builder, Some(display_config)))?;

let mut game = Application::new(resources_directory, AssetsExample, game_data)?;
game.run();
Ok(())
}
Expand Down
23 changes: 15 additions & 8 deletions examples/fly_camera/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
extern crate amethyst;

use amethyst::{Application, Error, State, Trans};
use amethyst::{Application, Error, GameData, GameDataBuilder, State, StateData, Trans};
use amethyst::assets::Loader;
use amethyst::config::Config;
use amethyst::controls::{FlyControlBundle, FlyControlTag};
Expand All @@ -18,8 +18,9 @@ use amethyst::renderer::{AmbientColor, Camera, DisplayConfig, DrawShaded, Elemen

struct ExampleState;

impl State for ExampleState {
fn on_start(&mut self, world: &mut World) {
impl<'a, 'b> State<GameData<'a, 'b>> for ExampleState {
fn on_start(&mut self, data: StateData<GameData>) {
let StateData { world, .. } = data;
initialise_camera(world);

let assets = load_assets(&world);
Expand All @@ -38,7 +39,7 @@ impl State for ExampleState {
world.add_resource(AmbientColor(Rgba::from([0.1; 3])));
}

fn handle_event(&mut self, _: &mut World, event: Event) -> Trans {
fn handle_event(&mut self, _: StateData<GameData>, event: Event) -> Trans<GameData<'a, 'b>> {
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
Expand All @@ -59,6 +60,11 @@ impl State for ExampleState {
}
Trans::None
}

fn update(&mut self, data: StateData<GameData>) -> Trans<GameData<'a, 'b>> {
data.data.update(&data.world);
Trans::None
}
}

struct Assets {
Expand Down Expand Up @@ -112,8 +118,7 @@ fn run() -> Result<(), Error> {
.clear_target([0.0, 0.0, 0.0, 1.0], 1.0)
.with_pass(DrawShaded::<PosNormTex>::new()),
);
let mut game = Application::build(resources_directory, ExampleState)?
.with_frame_limit(FrameRateLimitStrategy::Unlimited, 0)
let game_data = GameDataBuilder::default()
.with_bundle(FlyControlBundle::<String, String>::new(
Some(String::from("move_x")),
Some(String::from("move_y")),
Expand All @@ -123,8 +128,10 @@ fn run() -> Result<(), Error> {
.with_bundle(
InputBundle::<String, String>::new().with_bindings_from_file(&key_bindings_path),
)?
.with_bundle(RenderBundle::new(pipeline_builder, Some(display_config)))?
.build()?;
.with_bundle(RenderBundle::new(pipeline_builder, Some(display_config)))?;
let mut game = Application::build(resources_directory, ExampleState)?
.with_frame_limit(FrameRateLimitStrategy::Unlimited, 0)
.build(game_data)?;
game.run();
Ok(())
}
Expand Down
24 changes: 15 additions & 9 deletions examples/gltf/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern crate amethyst_gltf;
#[macro_use]
extern crate log;

use amethyst::assets::{AssetStorage, Handle, Loader};
use amethyst::assets::{Handle, Loader};
use amethyst::core::cgmath::{Deg, Quaternion, Rotation3, Vector3};
use amethyst::core::transform::{GlobalTransform, Transform, TransformBundle};
use amethyst::ecs::prelude::Entity;
Expand All @@ -23,8 +23,9 @@ struct Scene {
animation_index: usize,
}

impl State for Example {
fn on_start(&mut self, world: &mut World) {
impl<'a, 'b> State<GameData<'a, 'b>> for Example {
fn on_start(&mut self, data: StateData<GameData>) {
let StateData { world, .. } = data;
let gltf_scene = load_gltf_mesh(
&world,
&*world.read_resource(),
Expand Down Expand Up @@ -87,7 +88,8 @@ impl State for Example {
world.add_resource(AmbientColor(Rgba(0.2, 0.2, 0.2, 0.2)));
}

fn handle_event(&mut self, world: &mut World, event: Event) -> Trans {
fn handle_event(&mut self, data: StateData<GameData>, event: Event) -> Trans<GameData<'a, 'b>> {
let StateData { world, .. } = data;
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
Expand Down Expand Up @@ -139,6 +141,11 @@ impl State for Example {
_ => Trans::None,
}
}

fn update(&mut self, data: StateData<GameData>) -> Trans<GameData<'a, 'b>> {
data.data.update(&data.world);
Trans::None
}
}

fn run() -> Result<(), amethyst::Error> {
Expand All @@ -156,7 +163,7 @@ fn run() -> Result<(), amethyst::Error> {
.with_pass(DrawShadedSeparate::new().with_vertex_skinning()),
);

let mut game = Application::build(resources_directory, Example)?
let game_data = GameDataBuilder::default()
.with(GltfSceneLoaderSystem::new(), "loader_system", &[])
.with_bundle(RenderBundle::new(pipe, Some(config)))?
.with_bundle(
Expand All @@ -173,10 +180,9 @@ fn run() -> Result<(), amethyst::Error> {
"transform_system",
"animation_control_system",
"sampler_interpolation_system",
]))?
.with_resource(AssetStorage::<GltfSceneAsset>::new())
.register::<Handle<GltfSceneAsset>>()
.build()?;
]))?;

let mut game = Application::new(resources_directory, Example, game_data)?;
game.run();
Ok(())
}
Expand Down
16 changes: 8 additions & 8 deletions examples/hello_world/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ use amethyst::prelude::*;

struct Example;

impl State for Example {
fn on_start(&mut self, _: &mut World) {
impl State<()> for Example {
fn on_start(&mut self, _: StateData<()>) {
println!("Begin!");
}

fn update(&mut self, _: &mut World) -> Trans {
println!("Hello from Amethyst!");
Trans::Quit
fn on_stop(&mut self, _: StateData<()>) {
println!("End!");
}

fn on_stop(&mut self, _: &mut World) {
println!("End!");
fn update(&mut self, _: StateData<()>) -> Trans<()> {
println!("Hello from Amethyst!");
Trans::Quit
}
}

fn main() {
let mut game = Application::new("./", Example).expect("Fatal error");
let mut game = Application::new("./", Example, ()).expect("Fatal error");
game.run();
}
Loading

0 comments on commit 5bacee4

Please sign in to comment.