WIP: Display Furniture Assets #8

Closed
msrd0 wants to merge 4 commits from furniture into room-creation
3 changed files with 91 additions and 0 deletions
Showing only changes of commit 2b8b5d1816 - Show all commits

View file

View file

@ -0,0 +1,90 @@
use comfy::{error, texture_id, EngineContext, HashSet, Lazy, Mutex, TextureHandle};
use std::{fs, io, sync::Arc};
static ASSETS_LOADED: Lazy<Arc<Mutex<HashSet<String>>>> =
Lazy::new(|| Arc::new(Mutex::new(HashSet::new())));
struct FurnitureAsset {
folder: String,
name: String
}
struct FurnitureTextureHandles {
human: Option<TextureHandle>,
magnet: Option<TextureHandle>,
elec: Option<TextureHandle>
}
impl FurnitureAsset {
fn asset_path(&self) -> String {
format!("{}/{}.png", self.folder, self.name)
}
fn asset_path_magnet(&self) -> String {
format!("{}/magnet/{}.png", self.folder, self.name)
}
fn asset_path_elec(&self) -> String {
format!("{}/elec/{}.png", self.folder, self.name)
}
fn load_asset_path(
&self,
path: String,
ctx: &mut EngineContext<'_>
) -> Option<TextureHandle> {
let mut loaded = ASSETS_LOADED.lock();
if loaded.contains(&path) {
return None;
}
let bytes = match fs::read(format!(
"{}/assets/furniture/{path}",
env!("CARGO_MANIFEST_DIR")
)) {
Ok(bytes) => bytes,
Err(err) if err.kind() == io::ErrorKind::NotFound => return None,
Err(err) => {
error!("Failed to load asset {path:?}: {err}");
return None;
}
};
ctx.load_texture_from_bytes(&path, &bytes);
let handle = texture_id(&path);
loaded.insert(path);
Some(handle)
}
/// Attempt to load the assets. Silently ignore missing assets.
fn load_assets(&self, ctx: &mut EngineContext<'_>) -> FurnitureTextureHandles {
FurnitureTextureHandles {
human: self.load_asset_path(self.asset_path(), ctx),
magnet: self.load_asset_path(self.asset_path_magnet(), ctx),
elec: self.load_asset_path(self.asset_path_elec(), ctx)
}
}
}
pub struct Furniture {
asset: FurnitureAsset,
handles: FurnitureTextureHandles,
on: Box<dyn Fn() -> bool>
}
impl Furniture {
pub fn new<F: Into<String>, N: Into<String>>(
folder: F,
name: N,
ctx: &mut EngineContext<'_>
) -> Self {
let asset = FurnitureAsset {
folder: folder.into(),
name: name.into()
};
let handles = asset.load_assets(ctx);
Self {
asset,
handles,
on: Box::new(|| false)
}
}
}

View file

@ -1,3 +1,4 @@
mod furniture;
mod grid; mod grid;
mod player; mod player;