Skip to content

Commit a1a5835

Browse files
committed
make paddles move
1 parent b325951 commit a1a5835

File tree

5 files changed

+62
-8
lines changed

5 files changed

+62
-8
lines changed

config/bindings.ron

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(
2+
axes: {
3+
"left_paddle": Emulated(pos: Key(W), neg: Key(S)),
4+
"right_paddle": Emulated(pos: Key(Up), neg: Key(Down)),
5+
},
6+
actions: {},
7+
)

src/main.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
//! Pong Tutorial 1
2-
31
use amethyst::{
2+
core::transform::TransformBundle,
3+
input::{InputBundle, StringBindings},
44
prelude::*,
55
renderer::{
66
plugins::{RenderFlat2D, RenderToWindow},
77
types::DefaultBackend,
88
RenderingBundle,
99
},
1010
utils::application_root_dir,
11-
core::transform::TransformBundle,
1211
};
1312

1413
mod pong;
14+
mod systems;
1515

1616
use crate::pong::PongGame;
1717

@@ -21,6 +21,10 @@ fn main() -> amethyst::Result<()> {
2121
let app_root = application_root_dir()?;
2222
let display_config_path = app_root.join("config").join("display.ron");
2323

24+
let binding_path = app_root.join("config").join("bindings.ron");
25+
let input_bundle =
26+
InputBundle::<StringBindings>::new().with_bindings_from_file(binding_path)?;
27+
2428
let game_data = GameDataBuilder::default()
2529
.with_bundle(
2630
RenderingBundle::<DefaultBackend>::new()
@@ -34,7 +38,9 @@ fn main() -> amethyst::Result<()> {
3438
.with_plugin(RenderFlat2D::default()),
3539
)?
3640
// Add the transform bundle which handles tracking entity positions
37-
.with_bundle(TransformBundle::new())?;
41+
.with_bundle(TransformBundle::new())?
42+
.with_bundle(input_bundle)?
43+
.with(systems::PaddleSystem, "paddle_system", &["input_system"]);
3844

3945
let assets_dir = app_root.join("assets");
4046
let mut game = Application::new(assets_dir, PongGame, game_data)?;

src/pong.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use amethyst::{
2-
assets::{AssetStorage, Loader, Handle},
2+
assets::{AssetStorage, Handle, Loader},
33
core::transform::Transform,
44
ecs::prelude::{Component, DenseVecStorage},
55
prelude::*,
@@ -12,7 +12,7 @@ const IDENTITY: f32 = 1.0;
1212
const Z_FRONT: f32 = 0.0;
1313

1414
const ARENA_WIDTH: f32 = 100.0;
15-
const ARENA_HEIGHT: f32 = 100.0;
15+
pub const ARENA_HEIGHT: f32 = 100.0;
1616
const HALVE_WIDTH: f32 = ARENA_WIDTH * 0.5;
1717
const HALVE_HEIGHT: f32 = ARENA_HEIGHT * 0.5;
1818

@@ -135,8 +135,6 @@ impl SimpleState for PongGame {
135135
// Load the spritesheet necessary to render the graphics.
136136
let sprite_sheet_handle = load_sprite_sheet(world);
137137

138-
world.register::<Paddle>();
139-
140138
initialise_paddles(world, sprite_sheet_handle);
141139
initialise_camera(world);
142140
}

src/systems/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub use self::paddle::PaddleSystem;
2+
3+
mod paddle;

src/systems/paddle.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use amethyst::{
2+
core::Transform,
3+
derive::SystemDesc,
4+
ecs::{Join, Read, ReadStorage, System, SystemData, WriteStorage},
5+
input::{InputHandler, StringBindings},
6+
};
7+
8+
// You'll have to mark PADDLE_HEIGHT as public in pong.rs
9+
use crate::pong::{Paddle, Side, ARENA_HEIGHT};
10+
11+
#[derive(SystemDesc)]
12+
pub struct PaddleSystem;
13+
14+
impl<'s> System<'s> for PaddleSystem {
15+
type SystemData = (
16+
WriteStorage<'s, Transform>,
17+
ReadStorage<'s, Paddle>,
18+
Read<'s, InputHandler<StringBindings>>,
19+
);
20+
21+
fn run(&mut self, (mut transforms, paddles, input): Self::SystemData) {
22+
for (paddle, transform) in (&paddles, &mut transforms).join() {
23+
let movement = match paddle.side {
24+
Side::Left => input.axis_value("left_paddle"),
25+
Side::Right => input.axis_value("right_paddle"),
26+
};
27+
if let Some(mv_amount) = movement {
28+
if mv_amount != 0.0 {
29+
let scaled_amount = 1.2 * mv_amount as f32;
30+
let paddle_y = transform.translation().y;
31+
transform.set_translation_y(
32+
(paddle_y + scaled_amount)
33+
.min(ARENA_HEIGHT - paddle.height * 0.5)
34+
.max(paddle.height * 0.5),
35+
);
36+
}
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)