Unicopia/src/main/java/com/minelittlepony/unicopia/ability/BatEeeeAbility.java

147 lines
5.2 KiB
Java
Raw Normal View History

2020-09-25 20:24:48 +02:00
package com.minelittlepony.unicopia.ability;
2023-08-16 00:18:41 +02:00
import java.util.Optional;
2020-09-25 20:24:48 +02:00
import com.minelittlepony.unicopia.AwaitTickQueue;
import com.minelittlepony.unicopia.EquinePredicates;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.USounds;
import com.minelittlepony.unicopia.UTags;
2023-08-16 00:18:41 +02:00
import com.minelittlepony.unicopia.ability.data.Numeric;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
2021-08-19 14:43:15 +02:00
import com.minelittlepony.unicopia.advancement.UCriteria;
import com.minelittlepony.unicopia.client.render.PlayerPoser.Animation;
import com.minelittlepony.unicopia.entity.Living;
2023-06-02 21:20:30 +02:00
import com.minelittlepony.unicopia.entity.damage.UDamageTypes;
2020-09-25 20:24:48 +02:00
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.util.RegistryUtils;
import com.minelittlepony.unicopia.util.VecHelper;
2020-09-25 20:24:48 +02:00
import net.minecraft.entity.LivingEntity;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.util.math.MathHelper;
2020-09-25 20:24:48 +02:00
import net.minecraft.util.math.Vec3d;
2022-06-25 00:19:55 +02:00
import net.minecraft.util.math.random.Random;
2020-09-25 20:24:48 +02:00
/**
* A magic casting ability for unicorns.
* (only shields for now)
*/
2023-08-16 00:18:41 +02:00
public class BatEeeeAbility implements Ability<Numeric> {
public static final int SELF_SPOOK_PROBABILITY = 20000;
public static final int MOB_SPOOK_PROBABILITY = 1000;
2020-09-25 20:24:48 +02:00
@Override
public int getWarmupTime(Pony player) {
return 30;
2020-09-25 20:24:48 +02:00
}
@Override
public int getCooldownTime(Pony player) {
return 5;
2020-09-25 20:24:48 +02:00
}
@Override
public double getCostEstimate(Pony player) {
return 0;
}
@Override
public boolean activateOnEarlyRelease() {
return true;
}
2020-09-25 20:24:48 +02:00
@Override
public boolean canUse(Race race) {
return race == Race.BAT;
}
@Override
2023-08-16 00:18:41 +02:00
public Optional<Numeric> prepare(Pony player) {
return player.getAbilities().getActiveStat()
.map(stat -> (int)(stat.getWarmup() * getWarmupTime(player)))
.filter(i -> i >= 0)
.map(Numeric::new);
2020-09-25 20:24:48 +02:00
}
@Override
2023-08-16 00:18:41 +02:00
public Numeric.Serializer<Numeric> getSerializer() {
return Numeric.SERIALIZER;
2020-09-25 20:24:48 +02:00
}
@Override
2023-08-16 00:18:41 +02:00
public boolean apply(Pony player, Numeric data) {
float strength = 1 - MathHelper.clamp(data.type() / (float)getWarmupTime(player), 0, 1);
Random rng = player.asWorld().random;
int count = 1 + rng.nextInt(10) + (int)(strength * 10);
2020-09-25 20:24:48 +02:00
for (int i = 0; i < count; i++) {
player.playSound(USounds.ENTITY_PLAYER_BATPONY_SCREECH,
(0.9F + (rng.nextFloat() - 0.5F) / 2F) * strength,
2020-09-25 20:24:48 +02:00
1.6F + (rng.nextFloat() - 0.5F)
);
}
for (int j = 0; j < (int)(strength * 2); j++) {
for (int k = 0; k < count; k++) {
AwaitTickQueue.scheduleTask(player.asWorld(), w -> {
player.playSound(USounds.ENTITY_PLAYER_BATPONY_SCREECH,
(0.9F + (rng.nextFloat() - 0.5F) / 2F) * strength,
1.6F + (rng.nextFloat() - 0.5F)
);
}, rng.nextInt(3));
2020-09-25 20:24:48 +02:00
}
}
2020-09-25 20:24:48 +02:00
if (!player.getPhysics().isFlying()) {
player.setAnimation(Animation.SPREAD_WINGS, Animation.Recipient.ANYONE);
}
2020-09-25 20:24:48 +02:00
Vec3d origin = player.getOriginVector();
if (strength > 0.5F && rng.nextInt(SELF_SPOOK_PROBABILITY) == 0) {
2023-06-02 21:20:30 +02:00
player.asEntity().damage(player.damageOf(UDamageTypes.BAT_SCREECH, player), 0.1F);
UCriteria.SCREECH_SELF.trigger(player.asEntity());
2020-09-25 20:24:48 +02:00
}
int total = player.findAllEntitiesInRange((int)Math.max(1, 8 * strength)).mapToInt(e -> {
if (e instanceof LivingEntity living && !SpellType.SHIELD.isOn(e)) {
2020-09-25 20:24:48 +02:00
boolean isEarthPony = EquinePredicates.PLAYER_EARTH.test(e);
2023-06-02 21:20:30 +02:00
e.damage(player.damageOf(UDamageTypes.BAT_SCREECH, player), isEarthPony ? 0.1F : 0.3F);
if (e.getWorld().random.nextInt(MOB_SPOOK_PROBABILITY) == 0) {
RegistryUtils.pickRandom(e.getWorld(), UTags.SPOOKED_MOB_DROPS).ifPresent(drop -> {
e.dropStack(drop.getDefaultStack());
e.playSound(USounds.Vanilla.ENTITY_ITEM_PICKUP, 1, 0.1F);
UCriteria.SPOOK_MOB.trigger(player.asEntity());
});
}
2020-09-25 20:24:48 +02:00
Vec3d knockVec = origin.subtract(e.getPos()).multiply(strength);
living.takeKnockback((isEarthPony ? 0.3F : 0.5F) * strength, knockVec.getX(), knockVec.getZ());
2020-09-25 20:24:48 +02:00
if (!isEarthPony) {
e.addVelocity(0, 0.1 * strength, 0);
2020-09-25 20:24:48 +02:00
}
Living.updateVelocity(e);
return 1;
2020-09-25 20:24:48 +02:00
}
return 0;
2021-08-19 14:43:15 +02:00
}).sum();
if (total >= 20) {
UCriteria.SCREECH_TWENTY_MOBS.trigger(player.asEntity());
2021-08-19 14:43:15 +02:00
}
2023-08-16 00:18:41 +02:00
return true;
2020-09-25 20:24:48 +02:00
}
@Override
2023-08-16 00:18:41 +02:00
public void warmUp(Pony player, AbilitySlot slot) {
2020-09-25 20:24:48 +02:00
}
@Override
2023-08-16 00:18:41 +02:00
public void coolDown(Pony player, AbilitySlot slot) {
for (int i = 0; i < 20; i++) {
player.addParticle(ParticleTypes.BUBBLE_POP, player.getPhysics().getHeadPosition().toCenterPos(), VecHelper.supply(() -> player.asWorld().getRandom().nextGaussian() - 0.5));
}
2020-09-25 20:24:48 +02:00
}
}