aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2020-03-22 21:22:35 +0100
committerKatharina Fey <kookie@spacekookie.de>2020-03-22 21:22:35 +0100
commit581f9e80898e3df63868538e05648f05a4c03972 (patch)
treec6dbc283ed99cd5dc8e12a9431f633612984c26d
parent22c18205bcb3702ddf60f41f77f6f06554202f35 (diff)
Adding fixed time loop abstraction
-rw-r--r--src/_loop.rs22
-rw-r--r--src/data.rs16
-rw-r--r--src/lib.rs1
-rw-r--r--src/map.rs38
4 files changed, 66 insertions, 11 deletions
diff --git a/src/_loop.rs b/src/_loop.rs
new file mode 100644
index 000000000000..1fc1bf8fcc28
--- /dev/null
+++ b/src/_loop.rs
@@ -0,0 +1,22 @@
+//! A timed loop implementation
+
+use async_std::{future::Future, task};
+use chrono::{DateTime, Utc};
+use std::{thread, time::Duration};
+
+/// Number of ticks per second
+const TICKS: u64 = 100;
+const TICK_TIME: Duration = Duration::from_millis(1000 / TICKS);
+
+pub fn block_loop<F>(f: F)
+where
+ F: Future<Output = ()> + Send + Copy + 'static,
+{
+ loop {
+ let t1 = Utc::now();
+ task::block_on(f);
+ let t2 = Utc::now();
+ let t3 = (t2 - t1).to_std().unwrap();
+ thread::sleep(TICK_TIME - t3);
+ }
+}
diff --git a/src/data.rs b/src/data.rs
index 940bda10e7e8..5ce5517c2b07 100644
--- a/src/data.rs
+++ b/src/data.rs
@@ -19,22 +19,22 @@ pub type NodeId = usize;
#[derive(Serialize, Deserialize)]
pub struct Node {
/// Each node has a unique ID by which it's addressed
- id: NodeId,
+ pub id: NodeId,
/// The current health
- health: AtomicU32,
+ pub health: AtomicU32,
/// The max health
- max_health: AtomicU32,
+ pub max_health: AtomicU32,
/// The owner of this node
- owner: Owner,
+ pub owner: Owner,
/// Upgrade state
- type_: Upgrade,
+ pub type_: Upgrade,
/// Number of links on the map
- links: u8,
+ pub links: u8,
/// Active link states
- link_states: Vec<Arc<Link>>,
+ pub link_states: Vec<Arc<Link>>,
/// Input buffer
#[serde(skip)]
- buffer: Vec<Packet>,
+ pub buffer: Vec<Packet>,
}
pub type LinkId = usize;
diff --git a/src/lib.rs b/src/lib.rs
index d6e3f246a08d..94117ddbcffc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,6 +6,7 @@ pub use _if::GameIf;
pub mod data;
pub mod gens;
+mod _loop;
mod _match;
mod config;
mod io;
diff --git a/src/map.rs b/src/map.rs
index 212ce75100ad..37f758b4a433 100644
--- a/src/map.rs
+++ b/src/map.rs
@@ -7,12 +7,16 @@ use crate::{
wire::Response,
};
use async_std::sync::Arc;
-use quadtree_rs::Quadtree;
+use quadtree_rs::{
+ area::{Area, AreaBuilder},
+ point::Point,
+ Quadtree,
+};
use std::collections::BTreeMap;
pub struct MapNode {
- pos: (f64, f64),
- inner: Node,
+ pub pos: (f64, f64),
+ pub inner: Node,
}
/// A map that people fight on
@@ -45,4 +49,32 @@ impl Map {
{
unimplemented!()
}
+
+ /// Get all objects that can be selected by a single point
+ pub fn get_by_point(&self, x: i64, y: i64) -> Option<Vec<NodeId>> {
+ self.coord
+ .query(
+ AreaBuilder::default()
+ .anchor(Point::from((x, y)))
+ .dimensions((1, 1))
+ .build()
+ .ok()?,
+ )
+ .map(|entry| Some(entry.value_ref().inner.id))
+ .collect()
+ }
+
+ /// Get all objects that can be selected by a 2d area
+ pub fn get_by_area(&self, x: i64, y: i64, w: i64, h: i64) -> Option<Vec<NodeId>> {
+ self.coord
+ .query(
+ AreaBuilder::default()
+ .anchor(Point::from((x, y)))
+ .dimensions((w, h))
+ .build()
+ .ok()?,
+ )
+ .map(|entry| Some(entry.value_ref().inner.id))
+ .collect()
+ }
}