Cleanup spell orientation slightly

This commit is contained in:
Sollace 2022-09-18 11:34:57 +02:00
parent 33dba4db7e
commit f5c643ed50
5 changed files with 58 additions and 51 deletions

View file

@ -128,11 +128,11 @@ public abstract class AbstractDelegatingSpell implements ProjectileSpell {
protected abstract void saveDelegates(NbtCompound compound); protected abstract void saveDelegates(NbtCompound compound);
private <T> Stream<T> getDelegates(Function<? super Spell, T> cast) { protected <T> Stream<T> getDelegates(Function<? super Spell, T> cast) {
return getDelegates().stream().map(cast).filter(Objects::nonNull); return getDelegates().stream().map(cast).filter(Objects::nonNull);
} }
private static boolean execute(Stream<Spell> spells, Function<Spell, Boolean> action) { protected static boolean execute(Stream<Spell> spells, Function<Spell, Boolean> action) {
return spells.reduce(false, (u, a) -> action.apply(a), (a, b) -> a || b); return spells.reduce(false, (u, a) -> action.apply(a), (a, b) -> a || b);
} }
} }

View file

@ -0,0 +1,5 @@
package com.minelittlepony.unicopia.ability.magic.spell;
public interface OrientedSpell {
void setOrientation(float pitch, float yaw);
}

View file

