Fixed crystal heart rotation

This commit is contained in:
Sollace 2023-08-28 19:47:23 +01:00
parent 88252d8db9
commit ccad91087c
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
3 changed files with 80 additions and 63 deletions

View file

@ -21,37 +21,42 @@ import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
public class FloatingArtefactEntity extends Entity implements UDamageSources, MagicImmune {
private static final TrackedData<ItemStack> ITEM = DataTracker.registerData(FloatingArtefactEntity.class, TrackedDataHandlerRegistry.ITEM_STACK);
private static final TrackedData<Byte> STATE = DataTracker.registerData(FloatingArtefactEntity.class, TrackedDataHandlerRegistry.BYTE);
private static final TrackedData<Float> SPIN = DataTracker.registerData(FloatingArtefactEntity.class, TrackedDataHandlerRegistry.FLOAT);
private static final TrackedData<Float> TARGET_ROTATION_SPEED = DataTracker.registerData(FloatingArtefactEntity.class, TrackedDataHandlerRegistry.FLOAT);
private static final int REGEN_GAP_TICKS = 5;
private static final int REGEN_PAUSE_TICKS = 200;
public static final int INFINITE_BOOST_DURATION = -1;
private float bobAmount;
private float spinAmount;
private float health = 1;
private float prevRotationSpeed;
private float rotationSpeed;
private float prevRotation;
private float rotation;
private int boostDuration;
private float health;
private int ticksUntilRegen;
public final float positionSeed;
private int spinupDuration;
private float sourceSpin = 1;
private float targetSpin = 1;
private float spinChange;
private float spinChangeProgress;
private Optional<Altar> altar = Optional.empty();
public FloatingArtefactEntity(EntityType<?> entityType, World world) {
super(entityType, world);
positionSeed = (float)(Math.random() * Math.PI * 2);
health = getMaxHealth();
}
@Override
protected void initDataTracker() {
dataTracker.startTracking(ITEM, ItemStack.EMPTY);
dataTracker.startTracking(STATE, (byte)0);
dataTracker.startTracking(SPIN, 1F);
dataTracker.startTracking(TARGET_ROTATION_SPEED, 1F);
}
public void setAltar(Altar altar) {
@ -74,19 +79,29 @@ public class FloatingArtefactEntity extends Entity implements UDamageSources, Ma
dataTracker.set(STATE, (byte)state.ordinal());
}
public void addSpin(float spin, int duration) {
if (spin >= getSpin()) {
setSpin(spin);
spinupDuration = duration;
}
public void setRotationSpeed(float spin, int duration) {
dataTracker.set(TARGET_ROTATION_SPEED, Math.max(spin, 0));
boostDuration = duration;
}
public void setSpin(float spin) {
dataTracker.set(SPIN, spin);
public float getRotationSpeed() {
return dataTracker.get(TARGET_ROTATION_SPEED);
}
public float getSpin() {
return dataTracker.get(SPIN);
public float getRotationSpeed(float tickDelta) {
return MathHelper.lerp(tickDelta, prevRotationSpeed, rotationSpeed);
}
public int getMaxHealth() {
return 20;
}
public void setHealth(float health) {
this.health = MathHelper.clamp(health, 0, getMaxHealth());
}
public float getHealth() {
return health;
}
@Override
@ -104,36 +119,33 @@ public class FloatingArtefactEntity extends Entity implements UDamageSources, Ma
}
if (getWorld().isClient) {
float spin = getSpin();
if (Math.abs(spin - targetSpin) > 1.0E-5F) {
spinChange = spin - targetSpin;
targetSpin = spin;
spinChangeProgress = 0;
}
if (spinChange != 0) {
if (spinChangeProgress < 1) {
spinChangeProgress += 0.05F;
} else {
sourceSpin = targetSpin;
spinChange = 0;
spinChangeProgress = 0;
}
}
spinAmount += sourceSpin + (spinChange * spinChangeProgress);
bobAmount++;
}
float targetRotationSpeed = getRotationSpeed();
if (rotationSpeed != targetRotationSpeed) {
float difference = targetRotationSpeed - rotationSpeed;
rotationSpeed = Math.abs(difference) < 0.02F ? targetRotationSpeed : (rotationSpeed + difference * (difference > 0 ? 0.5F : 0.1F));
} else {
spinupDuration = Math.max(0, spinupDuration - 1);
if (spinupDuration <= 0) {
setSpin(1);
if (boostDuration > 0 && --boostDuration <= 0) {
setRotationSpeed(1, 0);
}
}
rotation %= 360;
prevRotation = rotation;
rotation += rotationSpeed;
if (stack.getItem() instanceof Artifact) {
((Artifact)stack.getItem()).onArtifactTick(this);
}
if (getHealth() < getMaxHealth() && --ticksUntilRegen <= 0) {
setHealth(getHealth() + 1);
ticksUntilRegen = REGEN_GAP_TICKS;
}
if (getWorld().getTime() % 80 == 0) {
State state = getState();
playSound(USounds.ENTITY_ARTEFACT_AMBIENT, state.getVolume(), state.getPitch());
@ -145,14 +157,16 @@ public class FloatingArtefactEntity extends Entity implements UDamageSources, Ma
}
public float getRotation(float tickDelta) {
return (spinAmount + tickDelta) / 20 + positionSeed;
return MathHelper.lerp(tickDelta, prevRotation, rotation) % 360;
}
@Override
protected void readCustomDataFromNbt(NbtCompound compound) {
setStack(ItemStack.fromNbt(compound.getCompound("Item")));
setState(State.valueOf(compound.getInt("State")));
setSpin(compound.getFloat("spin"));
setRotationSpeed(compound.getFloat("spin"), compound.getInt("spinDuration"));
setHealth(compound.getFloat("health"));
ticksUntilRegen = compound.getInt("regen");
altar = Altar.SERIALIZER.readOptional("altar", compound);
}
@ -163,7 +177,10 @@ public class FloatingArtefactEntity extends Entity implements UDamageSources, Ma
compound.put("Item", stack.writeNbt(new NbtCompound()));
}
compound.putInt("State", getState().ordinal());
compound.putFloat("spin", getSpin());
compound.putFloat("spin", getRotationSpeed());
compound.putInt("spinDuration", boostDuration);
compound.putFloat("health", getHealth());
compound.putInt("regen", ticksUntilRegen);
Altar.SERIALIZER.writeOptional("altar", compound, altar);
}
@ -173,9 +190,13 @@ public class FloatingArtefactEntity extends Entity implements UDamageSources, Ma
return false;
}
scheduleVelocityUpdate();
if (damageSource.isSourceCreativePlayer()) {
health = 0;
} else {
health -= amount;
ticksUntilRegen = REGEN_PAUSE_TICKS;
}
health -= amount;
if (health <= 0) {
remove(RemovalReason.KILLED);
@ -183,9 +204,13 @@ public class FloatingArtefactEntity extends Entity implements UDamageSources, Ma
if (altar.isEmpty()) {
if (!(stack.getItem() instanceof Artifact) || ((Artifact)stack.getItem()).onArtifactDestroyed(this) != ActionResult.SUCCESS) {
dropStack(stack);
if (!damageSource.isSourceCreativePlayer()) {
dropStack(stack);
}
}
}
} else {
playSound(USounds.ITEM_ICARUS_WINGS_WARN, 1, 1);
}
return false;

View file

@ -95,16 +95,15 @@ public class CrystalHeartItem extends Item implements FloatingArtefactEntity.Art
public void onArtifactTick(FloatingArtefactEntity entity) {
if (entity.getState() == State.INITIALISING) {
if (findStructure(entity)) {
if (entity.age % 30 == 0 && findStructure(entity)) {
entity.setState(State.RUNNING);
entity.setRotationSpeed(4, 30);
}
} else {
if (!findStructure(entity)) {
if (entity.age % 30 == 0 && !findStructure(entity)) {
entity.setState(State.INITIALISING);
}
entity.addSpin(2, 10);
BlockPos pos = entity.getBlockPos();
entity.getWorld().addParticle(ParticleTypes.COMPOSTER,
pos.getX() + entity.getWorld().getRandom().nextFloat(),
@ -112,7 +111,7 @@ public class CrystalHeartItem extends Item implements FloatingArtefactEntity.Art
pos.getZ() + entity.getWorld().getRandom().nextFloat(),
0, 0, 0);
if (entity.getWorld().getTime() % 80 == 0 && !entity.getWorld().isClient) {
if (entity.age % 80 == 0 && !entity.getWorld().isClient) {
List<LivingEntity> inputs = new ArrayList<>();
List<LivingEntity> outputs = new ArrayList<>();
List<ItemEntity> containers = new ArrayList<>();
@ -168,7 +167,9 @@ public class CrystalHeartItem extends Item implements FloatingArtefactEntity.Art
container.setStack(fill(container.getStack()));
});
entity.addSpin(gives > 0 ? 20 : 10, 30);
if (gives > 0) {
entity.setRotationSpeed(37, 80);
}
}
}

View file

@ -144,20 +144,11 @@ public record Altar(BlockPos origin, Set<BlockPos> pillars) {
public void generateDecorations(World world) {
world.setBlockState(origin, Blocks.SOUL_FIRE.getDefaultState(), Block.FORCE_STATE | Block.NOTIFY_ALL);
pillars.forEach(pillar -> {
/*
if (world.random.nextInt(3) == 0) {
world.setBlockState(pillar, Blocks.CRYING_OBSIDIAN.getDefaultState());
} else if (world.random.nextInt(3) == 0) {
world.setBlockState(pillar.down(), Blocks.CRYING_OBSIDIAN.getDefaultState());
}
*/
FloatingArtefactEntity artefact = UEntities.FLOATING_ARTEFACT.create(world);
artefact.setStack(UItems.ALICORN_BADGE.getDefaultStack());
artefact.setPosition(pillar.up().toCenterPos());
artefact.setInvulnerable(true);
artefact.setAltar(this);
artefact.addSpin(2, 9000);
removeExisting(null, world, pillar);
world.spawnEntity(artefact);
});