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

292 lines
9.2 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.ability;
import java.util.Collection;
2020-05-06 15:55:25 +02:00
import java.util.EnumMap;
import java.util.List;
2020-05-06 15:55:25 +02:00
import java.util.Map;
import java.util.Optional;
2021-08-04 15:38:03 +02:00
import org.jetbrains.annotations.Nullable;
2022-12-27 20:40:24 +01:00
import com.minelittlepony.unicopia.*;
import com.minelittlepony.unicopia.ability.data.Hit;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.network.MsgPlayerAbility;
import com.minelittlepony.unicopia.network.Channel;
2020-04-15 18:12:00 +02:00
import com.minelittlepony.unicopia.util.NbtSerialisable;
2021-08-04 15:38:03 +02:00
import com.minelittlepony.unicopia.util.Tickable;
2018-09-12 01:29:49 +02:00
2021-08-04 15:38:03 +02:00
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
2018-09-12 01:29:49 +02:00
public class AbilityDispatcher implements Tickable, NbtSerialisable {
2018-09-12 01:29:49 +02:00
2020-04-15 18:12:00 +02:00
private final Pony player;
2018-09-12 01:29:49 +02:00
2020-05-06 15:55:25 +02:00
private final Map<AbilitySlot, Stat> stats = new EnumMap<>(AbilitySlot.class);
2019-02-03 10:45:45 +01:00
@Nullable
private Race prevRace;
private long maxPage = -1;
public AbilityDispatcher(Pony player) {
2018-09-12 01:29:49 +02:00
this.player = player;
}
public void clear(AbilitySlot slot, ActivationType pressType, long page) {
Stat stat = getStat(slot);
2020-05-06 15:55:25 +02:00
if (stat.canSwitchStates()) {
2023-08-16 00:18:41 +02:00
stat.clear(pressType, page);
}
}
public Optional<Ability<?>> activate(AbilitySlot slot, long page) {
Stat stat = getStat(slot);
if (stat.canSwitchStates()) {
return stat.getAbility(page).flatMap(stat::setActiveAbility);
2019-02-03 10:45:45 +01:00
}
return Optional.empty();
2019-02-03 10:45:45 +01:00
}
public Collection<Stat> getStats() {
return stats.values();
}
2023-05-29 12:54:54 +02:00
public Optional<Stat> getActiveStat() {
return stats.values().stream().filter(stat -> stat.getFillProgress() > 0).findFirst();
}
2020-05-06 15:55:25 +02:00
public Stat getStat(AbilitySlot slot) {
return stats.computeIfAbsent(slot, Stat::new);
}
public boolean isFilled(AbilitySlot slot) {
return getStat(slot).getMaxPage() > 0;
}
2023-06-02 21:20:30 +02:00
public int getMaxPage() {
if (maxPage < 0 || prevRace != player.getSpecies()) {
prevRace = player.getSpecies();
maxPage = 0;
for (AbilitySlot slot : AbilitySlot.values()) {
maxPage = Math.max(maxPage, getStat(slot).getMaxPage() - 1);
}
}
2023-06-02 21:20:30 +02:00
return (int)maxPage;
}
2018-09-12 01:29:49 +02:00
@Override
public void tick() {
stats.values().forEach(Stat::tick);
2018-09-12 01:29:49 +02:00
}
@Override
2021-08-04 15:38:03 +02:00
public void toNBT(NbtCompound compound) {
2020-05-06 15:55:25 +02:00
if (compound.contains("stats")) {
stats.clear();
2021-08-04 15:38:03 +02:00
NbtCompound li = compound.getCompound("stats");
2020-05-06 15:55:25 +02:00
li.getKeys().forEach(key -> {
getStat(AbilitySlot.valueOf(key)).fromNBT(li.getCompound(key));
});
}
2018-09-12 01:29:49 +02:00
}
@Override
2021-08-04 15:38:03 +02:00
public void fromNBT(NbtCompound compound) {
NbtCompound li = new NbtCompound();
2020-05-06 15:55:25 +02:00
stats.forEach((key, value) -> li.put(key.name(), value.toNBT()));
compound.put("stats", li);
2018-09-12 01:29:49 +02:00
}
2020-05-06 15:55:25 +02:00
public class Stat implements NbtSerialisable {
/**
* Ticks of warmup before an ability is triggered.
*/
private int warmup;
private int maxWarmup;
/**
* Ticks of cooldown after an ability has been triggered.
*/
private int cooldown;
private int maxCooldown;
public final AbilitySlot slot;
/**
* True once the current ability has been triggered.
*/
private boolean triggered;
private Optional<Ability<?>> activeAbility = Optional.empty();
2020-05-06 15:55:25 +02:00
private Stat(AbilitySlot slot) {
this.slot = slot;
}
/**
* Returns true if the current ability can we swapped out.
*/
boolean canSwitchStates() {
return !activeAbility.isPresent() || (warmup != 0) || (triggered && cooldown == 0);
2020-05-06 15:55:25 +02:00
}
public int getRemainingCooldown() {
return cooldown;
}
public float getFillProgress() {
float cooldown = getWarmup();
if (cooldown <= 0 || cooldown >= 1) {
return getCooldown();
}
return 1 - cooldown;
}
public float getCooldown() {
return maxCooldown <= 0 ? 0 : ((float)cooldown / (float)maxCooldown);
}
public void setCooldown(int value) {
cooldown = value;
maxCooldown = value;
}
public float getWarmup() {
return maxWarmup <= 0 ? 0 : ((float)warmup / (float)maxWarmup);
}
public double getCost(long page) {
if (warmup <= 0) {
return 0;
}
return getAbility(page).map(ability -> ability.getCostEstimate(player)).orElse(0D);
}
2020-05-06 15:55:25 +02:00
public void setWarmup(int value) {
maxWarmup = value;
warmup = value;
}
public void tick() {
2023-08-16 00:18:41 +02:00
getActiveAbility().ifPresent(ability -> {
if (warmup > 0) {
warmup--;
ability.warmUp(player, slot);
return;
}
2023-08-16 00:18:41 +02:00
if (cooldown > 0 && cooldown-- > 0) {
ability.coolDown(player, slot);
2023-08-16 00:18:41 +02:00
if (cooldown <= 0) {
setActiveAbility(null);
}
return;
}
2023-08-16 00:18:41 +02:00
tryFire(ability);
});
}
private <T extends Hit> void tryFire(Ability<T> ability) {
if (triggered) {
return;
}
2023-08-16 00:18:41 +02:00
triggered = true;
setCooldown(ability.getCooldownTime(player));
2023-08-16 00:18:41 +02:00
if (player.isClientPlayer()) {
Optional<T> data = ability.prepare(player);
warmup = 0;
2023-08-16 00:18:41 +02:00
if (data.isPresent()) {
Channel.CLIENT_PLAYER_ABILITY.sendToServer(new MsgPlayerAbility<>(ability, data, ActivationType.NONE));
} else {
player.asEntity().playSound(USounds.GUI_ABILITY_FAIL, 1, 1);
setCooldown(0);
}
}
if (cooldown <= 0) {
setActiveAbility(null);
2020-05-06 15:55:25 +02:00
}
}
public Optional<Ability<?>> getAbility(long page) {
List<Ability<?>> found = Abilities.BY_SLOT_AND_COMPOSITE_RACE.apply(slot, player.getCompositeRace());
if (found.isEmpty()) {
return Optional.empty();
}
return Optional.ofNullable(found.get((int)Math.min(found.size() - 1, page)));
}
2023-08-16 00:18:41 +02:00
public void clear(ActivationType pressType, long page) {
if (pressType == ActivationType.NONE
|| getAbility(page).filter(ability -> !triggerQuickAction(ability, pressType)).isEmpty()) {
if (warmup > 0) {
getActiveAbility().filter(Ability::activateOnEarlyRelease).ifPresentOrElse(this::tryFire, () -> setActiveAbility(null));
} else {
setActiveAbility(null);
}
}
}
private <T extends Hit> boolean triggerQuickAction(Ability<T> ability, ActivationType pressType) {
Optional<T> data = ability.prepareQuickAction(player, pressType);
if (ability.onQuickAction(player, pressType, data)) {
Channel.CLIENT_PLAYER_ABILITY.sendToServer(new MsgPlayerAbility<>(ability, data, pressType));
return true;
}
return false;
}
public long getMaxPage() {
return Abilities.BY_SLOT_AND_COMPOSITE_RACE.apply(slot, player.getCompositeRace()).size();
}
protected synchronized Optional<Ability<?>> setActiveAbility(@Nullable Ability<?> power) {
if (activeAbility.orElse(null) != power) {
triggered = false;
activeAbility = Optional.ofNullable(power);
setWarmup(activeAbility.map(p -> p.getWarmupTime(player)).orElse(0));
setCooldown(0);
return activeAbility;
}
return Optional.empty();
}
2023-05-29 12:54:54 +02:00
public synchronized Optional<Ability<?>> getActiveAbility() {
return activeAbility.filter(ability -> {
return (!(ability == null || (triggered && warmup == 0 && cooldown == 0)) && ability.canUse(player.getCompositeRace()));
});
2020-05-06 15:55:25 +02:00
}
@Override
2021-08-04 15:38:03 +02:00
public void toNBT(NbtCompound compound) {
2020-05-06 15:55:25 +02:00
compound.putInt("warmup", warmup);
compound.putInt("cooldown", cooldown);
compound.putInt("maxWarmup", maxWarmup);
compound.putInt("maxCooldown", maxCooldown);
compound.putBoolean("triggered", triggered);
getActiveAbility().ifPresent(ability -> {
compound.putString("activeAbility", Abilities.REGISTRY.getId(ability).toString());
});
2020-05-06 15:55:25 +02:00
}
@Override
2021-08-04 15:38:03 +02:00
public void fromNBT(NbtCompound compound) {
2020-05-06 15:55:25 +02:00
warmup = compound.getInt("warmup");
cooldown = compound.getInt("cooldown");
maxWarmup = compound.getInt("maxWarmup");
maxCooldown = compound.getInt("maxCooldown");
triggered = compound.getBoolean("triggered");
activeAbility = Abilities.REGISTRY.getOrEmpty(new Identifier(compound.getString("activeAbility")));
2020-05-06 15:55:25 +02:00
}
}
2018-09-12 01:29:49 +02:00
}