From 1ab85960fce77a24bd90a624e2e47ec9e02d11b3 Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sun, 7 Jul 2024 18:02:02 +0200 Subject: [PATCH 1/4] Added human-attack --- src/activities/house/mod.rs | 47 +++++++++++++++++++++++++++----- src/activities/house/player.rs | 49 +++++++++++++++++++++++++++++++--- src/activities/house/room.rs | 49 +++++++++++++++++++++++++++++----- 3 files changed, 128 insertions(+), 17 deletions(-) diff --git a/src/activities/house/mod.rs b/src/activities/house/mod.rs index a8f3184..d31e680 100644 --- a/src/activities/house/mod.rs +++ b/src/activities/house/mod.rs @@ -3,40 +3,60 @@ mod grid; mod player; mod room; -use comfy::{random_i32, EngineContext}; +use comfy::{delta, random_i32, vec2, EngineContext, RandomRange}; use grid::Grid; +use indexmap::IndexSet; use log::error; use player::Player; use room::Room; +use super::Activity; + const MAX_ROOMS: i32 = 6; #[derive(Debug)] pub struct HouseState { current_room_id: usize, room_count: usize, + human_count: usize, rooms: Vec, + human_rooms: Vec, //grid: Grid, player: Player, - human_layer: bool //Human, magnetic, electric + human_layer: bool, //Human, magnetic, electric + exit_time: f32, } impl HouseState { pub fn generate_new_house(ctx: &mut EngineContext<'_>) -> Self { let room_count = random_i32(2, MAX_ROOMS) as usize; + let human_count = random_i32(1, room_count as i32) as usize; + + let mut possible_rooms : IndexSet:: = (0..room_count).collect(); + let mut human_rooms = Vec::new(); + for _ in 0..human_count { + if !possible_rooms.is_empty() { + let random_idx = usize::gen_range(0, possible_rooms.len()); + + human_rooms.push(possible_rooms.swap_remove_index(random_idx).unwrap()); + } + } let mut rooms = Vec::new(); - for _ in 0 .. room_count { - rooms.push(Room::new(ctx)); + for i in 0 .. room_count { + rooms.push(Room::new(ctx, human_rooms.contains(&i))); } let player = Player::new(rooms.first().unwrap()); HouseState { current_room_id: 0, room_count, + human_count, rooms, + human_rooms, player, - human_layer: false + human_layer: false, + exit_time: 0.0, } } } @@ -64,7 +84,7 @@ pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) { pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) { let house = state.house_mut(ctx); let current_room = house.rooms.get(house.current_room_id).unwrap(); - house.player.update(¤t_room.grid); + house.player.update(¤t_room); if house.player.is_moving_to_right_room(current_room) { if house.current_room_id < (house.room_count - 1) { @@ -85,4 +105,19 @@ pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) { house.player.reset_on_room(current_room, true); } } + + if house.player.successfull_attack { + house.human_layer = true; + } + + if house.human_layer { + house.exit_time += delta(); + } + + if house.exit_time >= 2.0 { + state.ghost.overworld_pos -= vec2(0.0, 1.0); + state.activity = Activity::Overworld; + } + + } diff --git a/src/activities/house/player.rs b/src/activities/house/player.rs index d78cfbb..7272fe9 100644 --- a/src/activities/house/player.rs +++ b/src/activities/house/player.rs @@ -2,7 +2,8 @@ use super::{ room::{Room, SCALE}, Grid }; -use comfy::{delta, draw_circle, is_key_down, vec2, KeyCode, Vec2, RED}; +use comfy::{delta, draw_circle, is_key_down, is_key_pressed, main_camera_mut, vec2, KeyCode, Vec2, RED}; +use log::error; use std::collections::HashSet; #[derive(Debug)] @@ -13,7 +14,8 @@ pub struct Player { current_acceleration: f32, movement_time: f32, connection: usize, - next_connections: Vec + next_connections: Vec, + pub successfull_attack: bool } impl Player { @@ -28,7 +30,8 @@ impl Player { current_acceleration: 0.0, movement_time: 0.0, connection: 0, - next_connections: vec![1] + next_connections: vec![1], + successfull_attack: false } } @@ -36,7 +39,8 @@ impl Player { draw_circle(self.position, 0.5, RED, 0); } - pub fn update(&mut self, grid: &Grid) { + pub fn update(&mut self, room: &Room) { + let grid = &room.grid; let allowed_movement = get_allowed_movement(self, grid); move_player(self, allowed_movement); @@ -44,6 +48,8 @@ impl Player { if !on_current_connection(self, grid) && !update_connections(self, grid) { snap_to_closest_node(self, grid); } + + attack_human_routine(self, room); } pub fn is_moving_to_right_room(&self, room: &Room) -> bool { @@ -353,3 +359,38 @@ fn snap_to_closest_node(player: &mut Player, grid: &Grid) { player.position = *closest_node; } + +fn attack_human_routine(player: &mut Player, room: &Room) { + let range = 0.5; + + if is_key_pressed(KeyCode::Space) { + //In some node range? + let mut in_range = false; + for node in &room.grid.nodes { + if in_node_range(&player.position, node, range) { + in_range = true; + break; + } + } + + if in_range { + let (human_pos, human_nodes) = room.get_human_information(); + + let mut human_attacked = false; + + if human_pos.is_some() { + for node_index in human_nodes { + if in_node_range(&player.position, room.grid.nodes.get(*node_index).unwrap(), range) { + human_attacked = true; + } + } + } + + if human_attacked { + player.successfull_attack = true; + } else { + main_camera_mut().shake(1.0, 1.0); + } + } + } +} \ No newline at end of file diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs index d43b720..ddaf9a0 100644 --- a/src/activities/house/room.rs +++ b/src/activities/house/room.rs @@ -1,8 +1,7 @@ use super::{furniture::Furniture, grid::Grid}; -use crate::game; +use crate::game::{self, ZLayer}; use comfy::{ - draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext, - HashSet, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE + draw_circle, draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext, HashSet, RandomRange as _, Vec2, GOLD, GREEN, PURPLE, RED, WHITE }; use indexmap::IndexSet; @@ -30,7 +29,9 @@ pub struct Room { _room_type: RoomType, pub size: (u8, u8), //(width, height) pub grid: Grid, - furnitures: Vec + furnitures: Vec, + human_pos: Option, + human_nodes: Vec } impl RoomType { @@ -47,20 +48,41 @@ impl RoomType { } impl Room { - pub fn new(ctx: &mut EngineContext<'_>) -> Self { + pub fn new(ctx: &mut EngineContext<'_>, with_human: bool) -> Self { let room_type = RoomType::random(); let size = Self::random_size(&room_type); let furnitures = Self::random_room_furniture(&room_type, size, ctx); + let grid = Self::create_grid(size.0, size.1); + + let (human_pos, human_nodes) = if with_human { + let mut human_pos = vec2(random_i32(1, size.0 as i32) as f32 + 0.5, 1.0f32); + human_pos -= vec2(size.0 as f32 / 2.0, size.1 as f32 / 2.0); + human_pos *= SCALE; + + let node_index_left = grid.nodes.iter().enumerate().filter(|(_i, &node)| node.y <= size.1 as f32 / 6.0).filter(|(_i, &node)| node.x <= (human_pos.x - 0.5*SCALE)).max_by_key(|(_i, &node)| node.x as i32).map(|(i, _)| i).unwrap(); + let node_index_right = grid.nodes.iter().enumerate().filter(|(_i, &node)| node.y <= size.1 as f32 / 6.0).filter(|(_i, &node)| node.x >= (human_pos.x + 0.5*SCALE)).min_by_key(|(_i, &node)| node.x as i32).map(|(i, _)| i).unwrap(); + + (Some(human_pos), (node_index_left..=node_index_right).collect()) + } else { + (None, Vec::new()) + }; + Room { _room_type: room_type, size, - grid: Self::create_grid(size.0, size.1), - furnitures + grid, + furnitures, + human_pos, + human_nodes } } + pub fn get_human_information(&self) -> (Option, &Vec) { + (self.human_pos, &self.human_nodes) + } + fn random_size(room_type: &RoomType) -> (u8, u8) { //Kitchen + Living Room 5-8 //Bath + sleepingroom 4-6 @@ -376,6 +398,19 @@ impl Room { } } + if let Some(human_pos) = self.human_pos { + + draw_circle(human_pos, 0.5, RED, 20); + draw_rect(human_pos , vec2(1.0, 2.0) * SCALE, GOLD, ZLayer::Human.into()); + } + + + + for node_index in &self.human_nodes { + draw_circle(*self.grid.nodes.get(*node_index).unwrap(), 0.5, GREEN, ZLayer::Human.into()); + } + + self.grid.draw(); } } -- 2.45.2 From 703e0f28fbe7dec4e1d4a7dcfcfdc4757448aa2d Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sun, 7 Jul 2024 18:10:57 +0200 Subject: [PATCH 2/4] Added ghost sprite and score --- src/activities/house/mod.rs | 1 + src/activities/house/player.rs | 7 ++++--- src/activities/house/room.rs | 7 +++---- src/game.rs | 8 ++++++++ 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/activities/house/mod.rs b/src/activities/house/mod.rs index d31e680..ef5f25f 100644 --- a/src/activities/house/mod.rs +++ b/src/activities/house/mod.rs @@ -115,6 +115,7 @@ pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) { } if house.exit_time >= 2.0 { + state.score += 500.0; state.ghost.overworld_pos -= vec2(0.0, 1.0); state.activity = Activity::Overworld; } diff --git a/src/activities/house/player.rs b/src/activities/house/player.rs index 7272fe9..5542826 100644 --- a/src/activities/house/player.rs +++ b/src/activities/house/player.rs @@ -2,7 +2,7 @@ use super::{ room::{Room, SCALE}, Grid }; -use comfy::{delta, draw_circle, is_key_down, is_key_pressed, main_camera_mut, vec2, KeyCode, Vec2, RED}; +use comfy::{delta, draw_circle, draw_sprite, is_key_down, is_key_pressed, main_camera_mut, texture_id, vec2, KeyCode, Vec2, RED, WHITE}; use log::error; use std::collections::HashSet; @@ -36,7 +36,8 @@ impl Player { } pub fn draw(&self) { - draw_circle(self.position, 0.5, RED, 0); + //draw_circle(self.position, 0.5, RED, 0); + draw_sprite(texture_id("ghost_house"), self.position, WHITE, 5, vec2(1.25, 1.25)); } pub fn update(&mut self, room: &Room) { @@ -361,7 +362,7 @@ fn snap_to_closest_node(player: &mut Player, grid: &Grid) { } fn attack_human_routine(player: &mut Player, room: &Room) { - let range = 0.5; + let range = 1.0; if is_key_pressed(KeyCode::Space) { //In some node range? diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs index ddaf9a0..ff5d308 100644 --- a/src/activities/house/room.rs +++ b/src/activities/house/room.rs @@ -399,16 +399,15 @@ impl Room { } if let Some(human_pos) = self.human_pos { - - draw_circle(human_pos, 0.5, RED, 20); + //draw_circle(human_pos, 0.5, RED, 20); draw_rect(human_pos , vec2(1.0, 2.0) * SCALE, GOLD, ZLayer::Human.into()); } - + /* Debug draw for node_index in &self.human_nodes { draw_circle(*self.grid.nodes.get(*node_index).unwrap(), 0.5, GREEN, ZLayer::Human.into()); - } + }*/ self.grid.draw(); diff --git a/src/game.rs b/src/game.rs index 9cdbbe1..23b36ca 100644 --- a/src/game.rs +++ b/src/game.rs @@ -93,6 +93,14 @@ pub fn setup(state: &mut State, ctx: &mut EngineContext<'_>) { Assets::load(ctx); //house::setup(state, ctx); + + ctx.load_texture_from_bytes( + "ghost_house", + include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/assets/entities/ghost.png" + )) + ); } pub fn update(state: &mut State, engine: &mut EngineContext<'_>) { -- 2.45.2 From f7d21428fff6bddb591e0e24f16c892576fb8a50 Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sun, 7 Jul 2024 18:19:49 +0200 Subject: [PATCH 3/4] Added human texture to house --- src/activities/house/room.rs | 6 ++++-- src/game.rs | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs index fd14e5f..4962574 100644 --- a/src/activities/house/room.rs +++ b/src/activities/house/room.rs @@ -1,7 +1,7 @@ use super::{furniture::Furniture, grid::Grid}; use crate::game::{self, ZLayer}; use comfy::{ - draw_circle, draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext, HashSet, RandomRange as _, Vec2, GOLD, GREEN, PURPLE, RED, WHITE + draw_circle, draw_rect, draw_rect_outline, draw_sprite, error, random_i32, texture_id, vec2, EngineContext, HashSet, RandomRange as _, Vec2, GOLD, GREEN, PURPLE, RED, WHITE }; use indexmap::IndexSet; @@ -465,10 +465,12 @@ impl Room { } } + if human_layer { if let Some(human_pos) = self.human_pos { //draw_circle(human_pos, 0.5, RED, 20); - draw_rect(human_pos , vec2(1.0, 2.0) * SCALE, GOLD, ZLayer::Human.into()); + draw_sprite(texture_id("human_house"), human_pos, WHITE, ZLayer::Human.into(), vec2(1.0, 2.0) * SCALE); } + } /* Debug draw diff --git a/src/game.rs b/src/game.rs index 5674bcd..6352858 100644 --- a/src/game.rs +++ b/src/game.rs @@ -114,6 +114,14 @@ pub fn setup(state: &mut State, ctx: &mut EngineContext<'_>) { "/assets/entities/ghost.png" )) ); + + ctx.load_texture_from_bytes( + "human_house", + include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/assets/entities/human_captured.png" + )) + ); } /// The amount of energy a ghost consumes idle. -- 2.45.2 From a29677523b348fbc6f629476829e462ce9ee738b Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sun, 7 Jul 2024 18:20:45 +0200 Subject: [PATCH 4/4] rust fmt --- src/activities/house/mod.rs | 8 +++--- src/activities/house/player.rs | 23 ++++++++++++---- src/activities/house/room.rs | 49 +++++++++++++++++++++++++--------- 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/src/activities/house/mod.rs b/src/activities/house/mod.rs index edcd05a..326ea4e 100644 --- a/src/activities/house/mod.rs +++ b/src/activities/house/mod.rs @@ -37,9 +37,9 @@ impl HouseState { let room_count = random_i32(2, MAX_ROOMS) as usize; let human_count = random_i32(1, room_count as i32) as usize; - let mut possible_rooms : IndexSet:: = (0..room_count).collect(); + let mut possible_rooms: IndexSet = (0 .. room_count).collect(); let mut human_rooms = Vec::new(); - for _ in 0..human_count { + for _ in 0 .. human_count { if !possible_rooms.is_empty() { let random_idx = usize::gen_range(0, possible_rooms.len()); @@ -62,7 +62,7 @@ impl HouseState { human_rooms, player, human_layer: false, - exit_time: 0.0, + exit_time: 0.0, // TODO this should be lower depending on the time elapsed charge: max_charge, max_charge @@ -132,6 +132,4 @@ pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) { state.ghost.overworld_pos -= vec2(0.0, 1.0); state.activity = Activity::Overworld; } - - } diff --git a/src/activities/house/player.rs b/src/activities/house/player.rs index 5542826..d3200be 100644 --- a/src/activities/house/player.rs +++ b/src/activities/house/player.rs @@ -2,7 +2,10 @@ use super::{ room::{Room, SCALE}, Grid }; -use comfy::{delta, draw_circle, draw_sprite, is_key_down, is_key_pressed, main_camera_mut, texture_id, vec2, KeyCode, Vec2, RED, WHITE}; +use comfy::{ + delta, draw_circle, draw_sprite, is_key_down, is_key_pressed, main_camera_mut, + texture_id, vec2, KeyCode, Vec2, RED, WHITE +}; use log::error; use std::collections::HashSet; @@ -37,7 +40,13 @@ impl Player { pub fn draw(&self) { //draw_circle(self.position, 0.5, RED, 0); - draw_sprite(texture_id("ghost_house"), self.position, WHITE, 5, vec2(1.25, 1.25)); + draw_sprite( + texture_id("ghost_house"), + self.position, + WHITE, + 5, + vec2(1.25, 1.25) + ); } pub fn update(&mut self, room: &Room) { @@ -376,12 +385,16 @@ fn attack_human_routine(player: &mut Player, room: &Room) { if in_range { let (human_pos, human_nodes) = room.get_human_information(); - + let mut human_attacked = false; if human_pos.is_some() { for node_index in human_nodes { - if in_node_range(&player.position, room.grid.nodes.get(*node_index).unwrap(), range) { + if in_node_range( + &player.position, + room.grid.nodes.get(*node_index).unwrap(), + range + ) { human_attacked = true; } } @@ -394,4 +407,4 @@ fn attack_human_routine(player: &mut Player, room: &Room) { } } } -} \ No newline at end of file +} diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs index 4962574..aa3170f 100644 --- a/src/activities/house/room.rs +++ b/src/activities/house/room.rs @@ -1,7 +1,9 @@ use super::{furniture::Furniture, grid::Grid}; use crate::game::{self, ZLayer}; use comfy::{ - draw_circle, draw_rect, draw_rect_outline, draw_sprite, error, random_i32, texture_id, vec2, EngineContext, HashSet, RandomRange as _, Vec2, GOLD, GREEN, PURPLE, RED, WHITE + draw_circle, draw_rect, draw_rect_outline, draw_sprite, error, random_i32, + texture_id, vec2, EngineContext, HashSet, RandomRange as _, Vec2, GOLD, GREEN, + PURPLE, RED, WHITE }; use indexmap::IndexSet; @@ -61,10 +63,29 @@ impl Room { human_pos -= vec2(size.0 as f32 / 2.0, size.1 as f32 / 2.0); human_pos *= SCALE; - let node_index_left = grid.nodes.iter().enumerate().filter(|(_i, &node)| node.y <= size.1 as f32 / 6.0).filter(|(_i, &node)| node.x <= (human_pos.x - 0.5*SCALE)).max_by_key(|(_i, &node)| node.x as i32).map(|(i, _)| i).unwrap(); - let node_index_right = grid.nodes.iter().enumerate().filter(|(_i, &node)| node.y <= size.1 as f32 / 6.0).filter(|(_i, &node)| node.x >= (human_pos.x + 0.5*SCALE)).min_by_key(|(_i, &node)| node.x as i32).map(|(i, _)| i).unwrap(); - - (Some(human_pos), (node_index_left..=node_index_right).collect()) + let node_index_left = grid + .nodes + .iter() + .enumerate() + .filter(|(_i, &node)| node.y <= size.1 as f32 / 6.0) + .filter(|(_i, &node)| node.x <= (human_pos.x - 0.5 * SCALE)) + .max_by_key(|(_i, &node)| node.x as i32) + .map(|(i, _)| i) + .unwrap(); + let node_index_right = grid + .nodes + .iter() + .enumerate() + .filter(|(_i, &node)| node.y <= size.1 as f32 / 6.0) + .filter(|(_i, &node)| node.x >= (human_pos.x + 0.5 * SCALE)) + .min_by_key(|(_i, &node)| node.x as i32) + .map(|(i, _)| i) + .unwrap(); + + ( + Some(human_pos), + (node_index_left ..= node_index_right).collect() + ) } else { (None, Vec::new()) }; @@ -74,7 +95,7 @@ impl Room { size, grid, furnitures, - human_pos, + human_pos, human_nodes } } @@ -466,18 +487,22 @@ impl Room { } if human_layer { - if let Some(human_pos) = self.human_pos { - //draw_circle(human_pos, 0.5, RED, 20); - draw_sprite(texture_id("human_house"), human_pos, WHITE, ZLayer::Human.into(), vec2(1.0, 2.0) * SCALE); + if let Some(human_pos) = self.human_pos { + //draw_circle(human_pos, 0.5, RED, 20); + draw_sprite( + texture_id("human_house"), + human_pos, + WHITE, + ZLayer::Human.into(), + vec2(1.0, 2.0) * SCALE + ); + } } - } - /* Debug draw for node_index in &self.human_nodes { draw_circle(*self.grid.nodes.get(*node_index).unwrap(), 0.5, GREEN, ZLayer::Human.into()); }*/ - self.grid.draw(); } -- 2.45.2