forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractable.rs
More file actions
62 lines (52 loc) · 1.61 KB
/
interactable.rs
File metadata and controls
62 lines (52 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::collections::HashMap;
use blocks::BlockKind;
use crate::Game;
#[derive(Default)]
pub struct InteractableRegistry {
registry: HashMap<BlockKind, usize>,
}
impl InteractableRegistry {
/// Creates a new, empty [`InteractableRegistry`]
pub fn new() -> Self {
Self {
registry: HashMap::new(),
}
}
/// Registers that there is a handler that handles interactions
/// with the [`BlockKind`].
pub fn register(&mut self, block: BlockKind) {
let value = self.registry.get(&block).copied();
match value {
Some(count) => {
self.registry.insert(block, count + 1);
}
None => {
self.registry.insert(block, 1);
}
}
}
/// Deregisters a handler for a block interaction.
pub fn deregister(&mut self, block: BlockKind) {
let value = self.registry.get(&block).copied();
match value {
Some(count) => {
if count == 0 {
panic!(
"Tried to deregister an interaction handler on a block with 0 handlers."
);
} else {
self.registry.insert(block, count - 1);
}
}
None => {
panic!("Tried to deregister an interaction handler on a block with 0 handlers.")
}
}
}
pub fn is_registered(&self, block: BlockKind) -> bool {
self.registry.get(&block).is_some()
}
}
pub fn register(game: &mut Game) {
game.insert_resource(InteractableRegistry::default());
}