Houses have energy that interact with ghosts (#18)
Some checks failed
Rust / rustfmt (push) Successful in 24s
Rust / clippy (push) Failing after 2m32s
Rust / build (push) Successful in 3m40s

Reviewed-on: #18
Co-authored-by: Dominic <git@msrd0.de>
Co-committed-by: Dominic <git@msrd0.de>
This commit is contained in:
Dominic 2024-07-07 15:18:32 +00:00 committed by msrd0.dev - Forgejo
parent 2354e34142
commit 0c9624d511
Signed by: msrd0.dev - Forgejo
GPG key ID: E2F16281CAA26E5F
5 changed files with 117 additions and 40 deletions

View file

@ -3,7 +3,7 @@ mod grid;
mod player;
mod room;
use comfy::{main_camera_mut, random_i32, vec2, EngineContext};
use comfy::{main_camera_mut, random_i32, vec2, EngineContext, RandomRange as _};
use grid::Grid;
use log::error;
use player::Player;
@ -18,7 +18,12 @@ pub struct HouseState {
rooms: Vec<Room>,
//grid: Grid,
player: Player,
human_layer: bool //Human, magnetic, electric
human_layer: bool, //Human, magnetic, electric
/// The energy level remaining in the house. Should decrease by itself, and much
/// faster when inhabited by the ghost.
pub charge: f32,
pub max_charge: f32
}
impl HouseState {
@ -31,12 +36,16 @@ impl HouseState {
}
let player = Player::new(rooms.first().unwrap());
let max_charge = f32::gen_range(2_000.0, 5_000.0);
HouseState {
current_room_id: 0,
room_count,
rooms,
player,
human_layer: false
human_layer: false,
// TODO this should be lower depending on the time elapsed
charge: max_charge,
max_charge
}
}
}

View file

@ -1,7 +1,8 @@
use super::{furniture::Furniture, grid::Grid};
use crate::game;
use comfy::{
draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext, HashSet, Index, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE
draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext,
HashSet, Index, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE
};
use indexmap::IndexSet;
@ -88,14 +89,17 @@ impl Room {
empty_spots.swap_remove_index(random_idx)
}
fn random_empty_spot_size(empty_spots: &mut IndexSet<u8>, size: u8) -> Option<u8> {
fn random_empty_spot_size(
empty_spots: &mut IndexSet<u8>,
size: u8
) -> Option<u8> {
let mut empty_spots_size = IndexSet::<u8>::new();
for &index in empty_spots.iter() {
let mut is_valid = true;
for offset in 0..size{
if !empty_spots.contains(&(index + offset)){
for offset in 0 .. size {
if !empty_spots.contains(&(index + offset)) {
is_valid = false;
break;
}
@ -109,7 +113,7 @@ impl Room {
return None;
}
let random_idx = usize::gen_range(0, empty_spots_size.len());
for offset in (0..size).rev() {
for offset in (0 .. size).rev() {
empty_spots.swap_remove_index(random_idx + offset as usize);
}
Some(random_idx as u8)
@ -293,10 +297,9 @@ impl Room {
pos: vec2(pos as f32, 0.0),
size: vec2(3.0, 1.0),
f: Furniture::new("bedroom", "couch", ctx),
z:0
z: 0
});
}
}
if let Some(pos) = random_empty_spot(&mut empty_spots) {
@ -304,7 +307,7 @@ impl Room {
pos: vec2(pos as f32, 0.0),
size: vec2(1.0, 2.0),
f: Furniture::new("bedroom", "bookshelf", ctx),
z:0
z: 0
});
}
@ -313,10 +316,9 @@ impl Room {
pos: vec2(pos as f32, 0.0),
size: vec2(0.5, 0.9),
f: Furniture::new("bedroom", "mini_ac", ctx),
z:0
z: 0
});
}
},
_ => {}

View file

@ -1,4 +1,8 @@
use crate::{activities::Activity, game::ZLayer, State};
use crate::{
activities::Activity,
game::{ZLayer, GHOST_DISCHARGE_RATE, GHOST_DISCHARGE_RATE_MOVEMENT},
State
};
use comfy::{
delta, draw_circle, draw_rect_outline, draw_sprite, error, info, is_key_down,
main_camera_mut, EngineContext, IVec2, KeyCode, Vec2, RED, WHITE
@ -177,9 +181,9 @@ pub fn update(state: &mut State, ctx: &mut EngineContext<'_>) {
// energie lost
{
let ghost = &mut state.ghost;
ghost.charge -= 70.0 * ctx.delta;
ghost.charge -= GHOST_DISCHARGE_RATE * ctx.delta;
if ghost.overworld_movement_pending != Vec2::ZERO {
ghost.charge -= 70.0 * ctx.delta;
ghost.charge -= GHOST_DISCHARGE_RATE_MOVEMENT * ctx.delta;
}
}