aboutsummaryrefslogtreecommitdiffstats
path: root/src/server.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/server.rs')
-rw-r--r--src/server.rs95
1 files changed, 60 insertions, 35 deletions
diff --git a/src/server.rs b/src/server.rs
index fab529f..1b9b7ef 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -1,5 +1,6 @@
use crate::protocol::*;
use crate::server::network::*;
+use crate::shared::champion::*;
use crate::shared::cooldown::*;
use crate::shared::health::*;
use crate::shared::imperative::*;
@@ -9,6 +10,7 @@ use bevy::prelude::*;
use bevy::utils::Duration;
use bevy::utils::HashMap;
use lightyear::prelude::*;
+use lightyear::server::events::MessageEvent;
use rand::Rng;
mod network;
@@ -47,6 +49,7 @@ impl Plugin for ServerPlugin {
})
.add_systems(Startup, setup)
.add_systems(Update, (connects, disconnects))
+ .add_systems(Update, receive_message)
.add_systems(Update, timers_ticket)
.add_systems(Update, health_regen.after(timers_ticket))
.add_systems(
@@ -94,6 +97,21 @@ fn connects(
}
}
+fn receive_message(
+ entity_map: Res<EntityMap>,
+ mut commands: Commands,
+ mut reader: EventReader<MessageEvent<SelectChampion>>,
+) {
+ for event in reader.read() {
+ let client_id = event.context();
+ let SelectChampion(champion) = event.message();
+ let Some(entity) = entity_map.0.get(client_id) else {
+ return;
+ };
+ commands.entity(*entity).insert(*champion);
+ }
+}
+
fn disconnects(
mut commands: Commands,
mut disconnects: EventReader<server::DisconnectEvent>,
@@ -175,47 +193,54 @@ fn imperative_attack(
mut cooldowns: Query<&mut Cooldown>,
mut players: Query<(&PlayerId, &mut Imperative)>,
mut positions: Query<&mut PlayerPosition>,
+ champions: Query<&Champion>,
time: Res<Time>,
) {
for (id, mut imperative) in players.iter_mut() {
match *imperative {
Imperative::Attack(target_player) => {
- if let Some(entity) = entity_map.0.get(&id.0) {
- if let Some(target_entity) = entity_map.0.get(&target_player.0) {
- if let Ok([mut position, target_position]) =
- positions.get_many_mut([*entity, *target_entity])
- {
- let distance = target_position.0.distance(position.0);
- if distance > ATTACK_RANGE {
- let (new_position, _) = move_to_target(
- position.0,
- target_position.0,
- MOVEMENT_SPEED * time.delta().as_secs_f32(),
- );
- position.0 = new_position;
- } else {
- if let Ok(mut cooldown) = cooldowns.get_mut(*entity) {
- if cooldown.a_cooldown.is_zero() {
- cooldown.a_cooldown = Duration::from_secs_f32(1.5);
- commands.spawn(ProjectileBundle {
- projectile: Projectile {
- target_player,
- source_player: *id,
- damage: 4.,
- },
- position: ProjectilePosition(position.0),
- replicate: Replicate::default(),
- });
- }
- } else {
- }
- *imperative = Imperative::Idle;
- }
- } else {
- *imperative = Imperative::Idle;
- }
- } else {
+ let Some(entity) = entity_map.0.get(&id.0) else {
+ *imperative = Imperative::Idle;
+ return;
+ };
+ let Ok(champion) = champions.get(*entity) else {
+ *imperative = Imperative::Idle;
+ return;
+ };
+ let Some(target_entity) = entity_map.0.get(&target_player.0) else {
+ *imperative = Imperative::Idle;
+ return;
+ };
+ let Ok([mut position, target_position]) =
+ positions.get_many_mut([*entity, *target_entity])
+ else {
+ *imperative = Imperative::Idle;
+ return;
+ };
+ let distance = target_position.0.distance(position.0);
+ if distance > Stats::from_champion(*champion).attack_range {
+ let (new_position, _) = move_to_target(
+ position.0,
+ target_position.0,
+ MOVEMENT_SPEED * time.delta().as_secs_f32(),
+ );
+ position.0 = new_position;
+ } else {
+ let Ok(mut cooldown) = cooldowns.get_mut(*entity) else {
*imperative = Imperative::Idle;
+ return;
+ };
+ if cooldown.a_cooldown.is_zero() {
+ cooldown.a_cooldown = Duration::from_secs_f32(1.5);
+ commands.spawn(ProjectileBundle {
+ projectile: Projectile {
+ target_player,
+ source_player: *id,
+ damage: 4.,
+ },
+ position: ProjectilePosition(position.0),
+ replicate: Replicate::default(),
+ });
}
}
}