mirror of
https://github.com/Sollace/Unicopia.git
synced 2025-02-20 03:44:23 +01:00
Fixed crystal heart rotation
This commit is contained in:
parent
88252d8db9
commit
ccad91087c
3 changed files with 80 additions and 63 deletions
|
@ -21,37 +21,42 @@ import net.minecraft.util.math.Vec3d;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
public class FloatingArtefactEntity extends Entity implements UDamageSources, MagicImmune {
|
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<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<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 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;
|
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();
|
private Optional<Altar> altar = Optional.empty();
|
||||||
|
|
||||||
public FloatingArtefactEntity(EntityType<?> entityType, World world) {
|
public FloatingArtefactEntity(EntityType<?> entityType, World world) {
|
||||||
super(entityType, world);
|
super(entityType, world);
|
||||||
|
|
||||||
positionSeed = (float)(Math.random() * Math.PI * 2);
|
positionSeed = (float)(Math.random() * Math.PI * 2);
|
||||||
|
health = getMaxHealth();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initDataTracker() {
|
protected void initDataTracker() {
|
||||||
dataTracker.startTracking(ITEM, ItemStack.EMPTY);
|
dataTracker.startTracking(ITEM, ItemStack.EMPTY);
|
||||||
dataTracker.startTracking(STATE, (byte)0);
|
dataTracker.startTracking(STATE, (byte)0);
|
||||||
dataTracker.startTracking(SPIN, 1F);
|
dataTracker.startTracking(TARGET_ROTATION_SPEED, 1F);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAltar(Altar altar) {
|
public void setAltar(Altar altar) {
|
||||||
|
@ -74,19 +79,29 @@ public class FloatingArtefactEntity extends Entity implements UDamageSources, Ma
|
||||||
dataTracker.set(STATE, (byte)state.ordinal());
|
dataTracker.set(STATE, (byte)state.ordinal());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSpin(float spin, int duration) {
|
public void setRotationSpeed(float spin, int duration) {
|
||||||
if (spin >= getSpin()) {
|
dataTracker.set(TARGET_ROTATION_SPEED, Math.max(spin, 0));
|
||||||
setSpin(spin);
|
boostDuration = duration;
|
||||||
spinupDuration = duration;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSpin(float spin) {
|
public float getRotationSpeed() {
|
||||||
dataTracker.set(SPIN, spin);
|
return dataTracker.get(TARGET_ROTATION_SPEED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getSpin() {
|
public float getRotationSpeed(float tickDelta) {
|
||||||
return dataTracker.get(SPIN);
|
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
|
@Override
|
||||||
|
@ -104,36 +119,33 @@ public class FloatingArtefactEntity extends Entity implements UDamageSources, Ma
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getWorld().isClient) {
|
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++;
|
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 {
|
} else {
|
||||||
spinupDuration = Math.max(0, spinupDuration - 1);
|
if (boostDuration > 0 && --boostDuration <= 0) {
|
||||||
if (spinupDuration <= 0) {
|
setRotationSpeed(1, 0);
|
||||||
setSpin(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rotation %= 360;
|
||||||
|
prevRotation = rotation;
|
||||||
|
rotation += rotationSpeed;
|
||||||
|
|
||||||
if (stack.getItem() instanceof Artifact) {
|
if (stack.getItem() instanceof Artifact) {
|
||||||
((Artifact)stack.getItem()).onArtifactTick(this);
|
((Artifact)stack.getItem()).onArtifactTick(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (getHealth() < getMaxHealth() && --ticksUntilRegen <= 0) {
|
||||||
|
setHealth(getHealth() + 1);
|
||||||
|
ticksUntilRegen = REGEN_GAP_TICKS;
|
||||||
|
}
|
||||||
|
|
||||||
if (getWorld().getTime() % 80 == 0) {
|
if (getWorld().getTime() % 80 == 0) {
|
||||||
State state = getState();
|
State state = getState();
|
||||||
playSound(USounds.ENTITY_ARTEFACT_AMBIENT, state.getVolume(), state.getPitch());
|
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) {
|
public float getRotation(float tickDelta) {
|
||||||
return (spinAmount + tickDelta) / 20 + positionSeed;
|
return MathHelper.lerp(tickDelta, prevRotation, rotation) % 360;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void readCustomDataFromNbt(NbtCompound compound) {
|
protected void readCustomDataFromNbt(NbtCompound compound) {
|
||||||
setStack(ItemStack.fromNbt(compound.getCompound("Item")));
|
setStack(ItemStack.fromNbt(compound.getCompound("Item")));
|
||||||
setState(State.valueOf(compound.getInt("State")));
|
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);
|
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.put("Item", stack.writeNbt(new NbtCompound()));
|
||||||
}
|
}
|
||||||
compound.putInt("State", getState().ordinal());
|
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);
|
Altar.SERIALIZER.writeOptional("altar", compound, altar);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,9 +190,13 @@ public class FloatingArtefactEntity extends Entity implements UDamageSources, Ma
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduleVelocityUpdate();
|
if (damageSource.isSourceCreativePlayer()) {
|
||||||
|
health = 0;
|
||||||
|
} else {
|
||||||
|
health -= amount;
|
||||||
|
ticksUntilRegen = REGEN_PAUSE_TICKS;
|
||||||
|
}
|
||||||
|
|
||||||
health -= amount;
|
|
||||||
if (health <= 0) {
|
if (health <= 0) {
|
||||||
remove(RemovalReason.KILLED);
|
remove(RemovalReason.KILLED);
|
||||||
|
|
||||||
|
@ -183,9 +204,13 @@ public class FloatingArtefactEntity extends Entity implements UDamageSources, Ma
|
||||||
|
|
||||||
if (altar.isEmpty()) {
|
if (altar.isEmpty()) {
|
||||||
if (!(stack.getItem() instanceof Artifact) || ((Artifact)stack.getItem()).onArtifactDestroyed(this) != ActionResult.SUCCESS) {
|
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;
|
return false;
|
||||||
|
|
|
@ -95,16 +95,15 @@ public class CrystalHeartItem extends Item implements FloatingArtefactEntity.Art
|
||||||
public void onArtifactTick(FloatingArtefactEntity entity) {
|
public void onArtifactTick(FloatingArtefactEntity entity) {
|
||||||
|
|
||||||
if (entity.getState() == State.INITIALISING) {
|
if (entity.getState() == State.INITIALISING) {
|
||||||
if (findStructure(entity)) {
|
if (entity.age % 30 == 0 && findStructure(entity)) {
|
||||||
entity.setState(State.RUNNING);
|
entity.setState(State.RUNNING);
|
||||||
|
entity.setRotationSpeed(4, 30);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!findStructure(entity)) {
|
if (entity.age % 30 == 0 && !findStructure(entity)) {
|
||||||
entity.setState(State.INITIALISING);
|
entity.setState(State.INITIALISING);
|
||||||
}
|
}
|
||||||
|
|
||||||
entity.addSpin(2, 10);
|
|
||||||
|
|
||||||
BlockPos pos = entity.getBlockPos();
|
BlockPos pos = entity.getBlockPos();
|
||||||
entity.getWorld().addParticle(ParticleTypes.COMPOSTER,
|
entity.getWorld().addParticle(ParticleTypes.COMPOSTER,
|
||||||
pos.getX() + entity.getWorld().getRandom().nextFloat(),
|
pos.getX() + entity.getWorld().getRandom().nextFloat(),
|
||||||
|
@ -112,7 +111,7 @@ public class CrystalHeartItem extends Item implements FloatingArtefactEntity.Art
|
||||||
pos.getZ() + entity.getWorld().getRandom().nextFloat(),
|
pos.getZ() + entity.getWorld().getRandom().nextFloat(),
|
||||||
0, 0, 0);
|
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> inputs = new ArrayList<>();
|
||||||
List<LivingEntity> outputs = new ArrayList<>();
|
List<LivingEntity> outputs = new ArrayList<>();
|
||||||
List<ItemEntity> containers = new ArrayList<>();
|
List<ItemEntity> containers = new ArrayList<>();
|
||||||
|
@ -168,7 +167,9 @@ public class CrystalHeartItem extends Item implements FloatingArtefactEntity.Art
|
||||||
container.setStack(fill(container.getStack()));
|
container.setStack(fill(container.getStack()));
|
||||||
});
|
});
|
||||||
|
|
||||||
entity.addSpin(gives > 0 ? 20 : 10, 30);
|
if (gives > 0) {
|
||||||
|
entity.setRotationSpeed(37, 80);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -144,20 +144,11 @@ public record Altar(BlockPos origin, Set<BlockPos> pillars) {
|
||||||
public void generateDecorations(World world) {
|
public void generateDecorations(World world) {
|
||||||
world.setBlockState(origin, Blocks.SOUL_FIRE.getDefaultState(), Block.FORCE_STATE | Block.NOTIFY_ALL);
|
world.setBlockState(origin, Blocks.SOUL_FIRE.getDefaultState(), Block.FORCE_STATE | Block.NOTIFY_ALL);
|
||||||
pillars.forEach(pillar -> {
|
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);
|
FloatingArtefactEntity artefact = UEntities.FLOATING_ARTEFACT.create(world);
|
||||||
artefact.setStack(UItems.ALICORN_BADGE.getDefaultStack());
|
artefact.setStack(UItems.ALICORN_BADGE.getDefaultStack());
|
||||||
artefact.setPosition(pillar.up().toCenterPos());
|
artefact.setPosition(pillar.up().toCenterPos());
|
||||||
artefact.setInvulnerable(true);
|
artefact.setInvulnerable(true);
|
||||||
artefact.setAltar(this);
|
artefact.setAltar(this);
|
||||||
artefact.addSpin(2, 9000);
|
|
||||||
removeExisting(null, world, pillar);
|
removeExisting(null, world, pillar);
|
||||||
world.spawnEntity(artefact);
|
world.spawnEntity(artefact);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue