WIP: Display Furniture Assets #8

Closed
msrd0 wants to merge 4 commits from furniture into room-creation
46 changed files with 91 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

BIN
assets/furniture/drawer.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

BIN
assets/furniture/dryer.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

BIN
assets/furniture/fridge.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 KiB

BIN
assets/furniture/kettle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

BIN
assets/furniture/oven.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

BIN
assets/furniture/sink.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

BIN
assets/furniture/stove.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

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 player;