Refactored into multiple files
This commit is contained in:
parent
400ea91352
commit
ee49fb493f
3 changed files with 103 additions and 85 deletions
69
src/activities/house/grid.rs
Normal file
69
src/activities/house/grid.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
23
src/activities/house/mod.rs
Normal file
23
src/activities/house/mod.rs
Normal 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);
|
||||
}
|
|
@ -1,69 +1,15 @@
|
|||
use comfy::*;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct HouseState {
|
||||
grid: Grid,
|
||||
player: Player,
|
||||
}
|
||||
use super::Grid;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Grid {
|
||||
nodes: Vec<Vec2>,
|
||||
connections: Vec<(usize, usize)>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Player {
|
||||
pub struct Player {
|
||||
position: Vec2,
|
||||
speed: f32,
|
||||
connection: 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 {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
|
@ -74,38 +20,18 @@ impl Default for Player {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(state: &crate::State, _engine: &comfy::EngineContext) {
|
||||
//Draw Grid
|
||||
for node in &state.house.grid.nodes {
|
||||
draw_circle(*node, 0.25, BLUE, 0);
|
||||
impl Player {
|
||||
pub fn draw(&self) {
|
||||
draw_circle(self.position, 0.5, RED, 0);
|
||||
}
|
||||
for (conn_i1, conn_i2) in &state.house.grid.connections {
|
||||
let node_1 = state.house.grid.nodes.get(*conn_i1);
|
||||
let node_2 = state.house.grid.nodes.get(*conn_i2);
|
||||
pub fn update(&mut self, grid: &Grid) {
|
||||
let allowed_movement = get_allowed_movement(self, grid);
|
||||
|
||||
if node_1.is_none() || node_2.is_none() {
|
||||
error!("Connection in grid not available {:?}", (conn_i1, conn_i2));
|
||||
continue;
|
||||
move_player(self, allowed_movement);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in a new issue