WIP: Display Furniture Assets #8
BIN
assets/furniture/blender.png
Normal file
After Width: | Height: | Size: 6.4 KiB |
BIN
assets/furniture/cupboard.png
Normal file
After Width: | Height: | Size: 338 KiB |
BIN
assets/furniture/dishwasher.png
Normal file
After Width: | Height: | Size: 5.9 KiB |
BIN
assets/furniture/drawer.png
Normal file
After Width: | Height: | Size: 338 KiB |
BIN
assets/furniture/drawer_cupboard.png
Normal file
After Width: | Height: | Size: 6.3 KiB |
BIN
assets/furniture/dryer.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
assets/furniture/elec/blender.png
Normal file
After Width: | Height: | Size: 4.6 KiB |
BIN
assets/furniture/elec/dishwasher.png
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
assets/furniture/elec/dryer.png
Normal file
After Width: | Height: | Size: 7.4 KiB |
BIN
assets/furniture/elec/fridge.png
Normal file
After Width: | Height: | Size: 5.5 KiB |
BIN
assets/furniture/elec/kettle.png
Normal file
After Width: | Height: | Size: 4.7 KiB |
BIN
assets/furniture/elec/minifridge.png
Normal file
After Width: | Height: | Size: 5.1 KiB |
BIN
assets/furniture/elec/oven.png
Normal file
After Width: | Height: | Size: 5 KiB |
BIN
assets/furniture/elec/stove.png
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
assets/furniture/elec/toaster.png
Normal file
After Width: | Height: | Size: 126 KiB |
BIN
assets/furniture/elec/washing_machine.png
Normal file
After Width: | Height: | Size: 6.8 KiB |
BIN
assets/furniture/fridge.png
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
assets/furniture/hanging_cupboard.png
Normal file
After Width: | Height: | Size: 298 KiB |
BIN
assets/furniture/kettle.png
Normal file
After Width: | Height: | Size: 7.6 KiB |
BIN
assets/furniture/metal/blender.png
Normal file
After Width: | Height: | Size: 4.6 KiB |
BIN
assets/furniture/metal/cupboard.png
Normal file
After Width: | Height: | Size: 5.2 KiB |
BIN
assets/furniture/metal/dishwasher.png
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
assets/furniture/metal/drawer.png
Normal file
After Width: | Height: | Size: 338 KiB |
BIN
assets/furniture/metal/drawer_cupboard.png
Normal file
After Width: | Height: | Size: 5.8 KiB |
BIN
assets/furniture/metal/dryer.png
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
assets/furniture/metal/fridge.png
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
assets/furniture/metal/hanging_cupboard.png
Normal file
After Width: | Height: | Size: 298 KiB |
BIN
assets/furniture/metal/kettle.png
Normal file
After Width: | Height: | Size: 4.6 KiB |
BIN
assets/furniture/metal/minifridge.png
Normal file
After Width: | Height: | Size: 7 KiB |
BIN
assets/furniture/metal/oven.png
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
assets/furniture/metal/sink.png
Normal file
After Width: | Height: | Size: 5.4 KiB |
BIN
assets/furniture/metal/stove.png
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
assets/furniture/metal/toaster.png
Normal file
After Width: | Height: | Size: 126 KiB |
BIN
assets/furniture/metal/washing_machine.png
Normal file
After Width: | Height: | Size: 8.6 KiB |
BIN
assets/furniture/minifridge.png
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
assets/furniture/oven.png
Normal file
After Width: | Height: | Size: 9.6 KiB |
BIN
assets/furniture/sideboard_1.png
Normal file
After Width: | Height: | Size: 4.7 KiB |
BIN
assets/furniture/sideboard_2.png
Normal file
After Width: | Height: | Size: 4.9 KiB |
BIN
assets/furniture/sideboard_3.png
Normal file
After Width: | Height: | Size: 298 KiB |
BIN
assets/furniture/sideboard_3.png.png
Normal file
After Width: | Height: | Size: 4.9 KiB |
BIN
assets/furniture/sink.png
Normal file
After Width: | Height: | Size: 5.7 KiB |
BIN
assets/furniture/stove.png
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
assets/furniture/toaster.png
Normal file
After Width: | Height: | Size: 126 KiB |
BIN
assets/furniture/washing_machine.png
Normal file
After Width: | Height: | Size: 16 KiB |
90
src/activities/house/furniture.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod furniture;
|
||||||
mod grid;
|
mod grid;
|
||||||
mod player;
|
mod player;
|
||||||
|
|
||||||
|
|