From 2a83e9124233e6e0f9cf131c9c69de2f8fe58efd Mon Sep 17 00:00:00 2001 From: CapsizeGlimmer <> Date: Sun, 24 May 2020 00:10:08 -0400 Subject: [PATCH] Waypoint notifications have a 10 second cooldown --- common/src/comp/location.rs | 9 +++++++-- server/src/cmd.rs | 3 ++- server/src/sys/waypoint.rs | 15 +++++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/common/src/comp/location.rs b/common/src/comp/location.rs index 492ca13114..97a48e547a 100644 --- a/common/src/comp/location.rs +++ b/common/src/comp/location.rs @@ -1,16 +1,21 @@ +use crate::state::Time; use specs::{Component, FlaggedStorage}; use specs_idvs::IDVStorage; use vek::*; -#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] pub struct Waypoint { pos: Vec3, + last_save: Time, } impl Waypoint { - pub fn new(pos: Vec3) -> Self { Self { pos } } + pub fn new(pos: Vec3, last_save: Time) -> Self { Self { pos, last_save } } pub fn get_pos(&self) -> Vec3 { self.pos } + + /// Time in seconds since this waypoint was saved + pub fn elapsed(&self, time: Time) -> f64 { time.0 - self.last_save.0 } } impl Component for Waypoint { diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 8f421902dc..e7ef27fa58 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -833,11 +833,12 @@ fn handle_waypoint( ) { match server.state.read_component_cloned::(target) { Some(pos) => { + let time = server.state.ecs().read_resource(); let _ = server .state .ecs() .write_storage::() - .insert(target, comp::Waypoint::new(pos.0)); + .insert(target, comp::Waypoint::new(pos.0, *time)); server.notify_client(client, ServerMsg::private(String::from("Waypoint saved!"))); server.notify_client(client, ServerMsg::Notification(Notification::WaypointSaved)); }, diff --git a/server/src/sys/waypoint.rs b/server/src/sys/waypoint.rs index 19abe9572e..23810bd20f 100644 --- a/server/src/sys/waypoint.rs +++ b/server/src/sys/waypoint.rs @@ -3,10 +3,12 @@ use crate::client::Client; use common::{ comp::{Player, Pos, Waypoint, WaypointArea}, msg::{Notification, ServerMsg}, + state::Time, }; -use specs::{Entities, Join, ReadStorage, System, Write, WriteStorage}; +use specs::{Entities, Join, Read, ReadStorage, System, Write, WriteStorage}; -const NOTIFY_DISTANCE: f32 = 10.0; +/// Cooldown time (in seconds) for "Waypoint Saved" notifications +const NOTIFY_TIME: f64 = 10.0; /// This system updates player waypoints /// TODO: Make this faster by only considering local waypoints @@ -19,12 +21,13 @@ impl<'a> System<'a> for Sys { ReadStorage<'a, WaypointArea>, WriteStorage<'a, Waypoint>, WriteStorage<'a, Client>, + Read<'a, Time>, Write<'a, SysTimer>, ); fn run( &mut self, - (entities, positions, players, waypoint_areas, mut waypoints, mut clients, mut timer): Self::SystemData, + (entities, positions, players, waypoint_areas, mut waypoints, mut clients, time, mut timer): Self::SystemData, ) { timer.start(); @@ -33,14 +36,14 @@ impl<'a> System<'a> for Sys { { for (waypoint_pos, waypoint_area) in (&positions, &waypoint_areas).join() { if player_pos.0.distance_squared(waypoint_pos.0) < waypoint_area.radius().powi(2) { - if let Some(wp) = waypoints.get(entity) { - if player_pos.0.distance_squared(wp.get_pos()) > NOTIFY_DISTANCE.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 .postbox .send_message(ServerMsg::Notification(Notification::WaypointSaved)); } } - let _ = waypoints.insert(entity, Waypoint::new(player_pos.0)); } } }