Skip to content

Commit 5305071

Browse files
committed
Improve documentation
1 parent 43538c6 commit 5305071

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

feather/common/src/block_break.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::mem;
2+
13
use base::{BlockKind, ItemStack, ValidBlockPosition};
24
use ecs::{EntityBuilder, SysResult, SystemExecutor};
35
use libcraft_items::EnchantmentKind;
@@ -14,6 +16,7 @@ pub enum BlockBreaker {
1416
Inactive,
1517
}
1618
impl BlockBreaker {
19+
/// Create a new active instance pointing to `block_pos`. Calculates the time needed using `world.block_at(block_pos)` and `equipped_item`.
1720
pub fn new(
1821
world: &mut World,
1922
block_pos: ValidBlockPosition,
@@ -23,59 +26,69 @@ impl BlockBreaker {
2326
.map(Self::Active)
2427
.unwrap_or(Self::Inactive)
2528
}
29+
/// If active, produces a `DestroyStateChange` event for the adequate position.
2630
pub fn destroy_change_event(&self) -> Option<DestroyStateChange> {
2731
Some(DestroyStateChange(self.position()?, self.destroy_stage()))
2832
}
33+
/// If active or finished, returns the pointed to position.
2934
pub fn position(&self) -> Option<ValidBlockPosition> {
3035
match self {
3136
BlockBreaker::Active(a) => Some(a.position),
3237
BlockBreaker::Finished(f) => Some(f.position),
3338
BlockBreaker::Inactive => None,
3439
}
3540
}
41+
/// If active, returns the underlying `ActiveBreaker`.
3642
pub fn active(&self) -> Option<&ActiveBreaker> {
3743
match self {
3844
Self::Active(a) => Some(a),
3945
_ => None,
4046
}
4147
}
48+
/// If finished, returns the underlying `FinishedBreaker`.
4249
pub fn finished(&self) -> Option<&FinishedBreaker> {
4350
match self {
4451
Self::Finished(f) => Some(f),
4552
_ => None,
4653
}
4754
}
55+
/// Progresses block breaking. Returns a (newly_finished, do_destry_state_change) tuple.
56+
/// If this operation finishes block breaking, this turns `self` into `Self::Finished` with the same position.
4857
pub fn tick(&mut self) -> (bool, bool) {
4958
let (block_break, stage_update) = if let Self::Active(breaker) = self {
5059
breaker.tick()
5160
} else {
5261
(false, false)
5362
};
5463
if block_break {
55-
let fin = match self {
56-
Self::Active(a) => a.clone().finish(),
64+
let fin = match mem::take(self) {
65+
Self::Active(a) => a.finish(),
5766
_ => unreachable!(),
5867
};
5968
*self = Self::Finished(fin);
6069
}
6170
(block_break, stage_update)
6271
}
72+
/// Returns the block destroying progress in a range of 0 - 9. When inactive or finished, returns 10.
6373
pub fn destroy_stage(&self) -> u8 {
6474
match self {
6575
BlockBreaker::Active(a) => a.destroy_stage(),
6676
_ => 10,
6777
}
6878
}
79+
/// Set `self` to `Self::Inactive`.
6980
pub fn cancel(&mut self) {
7081
*self = Self::Inactive;
7182
}
83+
/// Check if the breaker points to `pos`. Returns `true` when `self` is `Self::Inactive`.
7284
pub fn matches_position(&self, pos: ValidBlockPosition) -> bool {
7385
match self {
7486
BlockBreaker::Active(a) => a.position == pos,
7587
BlockBreaker::Finished(f) => f.position == pos,
7688
BlockBreaker::Inactive => true,
7789
}
7890
}
91+
/// Attempts to finish breaking the target block, optionally turning `self` into `Self::Finished`.
7992
pub fn try_finish(&mut self) -> Option<FinishedBreaker> {
8093
let this = self.clone();
8194
match this {
@@ -93,13 +106,19 @@ impl BlockBreaker {
93106
}
94107
}
95108
}
109+
impl Default for BlockBreaker {
110+
fn default() -> Self {
111+
Self::Inactive
112+
}
113+
}
96114
#[derive(Clone)]
97115
pub struct FinishedBreaker {
98116
pub position: ValidBlockPosition,
99117
pub drop_item: bool,
100118
pub fake_finished: bool,
101119
}
102120
impl FinishedBreaker {
121+
/// Breaks the targeted block and spawns its drops. TODO: make drops work.
103122
pub fn break_block(&self, game: &mut Game) -> SysResult {
104123
let target_block = match game.block(self.position) {
105124
Some(b) => b,

0 commit comments

Comments
 (0)