Sulfur cubes bounce, slide and keep speed by their block's archetype
This commit is contained in:
@@ -411,19 +411,45 @@ impl Mob {
|
||||
} else if !matches!(self.kind.species, super::Species::Bat | super::Species::Bee) {
|
||||
self.check_fall_damage(w, moved, events);
|
||||
}
|
||||
// restituteMovementAfterCollisions with no bounciness: the colliding
|
||||
// components become (signed) zero.
|
||||
// Entity.restituteMovementAfterCollisions: the colliding components
|
||||
// bounce back by the entity's bounciness (zero for most mobs, so they
|
||||
// become signed zero). Block bounciness is not modelled for mobs.
|
||||
if moved_vertically && vertical || self.horizontal_collision {
|
||||
let v = self.velocity;
|
||||
let restitution = self.entity_bounciness();
|
||||
let mut after = v;
|
||||
if x_hit {
|
||||
self.velocity.x = -v.x * 0.0;
|
||||
after.x = -v.x * restitution;
|
||||
}
|
||||
if z_hit {
|
||||
self.velocity.z = -v.z * 0.0;
|
||||
after.z = -v.z * restitution;
|
||||
}
|
||||
if vertical {
|
||||
self.velocity.y = (0.0 - v.y) * 1.0 * 0.0;
|
||||
let mut restitution = restitution;
|
||||
if self.vertical_collision_below {
|
||||
// Only a hard enough landing bounces, and never on honey.
|
||||
let on = (
|
||||
self.position.x.floor() as i32,
|
||||
(self.position.y - f64::from(0.2_f32)).floor() as i32,
|
||||
self.position.z.floor() as i32,
|
||||
);
|
||||
if -v.y <= self.gravity()
|
||||
|| restitution > 0.0
|
||||
&& w.block_name(on.0, on.1, on.2) == "minecraft:honey_block"
|
||||
{
|
||||
restitution = 0.0;
|
||||
}
|
||||
}
|
||||
let (compensation, drag) = if restitution > 0.0 {
|
||||
let portion = moved.y / v.y;
|
||||
let air_drag = f64::from(self.air_drag());
|
||||
(portion * self.gravity(), 1.0 + portion * (air_drag - 1.0))
|
||||
} else {
|
||||
(0.0, 1.0)
|
||||
};
|
||||
after.y = (compensation - v.y) * drag * restitution;
|
||||
}
|
||||
self.velocity = after;
|
||||
}
|
||||
// Entity.checkSupportingBlock
|
||||
self.supporting = self.supporting_block(w, moved);
|
||||
@@ -716,9 +742,33 @@ impl Mob {
|
||||
let y = (self.position.y - 0.500_001).floor() as i32;
|
||||
w.facts.get(w.world.block(x, y, z)).friction
|
||||
}
|
||||
/// `getAttributeValue(BOUNCINESS)`.
|
||||
fn entity_bounciness(&self) -> f64 {
|
||||
super::sulfur_cube::physics(self).map_or(0.0, |a| f64::from(a.bounciness.clamp(0.0, 1.0)))
|
||||
}
|
||||
/// `LivingEntity.computeModifiedFriction`.
|
||||
fn modified_friction(friction: f32, modifier: f32) -> f32 {
|
||||
(1.0 - (1.0 - friction) * modifier).clamp(0.0, 1.0)
|
||||
}
|
||||
/// `LivingEntity.getAirDrag`: what vertical air drag a bounce applies.
|
||||
fn air_drag(&self) -> f32 {
|
||||
let base = if self.kind.flags.omnidirectional {
|
||||
0.91
|
||||
} else {
|
||||
0.98
|
||||
};
|
||||
Self::modified_friction(base, self.air_drag_modifier())
|
||||
}
|
||||
/// `getAttributeValue(AIR_DRAG_MODIFIER)`.
|
||||
fn air_drag_modifier(&self) -> f32 {
|
||||
super::sulfur_cube::physics(self).map_or(1.0, |a| a.air_drag_modifier.clamp(0.0, 2048.0))
|
||||
}
|
||||
fn travel_in_air(&mut self, w: &MobWorld, input: Vec3, events: &mut Vec<MobEvent>) {
|
||||
let friction = if self.on_ground {
|
||||
self.block_friction(w)
|
||||
// FRICTION_MODIFIER scales the block's slipperiness.
|
||||
let modifier = super::sulfur_cube::physics(self)
|
||||
.map_or(1.0, |a| a.friction_modifier.clamp(0.0, 2048.0));
|
||||
Self::modified_friction(self.block_friction(w), modifier)
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
@@ -752,12 +802,13 @@ impl Mob {
|
||||
self.velocity = Vec3::new(movement.x, y, movement.z);
|
||||
return;
|
||||
}
|
||||
let drag = 0.91f32;
|
||||
let air_drag_modifier = self.air_drag_modifier();
|
||||
let drag = Self::modified_friction(0.91, air_drag_modifier);
|
||||
let f = friction * drag;
|
||||
let vertical = if self.kind.flags.omnidirectional {
|
||||
drag
|
||||
} else {
|
||||
0.98
|
||||
Self::modified_friction(0.98, air_drag_modifier)
|
||||
};
|
||||
self.velocity = Vec3::new(
|
||||
movement.x * f64::from(f),
|
||||
|
||||
@@ -32,6 +32,12 @@ pub struct Archetype {
|
||||
pub contact_damage: Option<f32>,
|
||||
/// The knockback resistance modifier (the attribute clamps to 0..1).
|
||||
pub knockback_resistance: f64,
|
||||
/// `Attributes.BOUNCINESS` (the modifier added to a base of 0).
|
||||
pub bounciness: f32,
|
||||
/// `Attributes.FRICTION_MODIFIER` and `AIR_DRAG_MODIFIER` (base 1 times
|
||||
/// the archetype's factor).
|
||||
pub friction_modifier: f32,
|
||||
pub air_drag_modifier: f32,
|
||||
pub horizontal_power: f32,
|
||||
pub vertical_power: f32,
|
||||
pub hit_sound: &'static str,
|
||||
@@ -40,13 +46,16 @@ pub struct Archetype {
|
||||
pub push_threshold: f32,
|
||||
}
|
||||
macro_rules! arch {
|
||||
($n:literal, $b:expr, $e:expr, $c:expr, $k:expr, $h:expr, $v:expr, $cd:expr, $t:expr) => {
|
||||
($n:literal, $b:expr, $e:expr, $c:expr, $k:expr, $phys:expr, $h:expr, $v:expr, $cd:expr, $t:expr) => {
|
||||
Archetype {
|
||||
name: $n,
|
||||
buoyant: $b,
|
||||
explosion: $e,
|
||||
contact_damage: $c,
|
||||
knockback_resistance: $k,
|
||||
bounciness: $phys.0,
|
||||
friction_modifier: $phys.1,
|
||||
air_drag_modifier: $phys.2,
|
||||
horizontal_power: $h,
|
||||
vertical_power: $v,
|
||||
hit_sound: concat!("entity.sulfur_cube.", $n, ".hit"),
|
||||
@@ -58,13 +67,25 @@ macro_rules! arch {
|
||||
}
|
||||
/// `data/minecraft/sulfur_cube_archetype/*.json`.
|
||||
pub const ARCHETYPES: [Archetype; 12] = [
|
||||
arch!("bouncy", true, None, None, -2.0, 0.4125, 0.105, 0.7, 0.3),
|
||||
arch!(
|
||||
"bouncy",
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
-2.0,
|
||||
(0.9, 0.3, 0.01),
|
||||
0.4125,
|
||||
0.105,
|
||||
0.7,
|
||||
0.3
|
||||
),
|
||||
arch!(
|
||||
"explosive",
|
||||
true,
|
||||
Some((120, 3.0, false)),
|
||||
None,
|
||||
-1.0,
|
||||
(0.5, 0.3, 0.3),
|
||||
0.4125,
|
||||
0.09,
|
||||
0.7,
|
||||
@@ -76,6 +97,7 @@ pub const ARCHETYPES: [Archetype; 12] = [
|
||||
None,
|
||||
None,
|
||||
-1.0,
|
||||
(0.5, 0.2, 0.01),
|
||||
0.9125,
|
||||
0.09,
|
||||
0.9,
|
||||
@@ -87,6 +109,7 @@ pub const ARCHETYPES: [Archetype; 12] = [
|
||||
None,
|
||||
None,
|
||||
0.5,
|
||||
(0.1, 0.05, 0.01),
|
||||
0.6625,
|
||||
0.09,
|
||||
1.0,
|
||||
@@ -98,20 +121,55 @@ pub const ARCHETYPES: [Archetype; 12] = [
|
||||
None,
|
||||
None,
|
||||
0.7,
|
||||
(0.2, 1.0, 0.01),
|
||||
0.4125,
|
||||
0.09,
|
||||
0.7,
|
||||
0.03
|
||||
),
|
||||
arch!("hot", true, None, Some(1.0), -1.0, 0.4125, 0.09, 0.7, 0.2),
|
||||
arch!("light", true, None, None, -1.0, 0.4125, 0.18, 0.7, 0.2),
|
||||
arch!("regular", true, None, None, -1.0, 0.4125, 0.09, 0.5, 0.2),
|
||||
arch!(
|
||||
"hot",
|
||||
true,
|
||||
None,
|
||||
Some(1.0),
|
||||
-1.0,
|
||||
(0.5, 0.3, 0.1),
|
||||
0.4125,
|
||||
0.09,
|
||||
0.7,
|
||||
0.2
|
||||
),
|
||||
arch!(
|
||||
"light",
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
-1.0,
|
||||
(1.0, 0.3, 1.8),
|
||||
0.4125,
|
||||
0.18,
|
||||
0.7,
|
||||
0.2
|
||||
),
|
||||
arch!(
|
||||
"regular",
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
-1.0,
|
||||
(0.5, 0.3, 0.1),
|
||||
0.4125,
|
||||
0.09,
|
||||
0.5,
|
||||
0.2
|
||||
),
|
||||
arch!(
|
||||
"slow_bouncy",
|
||||
false,
|
||||
None,
|
||||
None,
|
||||
0.4,
|
||||
(0.6, 0.3, 0.05),
|
||||
0.4125,
|
||||
0.24,
|
||||
0.5,
|
||||
@@ -123,6 +181,7 @@ pub const ARCHETYPES: [Archetype; 12] = [
|
||||
None,
|
||||
None,
|
||||
0.5,
|
||||
(0.4, 0.4, 0.1),
|
||||
0.4125,
|
||||
0.105,
|
||||
0.9,
|
||||
@@ -134,12 +193,24 @@ pub const ARCHETYPES: [Archetype; 12] = [
|
||||
None,
|
||||
None,
|
||||
0.8,
|
||||
(0.1, 0.05, 0.01),
|
||||
0.4125,
|
||||
0.09,
|
||||
1.0,
|
||||
0.02
|
||||
),
|
||||
arch!("sticky", false, None, None, -2.0, 0.4125, 0.09, 0.5, 0.05),
|
||||
arch!(
|
||||
"sticky",
|
||||
false,
|
||||
None,
|
||||
None,
|
||||
-2.0,
|
||||
(0.0, 2.0, 0.01),
|
||||
0.4125,
|
||||
0.09,
|
||||
0.5,
|
||||
0.05
|
||||
),
|
||||
];
|
||||
/// `SulfurCubeArchetype.DEFAULT_*`: no block swallowed.
|
||||
const DEFAULT_PUSH: (&str, f32, f32) = ("entity.sulfur_cube.regular.push", 0.5, 0.2);
|
||||
@@ -178,6 +249,15 @@ pub fn has_body(m: &Mob) -> bool {
|
||||
pub fn archetype(m: &Mob) -> Option<&'static Archetype> {
|
||||
m.extra.head.as_deref().and_then(archetype_of)
|
||||
}
|
||||
/// The archetype whose bounciness, friction and air drag attributes a
|
||||
/// sulfur cube's physics use (no other mob has those modifiers).
|
||||
pub fn physics(m: &Mob) -> Option<&'static Archetype> {
|
||||
if m.species() == super::Species::SulfurCube {
|
||||
archetype(m)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
pub fn fuse(m: &Mob) -> i32 {
|
||||
m.extra.counter2 - 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user