Refactored into multiple files
Some checks failed
Rust / rustfmt (pull_request) Failing after 27s
Rust / clippy (pull_request) Failing after 1m10s
Rust / build (pull_request) Successful in 2m56s

This commit is contained in:
Glaeder 2024-07-06 19:13:35 +02:00
parent 400ea91352
commit ee49fb493f
3 changed files with 103 additions and 85 deletions

View file

@ -0,0 +1,69 @@
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);
}
}
}

View file

@ -0,0 +1,23 @@
mod grid;
mod player;
use grid::Grid;
use player::Player;
#[derive(Debug, Default)]
pub struct HouseState {
grid: Grid,
player: Player,
}
pub fn draw(state: &crate::State, _engine: &comfy::EngineContext) {
//Draw Grid
state.house.grid.draw();
//Draw Player
state.house.player.draw();
}
pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext) {
state.house.player.update(&state.house.grid);
}

View file

@ -1,69 +1,15 @@
use comfy::*; use comfy::*;
#[derive(Debug, Default)] use super::Grid;
pub struct HouseState {
grid: Grid,
player: Player,
}
#[derive(Debug)] #[derive(Debug)]
struct Grid { pub struct Player {
nodes: Vec<Vec2>,
connections: Vec<(usize, usize)>,
}
#[derive(Debug)]
struct Player {
position: Vec2, position: Vec2,
speed: f32, speed: f32,
connection: usize, connection: usize,
next_connections: Vec<usize>, next_connections: Vec<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;
}
}
impl Default for Player { impl Default for Player {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -74,38 +20,18 @@ impl Default for Player {
} }
} }
} }
impl Player {
pub fn draw(state: &crate::State, _engine: &comfy::EngineContext) { pub fn draw(&self) {
//Draw Grid draw_circle(self.position, 0.5, RED, 0);
for node in &state.house.grid.nodes {
draw_circle(*node, 0.25, BLUE, 0);
} }
for (conn_i1, conn_i2) in &state.house.grid.connections { pub fn update(&mut self, grid: &Grid) {
let node_1 = state.house.grid.nodes.get(*conn_i1); let allowed_movement = get_allowed_movement(self, grid);
let node_2 = state.house.grid.nodes.get(*conn_i2);
if node_1.is_none() || node_2.is_none() { move_player(self, allowed_movement);
error!("Connection in grid not available {:?}", (conn_i1, conn_i2));
continue; if !on_current_connection(self, grid) && !update_connections(self, grid) {
snap_to_closest_node(self, grid);
} }
draw_line(*node_1.unwrap(), *node_2.unwrap(), 0.1, BLUE, 0);
}
//Draw Player
draw_circle(state.house.player.position, 0.5, RED, 0);
}
pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext) {
let player = &mut state.house.player;
let grid = &state.house.grid;
let allowed_movement = get_allowed_movement(player, grid);
move_player(player, allowed_movement);
if !on_current_connection(player, grid) && !update_connections(player, grid) {
snap_to_closest_node(player, grid);
} }
} }