Finish the light spell

This commit is contained in:
Sollace 2021-12-27 23:14:46 +02:00
parent 00758b0c15
commit 75dfb85092
4 changed files with 55 additions and 19 deletions

View file

@ -24,36 +24,42 @@ public class LightSpell extends AbstractSpell {
.with(Trait.ORDER, 25)
.build();
private int age;
private int duration;
private List<EntityReference<FairyEntity>> lights;
private final List<EntityReference<FairyEntity>> lights = new ArrayList<>();
protected LightSpell(SpellType<?> type, SpellTraits traits) {
super(type, traits);
duration = (int)(traits.get(Trait.FOCUS, 0, 160) * 19);
duration = 120 + (int)(traits.get(Trait.FOCUS, 0, 160) * 19);
}
@Override
public boolean tick(Caster<?> caster, Situation situation) {
if (duration-- <= 0) {
age++;
if (age % 20 == 0) {
duration--;
}
if (duration <= 0) {
return false;
}
if (lights == null) {
if (!caster.isClient()) {
if (lights.isEmpty()) {
int size = 2 + caster.getWorld().random.nextInt(2) + (int)(getTraits().get(Trait.LIFE, 10, 20) - 10)/10;
lights = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
lights.set(i, new EntityReference<FairyEntity>());
while (lights.size() < size) {
lights.add(new EntityReference<FairyEntity>());
}
}
if (!caster.isClient()) {
lights.forEach(ref -> {
if (!ref.isPresent(caster.getWorld())) {
FairyEntity entity = UEntities.TWITTERMITE.create(caster.getWorld());
entity.setPosition(ref.getPosition().orElseGet(() -> {
return caster.getMaster().getPos().add(VecHelper.supply(() -> caster.getWorld().random.nextInt(2) - 1));
return caster.getMaster().getPos().add(VecHelper.supply(() -> caster.getWorld().random.nextInt(3) - 1));
}));
entity.setMaster(caster.getMaster());
entity.world.spawnEntity(entity);
@ -70,7 +76,9 @@ public class LightSpell extends AbstractSpell {
@Override
public void toNBT(NbtCompound compound) {
super.toNBT(compound);
if (lights != null) {
compound.putInt("age", age);
compound.putInt("duration", duration);
if (!lights.isEmpty()) {
NbtList list = new NbtList();
lights.forEach(light -> {
list.add(light.toNBT());
@ -82,15 +90,15 @@ public class LightSpell extends AbstractSpell {
@Override
public void fromNBT(NbtCompound compound) {
super.fromNBT(compound);
age = compound.getInt("age");
duration = compound.getInt("duration");
lights.clear();
if (compound.contains("lights", NbtElement.LIST_TYPE)) {
lights = new ArrayList<>();
compound.getList("lights", NbtElement.COMPOUND_TYPE).forEach(nbt -> {
EntityReference<FairyEntity> light = new EntityReference<>();
light.fromNBT((NbtCompound)nbt);
lights.add(light);
});
} else {
lights = null;
}
}
}

View file

@ -26,7 +26,7 @@ public final class RenderLayers extends RenderLayer {
.shader(COLOR_SHADER)
.transparency(TRANSLUCENT_TRANSPARENCY)
.target(TRANSLUCENT_TARGET)
.texturing(solid(120, 120, 0, 0.6F))
.texturing(solid(0.8F, 0.9F, 1, 0.6F))
.build(false));
public static RenderLayer getMagicGlow() {

View file

@ -27,6 +27,10 @@ public class EntityReference<T extends Entity> implements NbtSerialisable {
uuid = entity.getUuid();
clientId = entity.getId();
pos = Optional.of(entity.getPos());
} else {
uuid = null;
clientId = 0;
pos = Optional.empty();
}
}

View file

@ -119,6 +119,11 @@ public class FairyEntity extends PathAwareEntity implements LightEmittingEntity,
return false;
}
@Override
public boolean isPushedByFluids() {
return false;
}
public boolean isStaying() {
return stayingPos.isPresent();
}
@ -172,14 +177,33 @@ public class FairyEntity extends PathAwareEntity implements LightEmittingEntity,
@Override
protected ActionResult interactMob(PlayerEntity player, Hand hand) {
if (hand == Hand.MAIN_HAND && isStaying() && player == getMaster()) {
if (hand == Hand.MAIN_HAND) {
if (isStaying()) {
stayingPos = Optional.empty();
if (player != getMaster()) {
assignment.set(player);
}
} else {
assignment.set(null);
setStaying(getBlockPos());
}
playSound(SoundEvents.ENTITY_BAT_AMBIENT, getSoundVolume() / 3, getSoundPitch() * 3);
return ActionResult.SUCCESS;
}
return ActionResult.PASS;
}
@Override
public boolean handleAttack(Entity attacker) {
attacker.damage(DamageSource.LIGHTNING_BOLT, (float)getAttackDistanceScalingFactor(attacker) * 3);
return false;
}
@Override
public void onRemoved() {
super.onRemoved();