Skip to content

Commit

Permalink
Added particles when trees hit the ground
Browse files Browse the repository at this point in the history
  • Loading branch information
supermassimo committed Sep 6, 2024
1 parent f7fd618 commit 1c4a9b0
Showing 1 changed file with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
import com.ferreusveritas.dynamictrees.entity.FallingTreeEntity;
import com.ferreusveritas.dynamictrees.init.DTConfigs;
import com.ferreusveritas.dynamictrees.tree.species.Species;
import com.ferreusveritas.dynamictrees.util.BranchDestructionData;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.BlockParticleOption;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
Expand All @@ -22,7 +26,6 @@
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.joml.Vector3f;

import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -77,6 +80,63 @@ protected void playFallThroughWaterSound(FallingTreeEntity entity){
}
}

private Vec3 rotateAroundAxis(Vec3 in, Vec3 axis, double theta){
double x = in.x;
double y = in.y;
double z = in.z;
double u = axis.x;
double v = axis.y;
double w = axis.z;
double v1 = u * x + v * y + w * z;
double xPrime = u* v1 *(1d - Math.cos(theta))
+ x*Math.cos(theta)
+ (-w*y + v*z)*Math.sin(theta);
double yPrime = v* v1 *(1d - Math.cos(theta))
+ y*Math.cos(theta)
+ (w*x - u*z)*Math.sin(theta);
double zPrime = w* v1 *(1d - Math.cos(theta))
+ z*Math.cos(theta)
+ (-v*x + u*y)*Math.sin(theta);
return new Vec3(xPrime, yPrime, zPrime);
}

protected void flingLeavesParticles(FallingTreeEntity entity, float fallSpeed){
int bounces = getData(entity).bounces;
if (bounces > 1) return;
double bounceDecay = Math.exp(-bounces);
BranchDestructionData data = entity.getDestroyData();
Direction.Axis toolAxis = data.toolDir.getAxis();
if (toolAxis == Direction.Axis.Y) return; //this one isn't possible anyways
Vec3 w = entity.getForward().scale(fallSpeed * data.toolDir.getAxisDirection().getStep());
if (toolAxis == Direction.Axis.X) w = new Vec3(w.z, w.x, w.y);
double speedVar = 0.2f * bounceDecay;
RandomSource rand = entity.level().random;
for (int i=0; i<data.getNumLeaves(); i++){
BlockPos leaves = data.getLeavesRelPos(i).offset(data.basePos);
double r = -0.1 * (leaves.getY() - data.basePos.getY()) * fallSpeed;
Vec3 velocity = w.scale(r);
Vec3 newPos = getRelativeLeavesPosition(entity, leaves.getCenter());
for (int j=0; j<fallSpeed*10; j++){
if (rand.nextDouble() < bounceDecay){
BlockState leavesState = entity.getDestroyData().getLeavesBlockState(i);
if (leavesState != null)
entity.level().addParticle(new BlockParticleOption(ParticleTypes.BLOCK, leavesState),
newPos.x+rand.nextFloat(), newPos.y+rand.nextFloat(), newPos.z+rand.nextFloat(),
velocity.x+rand.nextFloat()*speedVar - speedVar/2f, velocity.y+rand.nextFloat()*speedVar - speedVar/2f, velocity.z+rand.nextFloat()*speedVar - speedVar/2f);
}
}
}
}

protected Vec3 getRelativeLeavesPosition(FallingTreeEntity entity, Vec3 leaves){
BranchDestructionData data = entity.getDestroyData();
float angle = (data.toolDir.getAxis() == Direction.Axis.X ? entity.getYRot() : entity.getXRot()) * -data.toolDir.getAxisDirection().getStep() * 0.0174533f;
return rotateAroundAxis(
leaves.subtract(data.basePos.getCenter()),
new Vec3(-data.toolDir.getStepZ(),0, data.toolDir.getStepX()),
angle).add(data.basePos.getCenter()).subtract(0.5,0.5,0.5);
}

@Override
public void initMotion(FallingTreeEntity entity) {
entity.dataAnimationHandler = new HandlerData();
Expand All @@ -97,7 +157,7 @@ public void handleMotion(FallingTreeEntity entity) {

if (entity.onGround()) {
float height = (float) entity.getMassCenter().y * 2;
fallSpeed += (0.2 / height);
fallSpeed += (float) (0.2 / height);
addRotation(entity, fallSpeed);
}

Expand Down Expand Up @@ -132,6 +192,7 @@ public void handleMotion(FallingTreeEntity entity) {

if (fallSpeed > 0 && testCollision(entity)) {
playEndSound(entity);
flingLeavesParticles(entity, fallSpeed);
addRotation(entity, -fallSpeed);//pull back to before the collision
getData(entity).bounces++;
fallSpeed *= -AnimationConstants.TREE_ELASTICITY;//bounce with elasticity
Expand Down

0 comments on commit 1c4a9b0

Please sign in to comment.