Adding fixed time loop abstraction

wip/yesman
Katharina Fey 4 years ago
parent 22c18205bc
commit 581f9e8089
Signed by: kookie
GPG Key ID: F972AEEA2887D547
  1. 22
      src/_loop.rs
  2. 16
      src/data.rs
  3. 1
      src/lib.rs
  4. 38
      src/map.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);
}
}

@ -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;

@ -6,6 +6,7 @@ pub use _if::GameIf;
pub mod data;
pub mod gens;
mod _loop;
mod _match;
mod config;
mod io;

@ -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()
}
}

Loading…
Cancel
Save