made paddles move... the tutorial advances

This commit is contained in:
brad 2019-04-22 22:28:09 -05:00
parent 2143e546b1
commit 87c8fe2b1b
4 changed files with 54 additions and 8 deletions

View File

@ -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::<String, String>::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)?;

View File

@ -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::<Paddle>();
//world.register::<Paddle>();
initialise_paddles(world, sprite_sheet_handle);
initialise_camera(world);
}

3
src/systems/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod paddle;
pub use self::paddle::PaddleSystem;

34
src/systems/paddle.rs Normal file
View File

@ -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<String, String>>,
);
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),
);
}
}
}
}