From ae96d36d1b73176ac841c56a9e50d794c2f6fcc9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 8 Oct 2026 15:26:51 +0200 Subject: [PATCH] Explosion, crit and damage-indicator particles ParticleEngine gains ClientExplosionTracker-style explosion bursts, TrackingEmitter and ServerLevel.sendParticles spreads; explode() and Player.attack spawn them. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 12 +++ crates/horizon-client/src/particles.rs | 125 +++++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 35f8b04a6..536425fea 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2396,6 +2396,15 @@ impl Game<'_> { let mob = &mut self.mobs[index]; let center = [mob.position.x, mob.position.y + mob.kind.height / 2.0, mob.position.z]; if mob.hurt(amount, from, &mut self.world.random) { + // Player.attack: crit emitter and damage indicator hearts. + let (feet, width, height) = ([mob.position.x, mob.position.y, mob.position.z], mob.kind.width, mob.kind.height); + if crit { + self.particles.tracking_emitter(horizon_client::particles::ParticleOptions::simple(horizon_client::particles::ParticleType::Crit), feet, width, height); + } + if amount > 2.0 { + let indicator = horizon_client::particles::ParticleOptions::simple(horizon_client::particles::ParticleType::DamageIndicator); + self.particles.send_particles(&indicator, [feet[0], feet[1] + height * 0.5, feet[2]], [0.1, 0.0, 0.1], 0.2, (amount * 0.5) as u32); + } self.last_hurt_by_player.insert(mob.id); let kind = mob.kind.clone(); let dead = !mob.alive(); @@ -2447,6 +2456,7 @@ impl Game<'_> { } } } + let destroyed_count = destroyed.len(); for b in destroyed { let state = self.world.block(b[0], b[1], b[2]); if self.res.facts.is_air(state) { @@ -2461,6 +2471,8 @@ impl Game<'_> { } self.world.set(b[0], b[1], b[2], self.world.air)?; } + let (world, facts) = (&self.world, &self.res.facts); + self.particles.explosion(center, radius, destroyed_count, &|b| facts.is_air(world.block(b[0], b[1], b[2]))); self.audio.play("minecraft:entity.generic.explode", Some(center), 4.0, (1.0 + (self.world.random.next_float() - self.world.random.next_float()) * 0.2) * 0.7); // Entity damage by distance (exposure approximated as unobstructed). let diameter = f64::from(radius) * 2.0; diff --git a/crates/horizon-client/src/particles.rs b/crates/horizon-client/src/particles.rs index 01df21209..34b2fcf3b 100644 --- a/crates/horizon-client/src/particles.rs +++ b/crates/horizon-client/src/particles.rs @@ -575,6 +575,8 @@ pub struct ParticleEngine { atlas: HashMap, sets: HashMap, set_sprites: Vec>, + /// `TrackingEmitter`s: particle type, entity feet position, width, height, ticks left. + emitters: Vec<(ParticleOptions, [f64; 3], f64, f64, u32)>, /// Camera position for the 32-block spawn limiter. pub camera: [f64; 3], /// Local sounds requested by particles (dripstone drips): (event, position, volume, pitch). @@ -593,6 +595,7 @@ impl ParticleEngine { atlas: atlas.clone(), sets: HashMap::new(), set_sprites: Vec::new(), + emitters: Vec::new(), camera: [0.0; 3], sounds: Vec::new(), }; @@ -638,6 +641,7 @@ impl ParticleEngine { self.particles.is_empty() } pub fn clear(&mut self) { + self.emitters.clear(); self.particles.clear(); self.pending.clear(); } @@ -659,6 +663,121 @@ impl ParticleEngine { } self.create(options, p, v); } + /// `ClientPacketListener.handleExplosion` + `ClientExplosionTracker`: the + /// explosion particle (emitter for radius >= 2) and up to 512 poof/smoke + /// particles thrown from air inside the radius. + pub fn explosion( + &mut self, + center: [f64; 3], + radius: f32, + block_count: usize, + is_air: &dyn Fn([i32; 3]) -> bool, + ) { + let ty = if radius < 2.0 { + ParticleType::Explosion + } else { + ParticleType::ExplosionEmitter + }; + self.add(&ParticleOptions::simple(ty), center, [1.0, 0.0, 0.0]); + for _ in 0..block_count.min(512) { + let r = &mut self.random; + let d = Vec3::new( + f64::from(r.next_float() * 2.0 - 1.0), + f64::from(r.next_float() * 2.0 - 1.0), + f64::from(r.next_float() * 2.0 - 1.0), + ) + .normalize(); + let distance = r.next_float().cbrt() * radius; + let local = [ + d.x * f64::from(distance), + d.y * f64::from(distance), + d.z * f64::from(distance), + ]; + let at = [ + center[0] + local[0], + center[1] + local[1], + center[2] + local[2], + ]; + if !is_air(ParticleLevel::block_at(at)) { + continue; + } + let speed = 0.5 / (distance / radius + 0.1) * r.next_float() * r.next_float() + 0.3; + // Level.DEFAULT_EXPLOSION_BLOCK_PARTICLES: poof (scaling 0.5) or smoke, equally weighted. + let (ty, scaling) = if r.next_int_bound(2).unwrap_or(0) == 0 { + (ParticleType::Poof, 0.5) + } else { + (ParticleType::Smoke, 1.0) + }; + let p = [ + center[0] + local[0] * scaling, + center[1] + local[1] * scaling, + center[2] + local[2] * scaling, + ]; + let v = [ + d.x * f64::from(speed), + d.y * f64::from(speed), + d.z * f64::from(speed), + ]; + self.add(&ParticleOptions::simple(ty), p, v); + } + } + /// `ParticleEngine.createTrackingEmitter` (crits): 16 tries per tick for + /// three ticks inside a quarter of the entity's box, ticked once at creation. + pub fn tracking_emitter( + &mut self, + options: ParticleOptions, + feet: [f64; 3], + width: f64, + height: f64, + ) { + self.emit(&options, feet, width, height); + self.emitters.push((options, feet, width, height, 2)); + } + fn emit(&mut self, options: &ParticleOptions, feet: [f64; 3], width: f64, height: f64) { + for _ in 0..16 { + let r = &mut self.random; + let xa = f64::from(r.next_float() * 2.0 - 1.0); + let ya = f64::from(r.next_float() * 2.0 - 1.0); + let za = f64::from(r.next_float() * 2.0 - 1.0); + if xa * xa + ya * ya + za * za <= 1.0 { + let p = [ + feet[0] + width * xa / 4.0, + feet[1] + height * (0.5 + ya / 4.0), + feet[2] + width * za / 4.0, + ]; + self.add(options, p, [xa, ya + 0.2, za]); + } + } + } + /// `ClientPacketListener.handleParticleEvent` for `ServerLevel.sendParticles`. + pub fn send_particles( + &mut self, + options: &ParticleOptions, + at: [f64; 3], + spread: [f64; 3], + speed: f64, + count: u32, + ) { + if count == 0 { + let v = [speed * spread[0], speed * spread[1], speed * spread[2]]; + self.add(options, at, v); + return; + } + for _ in 0..count { + let r = &mut self.random; + let p = [ + at[0] + r.next_gaussian() * spread[0], + at[1] + r.next_gaussian() * spread[1], + at[2] + r.next_gaussian() * spread[2], + ]; + let v = [ + r.next_gaussian() * speed, + r.next_gaussian() * speed, + r.next_gaussian() * speed, + ]; + self.add(options, p, v); + } + } /// `ParticleEngine.createParticle`: always spawned. pub fn create(&mut self, options: &ParticleOptions, p: [f64; 3], v: [f64; 3]) { if let Some(particle) = self.make(options, p, v) { @@ -1517,6 +1636,12 @@ impl ParticleEngine { /// `ParticleEngine.tick`: tick live particles, then admit queued ones. pub fn tick(&mut self, level: &ParticleLevel) { + for (options, feet, width, height, life) in std::mem::take(&mut self.emitters) { + self.emit(&options, feet, width, height); + if life > 1 { + self.emitters.push((options, feet, width, height, life - 1)); + } + } let mut particles = std::mem::take(&mut self.particles); for p in &mut particles { self.tick_particle(p, level);