Push sonic boom targets only when hit, by their knockback resistance
This commit is contained in:
@@ -568,6 +568,10 @@ fn print_heart(
|
||||
fn player_damage(e: &MobEvent) -> f32 {
|
||||
match e {
|
||||
MobEvent::AttackPlayer(amount, _) | MobEvent::DamagePlayer { amount, .. } => *amount,
|
||||
MobEvent::SonicBoom {
|
||||
target: Who::Player,
|
||||
..
|
||||
} => 10.0,
|
||||
_ => 0.0,
|
||||
}
|
||||
}
|
||||
@@ -579,6 +583,10 @@ fn player_hurt(player: Option<&PlayerView>, e: &MobEvent) -> bool {
|
||||
};
|
||||
match e {
|
||||
MobEvent::AttackPlayer(amount, _) | MobEvent::DamagePlayer { amount, .. } => *amount > 0.0,
|
||||
MobEvent::SonicBoom {
|
||||
target: Who::Player,
|
||||
..
|
||||
} => true,
|
||||
// ServerExplosion.hurtEntities: within twice the radius every hit does damage.
|
||||
MobEvent::Explode(at, radius) => {
|
||||
let d = Vec3::new(at[0], at[1], at[2])
|
||||
|
||||
@@ -838,6 +838,44 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Melee or ranged damage to another mob; whether the hit landed.
|
||||
fn mob_hits_mob(&mut self, target: u32, amount: f32, source: Damage) -> bool {
|
||||
let mut killed = false;
|
||||
let mut hurt = false;
|
||||
if let Some(t) = self.mobs.iter_mut().find(|m| m.id == target) {
|
||||
let hit = t.damage(amount, source);
|
||||
hurt = hit;
|
||||
if hit {
|
||||
killed = !t.alive();
|
||||
// The hurt or death sound the mob raised.
|
||||
let center = t.center();
|
||||
for event in std::mem::take(&mut t.outbox) {
|
||||
if let MobEvent::Sound(s, _, volume, pitch) = event {
|
||||
self.audio
|
||||
.play(&format!("minecraft:{s}"), Some(center), volume, pitch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if hurt
|
||||
&& let Some(Who::Mob(attacker)) = source.attacker
|
||||
&& let Some(m) = self
|
||||
.mobs
|
||||
.iter()
|
||||
.find(|m| m.id == attacker && m.species() == Species::Axolotl)
|
||||
{
|
||||
self.audio.play(
|
||||
"minecraft:entity.axolotl.attack",
|
||||
Some([m.position.x, m.position.y, m.position.z]),
|
||||
1.0,
|
||||
1.0,
|
||||
);
|
||||
}
|
||||
if killed && let Some(Who::Mob(killer)) = source.attacker {
|
||||
self.zombie_killed(killer, target);
|
||||
}
|
||||
hurt
|
||||
}
|
||||
pub(super) fn handle_mob_events(&mut self, events: Vec<MobEvent>) -> Result<()> {
|
||||
for e in events {
|
||||
match e {
|
||||
@@ -926,43 +964,48 @@ impl Game<'_> {
|
||||
amount,
|
||||
source,
|
||||
} => {
|
||||
let mut killed = false;
|
||||
let mut hurt = false;
|
||||
if let Some(t) = self.mobs.iter_mut().find(|m| m.id == target) {
|
||||
let hit = t.damage(amount, source);
|
||||
hurt = hit;
|
||||
if hit {
|
||||
killed = !t.alive();
|
||||
// The hurt or death sound the mob raised.
|
||||
let center = t.center();
|
||||
for event in std::mem::take(&mut t.outbox) {
|
||||
if let MobEvent::Sound(s, _, volume, pitch) = event {
|
||||
self.audio.play(
|
||||
&format!("minecraft:{s}"),
|
||||
Some(center),
|
||||
volume,
|
||||
pitch,
|
||||
);
|
||||
}
|
||||
}
|
||||
self.mob_hits_mob(target, amount, source);
|
||||
}
|
||||
MobEvent::SonicBoom {
|
||||
target,
|
||||
attacker,
|
||||
from,
|
||||
direction,
|
||||
} => {
|
||||
// SonicBoom: the push only follows a hit that landed, scaled
|
||||
// by the target's KNOCKBACK_RESISTANCE.
|
||||
let (hurt, resistance) = match target {
|
||||
Who::Player => {
|
||||
let hurt = self.mob_hits_player(10.0, from, "minecraft:sonic_boom");
|
||||
let resistance = self.player.as_mut().map_or(0.0, |p| {
|
||||
p.attribute("minecraft:knockback_resistance").unwrap_or(0.0)
|
||||
});
|
||||
(hurt, resistance)
|
||||
}
|
||||
}
|
||||
if hurt
|
||||
&& let Some(Who::Mob(attacker)) = source.attacker
|
||||
&& let Some(m) = self
|
||||
.mobs
|
||||
.iter()
|
||||
.find(|m| m.id == attacker && m.species() == Species::Axolotl)
|
||||
{
|
||||
self.audio.play(
|
||||
"minecraft:entity.axolotl.attack",
|
||||
Some([m.position.x, m.position.y, m.position.z]),
|
||||
1.0,
|
||||
1.0,
|
||||
);
|
||||
}
|
||||
if killed && let Some(Who::Mob(killer)) = source.attacker {
|
||||
self.zombie_killed(killer, target);
|
||||
Who::Mob(id) => {
|
||||
let source = Damage {
|
||||
kind: DamageKind::SonicBoom,
|
||||
attacker: Some(Who::Mob(attacker)),
|
||||
from: Some(from),
|
||||
};
|
||||
let hurt = self.mob_hits_mob(id, 10.0, source);
|
||||
let resistance = self
|
||||
.mobs
|
||||
.iter()
|
||||
.find(|m| m.id == id)
|
||||
.map_or(0.0, |m| m.attrs.knockback_resistance);
|
||||
(hurt, resistance)
|
||||
}
|
||||
};
|
||||
if hurt {
|
||||
let horizontal = 2.5 * (1.0 - resistance);
|
||||
let vertical = 0.5 * (1.0 - resistance);
|
||||
let velocity = [
|
||||
direction[0] * horizontal,
|
||||
direction[1] * vertical,
|
||||
direction[2] * horizontal,
|
||||
];
|
||||
self.handle_mob_events(vec![MobEvent::Push { target, velocity }])?;
|
||||
}
|
||||
}
|
||||
MobEvent::FinishAdmiring { mob } => self.finish_admiring(mob),
|
||||
|
||||
@@ -66,6 +66,10 @@ pub(super) fn acts_on_player(e: &MobEvent) -> bool {
|
||||
e,
|
||||
MobEvent::AttackPlayer(..)
|
||||
| MobEvent::DamagePlayer { .. }
|
||||
| MobEvent::SonicBoom {
|
||||
target: Who::Player,
|
||||
..
|
||||
}
|
||||
| MobEvent::Push {
|
||||
target: Who::Player,
|
||||
..
|
||||
|
||||
@@ -961,6 +961,15 @@ pub enum MobEvent {
|
||||
target: Who,
|
||||
velocity: [f64; 3],
|
||||
},
|
||||
/// `SonicBoom`: 10 damage to the target, then a push along `direction`
|
||||
/// (the unit vector from the warden's chest to the target's eyes) only
|
||||
/// if the hit landed, less the target's knockback resistance.
|
||||
SonicBoom {
|
||||
target: Who,
|
||||
attacker: u32,
|
||||
from: [f64; 3],
|
||||
direction: [f64; 3],
|
||||
},
|
||||
/// `EvokerWololoSpellGoal`: a blue sheep turns red.
|
||||
Wololo {
|
||||
mob: u32,
|
||||
|
||||
@@ -697,34 +697,11 @@ pub(super) fn tick_sonic_boom(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEve
|
||||
});
|
||||
}
|
||||
sound(m, "entity.warden.sonic_boom", 3.0, 1.0, events);
|
||||
let from = at(m);
|
||||
// Players and the mobs hit here have no knockback resistance.
|
||||
let resistance = 0.0;
|
||||
match target {
|
||||
Who::Player => events.push(MobEvent::DamagePlayer {
|
||||
amount: 10.0,
|
||||
from,
|
||||
kind: "minecraft:sonic_boom",
|
||||
}),
|
||||
Who::Mob(id) => events.push(MobEvent::AttackMob {
|
||||
target: id,
|
||||
amount: 10.0,
|
||||
source: Damage {
|
||||
kind: DamageKind::SonicBoom,
|
||||
attacker: Some(Who::Mob(m.id)),
|
||||
from: Some(from),
|
||||
},
|
||||
}),
|
||||
}
|
||||
let horizontal = 2.5 * (1.0 - resistance);
|
||||
let vertical = 0.5 * (1.0 - resistance);
|
||||
events.push(MobEvent::Push {
|
||||
events.push(MobEvent::SonicBoom {
|
||||
target,
|
||||
velocity: [
|
||||
normal[0] * horizontal,
|
||||
normal[1] * vertical,
|
||||
normal[2] * horizontal,
|
||||
],
|
||||
attacker: m.id,
|
||||
from: at(m),
|
||||
direction: normal,
|
||||
});
|
||||
}
|
||||
/// `Warden.tick` on the client: the heartbeat (pitched by the client's copy
|
||||
|
||||
Reference in New Issue
Block a user