2020-11-02 18:30:56 +00:00
|
|
|
use crate::client::Client;
|
2020-05-14 16:56:10 +00:00
|
|
|
use common::{
|
|
|
|
comp::{Player, Pos, Waypoint, WaypointArea},
|
2020-12-01 00:28:00 +00:00
|
|
|
resources::Time,
|
2021-03-08 11:13:59 +00:00
|
|
|
system::{Job, Origin, Phase, System},
|
2020-05-14 16:56:10 +00:00
|
|
|
};
|
2020-12-13 17:11:55 +00:00
|
|
|
use common_net::msg::{Notification, ServerGeneral};
|
2021-03-08 08:36:53 +00:00
|
|
|
use specs::{Entities, Join, Read, ReadStorage, WriteStorage};
|
2020-01-25 12:27:36 +00:00
|
|
|
|
2020-05-24 04:10:08 +00:00
|
|
|
/// Cooldown time (in seconds) for "Waypoint Saved" notifications
|
|
|
|
const NOTIFY_TIME: f64 = 10.0;
|
2020-05-14 16:56:10 +00:00
|
|
|
|
2020-01-26 12:47:41 +00:00
|
|
|
/// This system updates player waypoints
|
|
|
|
/// TODO: Make this faster by only considering local waypoints
|
2021-03-04 14:00:16 +00:00
|
|
|
#[derive(Default)]
|
2020-01-25 12:27:36 +00:00
|
|
|
pub struct Sys;
|
2021-03-08 11:13:59 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2021-03-08 08:36:53 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2020-01-25 12:27:36 +00:00
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
|
|
|
ReadStorage<'a, Pos>,
|
|
|
|
ReadStorage<'a, Player>,
|
|
|
|
ReadStorage<'a, WaypointArea>,
|
|
|
|
WriteStorage<'a, Waypoint>,
|
2020-11-02 18:30:56 +00:00
|
|
|
ReadStorage<'a, Client>,
|
2020-05-24 04:10:08 +00:00
|
|
|
Read<'a, Time>,
|
2020-01-25 12:27:36 +00:00
|
|
|
);
|
|
|
|
|
2021-03-04 14:00:16 +00:00
|
|
|
const NAME: &'static str = "waypoint";
|
|
|
|
const ORIGIN: Origin = Origin::Server;
|
|
|
|
const PHASE: Phase = Phase::Create;
|
|
|
|
|
2020-01-25 12:27:36 +00:00
|
|
|
fn run(
|
2021-03-08 11:13:59 +00:00
|
|
|
_job: &mut Job<Self>,
|
2020-10-16 19:37:28 +00:00
|
|
|
(
|
|
|
|
entities,
|
|
|
|
positions,
|
|
|
|
players,
|
|
|
|
waypoint_areas,
|
|
|
|
mut waypoints,
|
2020-11-02 18:30:56 +00:00
|
|
|
clients,
|
2020-10-16 19:37:28 +00:00
|
|
|
time,
|
|
|
|
): Self::SystemData,
|
2020-01-25 12:27:36 +00:00
|
|
|
) {
|
2020-11-02 18:30:56 +00:00
|
|
|
for (entity, player_pos, _, client) in (&entities, &positions, &players, &clients).join() {
|
2020-01-25 12:27:36 +00:00
|
|
|
for (waypoint_pos, waypoint_area) in (&positions, &waypoint_areas).join() {
|
2020-05-14 16:56:10 +00:00
|
|
|
if player_pos.0.distance_squared(waypoint_pos.0) < waypoint_area.radius().powi(2) {
|
2020-05-24 04:10:08 +00:00
|
|
|
if let Ok(wp_old) = waypoints.insert(entity, Waypoint::new(player_pos.0, *time))
|
|
|
|
{
|
|
|
|
if wp_old.map_or(true, |w| w.elapsed(*time) > NOTIFY_TIME) {
|
2020-11-02 18:30:56 +00:00
|
|
|
client.send_fallible(ServerGeneral::Notification(
|
2020-10-17 09:36:44 +00:00
|
|
|
Notification::WaypointSaved,
|
|
|
|
));
|
2020-05-14 16:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-25 12:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|