BloodMagic/src/main/java/WayofTime/bloodmagic/tile/TileDemonCrystal.java

200 lines
6.6 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.tile;
import WayofTime.bloodmagic.tile.base.TileTicking;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.MathHelper;
import WayofTime.bloodmagic.api.soul.DemonWillHolder;
import WayofTime.bloodmagic.api.soul.EnumDemonWillType;
import WayofTime.bloodmagic.block.BlockDemonCrystal;
import WayofTime.bloodmagic.demonAura.WorldDemonWillHandler;
public class TileDemonCrystal extends TileTicking
{
public DemonWillHolder holder = new DemonWillHolder();
public final int maxWill = 100;
public final double drainRate = 1;
public static final double sameWillConversionRate = 50;
public static final double defaultWillConversionRate = 100;
public static final double timeDelayForWrongWill = 0.6;
2016-02-27 03:11:28 +00:00
public double progressToNextCrystal = 0;
public int internalCounter = 0;
public int crystalCount = 1;
public EnumFacing placement = EnumFacing.UP; //Side that this crystal is placed on.
public TileDemonCrystal()
{
this.crystalCount = 1;
}
@Override
public void onUpdate()
{
2016-12-13 03:56:36 +00:00
if (getWorld().isRemote)
{
return;
}
2016-02-27 03:11:28 +00:00
internalCounter++;
if (internalCounter % 20 == 0 && crystalCount < 7)
{
2016-02-27 03:11:28 +00:00
EnumDemonWillType type = EnumDemonWillType.values()[this.getBlockMetadata()];
2016-12-13 03:56:36 +00:00
double value = WorldDemonWillHandler.getCurrentWill(getWorld(), pos, type);
2016-02-27 03:11:28 +00:00
if (type != EnumDemonWillType.DEFAULT)
{
if (value >= 100)
{
double nextProgress = getCrystalGrowthPerSecond(value);
2016-12-13 03:56:36 +00:00
progressToNextCrystal += WorldDemonWillHandler.drainWill(getWorld(), getPos(), type, nextProgress * sameWillConversionRate, true) / sameWillConversionRate;
2016-02-27 03:11:28 +00:00
} else
{
2016-12-13 03:56:36 +00:00
value = WorldDemonWillHandler.getCurrentWill(getWorld(), pos, EnumDemonWillType.DEFAULT);
2016-02-27 03:11:28 +00:00
if (value > 0.5)
{
double nextProgress = getCrystalGrowthPerSecond(value) * timeDelayForWrongWill;
2016-12-13 03:56:36 +00:00
progressToNextCrystal += WorldDemonWillHandler.drainWill(getWorld(), getPos(), EnumDemonWillType.DEFAULT, nextProgress * defaultWillConversionRate, true) / defaultWillConversionRate;
2016-02-27 03:11:28 +00:00
}
}
} else
{
if (value > 0.5)
{
double nextProgress = getCrystalGrowthPerSecond(value);
2016-12-13 03:56:36 +00:00
progressToNextCrystal += WorldDemonWillHandler.drainWill(getWorld(), getPos(), type, nextProgress * sameWillConversionRate, true) / sameWillConversionRate;
2016-02-27 03:11:28 +00:00
}
}
checkAndGrowCrystal();
}
2016-02-27 03:11:28 +00:00
2016-12-13 03:56:36 +00:00
// if (getWorld().getWorldTime() % 200 == 0)
2016-02-27 03:11:28 +00:00
// {
// crystalCount = Math.min(crystalCount + 1, 7);
2016-12-13 03:56:36 +00:00
// getWorld().markBlockForUpdate(pos);
2016-02-27 03:11:28 +00:00
// }
}
/**
* Encourages the crystal to grow by a large percentage by telling it to
* drain will from the aura.
*
* @param willDrain
* The amount of drain that is needed for the crystal to grow
* successfully for the desired amount. Can be more than the base
* amount.
* @param progressPercentage
* @return percentage actually grown.
*/
public double growCrystalWithWillAmount(double willDrain, double progressPercentage)
{
if (crystalCount >= 7)
{
return 0;
}
2016-12-13 03:56:36 +00:00
IBlockState state = getWorld().getBlockState(pos);
2016-06-07 14:15:40 +00:00
int meta = this.getBlockType().getMetaFromState(state);
EnumDemonWillType type = EnumDemonWillType.values()[meta];
2016-12-13 03:56:36 +00:00
double value = WorldDemonWillHandler.getCurrentWill(getWorld(), pos, type);
double percentDrain = willDrain <= 0 ? 1 : Math.min(1, value / willDrain);
if (percentDrain <= 0)
{
return 0;
}
// Verification that you can actually drain the will from this chunk, for future proofing.
2016-12-13 03:56:36 +00:00
WorldDemonWillHandler.drainWill(getWorld(), pos, type, percentDrain * willDrain, true);
progressToNextCrystal += percentDrain * progressPercentage;
checkAndGrowCrystal();
return percentDrain * progressPercentage;
}
public void checkAndGrowCrystal()
{
if (progressToNextCrystal >= 1)
{
progressToNextCrystal--;
crystalCount++;
2016-12-13 03:56:36 +00:00
IBlockState thisState = getWorld().getBlockState(pos);
getWorld().notifyBlockUpdate(pos, thisState, thisState, 3);
markDirty();
}
}
public double getMaxWillForCrystal()
{
return 50;
}
public boolean dropSingleCrystal()
{
2016-12-13 03:56:36 +00:00
if (!getWorld().isRemote && crystalCount > 1)
{
2016-12-13 03:56:36 +00:00
IBlockState state = getWorld().getBlockState(pos);
EnumDemonWillType type = state.getValue(BlockDemonCrystal.TYPE);
ItemStack stack = BlockDemonCrystal.getItemStackDropped(type, 1);
if (stack != null)
{
crystalCount--;
2016-12-13 03:56:36 +00:00
getWorld().spawnEntity(new EntityItem(getWorld(), pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, stack));
return true;
}
}
return false;
}
2016-02-27 03:11:28 +00:00
public double getCrystalGrowthPerSecond(double will)
{
return 1.0 / 800 * Math.sqrt(will / 200);
}
public int getCrystalCountForRender()
{
2016-12-13 03:56:36 +00:00
return MathHelper.clamp(crystalCount - 1, 0, 6);
}
@Override
public void deserialize(NBTTagCompound tag)
{
holder.readFromNBT(tag, "Will");
crystalCount = tag.getInteger("crystalCount");
placement = EnumFacing.getFront(tag.getInteger("placement"));
2016-02-27 03:11:28 +00:00
progressToNextCrystal = tag.getDouble("progress");
}
@Override
public NBTTagCompound serialize(NBTTagCompound tag)
{
holder.writeToNBT(tag, "Will");
tag.setInteger("crystalCount", crystalCount);
tag.setInteger("placement", placement.getIndex());
2016-02-27 03:11:28 +00:00
tag.setDouble("progress", progressToNextCrystal);
return tag;
}
public int getCrystalCount() {
return crystalCount;
}
public void setCrystalCount(int crystalCount) {
this.crystalCount = crystalCount;
}
public EnumFacing getPlacement() {
return placement;
}
public void setPlacement(EnumFacing placement) {
this.placement = placement;
}
}