Skip to content
This repository has been archived by the owner on Oct 25, 2021. It is now read-only.

Commit

Permalink
Add camera movement when using the arrow keys. (#75)
Browse files Browse the repository at this point in the history
* Add camera movement
  • Loading branch information
sunreef authored and marotili committed Jun 2, 2019
1 parent 60cf0f1 commit efaf66d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
18 changes: 18 additions & 0 deletions resources/input.ron
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,23 @@
"SlowDown": [
[Key(Subtract)]
],
"CameraMoveUp": [
[Key(Up)]
],
"CameraMoveDown": [
[Key(Down)]
],
"CameraMoveLeft": [
[Key(Left)]
],
"CameraMoveRight": [
[Key(Right)]
],
"CameraMoveForward": [
[Key(LShift), Key(Up)]
],
"CameraMoveBackward": [
[Key(LShift), Key(Down)]
],
},
)
5 changes: 5 additions & 0 deletions src/states/main_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ impl MainGameState {
MainGameState {
dispatcher: DispatcherBuilder::new()
.with_pool(pool)
.with(
camera_movement::CameraMovementSystem::default(),
"camera_movement",
&[],
)
.with(perception::SpatialGridSystem, "spatial_grid", &[])
.with(
perception::EntityDetectionSystem,
Expand Down
46 changes: 46 additions & 0 deletions src/systems/camera_movement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use amethyst::{
core::{Named, Time, Transform},
ecs::*,
input::InputHandler,
renderer::Camera,
};

#[derive(Default)]
pub struct CameraMovementSystem {}

impl<'s> System<'s> for CameraMovementSystem {
type SystemData = (
ReadStorage<'s, Camera>,
ReadStorage<'s, Named>,
WriteStorage<'s, Transform>,
Read<'s, InputHandler<String, String>>,
Read<'s, Time>,
);

fn run(&mut self, (cameras, names, mut transforms, input_handler, time): Self::SystemData) {
let delta_time = time.delta_real_seconds();
let move_factor = 12.0 * delta_time;
for (_, name, transform) in (&cameras, &names, &mut transforms).join() {
if name.name == "Main camera" {
if input_handler.action_is_down("CameraMoveUp").unwrap() {
transform.move_up(move_factor);
}
if input_handler.action_is_down("CameraMoveDown").unwrap() {
transform.move_down(move_factor);
}
if input_handler.action_is_down("CameraMoveLeft").unwrap() {
transform.move_left(move_factor);
}
if input_handler.action_is_down("CameraMoveRight").unwrap() {
transform.move_right(move_factor);
}
if input_handler.action_is_down("CameraMoveForward").unwrap() {
transform.move_forward(move_factor);
}
if input_handler.action_is_down("CameraMoveBackward").unwrap() {
transform.move_backward(move_factor);
}
}
}
}
}
1 change: 1 addition & 0 deletions src/systems/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod behaviors;
pub mod camera_movement;
pub mod collision;
pub mod combat;
pub mod debug;
Expand Down
1 change: 0 additions & 1 deletion src/systems/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ impl<'s> System<'s> for MovementSystem {
if movement.velocity.magnitude() > movement.max_movement_speed {
movement.velocity = movement.velocity.normalize() * movement.max_movement_speed;
}

transform.translate_x(movement.velocity.x * delta_time);
transform.translate_y(movement.velocity.y * delta_time);
}
Expand Down

0 comments on commit efaf66d

Please sign in to comment.