2018-09-12 01:29:49 +02:00
|
|
|
package com.minelittlepony.unicopia.player;
|
|
|
|
|
2019-02-02 17:50:15 +01:00
|
|
|
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;
|
2019-01-22 17:39:30 +01:00
|
|
|
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;
|
|
|
|
|
2018-09-16 00:45:44 +02:00
|
|
|
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;
|
|
|
|
|
2019-02-02 17:50:15 +01:00
|
|
|
@Nullable
|
2018-09-12 01:29:49 +02:00
|
|
|
private IPower<?> activeAbility = null;
|
|
|
|
|
|
|
|
public PlayerAbilityDelegate(IPlayer player) {
|
|
|
|
this.player = player;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean canSwitchStates() {
|
2019-02-02 17:50:15 +01:00
|
|
|
return (warmup == 0 && cooldown == 0) || activeAbility == null;
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-02-02 17:50:15 +01:00
|
|
|
public synchronized void tryUseAbility(IPower<?> power) {
|
2018-09-12 01:29:49 +02:00
|
|
|
if (canSwitchStates() || activeAbility != power) {
|
|
|
|
activeAbility = power;
|
2019-02-02 17:50:15 +01:00
|
|
|
warmup = power.getWarmupTime(player);
|
|
|
|
cooldown = 0;
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-02-02 17:50:15 +01:00
|
|
|
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
|
2019-02-02 17:50:15 +01:00
|
|
|
public synchronized void onUpdate(EntityPlayer entity) {
|
2018-09-12 01:29:49 +02:00
|
|
|
if (activeAbility != null && activeAbility.canUse(player.getPlayerSpecies())) {
|
2019-02-02 17:50:15 +01:00
|
|
|
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
|
|
|
}
|
2019-02-02 17:50:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cooldown > 0 && activeAbility != null) {
|
2018-09-12 01:29:49 +02:00
|
|
|
cooldown--;
|
2019-02-02 17:50:15 +01:00
|
|
|
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");
|
2019-02-02 17:50:15 +01:00
|
|
|
activeAbility = null;
|
2018-09-12 01:29:49 +02:00
|
|
|
|
|
|
|
if (compound.hasKey("activeAbility")) {
|
|
|
|
PowersRegistry.instance().getPowerFromName(compound.getString("activeAbility")).ifPresent(p -> {
|
|
|
|
activeAbility = p;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 17:50:15 +01:00
|
|
|
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) {
|
2019-02-02 17:50:15 +01:00
|
|
|
Unicopia.channel.send(new MsgPlayerAbility(player.getOwner(), activeAbility, data), Target.SERVER);
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return data != null;
|
|
|
|
}
|
|
|
|
}
|