2020-03-09 03:32:34 +00:00
|
|
|
use super::SysTimer;
|
2020-10-17 09:36:44 +00:00
|
|
|
use crate::streams::{GeneralStream, GetStream};
|
2020-05-14 16:56:10 +00:00
|
|
|
use common::{
|
|
|
|
comp::{Player, Pos, Waypoint, WaypointArea},
|
2020-10-07 10:31:49 +00:00
|
|
|
msg::{Notification, ServerGeneral},
|
2020-08-29 06:39:16 +00:00
|
|
|
span,
|
2020-05-24 04:10:08 +00:00
|
|
|
state::Time,
|
2020-05-14 16:56:10 +00:00
|
|
|
};
|
2020-05-24 04:10:08 +00:00
|
|
|
use specs::{Entities, Join, Read, ReadStorage, System, Write, 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
|
2020-01-25 12:27:36 +00:00
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)] // TODO: Pending review in #587
|
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-10-16 19:37:28 +00:00
|
|
|
WriteStorage<'a, GeneralStream>,
|
2020-05-24 04:10:08 +00:00
|
|
|
Read<'a, Time>,
|
2020-03-09 03:32:34 +00:00
|
|
|
Write<'a, SysTimer<Self>>,
|
2020-01-25 12:27:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
2020-10-16 19:37:28 +00:00
|
|
|
(
|
|
|
|
entities,
|
|
|
|
positions,
|
|
|
|
players,
|
|
|
|
waypoint_areas,
|
|
|
|
mut waypoints,
|
|
|
|
mut general_streams,
|
|
|
|
time,
|
|
|
|
mut timer,
|
|
|
|
): Self::SystemData,
|
2020-01-25 12:27:36 +00:00
|
|
|
) {
|
2020-09-07 04:59:16 +00:00
|
|
|
span!(_guard, "run", "waypoint::Sys::run");
|
2020-03-09 03:32:34 +00:00
|
|
|
timer.start();
|
|
|
|
|
2020-10-16 19:37:28 +00:00
|
|
|
for (entity, player_pos, _, general_stream) in
|
|
|
|
(&entities, &positions, &players, &mut general_streams).join()
|
2020-05-14 16:56:10 +00:00
|
|
|
{
|
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-10-21 10:23:34 +00:00
|
|
|
general_stream.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 03:32:34 +00:00
|
|
|
|
|
|
|
timer.end();
|
2020-01-25 12:27:36 +00:00
|
|
|
}
|
|
|
|
}
|