-
I'm creating the game pong. I have two systems an input system that receives input and a paddle movement system. I communicate between the two systems using events. I having trouble getting the player2 paddle to work. This is what my code looks like: #[derive(Component)]
struct Paddle {
speed: f32,
}
#[derive(Component, PartialEq, Eq, Debug)]
enum PlayerType {
Player1,
Player2,
}
#[derive(PartialEq)]
enum Movement {
Up,
Down,
}
#[derive(PartialEq)]
struct InputEvent {
Movement: Movement,
PlayerType: PlayerType,
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>, windows: Res<Windows>) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(UiCameraBundle::default());
let window = windows.get_primary().unwrap();
//Player 1
commands
.spawn_bundle(SpriteBundle {
transform: Transform {
translation: Vec3::new(-500.0, -115.0, 0.0),
scale: Vec3::new(30.0, 120.0, 0.0),
..Default::default()
},
sprite: Sprite {
color: Color::rgb(0.5, 0.5, 1.0),
..Default::default()
},
..Default::default()
})
.insert(Paddle { speed: 500.0 })
.insert(PlayerType::Player1)
.insert(Collider::Paddle);
// player 2
commands
.spawn_bundle(SpriteBundle {
transform: Transform {
translation: Vec3::new(500.0, -115.0, 0.0),
scale: Vec3::new(30.0, 120.0, 0.0),
..Default::default()
},
sprite: Sprite {
color: Color::rgb(0.5, 0.5, 1.0),
..Default::default()
},
..Default::default()
})
.insert(Paddle { speed: 500.0 })
.insert(PlayerType::Player2)
.insert(Collider::Paddle);
}
fn get_input(mut ev_input: EventWriter<InputEvent>, keyboard_input: Res<Input<KeyCode>>) {
if keyboard_input.pressed(KeyCode::W) {
ev_input.send(InputEvent {
Movement: Movement::Up,
PlayerType: PlayerType::Player1,
});
}
if keyboard_input.pressed(KeyCode::S) {
ev_input.send(InputEvent {
Movement: Movement::Down,
PlayerType: PlayerType::Player1,
});
}
//Testing input by sending every frame
ev_input.send(InputEvent {
Movement: Movement::Up,
PlayerType: PlayerType::Player2,
});
if keyboard_input.pressed(KeyCode::Up) {
ev_input.send(InputEvent {
Movement: Movement::Up,
PlayerType: PlayerType::Player2,
});
}
if keyboard_input.pressed(KeyCode::Down) {
ev_input.send(InputEvent {
Movement: Movement::Down,
PlayerType: PlayerType::Player2,
});
}
}
fn paddle_movement_system(
windows: Res<Windows>,
mut ev_input: EventReader<InputEvent>,
mut query: Query<(&Paddle, &mut Transform, &PlayerType)>,
) {
let window = windows.get_primary().unwrap();
for (paddle, mut transform, playerType) in query.iter_mut() {
let mut direction = 0.0;
// Problem is prob here:
for ev in ev_input.iter().filter(|x| x.PlayerType == *playerType) {
println!("{:?}", ev.PlayerType);
match ev {
InputEvent {
Movement: Movement::Down,
..
} => direction -= 1.0,
InputEvent {
Movement: Movement::Up,
..
} => direction += 1.0,
}
}
let scale = transform.scale;
let translation = &mut transform.translation;
// move the paddle vertically
translation.y += direction * paddle.speed * TIME_STEP;
translation.y = translation
.y
.min(window.height() / 2. - scale.y / 2.0)
.max(-(window.height()) / 2. + scale.y / 2.);
}
}
const TIME_STEP: f32 = 1.0 / 60.0;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(ClearColor(Color::rgb(0.9, 0.9, 0.9)))
.add_event::<InputEvent>()
.add_startup_system(setup)
.add_system_set(SystemSet::new().label("input").with_system(get_input))
.add_system_set(
SystemSet::new()
.after("input")
.with_run_criteria(FixedTimestep::step(TIME_STEP as f64))
.with_system(paddle_movement_system)
)
.run();
} Any help would be appreciated! |
Beta Was this translation helpful? Give feedback.
Answered by
hymm
Mar 11, 2022
Replies: 1 comment 1 reply
-
If you want to iterate over events twice, you shouldn't use |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
jak6jak
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to iterate over events twice, you shouldn't use
EventReader
. It has memory of which events it already iterated over. In this case, I would probably just switch the order of the looping. First loop over events and then loop over the player query.