Merge remote-tracking branch 'origin/main' into attack-human

This commit is contained in:
Glaeder 2024-07-07 18:16:44 +02:00
commit cc39434310
11 changed files with 242 additions and 46 deletions

View file

@ -1,5 +1,5 @@
use comfy::{error, texture_id, EngineContext, HashSet, Lazy, Mutex, TextureHandle};
use std::{fmt::Debug, fs, io, sync::Arc};
use std::{fmt::Debug, fs, io, path::PathBuf, sync::Arc};
static ASSETS_LOADED: Lazy<Arc<Mutex<HashSet<String>>>> =
Lazy::new(|| Arc::new(Mutex::new(HashSet::new())));
@ -39,10 +39,13 @@ impl FurnitureAsset {
if loaded.contains(&path) {
return Some(texture_id(&path));
}
let bytes = match fs::read(format!(
"{}/assets/furniture/{path}",
env!("CARGO_MANIFEST_DIR")
)) {
let mut asset_path = PathBuf::from(format!("assets/furniture/{path}"));
if !asset_path.exists() {
asset_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(asset_path);
}
let bytes = match fs::read(asset_path) {
Ok(bytes) => bytes,
Err(err) if err.kind() == io::ErrorKind::NotFound => return None,
Err(err) => {

View file

@ -3,7 +3,7 @@ mod grid;
mod player;
mod room;
use comfy::{delta, random_i32, vec2, EngineContext, RandomRange};
use comfy::{delta, main_camera_mut, random_i32, vec2, EngineContext, RandomRange as _};
use grid::Grid;
use indexmap::IndexSet;
use log::error;
@ -25,6 +25,11 @@ pub struct HouseState {
player: Player,
human_layer: bool, //Human, magnetic, electric
exit_time: f32,
/// 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 {
@ -48,6 +53,7 @@ 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,
@ -57,6 +63,9 @@ impl HouseState {
player,
human_layer: false,
exit_time: 0.0,
// TODO this should be lower depending on the time elapsed
charge: max_charge,
max_charge
}
}
}
@ -82,6 +91,10 @@ pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) {
}
pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) {
let mut camera = main_camera_mut();
camera.center = vec2(0.0, 0.0);
drop(camera);
let house = state.house_mut(ctx);
let current_room = house.rooms.get(house.current_room_id).unwrap();
house.player.update(&current_room);

View file

@ -110,6 +110,37 @@ impl Room {
let random_idx = usize::gen_range(0, empty_spots.len());
empty_spots.swap_remove_index(random_idx)
}
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)) {
is_valid = false;
break;
}
}
if is_valid {
empty_spots_size.insert(index);
}
}
if empty_spots_size.is_empty() {
return None;
}
let random_idx = usize::gen_range(0, empty_spots_size.len());
for offset in (0 .. size).rev() {
empty_spots.swap_remove_index(random_idx + offset as usize);
}
Some(random_idx as u8)
}
fn random_appliance<T>(empty_spots: &mut Vec<T>) -> Option<T> {
if empty_spots.is_empty() {
return None;
@ -276,6 +307,42 @@ impl Room {
}
},
RoomType::LivingRoom => {
let has_couch = match u8::gen_range(0, 2) {
0 => false,
1 => true,
_ => unreachable!()
};
if has_couch {
if let Some(pos) = random_empty_spot_size(&mut empty_spots, 3) {
furnitures.push(Tile {
pos: vec2(pos as f32, 0.0),
size: vec2(3.0, 1.0),
f: Furniture::new("bedroom", "couch", ctx),
z: 0
});
}
}
if let Some(pos) = random_empty_spot(&mut empty_spots) {
furnitures.push(Tile {
pos: vec2(pos as f32, 0.0),
size: vec2(1.0, 2.0),
f: Furniture::new("bedroom", "bookshelf", ctx),
z: 0
});
}
if let Some(pos) = random_empty_spot(&mut empty_spots) {
furnitures.push(Tile {
pos: vec2(pos as f32, 0.0),
size: vec2(0.5, 0.9),
f: Furniture::new("bedroom", "mini_ac", ctx),
z: 0
});
}
},
_ => {}
}

View file

@ -1,14 +1,28 @@
use crate::{activities::Activity, game::ZLayer, State};
use crate::{
activities::Activity,
game::{ZLayer, GHOST_DISCHARGE_RATE, GHOST_DISCHARGE_RATE_MOVEMENT},
State
};
use comfy::{
draw_circle, draw_rect_outline, draw_sprite, error, info, is_key_down,
main_camera_mut, EngineContext, IVec2, KeyCode, Vec2, RED, WHITE
draw_rect_outline, draw_sprite, error, info, is_key_down, main_camera_mut,
texture_id, vec2, EngineContext, IVec2, KeyCode, Vec2, RED, WHITE
};
use std::time::Instant;
use worldgen::MovementCost;
pub mod worldgen;
pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) {
pub fn setup(_state: &State, ctx: &EngineContext<'_>) {
ctx.load_texture_from_bytes(
"human_overworld",
include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/assets/entities/human_overworld.png"
))
);
}
pub fn draw(state: &State, _ctx: &EngineContext<'_>) {
for (coords, tile) in state.overworld.iter_tiles() {
for (i, texture) in tile.textures().iter().rev().enumerate() {
let i = i as i32;
@ -24,10 +38,19 @@ pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) {
}
}
}
draw_circle(state.ghost.overworld_pos, 0.5, RED, ZLayer::Ghost.into());
let mut ghost_pos = state.ghost.overworld_pos;
ghost_pos.y += 0.5;
draw_sprite(
texture_id("human_overworld"),
ghost_pos,
WHITE,
ZLayer::Ghost.into(),
vec2(1.0, 1.25)
);
}
fn update_move_player(state: &mut State, ctx: &mut EngineContext<'_>) {
fn update_move_player(state: &mut State, _ctx: &mut EngineContext<'_>) {
let now = Instant::now();
// Are there any pending position updates? If so, we ignore all user input and execute
@ -174,6 +197,15 @@ pub fn update(state: &mut State, ctx: &mut EngineContext<'_>) {
}
}
// energie lost
{
let ghost = &mut state.ghost;
ghost.charge -= GHOST_DISCHARGE_RATE * ctx.delta;
if ghost.overworld_movement_pending != Vec2::ZERO {
ghost.charge -= GHOST_DISCHARGE_RATE_MOVEMENT * ctx.delta;
}
}
// generate more chunks if needed
{
let half_viewport = (camera.world_viewport() * 0.5 + 3.0).as_ivec2();