turtlegame/src/activities/house/grid.rs
Glaeder 5a4451d6a7
All checks were successful
Rust / rustfmt (push) Successful in 25s
Rust / clippy (push) Successful in 1m21s
Rust / build (push) Successful in 3m1s
cabel-movement (#3)
Cable Movemnt is finished. grid is hard coded

Reviewed-on: #3
Co-authored-by: Glaeder <niklas@vousten.dev>
Co-committed-by: Glaeder <niklas@vousten.dev>
2024-07-06 17:59:56 +00:00

69 lines
1.3 KiB
Rust

use comfy::*;
#[derive(Debug)]
pub struct Grid {
pub nodes: Vec<Vec2>,
pub connections: Vec<(usize, usize)>
}
impl Default for Grid {
fn default() -> Self {
Grid::load() //Just for testing purposes
}
}
impl Grid {
fn load() -> Self {
let mut grid = Self {
nodes: vec![
vec2(10.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 10.0),
vec2(-10.0, 0.0),
vec2(10.0, 10.0),
],
connections: vec![(0, 1), (1, 2), (1, 3), (0, 4), (0, 5), (5, 1), (6, 7)]
};
grid.sanitize();
grid
}
fn sanitize(&mut self) {
let mut len = self.nodes.len();
let connections = self
.connections
.iter()
.filter(|(conn_i1, conn_i2)| {
if conn_i1 >= &mut len || conn_i2 >= &mut len {
error!("Connection in grid not possible {:?}", (conn_i1, conn_i2));
false
} else {
true
}
})
.map(|(conn_i1, conn_i2)| (*conn_i1, *conn_i2))
.collect();
self.connections = connections;
}
pub fn draw(&self) {
//Draw Grid
for node in &self.nodes {
draw_circle(*node, 0.25, BLUE, 0);
}
for (conn_i1, conn_i2) in &self.connections {
let node_1 = self.nodes.get(*conn_i1);
let node_2 = self.nodes.get(*conn_i2);
if node_1.is_none() || node_2.is_none() {
error!("Connection in grid not available {:?}", (conn_i1, conn_i2));
continue;
}
draw_line(*node_1.unwrap(), *node_2.unwrap(), 0.1, BLUE, 0);
}
}
}