|
| 1 | +package com.lambda.util.combat |
| 2 | + |
| 3 | +import com.lambda.context.SafeContext |
| 4 | +import com.lambda.util.combat.DamageUtils.applyProtection |
| 5 | +import com.lambda.util.math.VecUtils.minus |
| 6 | +import com.lambda.util.math.VecUtils.times |
| 7 | +import net.minecraft.enchantment.EnchantmentHelper |
| 8 | +import net.minecraft.enchantment.ProtectionEnchantment |
| 9 | +import net.minecraft.entity.LivingEntity |
| 10 | +import net.minecraft.entity.damage.DamageSource |
| 11 | +import net.minecraft.entity.effect.StatusEffects |
| 12 | +import net.minecraft.registry.tag.DamageTypeTags |
| 13 | +import net.minecraft.server.network.ServerPlayerEntity |
| 14 | +import net.minecraft.stat.Stats |
| 15 | +import net.minecraft.util.math.MathHelper |
| 16 | +import net.minecraft.util.math.MathHelper.clamp |
| 17 | +import net.minecraft.util.math.Vec3d |
| 18 | +import net.minecraft.world.World.ExplosionSourceType |
| 19 | +import net.minecraft.world.explosion.Explosion |
| 20 | +import kotlin.math.max |
| 21 | + |
| 22 | +/** |
| 23 | + * Utility functions related to explosion effects and calculations. |
| 24 | + */ |
| 25 | +object CrystalUtils { |
| 26 | + /** |
| 27 | + * Calculates the radius of an explosion based on its power. |
| 28 | + * @param power The strength of the explosion. |
| 29 | + * @return The radius of the explosion. |
| 30 | + */ |
| 31 | + fun SafeContext.explosionRadius(power: Double): Double = |
| 32 | + 1.3 * (power / 0.225) * 0.3 |
| 33 | + |
| 34 | + /** |
| 35 | + * Calculates the damage dealt by an explosion to a living entity. |
| 36 | + * @param source The source of the explosion. |
| 37 | + * @param entity The entity to calculate the damage for. |
| 38 | + * @return The damage dealt by the explosion. |
| 39 | + */ |
| 40 | + fun SafeContext.explosionDamage(source: Explosion, entity: LivingEntity): Double = |
| 41 | + explosionDamage(source.position, entity, source.power.toDouble()) |
| 42 | + |
| 43 | + /** |
| 44 | + * Calculates the damage dealt by an explosion to a living entity. |
| 45 | + * @param position The position of the explosion. |
| 46 | + * @param entity The entity to calculate the damage for. |
| 47 | + * @param power The strength of the explosion. |
| 48 | + * @return The damage dealt by the explosion. |
| 49 | + */ |
| 50 | + fun SafeContext.explosionDamage(position: Vec3d, entity: LivingEntity, power: Double): Double { |
| 51 | + val distance = entity.pos.distanceTo(position) |
| 52 | + if (explosionRadius(power) < distance) return 0.0 // Avoid unnecessary calculations |
| 53 | + val size = power * 2.0 |
| 54 | + val impact = (1.0 - distance / size) * Explosion.getExposure(position, entity) |
| 55 | + val damage = (impact * impact + impact) / 2.0 * 7.0 * size + 1 |
| 56 | + return applyProtection(entity, damage, Explosion.createDamageSource(world, null)) |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Calculates the velocity of a living entity affected by an explosion. |
| 61 | + * @param entity The entity to calculate the velocity for. |
| 62 | + * @param explosion The explosion to calculate the velocity for. |
| 63 | + * @return The velocity of the entity. |
| 64 | + */ |
| 65 | + fun SafeContext.explosionVelocity(entity: LivingEntity, explosion: Explosion): Vec3d = |
| 66 | + explosionVelocity(entity, explosion.position, explosion.power.toDouble()) |
| 67 | + |
| 68 | + /** |
| 69 | + * Calculates the velocity of a living entity affected by an explosion. |
| 70 | + * @param entity The entity to calculate the velocity for. |
| 71 | + * @param position The position of the explosion. |
| 72 | + * @param power The strength of the explosion. |
| 73 | + * @return The velocity of the entity. |
| 74 | + */ |
| 75 | + fun SafeContext.explosionVelocity(entity: LivingEntity, position: Vec3d, power: Double): Vec3d { |
| 76 | + val distance = entity.pos.distanceTo(position) |
| 77 | + if (explosionRadius(power) < distance) return Vec3d.ZERO // Avoid unnecessary calculations |
| 78 | + val size = power * 2.0 |
| 79 | + val vel = ProtectionEnchantment.transformExplosionKnockback( |
| 80 | + entity, |
| 81 | + (1.0 - distance / size) * Explosion.getExposure(position, entity) |
| 82 | + ) |
| 83 | + |
| 84 | + val diff = entity.eyePos - position |
| 85 | + return diff.normalize() * vel |
| 86 | + } |
| 87 | +} |
0 commit comments