diff --git a/src/main.rs b/src/main.rs index 91772f5..f0fb88b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,14 @@ extern crate amethyst; mod pong; +mod systems; use amethyst::prelude::*; use amethyst::renderer::{DisplayConfig, DrawFlat2D, Event, Pipeline, RenderBundle, Stage, VirtualKeyCode}; use amethyst::utils::application_root_dir; use amethyst::core::transform::TransformBundle; +use amethyst::input::InputBundle; use crate::pong::Pong; fn main() -> amethyst::Result<()> { @@ -17,16 +19,23 @@ fn main() -> amethyst::Result<()> { let pipe = Pipeline::build() .with_stage( Stage::with_backbuffer() - .clear_target([0.00196, 0.23726, 0.21765, 1.0], 1.0) + .clear_target([0.00196, 0.23726, 0.11765, 1.0], 1.0) .with_pass(DrawFlat2D::new()), ); + + let binding_path = format!( + "{}/resources/bindings_config.ron", + application_root_dir() + ); + + let input_bundle = InputBundle::::new() + .with_bindings_from_file(binding_path)?; let game_data = GameDataBuilder::default() - .with_bundle( - RenderBundle::new(pipe, Some(config)) - .with_sprite_sheet_processor() - )? - .with_bundle(TransformBundle::new())?; + .with_bundle(RenderBundle::new(pipe, Some(config)).with_sprite_sheet_processor())? + .with_bundle(TransformBundle::new())? + .with_bundle(input_bundle)? + .with(systems::PaddleSystem, "paddle_system", &["input_system"]); let mut game = Application::new("./", Pong, game_data)?; diff --git a/src/pong.rs b/src/pong.rs index 6e62bb3..f7198c6 100644 --- a/src/pong.rs +++ b/src/pong.rs @@ -10,8 +10,8 @@ use amethyst::renderer::{ pub const ARENA_HEIGHT: f32 = 100.0; pub const ARENA_WIDTH: f32 = 100.0; +pub const PADDLE_HEIGHT: f32 = 16.0; -const PADDLE_HEIGHT: f32 = 16.0; const PADDLE_WIDTH: f32 = 4.0; pub struct Pong; @@ -21,7 +21,7 @@ impl SimpleState for Pong { let world = data.world; let sprite_sheet_handle = load_sprite_sheet(world); - world.register::(); + //world.register::(); initialise_paddles(world, sprite_sheet_handle); initialise_camera(world); } diff --git a/src/systems/mod.rs b/src/systems/mod.rs new file mode 100644 index 0000000..725bb82 --- /dev/null +++ b/src/systems/mod.rs @@ -0,0 +1,3 @@ +mod paddle; + +pub use self::paddle::PaddleSystem; \ No newline at end of file diff --git a/src/systems/paddle.rs b/src/systems/paddle.rs new file mode 100644 index 0000000..76e92c3 --- /dev/null +++ b/src/systems/paddle.rs @@ -0,0 +1,34 @@ +use amethyst::core::Transform; +use amethyst::ecs::{Join, Read, ReadStorage, System, WriteStorage}; +use amethyst::input::InputHandler; + +// You'll have to mark PADDLE_HEIGHT as public in pong.rs +use crate::pong::{Paddle, Side, ARENA_HEIGHT, PADDLE_HEIGHT}; + +pub struct PaddleSystem; + +impl<'s> System<'s> for PaddleSystem { + type SystemData = ( + WriteStorage<'s, Transform>, + ReadStorage<'s, Paddle>, + Read<'s, InputHandler>, + ); + + fn run(&mut self, (mut transforms, paddles, input): Self::SystemData) { + for (paddle, transform) in (&paddles, &mut transforms).join() { + let movement = match paddle.side { + Side::Left => input.axis_value("left_paddle"), + Side::Right => input.axis_value("right_paddle"), + }; + if let Some(mv_amount) = movement { + let scaled_amount = 1.2 * mv_amount as f32; + let paddle_y = transform.translation().y; + transform.set_y( + (paddle_y + scaled_amount) + .min(ARENA_HEIGHT - PADDLE_HEIGHT * 0.5) + .max(PADDLE_HEIGHT * 0.5), + ); + } + } + } +} \ No newline at end of file