use of cn.nukkit.entity.EntityLiving in project Nukkit by Nukkit.
the class EntityMinecartAbstract method applyEntityCollision.
@Override
public void applyEntityCollision(Entity entity) {
if (entity != riding) {
if (entity instanceof EntityLiving && !(entity instanceof EntityHuman) && motionX * motionX + motionZ * motionZ > 0.01D && linkedEntity == null && entity.riding == null && blockInside == null) {
if (riding == null && devs) {
// TODO: rewrite (weird riding)
mountEntity(entity);
}
}
double motiveX = entity.x - x;
double motiveZ = entity.z - z;
double square = motiveX * motiveX + motiveZ * motiveZ;
if (square >= 9.999999747378752E-5D) {
square = Math.sqrt(square);
motiveX /= square;
motiveZ /= square;
double next = 1 / square;
if (next > 1) {
next = 1;
}
motiveX *= next;
motiveZ *= next;
motiveX *= 0.10000000149011612D;
motiveZ *= 0.10000000149011612D;
motiveX *= 1 + entityCollisionReduction;
motiveZ *= 1 + entityCollisionReduction;
motiveX *= 0.5D;
motiveZ *= 0.5D;
if (entity instanceof EntityMinecartAbstract) {
EntityMinecartAbstract mine = (EntityMinecartAbstract) entity;
double desinityX = mine.x - x;
double desinityZ = mine.z - z;
Vector3 vector = new Vector3(desinityX, 0, desinityZ).normalize();
Vector3 vec = new Vector3((double) MathHelper.cos((float) yaw * 0.017453292F), 0, (double) MathHelper.sin((float) yaw * 0.017453292F)).normalize();
double desinityXZ = Math.abs(vector.dot(vec));
if (desinityXZ < 0.800000011920929D) {
return;
}
double motX = mine.motionX + motionX;
double motZ = mine.motionZ + motionZ;
if (mine.getType().getId() == 2 && getType().getId() != 2) {
motionX *= 0.20000000298023224D;
motionZ *= 0.20000000298023224D;
motionX += mine.motionX - motiveX;
motionZ += mine.motionZ - motiveZ;
mine.motionX *= 0.949999988079071D;
mine.motionZ *= 0.949999988079071D;
} else if (mine.getType().getId() != 2 && getType().getId() == 2) {
mine.motionX *= 0.20000000298023224D;
mine.motionZ *= 0.20000000298023224D;
motionX += mine.motionX + motiveX;
motionZ += mine.motionZ + motiveZ;
motionX *= 0.949999988079071D;
motionZ *= 0.949999988079071D;
} else {
motX /= 2;
motZ /= 2;
motionX *= 0.20000000298023224D;
motionZ *= 0.20000000298023224D;
motionX += motX - motiveX;
motionZ += motZ - motiveZ;
mine.motionX *= 0.20000000298023224D;
mine.motionZ *= 0.20000000298023224D;
mine.motionX += motX + motiveX;
mine.motionZ += motZ + motiveZ;
}
} else {
motionX -= motiveX;
motionZ -= motiveZ;
}
}
}
}
use of cn.nukkit.entity.EntityLiving in project Nukkit by Nukkit.
the class Potion method applyPotion.
public void applyPotion(Entity entity, double health) {
if (!(entity instanceof EntityLiving)) {
return;
}
Effect applyEffect = getEffect(this.getId(), this.isSplash());
if (applyEffect == null) {
return;
}
if (entity instanceof Player) {
if (!((Player) entity).isSurvival() && applyEffect.isBad()) {
return;
}
}
PotionApplyEvent event = new PotionApplyEvent(this, applyEffect, entity);
entity.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
applyEffect = event.getApplyEffect();
switch(this.getId()) {
case INSTANT_HEALTH:
case INSTANT_HEALTH_II:
entity.heal(new EntityRegainHealthEvent(entity, (float) (health * (double) (4 << (applyEffect.getAmplifier() + 1))), EntityRegainHealthEvent.CAUSE_EATING));
break;
case HARMING:
case HARMING_II:
entity.attack(new EntityDamageEvent(entity, DamageCause.MAGIC, (float) (health * (double) (6 << (applyEffect.getAmplifier() + 1)))));
break;
default:
int duration = (int) ((isSplash() ? health : 1) * (double) applyEffect.getDuration() + 0.5);
applyEffect.setDuration(duration);
entity.addEffect(applyEffect);
}
}
use of cn.nukkit.entity.EntityLiving in project Nukkit by Nukkit.
the class EntityMinecartAbstract method processMovement.
private void processMovement(int dx, int dy, int dz, BlockRail block) {
fallDistance = 0.0F;
Vector3 vector = getNextRail(x, y, z);
y = (double) dy;
boolean isPowered = false;
boolean isSlowed = false;
if (block instanceof BlockRailPowered) {
isPowered = block.isActive();
isSlowed = !block.isActive();
}
switch(Orientation.byMetadata(block.getRealMeta())) {
case ASCENDING_NORTH:
motionX -= 0.0078125D;
y += 1;
break;
case ASCENDING_SOUTH:
motionX += 0.0078125D;
y += 1;
break;
case ASCENDING_EAST:
motionZ += 0.0078125D;
y += 1;
break;
case ASCENDING_WEST:
motionZ -= 0.0078125D;
y += 1;
break;
}
int[][] facing = matrix[block.getRealMeta()];
double facing1 = (double) (facing[1][0] - facing[0][0]);
double facing2 = (double) (facing[1][2] - facing[0][2]);
double speedOnTurns = Math.sqrt(facing1 * facing1 + facing2 * facing2);
double realFacing = motionX * facing1 + motionZ * facing2;
if (realFacing < 0) {
facing1 = -facing1;
facing2 = -facing2;
}
double squareOfFame = Math.sqrt(motionX * motionX + motionZ * motionZ);
if (squareOfFame > 2) {
squareOfFame = 2;
}
motionX = squareOfFame * facing1 / speedOnTurns;
motionZ = squareOfFame * facing2 / speedOnTurns;
double expectedSpeed;
// PlayerYawNegative
double playerYawNeg;
// PlayerYawPositive
double playerYawPos;
double motion;
if (linkedEntity != null && linkedEntity instanceof EntityLiving) {
expectedSpeed = currentSpeed;
if (expectedSpeed > 0) {
// This is a trajectory (Angle of elevation)
playerYawNeg = -Math.sin(linkedEntity.yaw * 3.1415927F / 180.0F);
playerYawPos = Math.cos(linkedEntity.yaw * 3.1415927F / 180.0F);
motion = motionX * motionX + motionZ * motionZ;
if (motion < 0.01D) {
motionX += playerYawNeg * 0.1D;
motionZ += playerYawPos * 0.1D;
isSlowed = false;
}
}
}
// http://minecraft.gamepedia.com/Powered_Rail#Rail
if (isSlowed) {
expectedSpeed = Math.sqrt(motionX * motionX + motionZ * motionZ);
if (expectedSpeed < 0.03D) {
motionX *= 0;
motionY *= 0;
motionZ *= 0;
} else {
motionX *= 0.5D;
motionY *= 0;
motionZ *= 0.5D;
}
}
playerYawNeg = (double) dx + 0.5D + (double) facing[0][0] * 0.5D;
playerYawPos = (double) dz + 0.5D + (double) facing[0][2] * 0.5D;
motion = (double) dx + 0.5D + (double) facing[1][0] * 0.5D;
double wallOfFame = (double) dz + 0.5D + (double) facing[1][2] * 0.5D;
facing1 = motion - playerYawNeg;
facing2 = wallOfFame - playerYawPos;
double motX;
double motZ;
if (facing1 == 0) {
x = (double) dx + 0.5D;
expectedSpeed = z - (double) dz;
} else if (facing2 == 0) {
z = (double) dz + 0.5D;
expectedSpeed = x - (double) dx;
} else {
motX = x - playerYawNeg;
motZ = z - playerYawPos;
expectedSpeed = (motX * facing1 + motZ * facing2) * 2;
}
x = playerYawNeg + facing1 * expectedSpeed;
z = playerYawPos + facing2 * expectedSpeed;
// Hehe, my minstake :3
setPosition(new Vector3(x, y, z));
motX = motionX;
motZ = motionZ;
if (linkedEntity != null) {
motX *= 0.75D;
motZ *= 0.75D;
}
motX = NukkitMath.clamp(motX, -getMaxSpeed(), getMaxSpeed());
motZ = NukkitMath.clamp(motZ, -getMaxSpeed(), getMaxSpeed());
move(motX, 0, motZ);
if (facing[0][1] != 0 && MathHelper.floor(x) - dx == facing[0][0] && MathHelper.floor(z) - dz == facing[0][2]) {
setPosition(new Vector3(x, y + (double) facing[0][1], z));
} else if (facing[1][1] != 0 && MathHelper.floor(x) - dx == facing[1][0] && MathHelper.floor(z) - dz == facing[1][2]) {
setPosition(new Vector3(x, y + (double) facing[1][1], z));
}
applyDrag();
Vector3 vector1 = getNextRail(x, y, z);
if (vector1 != null && vector != null) {
double d14 = (vector.y - vector1.y) * 0.05D;
squareOfFame = Math.sqrt(motionX * motionX + motionZ * motionZ);
if (squareOfFame > 0) {
motionX = motionX / squareOfFame * (squareOfFame + d14);
motionZ = motionZ / squareOfFame * (squareOfFame + d14);
}
setPosition(new Vector3(x, vector1.y, z));
}
int floorX = MathHelper.floor(x);
int floorZ = MathHelper.floor(z);
if (floorX != dx || floorZ != dz) {
squareOfFame = Math.sqrt(motionX * motionX + motionZ * motionZ);
motionX = squareOfFame * (double) (floorX - dx);
motionZ = squareOfFame * (double) (floorZ - dz);
}
if (isPowered) {
double newMovie = Math.sqrt(motionX * motionX + motionZ * motionZ);
if (newMovie > 0.01D) {
double nextMovie = 0.06D;
motionX += motionX / newMovie * nextMovie;
motionZ += motionZ / newMovie * nextMovie;
} else if (block.getOrientation() == Orientation.STRAIGHT_NORTH_SOUTH) {
if (level.getBlock(new Vector3(dx - 1, dy, dz)).isNormalBlock()) {
motionX = 0.02D;
} else if (level.getBlock(new Vector3(dx + 1, dy, dz)).isNormalBlock()) {
motionX = -0.02D;
}
} else if (block.getOrientation() == Orientation.STRAIGHT_EAST_WEST) {
if (level.getBlock(new Vector3(dx, dy, dz - 1)).isNormalBlock()) {
motionZ = 0.02D;
} else if (level.getBlock(new Vector3(dx, dy, dz + 1)).isNormalBlock()) {
motionZ = -0.02D;
}
}
}
}
Aggregations