From 3904a7c8243d697d4322024904d31ce52fb2dff9 Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sat, 6 Jul 2024 15:24:06 +0200 Subject: [PATCH 1/6] Added gitignore and WIP Grid and player --- .gitignore | 2 + src/activities/house.rs | 96 +++++++++++++++++++++++++++++++++++++++-- src/activities/mod.rs | 5 ++- src/game.rs | 6 +-- src/main.rs | 14 ++---- 5 files changed, 105 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..5b325cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target +shell.nix +.vscode/ \ No newline at end of file diff --git a/src/activities/house.rs b/src/activities/house.rs index 8043a2d..849cafd 100644 --- a/src/activities/house.rs +++ b/src/activities/house.rs @@ -1,7 +1,97 @@ use comfy::*; -pub fn draw(_state: &crate::State, _engine: &comfy::EngineContext) { - draw_circle(vec2(0.0, 0.0), 0.5, RED, 0); +#[derive(Debug, Default)] +pub struct HouseState { + grid: Grid, + player: Player } -pub fn update(_state: &mut crate::State, _engine: &mut comfy::EngineContext) {} +#[derive(Debug)] +struct Grid { + nodes: Vec, + connections: Vec<(usize, usize)>, +} + +#[derive(Debug, Default)] +struct Player { + position: Vec2 +} + +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(state: &crate::State, _engine: &comfy::EngineContext) { + //Draw Grid + for pos in &state.house.grid.nodes { + draw_circle(*pos, 0.25, BLUE, 0); + } + for (conn_i1, conn_i2) in &state.house.grid.connections { + let p1 = state.house.grid.nodes.get(*conn_i1); + let p2 = state.house.grid.nodes.get(*conn_i2); + + if p1.is_none() || p2.is_none() { + error!("Connection in grid not available {:?}", (conn_i1, conn_i2)); + continue; + } + + draw_line(*p1.unwrap(), *p2.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) { + if is_key_down(KeyCode::Up) { + error!("KEY UPPPPPPPPP"); + state.house.player.position += vec2(0.0, 1.0) * delta(); + } + + if is_key_down(KeyCode::Down) { + state.house.player.position += vec2(0.0, -1.0) * delta(); + } + + if is_key_down(KeyCode::Right) { + state.house.player.position += vec2(1.0, 0.0) * delta(); + } + + if is_key_down(KeyCode::Left) { + state.house.player.position += vec2(-1.0, 0.0) * delta(); + } +} diff --git a/src/activities/mod.rs b/src/activities/mod.rs index 5d0b11f..906af6e 100644 --- a/src/activities/mod.rs +++ b/src/activities/mod.rs @@ -1,8 +1,9 @@ pub mod house; pub mod overworld; -#[derive(Debug)] +#[derive(Debug, Default)] pub enum Activity { + #[default] House, - Overworld + Overworld, } diff --git a/src/game.rs b/src/game.rs index a91fe39..01e7f4d 100644 --- a/src/game.rs +++ b/src/game.rs @@ -2,19 +2,19 @@ use comfy::*; use crate::{ activities::{house, overworld, Activity}, - State + State, }; pub fn update(state: &mut State, engine: &mut EngineContext) { match state.activity { Activity::House => house::update(state, engine), - Activity::Overworld => overworld::update(state, engine) + Activity::Overworld => overworld::update(state, engine), } } pub fn draw(state: &State, engine: &EngineContext) { match state.activity { Activity::House => house::draw(state, engine), - Activity::Overworld => overworld::draw(state, engine) + Activity::Overworld => overworld::draw(state, engine), } } diff --git a/src/main.rs b/src/main.rs index 4992bdf..23315bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,16 @@ mod activities; mod game; +use activities::house::HouseState; use activities::Activity; use comfy::*; const GAME_NAME: &str = "Powercreep"; -#[derive(Debug)] +#[derive(Debug, Default)] struct State { - activity: Activity -} - -impl Default for State { - fn default() -> Self { - Self { - activity: Activity::House - } - } + activity: Activity, + house: HouseState, } impl GameLoop for State { -- 2.45.2 From b6546dc145c44c6e8cae742858b651b64cc9bf74 Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sat, 6 Jul 2024 15:47:49 +0200 Subject: [PATCH 2/6] Patched comfy due to keyboard issues --- Cargo.lock | 9 +++------ Cargo.toml | 5 +++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 431f36e..51a9a0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -478,8 +478,7 @@ dependencies = [ [[package]] name = "comfy" version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e8ff7c66a696e9a9e5523d0c4a6306711acb7f6d490d9c412b044a8c95f17f4" +source = "git+https://github.com/Plonq/comfy/?branch=101-fix-keyboard-events#6293590aec51875e9832ef05a670149d371f849c" dependencies = [ "comfy-core", "comfy-wgpu", @@ -499,8 +498,7 @@ dependencies = [ [[package]] name = "comfy-core" version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "398260ce649e9d35adc3ae75565406aab9b360c1e892a48ae3606e3138df150b" +source = "git+https://github.com/Plonq/comfy/?branch=101-fix-keyboard-events#6293590aec51875e9832ef05a670149d371f849c" dependencies = [ "ahash", "anyhow", @@ -558,8 +556,7 @@ dependencies = [ [[package]] name = "comfy-wgpu" version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6118f09fd99577503ba6861dac53840bae6cf202d70f97042f5cdf43c222e39a" +source = "git+https://github.com/Plonq/comfy/?branch=101-fix-keyboard-events#6293590aec51875e9832ef05a670149d371f849c" dependencies = [ "bytemuck", "comfy-core", diff --git a/Cargo.toml b/Cargo.toml index 9585c1b..9ec0f12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,8 @@ opt-level = 3 [dependencies] comfy = { version = "0.4.0", features = ["wayland"] } log = "0.4.22" + +[patch.crates-io] +# https://github.com/darthdeus/comfy/issues/101 +# https://github.com/darthdeus/comfy/pull/102 +comfy = { git = "https://github.com/Plonq/comfy/", branch = "101-fix-keyboard-events" } -- 2.45.2 From 13018c034e016daad8f437d4ec8e2e608f009189 Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sat, 6 Jul 2024 18:29:40 +0200 Subject: [PATCH 3/6] Finsihed cable movement --- src/activities/house.rs | 278 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 251 insertions(+), 27 deletions(-) diff --git a/src/activities/house.rs b/src/activities/house.rs index 849cafd..785d94e 100644 --- a/src/activities/house.rs +++ b/src/activities/house.rs @@ -3,7 +3,7 @@ use comfy::*; #[derive(Debug, Default)] pub struct HouseState { grid: Grid, - player: Player + player: Player, } #[derive(Debug)] @@ -12,9 +12,12 @@ struct Grid { connections: Vec<(usize, usize)>, } -#[derive(Debug, Default)] +#[derive(Debug)] struct Player { - position: Vec2 + position: Vec2, + speed: f32, + connection: usize, + next_connections: Vec, } impl Default for Grid { @@ -43,34 +46,50 @@ impl 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(); + 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 { + position: Default::default(), + speed: 10.0, + connection: 0, + next_connections: vec![1, 2, 3], + } + } +} + pub fn draw(state: &crate::State, _engine: &comfy::EngineContext) { //Draw Grid - for pos in &state.house.grid.nodes { - draw_circle(*pos, 0.25, BLUE, 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 { - let p1 = state.house.grid.nodes.get(*conn_i1); - let p2 = state.house.grid.nodes.get(*conn_i2); + let node_1 = state.house.grid.nodes.get(*conn_i1); + let node_2 = state.house.grid.nodes.get(*conn_i2); - if p1.is_none() || p2.is_none() { + if node_1.is_none() || node_2.is_none() { error!("Connection in grid not available {:?}", (conn_i1, conn_i2)); continue; } - - draw_line(*p1.unwrap(), *p2.unwrap(), 0.1, BLUE, 0); + + draw_line(*node_1.unwrap(), *node_2.unwrap(), 0.1, BLUE, 0); } //Draw Player @@ -78,20 +97,225 @@ pub fn draw(state: &crate::State, _engine: &comfy::EngineContext) { } pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext) { - if is_key_down(KeyCode::Up) { - error!("KEY UPPPPPPPPP"); - state.house.player.position += vec2(0.0, 1.0) * delta(); + 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 range = 0.25; + let ( + allow_up_movement, + allow_down_movement, + allow_left_movement, + allow_right_movement, + ) = if in_node_range(&player.position, node_1, range) + || in_node_range(&player.position, node_2, range) + { + (true, true, true, true) + } else { + let ( + mut allow_up_movement, + mut allow_down_movement, + mut allow_left_movement, + mut allow_right_movement, + ) = get_allowed_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(); + let next_node_1 = grid.nodes.get(*next_conn_i1).unwrap(); + let next_node_2 = grid.nodes.get(*next_conn_i2).unwrap(); + + let ( + next_allow_up_movement, + next_allow_down_movement, + next_allow_left_movement, + next_allow_right_movement, + ) = get_allowed_movement(&player.position, next_node_1, next_node_2); + + ( + allow_up_movement, + allow_down_movement, + allow_left_movement, + allow_right_movement, + ) = ( + allow_up_movement || next_allow_up_movement, + allow_down_movement || next_allow_down_movement, + allow_left_movement || next_allow_left_movement, + allow_right_movement || next_allow_right_movement, + ); + } + ( + allow_up_movement, + allow_down_movement, + allow_left_movement, + allow_right_movement, + ) + }; + + if allow_up_movement && is_key_down(KeyCode::Up) { + player.position += vec2(0.0, player.speed) * delta(); } - if is_key_down(KeyCode::Down) { - state.house.player.position += vec2(0.0, -1.0) * delta(); + if allow_down_movement && is_key_down(KeyCode::Down) { + player.position += vec2(0.0, -player.speed) * delta(); } - if is_key_down(KeyCode::Right) { - state.house.player.position += vec2(1.0, 0.0) * delta(); + if allow_right_movement && is_key_down(KeyCode::Right) { + player.position += vec2(player.speed, 0.0) * delta(); } - if is_key_down(KeyCode::Left) { - state.house.player.position += vec2(-1.0, 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( + player_position: &Vec2, + node_1: &Vec2, + node_2: &Vec2, +) -> (bool, bool, bool, bool) { + let allow_left_movement = { + if node_1.x <= node_2.x { + node_1.x < player_position.x + && player_position.x <= node_2.x + && player_position.y == node_1.y + } else { + node_2.x < player_position.x + && player_position.x <= node_1.x + && player_position.y == node_1.y + } + }; + + let allow_right_movement = { + if node_1.x <= node_2.x { + node_1.x <= player_position.x + && player_position.x < node_2.x + && player_position.y == node_1.y + } else { + node_2.x <= player_position.x + && player_position.x < node_1.x + && player_position.y == node_1.y + } + }; + + let allow_up_movement = { + if node_1.y <= node_2.y { + node_1.y <= player_position.y + && player_position.y < node_2.y + && player_position.x == node_1.x + } else { + node_2.y <= player_position.y + && player_position.y < node_1.y + && player_position.x == node_1.x + } + }; + + let allow_down_movement = { + if node_1.y <= node_2.y { + node_1.y < player_position.y + && player_position.y <= node_2.y + && player_position.x == node_1.x + } else { + node_2.y < player_position.y + && player_position.y <= node_1.y + && player_position.x == node_1.x + } + }; + + ( + allow_up_movement, + allow_down_movement, + allow_left_movement, + allow_right_movement, + ) +} + +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 + } else { + node_2.x <= player_position.x && player_position.x <= node_1.x + }; + + let on_y = if node_1.y <= node_2.y { + node_1.y <= player_position.y && player_position.y <= node_2.y + } else { + node_2.y <= player_position.y && player_position.y <= node_1.y + }; + + on_x && on_y +} + +fn in_node_range(player_position: &Vec2, node: &Vec2, range: f32) -> bool { + node.x - range <= player_position.x + && player_position.x <= node.x + range + && node.y - range <= player_position.y + && player_position.y <= node.y + range +} + +fn get_squared_node_range(player_position: &Vec2, node: &Vec2) -> f32 { + (player_position.x - node.x).abs() + (player_position.y - node.y).abs() +} \ No newline at end of file -- 2.45.2 From 400ea91352aeee89b59c5846ae923f10dea9d0e5 Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sat, 6 Jul 2024 18:55:44 +0200 Subject: [PATCH 4/6] Refactored into multiple functions --- src/activities/house.rs | 191 ++++++++++++++++++++++------------------ 1 file changed, 106 insertions(+), 85 deletions(-) diff --git a/src/activities/house.rs b/src/activities/house.rs index 785d94e..8b6d612 100644 --- a/src/activities/house.rs +++ b/src/activities/house.rs @@ -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() -} \ No newline at end of file +} + +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; +} -- 2.45.2 From ee49fb493f2bcfe9d697f3fcf47bc6ccd9139bae Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sat, 6 Jul 2024 19:13:35 +0200 Subject: [PATCH 5/6] Refactored into multiple files --- src/activities/house/grid.rs | 69 ++++++++++++++ src/activities/house/mod.rs | 23 +++++ src/activities/{house.rs => house/player.rs} | 96 +++----------------- 3 files changed, 103 insertions(+), 85 deletions(-) create mode 100644 src/activities/house/grid.rs create mode 100644 src/activities/house/mod.rs rename src/activities/{house.rs => house/player.rs} (77%) diff --git a/src/activities/house/grid.rs b/src/activities/house/grid.rs new file mode 100644 index 0000000..5c9cee7 --- /dev/null +++ b/src/activities/house/grid.rs @@ -0,0 +1,69 @@ +use comfy::*; + +#[derive(Debug)] +pub struct Grid { + pub nodes: Vec, + 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); + } + } +} diff --git a/src/activities/house/mod.rs b/src/activities/house/mod.rs new file mode 100644 index 0000000..13ac5f0 --- /dev/null +++ b/src/activities/house/mod.rs @@ -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); +} diff --git a/src/activities/house.rs b/src/activities/house/player.rs similarity index 77% rename from src/activities/house.rs rename to src/activities/house/player.rs index 8b6d612..f391c82 100644 --- a/src/activities/house.rs +++ b/src/activities/house/player.rs @@ -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, - connections: Vec<(usize, usize)>, -} - -#[derive(Debug)] -struct Player { +pub struct Player { position: Vec2, speed: f32, connection: usize, next_connections: Vec, } -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); } } -- 2.45.2 From 901d457f815ac9022b1a47dc17308511a07d9f98 Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sat, 6 Jul 2024 19:56:33 +0200 Subject: [PATCH 6/6] Formatted and fixed clippy with nightly --- .gitignore | 2 +- src/activities/house/grid.rs | 4 ++-- src/activities/house/mod.rs | 2 +- src/activities/house/player.rs | 20 ++++++++++---------- src/activities/mod.rs | 3 ++- src/game.rs | 6 +++--- src/main.rs | 5 ++--- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 5b325cd..0b4fc31 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /target -shell.nix +*.nix .vscode/ \ No newline at end of file diff --git a/src/activities/house/grid.rs b/src/activities/house/grid.rs index 5c9cee7..114eace 100644 --- a/src/activities/house/grid.rs +++ b/src/activities/house/grid.rs @@ -3,7 +3,7 @@ use comfy::*; #[derive(Debug)] pub struct Grid { pub nodes: Vec, - pub connections: Vec<(usize, usize)>, + pub connections: Vec<(usize, usize)> } impl Default for Grid { @@ -22,7 +22,7 @@ impl Grid { 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)], + connections: vec![(0, 1), (1, 2), (1, 3), (0, 4), (0, 5), (5, 1), (6, 7)] }; grid.sanitize(); diff --git a/src/activities/house/mod.rs b/src/activities/house/mod.rs index 13ac5f0..643e666 100644 --- a/src/activities/house/mod.rs +++ b/src/activities/house/mod.rs @@ -7,7 +7,7 @@ use player::Player; #[derive(Debug, Default)] pub struct HouseState { grid: Grid, - player: Player, + player: Player } pub fn draw(state: &crate::State, _engine: &comfy::EngineContext) { diff --git a/src/activities/house/player.rs b/src/activities/house/player.rs index f391c82..eb60107 100644 --- a/src/activities/house/player.rs +++ b/src/activities/house/player.rs @@ -7,7 +7,7 @@ pub struct Player { position: Vec2, speed: f32, connection: usize, - next_connections: Vec, + next_connections: Vec } impl Default for Player { @@ -16,7 +16,7 @@ impl Default for Player { position: Default::default(), speed: 10.0, connection: 0, - next_connections: vec![1, 2, 3], + next_connections: vec![1, 2, 3] } } } @@ -40,7 +40,7 @@ fn move_player(player: &mut Player, allowed_movement: (bool, bool, bool, bool)) allow_up_movement, allow_down_movement, allow_left_movement, - allow_right_movement, + allow_right_movement ) = allowed_movement; if allow_up_movement && is_key_down(KeyCode::Up) { @@ -77,7 +77,7 @@ fn get_allowed_movement(player: &Player, grid: &Grid) -> (bool, bool, bool, bool mut allow_up_movement, mut allow_down_movement, mut allow_left_movement, - mut allow_right_movement, + mut allow_right_movement ) = get_allowed_connection_movement(&player.position, node_1, node_2); for conn in &player.next_connections { @@ -89,26 +89,26 @@ fn get_allowed_movement(player: &Player, grid: &Grid) -> (bool, bool, bool, bool next_allow_up_movement, next_allow_down_movement, next_allow_left_movement, - next_allow_right_movement, + next_allow_right_movement ) = get_allowed_connection_movement(&player.position, next_node_1, next_node_2); ( allow_up_movement, allow_down_movement, allow_left_movement, - allow_right_movement, + allow_right_movement ) = ( allow_up_movement || next_allow_up_movement, allow_down_movement || next_allow_down_movement, allow_left_movement || next_allow_left_movement, - allow_right_movement || next_allow_right_movement, + allow_right_movement || next_allow_right_movement ); } ( allow_up_movement, allow_down_movement, allow_left_movement, - allow_right_movement, + allow_right_movement ) } } @@ -117,7 +117,7 @@ fn get_allowed_movement(player: &Player, grid: &Grid) -> (bool, bool, bool, bool fn get_allowed_connection_movement( player_position: &Vec2, node_1: &Vec2, - node_2: &Vec2, + node_2: &Vec2 ) -> (bool, bool, bool, bool) { let allow_left_movement = { if node_1.x <= node_2.x { @@ -171,7 +171,7 @@ fn get_allowed_connection_movement( allow_up_movement, allow_down_movement, allow_left_movement, - allow_right_movement, + allow_right_movement ) } diff --git a/src/activities/mod.rs b/src/activities/mod.rs index 906af6e..dfa2b9e 100644 --- a/src/activities/mod.rs +++ b/src/activities/mod.rs @@ -5,5 +5,6 @@ pub mod overworld; pub enum Activity { #[default] House, - Overworld, + #[allow(dead_code)] + Overworld } diff --git a/src/game.rs b/src/game.rs index 01e7f4d..a91fe39 100644 --- a/src/game.rs +++ b/src/game.rs @@ -2,19 +2,19 @@ use comfy::*; use crate::{ activities::{house, overworld, Activity}, - State, + State }; pub fn update(state: &mut State, engine: &mut EngineContext) { match state.activity { Activity::House => house::update(state, engine), - Activity::Overworld => overworld::update(state, engine), + Activity::Overworld => overworld::update(state, engine) } } pub fn draw(state: &State, engine: &EngineContext) { match state.activity { Activity::House => house::draw(state, engine), - Activity::Overworld => overworld::draw(state, engine), + Activity::Overworld => overworld::draw(state, engine) } } diff --git a/src/main.rs b/src/main.rs index 23315bd..863ddb3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,7 @@ mod activities; mod game; -use activities::house::HouseState; -use activities::Activity; +use activities::{house::HouseState, Activity}; use comfy::*; const GAME_NAME: &str = "Powercreep"; @@ -10,7 +9,7 @@ const GAME_NAME: &str = "Powercreep"; #[derive(Debug, Default)] struct State { activity: Activity, - house: HouseState, + house: HouseState } impl GameLoop for State { -- 2.45.2