Waypoint notifications have a 10 second cooldown

This commit is contained in:
CapsizeGlimmer 2020-05-24 00:10:08 -04:00
parent b849f654c5
commit 2a83e91242
3 changed files with 18 additions and 9 deletions

View File

@ -1,16 +1,21 @@
use crate::state::Time;
use specs::{Component, FlaggedStorage}; use specs::{Component, FlaggedStorage};
use specs_idvs::IDVStorage; use specs_idvs::IDVStorage;
use vek::*; use vek::*;
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct Waypoint { pub struct Waypoint {
pos: Vec3<f32>, pos: Vec3<f32>,
last_save: Time,
} }
impl Waypoint { impl Waypoint {
pub fn new(pos: Vec3<f32>) -> Self { Self { pos } } pub fn new(pos: Vec3<f32>, last_save: Time) -> Self { Self { pos, last_save } }
pub fn get_pos(&self) -> Vec3<f32> { self.pos } pub fn get_pos(&self) -> Vec3<f32> { 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 { impl Component for Waypoint {

View File

@ -833,11 +833,12 @@ fn handle_waypoint(
) { ) {
match server.state.read_component_cloned::<comp::Pos>(target) { match server.state.read_component_cloned::<comp::Pos>(target) {
Some(pos) => { Some(pos) => {
let time = server.state.ecs().read_resource();
let _ = server let _ = server
.state .state
.ecs() .ecs()
.write_storage::<comp::Waypoint>() .write_storage::<comp::Waypoint>()
.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::private(String::from("Waypoint saved!")));
server.notify_client(client, ServerMsg::Notification(Notification::WaypointSaved)); server.notify_client(client, ServerMsg::Notification(Notification::WaypointSaved));
}, },

View File

@ -3,10 +3,12 @@ use crate::client::Client;
use common::{ use common::{
comp::{Player, Pos, Waypoint, WaypointArea}, comp::{Player, Pos, Waypoint, WaypointArea},
msg::{Notification, ServerMsg}, 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 /// This system updates player waypoints
/// TODO: Make this faster by only considering local waypoints /// TODO: Make this faster by only considering local waypoints
@ -19,12 +21,13 @@ impl<'a> System<'a> for Sys {
ReadStorage<'a, WaypointArea>, ReadStorage<'a, WaypointArea>,
WriteStorage<'a, Waypoint>, WriteStorage<'a, Waypoint>,
WriteStorage<'a, Client>, WriteStorage<'a, Client>,
Read<'a, Time>,
Write<'a, SysTimer<Self>>, Write<'a, SysTimer<Self>>,
); );
fn run( fn run(
&mut self, &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(); timer.start();
@ -33,14 +36,14 @@ impl<'a> System<'a> for Sys {
{ {
for (waypoint_pos, waypoint_area) in (&positions, &waypoint_areas).join() { 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 player_pos.0.distance_squared(waypoint_pos.0) < waypoint_area.radius().powi(2) {
if let Some(wp) = waypoints.get(entity) { if let Ok(wp_old) = waypoints.insert(entity, Waypoint::new(player_pos.0, *time))
if player_pos.0.distance_squared(wp.get_pos()) > NOTIFY_DISTANCE.powi(2) { {
if wp_old.map_or(true, |w| w.elapsed(*time) > NOTIFY_TIME) {
client client
.postbox .postbox
.send_message(ServerMsg::Notification(Notification::WaypointSaved)); .send_message(ServerMsg::Notification(Notification::WaypointSaved));
} }
} }
let _ = waypoints.insert(entity, Waypoint::new(player_pos.0));
} }
} }
} }