Unicopia/src/main/java/com/minelittlepony/unicopia/player/PlayerAbilityDelegate.java

116 lines
3.3 KiB
Java
Raw Normal View History

2018-09-12 01:29:49 +02:00
package com.minelittlepony.unicopia.player;
import javax.annotation.Nullable;
2018-09-12 01:29:49 +02:00
import com.minelittlepony.jumpingcastle.api.Target;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.network.MsgPlayerAbility;
import com.minelittlepony.unicopia.power.IData;
import com.minelittlepony.unicopia.power.IPower;
import com.minelittlepony.unicopia.power.PowersRegistry;
import com.minelittlepony.unicopia.util.serialisation.InbtSerialisable;
2018-09-12 01:29:49 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
class PlayerAbilityDelegate implements IAbilityReceiver, IUpdatable<EntityPlayer>, InbtSerialisable {
2018-09-12 01:29:49 +02:00
private final IPlayer player;
private int warmup = 0;
private int cooldown = 0;
@Nullable
2018-09-12 01:29:49 +02:00
private IPower<?> activeAbility = null;
public PlayerAbilityDelegate(IPlayer player) {
this.player = player;
}
boolean canSwitchStates() {
return (warmup == 0 && cooldown == 0) || activeAbility == null;
2018-09-12 01:29:49 +02:00
}
@Override
public synchronized void tryUseAbility(IPower<?> power) {
2018-09-12 01:29:49 +02:00
if (canSwitchStates() || activeAbility != power) {
activeAbility = power;
warmup = power.getWarmupTime(player);
cooldown = 0;
2018-09-12 01:29:49 +02:00
}
}
@Override
public synchronized void tryClearAbility() {
if (canSwitchStates()) {
2018-09-12 01:29:49 +02:00
activeAbility = null;
warmup = 0;
cooldown = 0;
}
}
@Override
public int getRemainingCooldown() {
return cooldown;
}
@Override
public synchronized void onUpdate(EntityPlayer entity) {
2018-09-12 01:29:49 +02:00
if (activeAbility != null && activeAbility.canUse(player.getPlayerSpecies())) {
if (warmup > 0) {
warmup--;
activeAbility.preApply(player);
} else if (player.isClientPlayer()) {
if (activateAbility()) {
cooldown = activeAbility.getCooldownTime(player);
} else {
cooldown = 0;
2018-09-12 01:29:49 +02:00
}
}
if (cooldown > 0 && activeAbility != null) {
2018-09-12 01:29:49 +02:00
cooldown--;
activeAbility.postApply(player);
2018-09-12 01:29:49 +02:00
}
}
}
@Override
public void writeToNBT(NBTTagCompound compound) {
compound.setInteger("warmup", warmup);
compound.setInteger("cooldown", cooldown);
if (activeAbility != null) {
compound.setString("activeAbility", activeAbility.getKeyName());
}
}
@Override
public void readFromNBT(NBTTagCompound compound) {
warmup = compound.getInteger("warmup");
cooldown = compound.getInteger("cooldown");
activeAbility = null;
2018-09-12 01:29:49 +02:00
if (compound.hasKey("activeAbility")) {
PowersRegistry.instance().getPowerFromName(compound.getString("activeAbility")).ifPresent(p -> {
activeAbility = p;
});
}
}
protected boolean activateAbility() {
if (activeAbility == null || !activeAbility.canActivate(player.getWorld(), player)) {
return false;
}
IData data = activeAbility.tryActivate(player.getOwner(), player.getWorld());
2018-09-12 01:29:49 +02:00
if (data != null) {
Unicopia.channel.send(new MsgPlayerAbility(player.getOwner(), activeAbility, data), Target.SERVER);
2018-09-12 01:29:49 +02:00
}
return data != null;
}
}