Search in sources :

Example 1 with EntityIronGolem

use of net.minecraft.entity.monster.EntityIronGolem in project Railcraft by Railcraft.

the class MinecartHooks method onEntityCollision.

@Override
public void onEntityCollision(EntityMinecart cart, Entity other) {
    if (Game.isClient(cart.worldObj) || cart.isPassenger(other) || !other.isEntityAlive() || !cart.isEntityAlive())
        return;
    ILinkageManager lm = LinkageManager.instance();
    EntityMinecart link = lm.getLinkedCartA(cart);
    if (link != null && (link == other || link.isPassenger(other)))
        return;
    link = lm.getLinkedCartB(cart);
    if (link != null && (link == other || link.isPassenger(other)))
        return;
    boolean isLiving = other instanceof EntityLivingBase;
    boolean isPlayer = other instanceof EntityPlayer;
    //TODO: needs more thought in regards to passenger handling
    if (isLiving && !isPlayer && cart.canBeRidden() && !(other instanceof EntityIronGolem) && cart.motionX * cart.motionX + cart.motionZ * cart.motionZ > 0.001D && !cart.isBeingRidden() && !other.isRiding()) {
        int mountPrevention = cart.getEntityData().getInteger("MountPrevention");
        if (mountPrevention <= 0)
            other.startRiding(cart);
    }
    if (isLiving && WorldPlugin.isBlockAt(cart.worldObj, cart.getPosition(), RailcraftBlocks.TRACK_ELEVATOR.block()))
        return;
    //        System.out.println(cart.getClass().getSimpleName() + ": " + cart.entityId + " collided with " + other.getClass().getSimpleName() + ": " + other.entityId);
    Vec2D cartPos = new Vec2D(cart.posX, cart.posZ);
    Vec2D otherPos = new Vec2D(other.posX, other.posZ);
    Vec2D unit = Vec2D.subtract(otherPos, cartPos);
    unit.normalize();
    double distance = cart.getDistanceToEntity(other);
    double depth = distance - OPTIMAL_DISTANCE;
    double forceX = 0;
    double forceZ = 0;
    if (depth < 0) {
        double spring = isPlayer ? COEF_SPRING_PLAYER : COEF_SPRING;
        double penaltyX = spring * depth * unit.getX();
        double penaltyZ = spring * depth * unit.getY();
        forceX += penaltyX;
        forceZ += penaltyZ;
        if (!isPlayer) {
            double impulseX = unit.getX();
            double impulseZ = unit.getY();
            impulseX *= -(1.0 + COEF_RESTITUTION);
            impulseZ *= -(1.0 + COEF_RESTITUTION);
            Vec2D cartVel = new Vec2D(cart.motionX, cart.motionZ);
            Vec2D otherVel = new Vec2D(other.motionX, other.motionZ);
            double dot = Vec2D.subtract(otherVel, cartVel).dotProduct(unit);
            impulseX *= dot;
            impulseZ *= dot;
            impulseX *= 0.5;
            impulseZ *= 0.5;
            forceX -= impulseX;
            forceZ -= impulseZ;
        }
    }
    if (other instanceof EntityMinecart) {
        EntityMinecart otherCart = (EntityMinecart) other;
        if (!cart.isPoweredCart() || otherCart.isPoweredCart())
            if (!TrackToolsAPI.isCartLockedDown(cart))
                cart.addVelocity(forceX, 0, forceZ);
        if (!otherCart.isPoweredCart() || cart.isPoweredCart())
            if (!TrackToolsAPI.isCartLockedDown(otherCart))
                other.addVelocity(-forceX, 0, -forceZ);
    } else {
        //            if(isPlayer) {
        //                forceX += Math.abs(cart.motionX - other.motionX) / 2;
        //                forceZ += Math.abs(cart.motionZ - other.motionZ) / 2;
        //            }
        //            System.out.printf("forceX=%f, forceZ=%f%n", forceX, forceZ);
        Vec2D cartVel = new Vec2D(cart.motionX + forceX, cart.motionZ + forceZ);
        Vec2D otherVel = new Vec2D(other.motionX - forceX, other.motionZ - forceZ);
        double dot = Vec2D.subtract(otherVel, cartVel).dotProduct(unit);
        double dampX = COEF_DAMPING * dot * unit.getX();
        double dampZ = COEF_DAMPING * dot * unit.getY();
        forceX += dampX;
        forceZ += dampZ;
        //            System.out.printf("dampX=%f, dampZ=%f%n", dampX, dampZ);
        if (!isPlayer)
            other.addVelocity(-forceX, 0.0D, -forceZ);
        if (!TrackToolsAPI.isCartLockedDown(cart))
            cart.addVelocity(forceX, 0, forceZ);
    }
}
Also used : EntityIronGolem(net.minecraft.entity.monster.EntityIronGolem) ILinkageManager(mods.railcraft.api.carts.ILinkageManager) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityMinecart(net.minecraft.entity.item.EntityMinecart)

Aggregations

ILinkageManager (mods.railcraft.api.carts.ILinkageManager)1 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityMinecart (net.minecraft.entity.item.EntityMinecart)1 EntityIronGolem (net.minecraft.entity.monster.EntityIronGolem)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1