Skip to content

Commit d6dd708

Browse files
committed
Switch to float based break progress
1 parent 5305071 commit d6dd708

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

feather/common/src/block_break.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl BlockBreaker {
9393
let this = self.clone();
9494
match this {
9595
BlockBreaker::Active(a) => {
96-
if a.ticks_remaining == 1 {
96+
if a.can_break() {
9797
let fin = a.finish();
9898
*self = Self::Finished(fin.clone());
9999
Some(fin)
@@ -142,18 +142,23 @@ pub struct ActiveBreaker {
142142
pub position: ValidBlockPosition,
143143
pub drop_item: bool,
144144
pub fake_finished: bool,
145-
pub total_ticks: u32,
146-
pub ticks_remaining: u32,
145+
pub progress: f32,
146+
pub damage: f32,
147147
}
148148
impl ActiveBreaker {
149149
pub fn tick(&mut self) -> (bool, bool) {
150150
let before = self.destroy_stage();
151-
self.ticks_remaining = self.ticks_remaining.saturating_sub(1);
151+
self.progress += self.damage;
152152
let after = self.destroy_stage();
153-
let break_block = self.ticks_remaining == 0;
153+
let break_block = self.can_break();
154154
let change_stage = before != after || break_block;
155155
(break_block, change_stage)
156156
}
157+
/// Check if the block has been damaged enough to break.
158+
pub fn can_break(&self) -> bool {
159+
// Comparing to 0.7 ensures good feeling in the client
160+
self.progress >= 0.7 - self.damage / 2.0
161+
}
157162
pub fn new(
158163
world: &mut World,
159164
block_pos: ValidBlockPosition,
@@ -202,32 +207,20 @@ impl ActiveBreaker {
202207
} else {
203208
1.0 / block.hardness() / 100.0
204209
};
205-
let ticks = if damage > 1.0 {
206-
0
207-
} else {
208-
(1.0 / damage / 1.2).ceil() as u32
209-
};
210-
println!(
211-
"Mining {} with {} takes {} ticks",
212-
block.display_name(),
213-
equipped_item
214-
.map(|e| e.get_item().item().display_name())
215-
.unwrap_or("bare hands"),
216-
ticks
217-
);
218210
Some(Self {
219211
position: block_pos,
220212
drop_item: true,
221213
fake_finished: false,
222-
total_ticks: ticks,
223-
ticks_remaining: ticks,
214+
progress: 0.0,
215+
damage,
224216
})
225217
}
218+
/// Get the destroying progress.
226219
pub fn destroy_stage(&self) -> u8 {
227220
if self.fake_finished {
228221
10
229222
} else {
230-
9 - (self.ticks_remaining as f32 / self.total_ticks as f32 * 9.0).round() as u8
223+
(self.progress * 9.0).round() as u8
231224
}
232225
}
233226
pub fn finish(self) -> FinishedBreaker {

feather/server/src/packet_handlers/interaction.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,12 @@ pub fn handle_player_digging(
182182
} else {
183183
if let BlockBreaker::Active(a) = &mut *breaker {
184184
a.fake_finished = true;
185-
println!("{} ticks remaining", a.ticks_remaining);
186185
}
187186
false
188187
};
189188
success && breaker.matches_position(packet.position)
190189
};
191190
if success {
192-
println!("confirm");
193191
finished.unwrap().break_block(game)?;
194192
}
195193
game.ecs

0 commit comments

Comments
 (0)