veloren/server/src/sys/waypoint.rs

55 lines
1.8 KiB
Rust
Raw Normal View History

use super::SysTimer;
2020-05-14 16:56:10 +00:00
use crate::client::Client;
use common::{
comp::{Player, Pos, Waypoint, WaypointArea},
msg::{Notification, ServerMsg},
2020-08-29 06:39:16 +00:00
span,
state::Time,
2020-05-14 16:56:10 +00:00
};
use specs::{Entities, Join, Read, ReadStorage, System, Write, WriteStorage};
/// Cooldown time (in seconds) for "Waypoint Saved" notifications
const NOTIFY_TIME: f64 = 10.0;
2020-05-14 16:56:10 +00:00
/// This system updates player waypoints
/// TODO: Make this faster by only considering local waypoints
pub struct Sys;
impl<'a> System<'a> for Sys {
#[allow(clippy::type_complexity)] // TODO: Pending review in #587
type SystemData = (
Entities<'a>,
ReadStorage<'a, Pos>,
ReadStorage<'a, Player>,
ReadStorage<'a, WaypointArea>,
WriteStorage<'a, Waypoint>,
2020-05-14 16:56:10 +00:00
WriteStorage<'a, Client>,
Read<'a, Time>,
Write<'a, SysTimer<Self>>,
);
fn run(
&mut self,
(entities, positions, players, waypoint_areas, mut waypoints, mut clients, time, mut timer): Self::SystemData,
) {
2020-08-29 06:39:16 +00:00
span!(_guard, "waypoint::Sys::run");
timer.start();
2020-05-14 16:56:10 +00:00
for (entity, player_pos, _, client) in
(&entities, &positions, &players, &mut clients).join()
{
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) {
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) {
client.notify(ServerMsg::Notification(Notification::WaypointSaved));
2020-05-14 16:56:10 +00:00
}
}
}
}
}
timer.end();
}
}