Skip to main content

ursus_materials/
material.rs

1use crate::value::{MaterialValue, PropertyId};
2use std::collections::HashMap;
3
4/// Stable handle identifying a material instance. Lives here rather than in
5/// ursus-core so ursus-materials stays the single source of truth for
6/// material identity - ursus-core's ECS just stores this as a component.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub struct MaterialHandle(pub u32);
9
10#[derive(Debug, Clone)]
11pub struct Material {
12    pub name: String,
13    pub strategy: &'static str,
14    properties: HashMap<PropertyId, MaterialValue>,
15}
16
17impl Material {
18    pub fn new(name: impl Into<String>, strategy: &'static str) -> Self {
19        Self { name: name.into(), strategy, properties: HashMap::new() }
20    }
21
22    pub fn with(mut self, id: PropertyId, value: MaterialValue) -> Self {
23        self.properties.insert(id, value);
24        self
25    }
26
27    pub fn set(&mut self, id: PropertyId, value: MaterialValue) {
28        self.properties.insert(id, value);
29    }
30
31    pub fn get(&self, id: PropertyId) -> Option<&MaterialValue> {
32        self.properties.get(&id)
33    }
34
35    pub fn contains(&self, id: PropertyId) -> bool {
36        self.properties.contains_key(&id)
37    }
38
39    pub fn iter(&self) -> impl Iterator<Item = (&PropertyId, &MaterialValue)> {
40        self.properties.iter()
41    }
42}