Refactored into multiple functions

This commit is contained in:
Glaeder 2024-07-06 18:55:44 +02:00
parent 13018c034e
commit 400ea91352

View file

@ -100,17 +100,49 @@ pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext) {
let player = &mut state.house.player;
let grid = &state.house.grid;
let (conn_i1, conn_i2) = grid.connections.get(player.connection).unwrap();
let node_1 = grid.nodes.get(*conn_i1).unwrap();
let node_2 = grid.nodes.get(*conn_i2).unwrap();
let allowed_movement = get_allowed_movement(player, grid);
let range = 0.25;
move_player(player, allowed_movement);
if !on_current_connection(player, grid) && !update_connections(player, grid) {
snap_to_closest_node(player, grid);
}
}
fn move_player(player: &mut Player, allowed_movement: (bool, bool, bool, bool)) {
let (
allow_up_movement,
allow_down_movement,
allow_left_movement,
allow_right_movement,
) = if in_node_range(&player.position, node_1, range)
) = allowed_movement;
if allow_up_movement && is_key_down(KeyCode::Up) {
player.position += vec2(0.0, player.speed) * delta();
}
if allow_down_movement && is_key_down(KeyCode::Down) {
player.position += vec2(0.0, -player.speed) * delta();
}
if allow_right_movement && is_key_down(KeyCode::Right) {
player.position += vec2(player.speed, 0.0) * delta();
}
if allow_left_movement && is_key_down(KeyCode::Left) {
player.position += vec2(-player.speed, 0.0) * delta();
}
}
//(UP, DOWN, LEFT, RIGHT)
fn get_allowed_movement(player: &Player, grid: &Grid) -> (bool, bool, bool, bool) {
let (conn_i1, conn_i2) = grid.connections.get(player.connection).unwrap();
let node_1 = grid.nodes.get(*conn_i1).unwrap();
let node_2 = grid.nodes.get(*conn_i2).unwrap();
let range = 0.25;
if in_node_range(&player.position, node_1, range)
|| in_node_range(&player.position, node_2, range)
{
(true, true, true, true)
@ -120,7 +152,7 @@ pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext) {
mut allow_down_movement,
mut allow_left_movement,
mut allow_right_movement,
) = get_allowed_movement(&player.position, node_1, node_2);
) = get_allowed_connection_movement(&player.position, node_1, node_2);
for conn in &player.next_connections {
let (next_conn_i1, next_conn_i2) = grid.connections.get(*conn).unwrap();
@ -132,7 +164,7 @@ pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext) {
next_allow_down_movement,
next_allow_left_movement,
next_allow_right_movement,
) = get_allowed_movement(&player.position, next_node_1, next_node_2);
) = get_allowed_connection_movement(&player.position, next_node_1, next_node_2);
(
allow_up_movement,
@ -152,87 +184,11 @@ pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext) {
allow_left_movement,
allow_right_movement,
)
};
if allow_up_movement && is_key_down(KeyCode::Up) {
player.position += vec2(0.0, player.speed) * delta();
}
if allow_down_movement && is_key_down(KeyCode::Down) {
player.position += vec2(0.0, -player.speed) * delta();
}
if allow_right_movement && is_key_down(KeyCode::Right) {
player.position += vec2(player.speed, 0.0) * delta();
}
if allow_left_movement && is_key_down(KeyCode::Left) {
player.position += vec2(-player.speed, 0.0) * delta();
}
if !on_connection(&player.position, node_1, node_2) {
let mut changed = false;
for conn in &player.next_connections {
let (next_conn_i1, next_conn_i2) = grid.connections.get(*conn).unwrap();
let next_node_1 = grid.nodes.get(*next_conn_i1).unwrap();
let next_node_2 = grid.nodes.get(*next_conn_i2).unwrap();
if on_connection(&player.position, next_node_1, next_node_2) {
changed = true;
player.connection = *conn;
//Update next connections
let mut next_connections = Vec::new();
for (i, (poss_conn_i1, poss_conn_i2)) in
grid.connections.iter().enumerate()
{
if (next_conn_i1, next_conn_i2) != (poss_conn_i1, poss_conn_i2) {
if next_conn_i1 == poss_conn_i1
|| next_conn_i1 == poss_conn_i2
|| next_conn_i2 == poss_conn_i1
|| next_conn_i2 == poss_conn_i2
{
next_connections.push(i);
}
}
}
player.next_connections = next_connections;
break;
}
}
if !changed {
//Get closest node to snap to
let mut nodes = HashSet::new();
nodes.insert(conn_i1);
nodes.insert(conn_i2);
for conn in &player.next_connections {
let (next_conn_i1, next_conn_i2) = grid.connections.get(*conn).unwrap();
nodes.insert(next_conn_i1);
nodes.insert(next_conn_i2);
}
let mut closest_sqared_range = f32::MAX;
let mut closest_node = node_1;
for node_index in nodes {
let current_node = grid.nodes.get(*node_index).unwrap();
let current_squard_range = get_squared_node_range(&player.position, current_node);
if closest_sqared_range > current_squard_range {
closest_sqared_range = current_squard_range;
closest_node = current_node;
}
}
player.position = *closest_node;
}
}
}
//(UP, DOWN, LEFT, RIGHT)
fn get_allowed_movement(
fn get_allowed_connection_movement(
player_position: &Vec2,
node_1: &Vec2,
node_2: &Vec2,
@ -293,6 +249,14 @@ fn get_allowed_movement(
)
}
fn on_current_connection(player: &Player, grid: &Grid) -> bool {
let (conn_i1, conn_i2) = grid.connections.get(player.connection).unwrap();
let node_1 = grid.nodes.get(*conn_i1).unwrap();
let node_2 = grid.nodes.get(*conn_i2).unwrap();
on_connection(&player.position, node_1, node_2)
}
fn on_connection(player_position: &Vec2, node_1: &Vec2, node_2: &Vec2) -> bool {
let on_x = if node_1.x <= node_2.x {
node_1.x <= player_position.x && player_position.x <= node_2.x
@ -309,6 +273,34 @@ fn on_connection(player_position: &Vec2, node_1: &Vec2, node_2: &Vec2) -> bool {
on_x && on_y
}
fn update_connections(player: &mut Player, grid: &Grid) -> bool {
for conn in &player.next_connections {
let (next_conn_i1, next_conn_i2) = grid.connections.get(*conn).unwrap();
let next_node_1 = grid.nodes.get(*next_conn_i1).unwrap();
let next_node_2 = grid.nodes.get(*next_conn_i2).unwrap();
if on_connection(&player.position, next_node_1, next_node_2) {
player.connection = *conn;
//Update next connections
let mut next_connections = Vec::new();
for (i, (poss_conn_i1, poss_conn_i2)) in grid.connections.iter().enumerate() {
if (next_conn_i1, next_conn_i2) != (poss_conn_i1, poss_conn_i2)
&& next_conn_i1 == poss_conn_i1
|| next_conn_i1 == poss_conn_i2
|| next_conn_i2 == poss_conn_i1
|| next_conn_i2 == poss_conn_i2
{
next_connections.push(i);
}
}
player.next_connections = next_connections;
return true;
}
}
false
}
fn in_node_range(player_position: &Vec2, node: &Vec2, range: f32) -> bool {
node.x - range <= player_position.x
&& player_position.x <= node.x + range
@ -318,4 +310,33 @@ fn in_node_range(player_position: &Vec2, node: &Vec2, range: f32) -> bool {
fn get_squared_node_range(player_position: &Vec2, node: &Vec2) -> f32 {
(player_position.x - node.x).abs() + (player_position.y - node.y).abs()
}
}
fn snap_to_closest_node(player: &mut Player, grid: &Grid) {
let (current_connection_index_1, current_connection_index_2) =
grid.connections.get(player.connection).unwrap();
let mut nodes = HashSet::new();
nodes.insert(current_connection_index_1);
nodes.insert(current_connection_index_2);
for conn in &player.next_connections {
let (next_connection_index_1, next_connection_index_2) =
grid.connections.get(*conn).unwrap();
nodes.insert(next_connection_index_1);
nodes.insert(next_connection_index_2);
}
let mut closest_sqared_range = f32::MAX;
let mut closest_node = &vec2(0.0, 0.0);
for node_index in nodes {
let current_node = grid.nodes.get(*node_index).unwrap();
let current_squard_range = get_squared_node_range(&player.position, current_node);
if closest_sqared_range > current_squard_range {
closest_sqared_range = current_squard_range;
closest_node = current_node;
}
}
player.position = *closest_node;
}