Unicopia/src/main/java/com/minelittlepony/unicopia/player/PlayerGravityDelegate.java

193 lines
6.3 KiB
Java
Raw Normal View History

2018-09-12 01:29:49 +02:00
package com.minelittlepony.unicopia.player;
import com.minelittlepony.unicopia.InbtSerialisable;
import com.minelittlepony.unicopia.USounds;
import com.minelittlepony.unicopia.mixin.MixinEntity;
2018-09-12 01:29:49 +02:00
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
2018-09-25 23:30:21 +02:00
import net.minecraft.entity.Entity;
2018-09-12 01:29:49 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
2018-09-12 01:29:49 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
2018-09-12 01:29:49 +02:00
class PlayerGravityDelegate implements IUpdatable<EntityPlayer>, IGravity, InbtSerialisable {
2018-09-12 01:29:49 +02:00
private final IPlayer player;
2018-09-24 21:38:21 +02:00
private static final float MAXIMUM_FLIGHT_EXPERIENCE = 500;
public int ticksInAir = 0;
public float flightExperience = 0;
2018-09-12 01:29:49 +02:00
public boolean isFlying = false;
public PlayerGravityDelegate(IPlayer player) {
this.player = player;
}
@Override
public void onUpdate(EntityPlayer entity) {
entity.capabilities.allowFlying = entity.capabilities.isCreativeMode || player.getPlayerSpecies().canFly();
2018-09-24 21:38:21 +02:00
if (!entity.capabilities.isCreativeMode) {
entity.capabilities.isFlying |= entity.capabilities.allowFlying && isFlying && !entity.onGround;
}
isFlying = entity.capabilities.isFlying && !entity.capabilities.isCreativeMode;
boolean rainboom = Math.sqrt(getHorizontalMotion(entity)) > 0.4F;
if (isFlying && rainboom) {
MixinEntity.setSize(entity, entity.width, 0.5F);
entity.eyeHeight = entity.height / 2;
} else {
entity.eyeHeight = entity.getDefaultEyeHeight();
}
if (!entity.capabilities.isCreativeMode && !entity.isElytraFlying()) {
2018-09-24 21:38:21 +02:00
if (isFlying && !entity.isRiding()) {
entity.fallDistance = 0;
float exhaustion = (0.2F * ticksInAir++) / 90;
if (entity.isSprinting()) {
exhaustion *= 3.11F;
2018-09-12 01:29:49 +02:00
}
exhaustion *= (1 - flightExperience/MAXIMUM_FLIGHT_EXPERIENCE);
entity.addExhaustion(exhaustion);
if (ticksInAir >= MAXIMUM_FLIGHT_EXPERIENCE) {
ticksInAir = 0;
addFlightExperience(entity);
2018-09-24 21:38:21 +02:00
entity.playSound(SoundEvents.ENTITY_GUARDIAN_FLOP, 1, 1);
}
2018-09-24 21:38:21 +02:00
float forward = 0.00015F * flightExperience;
entity.motionX += - forward * MathHelper.sin(entity.rotationYaw * 0.017453292F);
2018-09-25 23:30:21 +02:00
entity.motionY -= 0.05F - getHorizontalMotion(entity) / 100;
2018-09-24 21:38:21 +02:00
entity.motionZ += forward * MathHelper.cos(entity.rotationYaw * 0.017453292F);
if (ticksInAir > 0 && ticksInAir % 12 == 0) {
entity.playSound(USounds.WING_FLAP, 0.1F, 1);
}
} else {
if (ticksInAir != 0) {
entity.playSound(USounds.WING_FLAP, 0.04F, 1);
}
ticksInAir = 0;
flightExperience *= 0.9991342;
2018-09-12 01:29:49 +02:00
}
}
}
2018-09-12 01:29:49 +02:00
public void landHard(EntityPlayer player, float distance, float damageMultiplier) {
if (distance <= 0) {
return;
2018-09-12 01:29:49 +02:00
}
PotionEffect potioneffect = player.getActivePotionEffect(MobEffects.JUMP_BOOST);
float potion = potioneffect != null ? potioneffect.getAmplifier() + 1 : 0;
int i = MathHelper.ceil((distance - 8.0F - potion) * damageMultiplier);
if (i > 0) {
int j = MathHelper.floor(player.posX);
int k = MathHelper.floor(player.posY - 0.20000000298023224D);
int l = MathHelper.floor(player.posZ);
BlockPos pos = new BlockPos(j, k, l);
IBlockState state = player.world.getBlockState(pos);
Block block = state.getBlock();
if (state.getMaterial() != Material.AIR) {
player.playSound(getFallSound(i), 1, 1);
player.attackEntityFrom(DamageSource.FALL, i);
SoundType soundtype = block.getSoundType(state, player.getEntityWorld(), pos, player);
player.playSound(soundtype.getStepSound(), soundtype.getVolume() * 0.5f, soundtype.getPitch() * 0.75f);
}
}
}
2018-09-25 23:30:21 +02:00
protected double getHorizontalMotion(Entity e) {
return (e.motionX * e.motionX)
+ (e.motionZ * e.motionZ);
}
protected SoundEvent getFallSound(int distance) {
return distance > 4 ? SoundEvents.ENTITY_PLAYER_BIG_FALL : SoundEvents.ENTITY_PLAYER_SMALL_FALL;
}
private void addFlightExperience(EntityPlayer entity) {
entity.addExperience(1);
2018-09-24 21:38:21 +02:00
flightExperience += (MAXIMUM_FLIGHT_EXPERIENCE - flightExperience) / 20;
2018-09-12 01:29:49 +02:00
}
public void updateFlightStat(EntityPlayer entity, boolean flying) {
if (!entity.capabilities.isCreativeMode) {
entity.capabilities.allowFlying = player.getPlayerSpecies().canFly();
if (entity.capabilities.allowFlying) {
entity.capabilities.isFlying |= flying;
isFlying = entity.capabilities.isFlying;
if (isFlying) {
ticksInAir = 0;
2018-09-12 01:29:49 +02:00
}
} else {
entity.capabilities.isFlying = false;
isFlying = false;
}
}
}
@Override
public void writeToNBT(NBTTagCompound compound) {
compound.setInteger("flightDuration", ticksInAir);
compound.setFloat("flightExperience", flightExperience);
2018-09-12 01:29:49 +02:00
compound.setBoolean("isFlying", isFlying);
}
@Override
public void readFromNBT(NBTTagCompound compound) {
ticksInAir = compound.getInteger("flightDuration");
flightExperience = compound.getFloat("flightExperience");
2018-09-12 01:29:49 +02:00
isFlying = compound.getBoolean("isFlying");
}
@Override
public boolean isFlying() {
return isFlying;
}
@Override
public float getFlightExperience() {
return flightExperience / MAXIMUM_FLIGHT_EXPERIENCE;
}
@Override
public float getFlightDuration() {
return ticksInAir / MAXIMUM_FLIGHT_EXPERIENCE;
}
2018-09-12 01:29:49 +02:00
}