package fr.koka99cab.ibelievechickenfly.chickenboat;

import net.minecraft.world.entity.vehicle.boat.AbstractBoat;
import net.minecraft.world.phys.Vec3;

public final class ChickenBoatFlightPhysics {
	private static final double ASCEND_ACCELERATION = 0.07D;
	private static final double MIN_ASCEND_SPEED = 0.1D;
	private static final double MAX_ASCEND_SPEED = 0.24D;
	private static final double DESCEND_ACCELERATION = 0.025D;
	private static final double MAX_DESCEND_SPEED = -0.04D;

	private ChickenBoatFlightPhysics() {
	}

	public static void apply(AbstractBoat boat, boolean up) {
		Vec3 velocity = boat.getDeltaMovement();
		double yVelocity;

		if (up) {
			yVelocity = clamp(velocity.y + ASCEND_ACCELERATION, MIN_ASCEND_SPEED, MAX_ASCEND_SPEED);
		} else {
			yVelocity = glideVelocity(velocity.y);
		}

		boat.setDeltaMovement(velocity.x, yVelocity, velocity.z);
		boat.fallDistance = 0.0F;
		boat.push(0.0D, 0.0D, 0.0D);
	}

	private static double glideVelocity(double yVelocity) {
		return Math.max(yVelocity - DESCEND_ACCELERATION, MAX_DESCEND_SPEED);
	}

	private static double clamp(double value, double min, double max) {
		return Math.max(min, Math.min(max, value));
	}
}
