From 0c1251e4d9dd4ae64fb06e4caea87c5306ffb39c Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Sat, 23 Mar 2024 07:52:14 +0100 Subject: feat: movement collision --- src/server.rs | 148 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 111 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/server.rs b/src/server.rs index b09e96b..0a5e4cf 100644 --- a/src/server.rs +++ b/src/server.rs @@ -20,9 +20,11 @@ use crate::shared::shape::*; use crate::shared::stats::*; use crate::shared::tower::*; use bevy::prelude::*; +use bevy::utils::hashbrown::HashMap; use bevy::utils::*; use lightyear::prelude::*; use lightyear::server::events::MessageEvent; +use rand::rngs::ThreadRng; use rand::Rng; pub mod entity_map; @@ -100,59 +102,51 @@ impl Plugin for ServerPlugin { .before(imperative_attack_approach), ) .add_systems(FixedUpdate, minion_despawn) - .add_systems(FixedUpdate, (nexus_tick, nexus_spawn_minions)); + .add_systems(FixedUpdate, (nexus_tick, nexus_spawn_minions)) + .add_systems( + FixedUpdate, + movement_collision + .after(imperative_attack_approach) + .after(imperative_walk_to), + ); } } fn setup(mut commands: Commands, mut entity_map: ResMut) { - { - let client_id = 1; + 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() { let entity = commands.spawn(TowerBundle::new( - client_id, - Vec2::new(0., 100.), + *client_id, + Vec2::new(0., *n as f32 * 250.), Faction::Blue, )); - entity_map.0.insert(client_id, entity.id()); + entity_map.0.insert(*client_id, entity.id()); } { - let client_id = 2; - let entity = commands.spawn(TowerBundle::new( - client_id, - Vec2::new(0., -100.), - Faction::Red, - )); - entity_map.0.insert(client_id, entity.id()); - } - for client_id in 3..=8 { - let entity = commands.spawn(MinionBundle::new( + let client_id = blue_nexus; + let entity = commands.spawn(NexusBundle::new( client_id, - Vec2::new((client_id - 2) as f32 * 25., 0.), + Vec2::new(0., 1000.0), Faction::Blue, )); entity_map.0.insert(client_id, entity.id()); } - for client_id in 9..=14 { - let entity = commands.spawn(MinionBundle::new( - client_id, - Vec2::new((client_id - 8) as f32 * -25., 0.), + + for (n, client_id) in [(1, red_tower_1), (2, red_tower_2), (3, red_tower_3)].iter() { + let entity = commands.spawn(TowerBundle::new( + *client_id, + Vec2::new(0., *n as f32 * -250.), Faction::Red, )); - entity_map.0.insert(client_id, entity.id()); - } - { - let client_id = 15; - let entity = commands.spawn(NexusBundle::new( - client_id, - Vec2::new(100., 100.0), - Faction::Blue, - )); - entity_map.0.insert(client_id, entity.id()); + entity_map.0.insert(*client_id, entity.id()); } { - let client_id = 16; + let client_id = red_nexus; let entity = commands.spawn(NexusBundle::new( client_id, - Vec2::new(-100., -100.0), + Vec2::new(0., -1000.0), Faction::Red, )); entity_map.0.insert(client_id, entity.id()); @@ -858,7 +852,7 @@ fn nexus_spawn_minions( ) { for (nexus_nexus, nexus_player_position, nexus_faction) in nexuses.iter() { if nexus_nexus.spawn_minions.just_finished() { - let client_ids = generate_client_ids(5, &entity_map); + let client_ids = generate_client_ids::<5>(&entity_map); for client_id in client_ids { let entity = commands.spawn(MinionBundle::new( client_id, @@ -871,11 +865,11 @@ fn nexus_spawn_minions( } } -fn generate_client_ids(n: u64, entity_map: &ResMut) -> Vec { +fn generate_client_ids(entity_map: &ResMut) -> [u64; N] { let mut rng = rand::thread_rng(); let mut k = 0; let mut client_ids = vec![]; - while k < n { + while k < N { let client_id = rng.gen(); if entity_map.0.contains_key(&client_id) { continue; @@ -883,7 +877,7 @@ fn generate_client_ids(n: u64, entity_map: &ResMut) -> Vec { client_ids.push(client_id); k += 1; } - client_ids + client_ids.try_into().unwrap() } fn nexus_tick(mut nexuses: Query<&mut Nexus>, time: Res