@ -28,7 +28,7 @@ import net.minecraft.world.World;
* The spell's effects are still powered by the casting player, so if the player dies or leaves the area, their * The spell's effects are still powered by the casting player, so if the player dies or leaves the area, their
* spell loses affect until they return. * spell loses affect until they return.
*/ */
public class PlaceableSpell extends AbstractDelegatingSpell { public class PlaceableSpell extends AbstractDelegatingSpell implements OrientedSpell {
/** /**
* Dimension the spell was originally cast in * Dimension the spell was originally cast in
*/ */
@ -83,41 +83,19 @@ public class PlaceableSpell extends AbstractDelegatingSpell {
setDirty(); setDirty();
} }
if (getWorld(source).map(castEntity::isUnlinked).orElse(false)) { castEntity.getId().ifPresentOrElse(
CastSpellEntity entity = UEntities.CAST_SPELL.create(source.getReferenceWorld()); id -> checkDetachment(source, id),
Vec3d pos = castEntity.getPosition().orElse(source.getOriginVector()); () -> spawnPlacedEntity(source)
entity.updatePositionAndAngles(pos.x, pos.y, pos.z, 0, 0); );
PlaceableSpell copy = spell.toPlaceable();
if (spell instanceof PlacementDelegate delegate) {
delegate.onPlaced(source, copy, entity);
}
entity.getSpellSlot().put(copy);
entity.setCaster(source);
entity.world.spawnEntity(entity);
Ether.get(entity.world).put(getType(), entity);
castEntity.set(entity);
setDirty();
} else {
if (getWorld(source).map(Ether::get)
.flatMap(ether -> castEntity.getId().flatMap(id -> ether.getEntry(getType(), id)))
.isEmpty()) {
setDead();
}
}
} }
return !isDead(); return !isDead();
} }
if (situation == Situation.GROUND_ENTITY) { if (situation == Situation.GROUND_ENTITY) {
if (!source.isClient() && Ether.get(source.getReferenceWorld()).getEntry(getType(), source).isEmpty()) {
if (!source.isClient()) { setDead();
if (Ether.get(source.getReferenceWorld()).getEntry(getType(), source).isEmpty()) { return false;
setDead();
return false;
}
} }
if (spell instanceof PlacementDelegate delegate) { if (spell instanceof PlacementDelegate delegate) {
@ -134,6 +112,38 @@ public class PlaceableSpell extends AbstractDelegatingSpell {
return !isDead(); return !isDead();
} }
private void checkDetachment(Caster<?> source, UUID id) {
if (getWorld(source).map(Ether::get).flatMap(ether -> ether.getEntry(getType(), id)).isEmpty()) {
setDead();
}
}
private void spawnPlacedEntity(Caster<?> source) {
CastSpellEntity entity = UEntities.CAST_SPELL.create(source.getReferenceWorld());
Vec3d pos = castEntity.getPosition().orElse(source.getOriginVector());
entity.updatePositionAndAngles(pos.x, pos.y, pos.z, source.getEntity().getYaw(), source.getEntity().getPitch());
PlaceableSpell copy = spell.toPlaceable();
if (spell instanceof PlacementDelegate delegate) {
delegate.onPlaced(source, copy, entity);
}
entity.getSpellSlot().put(copy);
entity.setCaster(source);
entity.world.spawnEntity(entity);
Ether.get(entity.world).put(getType(), entity);
castEntity.set(entity);
setDirty();
}
@Override
public void setOrientation(float pitch, float yaw) {
this.pitch = -90 - pitch;
this.yaw = -yaw;
getDelegates(spell -> spell instanceof OrientedSpell o ? o : null)
.forEach(oriented -> oriented.setOrientation(pitch, yaw));
setDirty();
}
@Override @Override
public void onDestroyed(Caster<?> source) { public void onDestroyed(Caster<?> source) {
if (!source.isClient()) { if (!source.isClient()) {

View file

@ -26,7 +26,7 @@ import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.WorldEvents; import net.minecraft.world.WorldEvents;
public class PortalSpell extends AbstractSpell implements PlaceableSpell.PlacementDelegate { public class PortalSpell extends AbstractSpell implements PlaceableSpell.PlacementDelegate, OrientedSpell {
public static final SpellTraits DEFAULT_TRAITS = new SpellTraits.Builder() public static final SpellTraits DEFAULT_TRAITS = new SpellTraits.Builder()
.with(Trait.LIFE, 10) .with(Trait.LIFE, 10)
.with(Trait.KNOWLEDGE, 1) .with(Trait.KNOWLEDGE, 1)
@ -48,8 +48,7 @@ public class PortalSpell extends AbstractSpell implements PlaceableSpell.Placeme
@Override @Override
public boolean apply(Caster<?> caster) { public boolean apply(Caster<?> caster) {
pitch = caster.getEntity().getPitch(1); setOrientation(caster.getEntity().getPitch(), caster.getEntity().getYaw());
yaw = caster.getEntity().getYaw(1);
return toPlaceable().apply(caster); return toPlaceable().apply(caster);
} }
@ -118,7 +117,7 @@ public class PortalSpell extends AbstractSpell implements PlaceableSpell.Placeme
Vec3d dest = targetPos.add(offset.rotateY(yawDifference * MathHelper.RADIANS_PER_DEGREE)).add(0, 0.05, 0); Vec3d dest = targetPos.add(offset.rotateY(yawDifference * MathHelper.RADIANS_PER_DEGREE)).add(0, 0.05, 0);
entity.resetPortalCooldown(); entity.resetPortalCooldown();
entity.timeUntilRegen = 20; entity.timeUntilRegen = 100;
entity.setYaw(entity.getYaw() + yawDifference); entity.setYaw(entity.getYaw() + yawDifference);
entity.setVelocity(entity.getVelocity().rotateY(yawDifference * MathHelper.RADIANS_PER_DEGREE)); entity.setVelocity(entity.getVelocity().rotateY(yawDifference * MathHelper.RADIANS_PER_DEGREE));
@ -157,21 +156,18 @@ public class PortalSpell extends AbstractSpell implements PlaceableSpell.Placeme
return teleportationTarget.getId().flatMap(id -> Ether.get(source.getReferenceWorld()).getEntry(getType(), id)); return teleportationTarget.getId().flatMap(id -> Ether.get(source.getReferenceWorld()).getEntry(getType(), id));
} }
@Override
public void setOrientation(float pitch, float yaw) {
this.pitch = pitch;
this.yaw = yaw;
setDirty();
}
@Override @Override
public void onPlaced(Caster<?> source, PlaceableSpell parent, CastSpellEntity entity) { public void onPlaced(Caster<?> source, PlaceableSpell parent, CastSpellEntity entity) {
LivingEntity caster = source.getMaster(); LivingEntity caster = source.getMaster();
Vec3d targetPos = caster.getRotationVector().multiply(3).add(caster.getEyePos()); Vec3d targetPos = caster.getRotationVector().multiply(3).add(caster.getEyePos());
parent.setOrientation(pitch, yaw);
parent.pitch = -90 - pitch;
parent.yaw = -yaw;
for (Spell delegate : parent.getDelegates()) {
if (delegate instanceof PortalSpell copy) {
copy.pitch = pitch;
copy.yaw = yaw;
}
}
entity.setPos(targetPos.x, caster.getY() + 1.5, targetPos.z); entity.setPos(targetPos.x, caster.getY() + 1.5, targetPos.z);
} }

View file

@ -84,10 +84,6 @@ public class EntityReference<T extends Entity> implements NbtSerialisable {
return entity != null && entity.getUuid().equals(uuid.orElse(null)); return entity != null && entity.getUuid().equals(uuid.orElse(null));
} }
public boolean isUnlinked(World world) {
return getId().isEmpty() || getOrEmpty(world).map(e -> e.isRemoved()).orElse(false);
}
public boolean isPresent(World world) { public boolean isPresent(World world) {
return getOrEmpty(world).isPresent(); return getOrEmpty(world).isPresent();
} }