Refactor power connection to work with different electric poles
This commit is contained in:
parent
b4ab291884
commit
642f815f9d
8 changed files with 394 additions and 144 deletions
|
|
@ -45,6 +45,10 @@ impl AABB {
|
|||
&& self.min.y <= other.max.y
|
||||
&& self.max.y >= other.min.y
|
||||
}
|
||||
|
||||
pub fn contains_pos(self, pos: Position) -> bool {
|
||||
self.min.x <= pos.x && self.max.x >= pos.x && self.min.y <= pos.y && self.max.y >= pos.y
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
pub mod arena;
|
||||
pub mod map;
|
||||
pub mod position_map;
|
||||
|
||||
pub use arena::*;
|
||||
pub use map::*;
|
||||
pub use position_map::*;
|
||||
|
|
|
|||
62
factorio-core/src/misc/position_map.rs
Normal file
62
factorio-core/src/misc/position_map.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
use std::ops::{Index, IndexMut};
|
||||
|
||||
use crate::{aabb::AABB, prelude::Position};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PositionMap<T> {
|
||||
data: Vec<T>,
|
||||
aabb: AABB,
|
||||
}
|
||||
|
||||
impl<T> PositionMap<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
pub fn new(aabb: AABB, value: &T) -> Self {
|
||||
Self::new_internal(aabb, || value.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PositionMap<T>
|
||||
where
|
||||
T: Default,
|
||||
{
|
||||
pub fn new_with_default(aabb: AABB) -> Self {
|
||||
Self::new_internal(aabb, || T::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PositionMap<T> {
|
||||
fn new_internal(aabb: AABB, f: impl Fn() -> T) -> Self {
|
||||
let len = aabb.size().x as usize * aabb.size().y as usize;
|
||||
Self {
|
||||
data: (0..len).map(|_| f()).collect(),
|
||||
aabb,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_index(&self, pos: Position) -> usize {
|
||||
let p = pos - self.aabb.min();
|
||||
|
||||
p.y as usize * self.aabb.size().x as usize + p.x as usize
|
||||
}
|
||||
|
||||
pub fn get_aabb(&self) -> AABB {
|
||||
self.aabb
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Index<Position> for PositionMap<T> {
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, index: Position) -> &Self::Output {
|
||||
&self.data[self.get_index(index)]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IndexMut<Position> for PositionMap<T> {
|
||||
fn index_mut(&mut self, index: Position) -> &mut Self::Output {
|
||||
let i = self.get_index(index);
|
||||
&mut self.data[i]
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue