2018-09-12 01:29:49 +02:00
|
|
|
package com.minelittlepony.unicopia.player;
|
|
|
|
|
|
|
|
import com.minelittlepony.jumpingcastle.api.Target;
|
|
|
|
import com.minelittlepony.unicopia.InbtSerialisable;
|
|
|
|
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 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 boolean abilityTriggered = false;
|
|
|
|
|
|
|
|
private int warmup = 0;
|
|
|
|
|
|
|
|
private int cooldown = 0;
|
|
|
|
|
|
|
|
private IPower<?> activeAbility = null;
|
|
|
|
|
|
|
|
public PlayerAbilityDelegate(IPlayer player) {
|
|
|
|
this.player = player;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean canSwitchStates() {
|
|
|
|
return abilityTriggered && cooldown <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void tryUseAbility(IPower<?> power) {
|
|
|
|
if (canSwitchStates() || activeAbility != power) {
|
|
|
|
abilityTriggered = false;
|
|
|
|
activeAbility = power;
|
|
|
|
warmup = 0;
|
|
|
|
cooldown = power.getCooldownTime(player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void tryClearAbility() {
|
|
|
|
if (canSwitchStates() || activeAbility != null) {
|
|
|
|
abilityTriggered = false;
|
|
|
|
activeAbility = null;
|
|
|
|
warmup = 0;
|
|
|
|
cooldown = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getRemainingCooldown() {
|
|
|
|
return cooldown;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpdate(EntityPlayer entity) {
|
|
|
|
if (activeAbility != null && activeAbility.canUse(player.getPlayerSpecies())) {
|
|
|
|
if (!abilityTriggered) {
|
|
|
|
if (warmup < activeAbility.getWarmupTime(player)) {
|
2018-09-13 14:04:24 +02:00
|
|
|
activeAbility.preApply(player);
|
2018-09-12 01:29:49 +02:00
|
|
|
warmup++;
|
|
|
|
} else if (player.isClientPlayer()) {
|
|
|
|
if (activeAbility.canActivate(entity.getEntityWorld(), player)) {
|
|
|
|
abilityTriggered = activateAbility(entity);
|
|
|
|
if (!abilityTriggered) {
|
|
|
|
activeAbility = null;
|
|
|
|
cooldown = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
activeAbility = null;
|
|
|
|
cooldown = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (cooldown > 0) {
|
2018-09-13 14:04:24 +02:00
|
|
|
activeAbility.postApply(player);
|
2018-09-12 01:29:49 +02:00
|
|
|
cooldown--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound compound) {
|
|
|
|
compound.setBoolean("triggered", abilityTriggered);
|
|
|
|
compound.setInteger("warmup", warmup);
|
|
|
|
compound.setInteger("cooldown", cooldown);
|
|
|
|
|
|
|
|
if (activeAbility != null) {
|
|
|
|
compound.setString("activeAbility", activeAbility.getKeyName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound compound) {
|
|
|
|
activeAbility = null;
|
|
|
|
abilityTriggered = compound.getBoolean("triggered");
|
|
|
|
warmup = compound.getInteger("warmup");
|
|
|
|
cooldown = compound.getInteger("cooldown");
|
|
|
|
|
|
|
|
if (compound.hasKey("activeAbility")) {
|
|
|
|
PowersRegistry.instance().getPowerFromName(compound.getString("activeAbility")).ifPresent(p -> {
|
|
|
|
activeAbility = p;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean activateAbility(EntityPlayer entity) {
|
|
|
|
IData data = activeAbility.tryActivate(entity, entity.getEntityWorld());
|
|
|
|
|
|
|
|
if (data != null) {
|
2018-09-12 22:37:06 +02:00
|
|
|
Unicopia.channel.send(new MsgPlayerAbility(entity, activeAbility, data), Target.SERVER);
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return data != null;
|
|
|
|
}
|
|
|
|
}
|