aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-03-23 08:53:12 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-03-23 09:14:56 +0100
commit23d32213ff8832d2c4360618eb511a0098153818 (patch)
tree9004032aca31dd65c65cd1257f67ce1ed17f50c6 /src
parent0c1251e4d9dd4ae64fb06e4caea87c5306ffb39c (diff)
feat: camera movement
Diffstat (limited to 'src')
-rw-r--r--src/client.rs25
-rw-r--r--src/server.rs14
2 files changed, 33 insertions, 6 deletions
diff --git a/src/client.rs b/src/client.rs
index 6fa6ff1..7f1220e 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -94,6 +94,7 @@ impl Plugin for ClientPlugin {
.chain(),
),
)
+ .add_systems(Update, move_camera)
.add_systems(
FixedPreUpdate,
buffer_input.in_set(InputSystemSet::BufferInputs),
@@ -790,3 +791,27 @@ fn health_indicator_despawn(
commands.entity(entity).despawn();
}
}
+
+const CAMERA_MOVEMENT_SPEED: f32 = 100.;
+
+fn move_camera(
+ mut cameras: Query<&mut Transform, With<Camera>>,
+ players: Query<(&PlayerId, &PlayerPosition, &Shape)>,
+ client_id: Res<ClientId>,
+ time: Res<Time>,
+) {
+ let Some(player_position) = player_position(&client_id, &players) else {
+ return;
+ };
+ let dt = time.delta().as_secs_f32();
+ for mut transform in cameras.iter_mut() {
+ let direction = (Vec3::new(
+ player_position.0.x,
+ player_position.0.y,
+ transform.translation.z,
+ ) - transform.translation)
+ .normalize_or_zero();
+ transform.translation +=
+ (dt * CAMERA_MOVEMENT_SPEED).min(direction.length()) * direction.normalize_or_zero();
+ }
+}
diff --git a/src/server.rs b/src/server.rs
index 0a5e4cf..f426b38 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -112,14 +112,16 @@ impl Plugin for ServerPlugin {
}
}
+const TOWER_DISTANCE: f32 = 400.;
+
fn setup(mut commands: Commands, mut entity_map: ResMut<EntityMap>) {
let [blue_tower_1, blue_tower_2, blue_tower_3, blue_nexus, red_tower_1, red_tower_2, red_tower_3, red_nexus] =
generate_client_ids::<8>(&entity_map).into();
- for (n, client_id) in [(1, blue_tower_1), (2, blue_tower_2), (3, blue_tower_3)].iter() {
+ for (n, client_id) in [(0, blue_tower_1), (1, blue_tower_2), (2, blue_tower_3)].iter() {
let entity = commands.spawn(TowerBundle::new(
*client_id,
- Vec2::new(0., *n as f32 * 250.),
+ Vec2::new(0., TOWER_DISTANCE / 2. + *n as f32 * TOWER_DISTANCE),
Faction::Blue,
));
entity_map.0.insert(*client_id, entity.id());
@@ -128,16 +130,16 @@ fn setup(mut commands: Commands, mut entity_map: ResMut<EntityMap>) {
let client_id = blue_nexus;
let entity = commands.spawn(NexusBundle::new(
client_id,
- Vec2::new(0., 1000.0),
+ Vec2::new(0., TOWER_DISTANCE / 2. + 3. * TOWER_DISTANCE),
Faction::Blue,
));
entity_map.0.insert(client_id, entity.id());
}
- for (n, client_id) in [(1, red_tower_1), (2, red_tower_2), (3, red_tower_3)].iter() {
+ for (n, client_id) in [(0, red_tower_1), (1, red_tower_2), (2, red_tower_3)].iter() {
let entity = commands.spawn(TowerBundle::new(
*client_id,
- Vec2::new(0., *n as f32 * -250.),
+ Vec2::new(0., -TOWER_DISTANCE / 2. - *n as f32 * TOWER_DISTANCE),
Faction::Red,
));
entity_map.0.insert(*client_id, entity.id());
@@ -146,7 +148,7 @@ fn setup(mut commands: Commands, mut entity_map: ResMut<EntityMap>) {
let client_id = red_nexus;
let entity = commands.spawn(NexusBundle::new(
client_id,
- Vec2::new(0., -1000.0),
+ Vec2::new(0., -TOWER_DISTANCE / 2. - 3. * TOWER_DISTANCE),
Faction::Red,
));
entity_map.0.insert(client_id, entity.id());