Unicopia/src/main/java/com/minelittlepony/unicopia/entity/Creature.java

255 lines
8.2 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.entity;
2021-12-29 23:59:17 +01:00
import java.util.Optional;
import java.util.function.Predicate;
import org.jetbrains.annotations.NotNull;
2021-12-29 23:59:17 +01:00
import org.jetbrains.annotations.Nullable;
2020-09-23 17:19:28 +02:00
import com.minelittlepony.unicopia.Affinity;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.WeaklyOwned;
2022-09-01 22:57:19 +02:00
import com.minelittlepony.unicopia.ability.magic.*;
2021-12-29 17:11:32 +01:00
import com.minelittlepony.unicopia.ability.magic.spell.Spell;
2021-12-29 23:59:17 +01:00
import com.minelittlepony.unicopia.ability.magic.spell.effect.TargetSelecter;
import com.minelittlepony.unicopia.entity.ai.BreakHeartGoal;
2021-02-20 00:34:30 +01:00
import com.minelittlepony.unicopia.entity.ai.DynamicTargetGoal;
2022-03-27 17:47:52 +02:00
import com.minelittlepony.unicopia.entity.ai.EatMuffinGoal;
import com.minelittlepony.unicopia.entity.ai.WantItTakeItGoal;
2021-12-29 23:59:17 +01:00
import net.minecraft.entity.Entity;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.SpawnGroup;
2021-12-29 23:59:17 +01:00
import net.minecraft.entity.ai.goal.ActiveTargetGoal;
import net.minecraft.entity.ai.goal.GoalSelector;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
2021-12-29 23:59:17 +01:00
import net.minecraft.entity.mob.HostileEntity;
import net.minecraft.entity.mob.MobEntity;
2021-12-29 23:59:17 +01:00
import net.minecraft.entity.mob.SlimeEntity;
2022-03-27 17:47:52 +02:00
import net.minecraft.entity.passive.PigEntity;
2021-12-29 23:59:17 +01:00
import net.minecraft.entity.player.PlayerEntity;
2021-08-04 15:38:03 +02:00
import net.minecraft.nbt.NbtCompound;
2021-12-29 23:59:17 +01:00
import net.minecraft.nbt.NbtElement;
2022-03-27 17:47:52 +02:00
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
2020-01-16 12:35:46 +01:00
public class Creature extends Living<LivingEntity> implements WeaklyOwned<LivingEntity> {
2022-03-26 20:34:15 +01:00
private static final TrackedData<NbtCompound> EFFECT = DataTracker.registerData(LivingEntity.class, TrackedDataHandlerRegistry.NBT_COMPOUND);
private static final TrackedData<NbtCompound> MASTER = DataTracker.registerData(LivingEntity.class, TrackedDataHandlerRegistry.NBT_COMPOUND);
public static final TrackedData<Float> GRAVITY = DataTracker.registerData(LivingEntity.class, TrackedDataHandlerRegistry.FLOAT);
2022-03-27 17:47:52 +02:00
private static final TrackedData<Integer> EATING = DataTracker.registerData(LivingEntity.class, TrackedDataHandlerRegistry.INTEGER);
2020-01-16 12:35:46 +01:00
2020-10-02 09:39:00 +02:00
private static final LevelStore LEVELS = Levelled.fixed(0);
2020-01-16 12:35:46 +01:00
public static void boostrap() {}
private final EntityPhysics<LivingEntity> physics;
2021-12-29 23:59:17 +01:00
private final EntityReference<LivingEntity> master = new EntityReference<>();
@Nullable
private GoalSelector goals;
@Nullable
private GoalSelector targets;
2022-03-27 17:47:52 +02:00
private int eatTimer;
@Nullable
private EatMuffinGoal eatMuffinGoal;
public Creature(LivingEntity entity) {
2021-02-14 16:52:56 +01:00
super(entity, EFFECT);
physics = new EntityPhysics<>(entity, GRAVITY);
entity.getDataTracker().startTracking(MASTER, master.toNBT());
2022-03-27 17:47:52 +02:00
entity.getDataTracker().startTracking(EATING, 0);
2020-01-16 12:35:46 +01:00
}
2021-12-29 23:59:17 +01:00
@Override
public void setMaster(LivingEntity owner) {
master.set(owner);
entity.getDataTracker().set(MASTER, master.toNBT());
2021-12-29 23:59:17 +01:00
if (targets != null && owner != null) {
initMinionAi();
}
}
public boolean isMinion() {
return master.getId().isPresent();
}
2021-12-29 23:59:17 +01:00
@Override
2022-06-23 16:24:45 +02:00
public World getReferenceWorld() {
return super.getReferenceWorld();
}
@Override
@NotNull
2021-12-29 23:59:17 +01:00
public LivingEntity getMaster() {
NbtCompound data = entity.getDataTracker().get(MASTER);
master.fromNBT(data);
2022-06-23 16:24:45 +02:00
return master.getOrEmpty(getReferenceWorld()).orElse(entity);
2021-12-29 23:59:17 +01:00
}
@Override
public EntityReference<LivingEntity> getMasterReference() {
return master;
}
2021-12-29 23:59:17 +01:00
@Override
public Entity getEntity() {
return entity;
}
public Optional<GoalSelector> getTargets() {
return Optional.ofNullable(targets);
}
public Optional<GoalSelector> getGoals() {
return Optional.ofNullable(goals);
}
public void initAi(GoalSelector goals, GoalSelector targets) {
2021-12-29 23:59:17 +01:00
this.goals = goals;
this.targets = targets;
2021-02-20 00:34:30 +01:00
DynamicTargetGoal targetter = new DynamicTargetGoal((MobEntity)entity);
targets.add(1, targetter);
goals.add(1, new WantItTakeItGoal((MobEntity)entity, targetter));
if (entity.getType().getSpawnGroup() == SpawnGroup.MONSTER) {
2021-02-20 00:34:30 +01:00
goals.add(3, new BreakHeartGoal((MobEntity)entity, targetter));
}
2022-03-27 17:47:52 +02:00
if (entity instanceof PigEntity) {
eatMuffinGoal = new EatMuffinGoal((MobEntity)entity, targetter);
goals.add(3, eatMuffinGoal);
}
2021-12-29 23:59:17 +01:00
2022-06-23 16:24:45 +02:00
if (master.isPresent(getReferenceWorld())) {
2021-12-29 23:59:17 +01:00
initMinionAi();
}
}
private void initMinionAi() {
Predicate<LivingEntity> filter = TargetSelecter.<LivingEntity>notOwnerOrFriend(this, this).and(e -> {
return Equine.of(e)
.filter(eq -> eq instanceof Creature)
.filter(eq -> ((Creature)eq).hasCommonOwner(this))
2021-12-29 23:59:17 +01:00
.isEmpty();
});
targets.clear();
targets.add(2, new ActiveTargetGoal<>((MobEntity)entity, PlayerEntity.class, true, filter));
targets.add(2, new ActiveTargetGoal<>((MobEntity)entity, HostileEntity.class, true, filter));
targets.add(2, new ActiveTargetGoal<>((MobEntity)entity, SlimeEntity.class, true, filter));
}
public static void registerAttributes(DefaultAttributeContainer.Builder builder) {
builder.add(EntityAttributes.GENERIC_ATTACK_DAMAGE);
builder.add(EntityAttributes.GENERIC_ATTACK_KNOCKBACK);
builder.add(UEntityAttributes.ENTITY_GRAVTY_MODIFIER);
}
@Override
public void tick() {
super.tick();
physics.tick();
2022-03-27 17:47:52 +02:00
if (isClient()) {
eatTimer = entity.getDataTracker().get(EATING);
} else if (eatMuffinGoal != null) {
eatTimer = eatMuffinGoal.getTimer();
entity.getDataTracker().set(EATING, eatTimer);
}
}
public float getNeckAngle(float delta) {
if (eatTimer <= 0) {
return 0;
}
if (eatTimer >= 4 && eatTimer <= 36) {
return 1;
}
if (eatTimer < 4) {
return (eatTimer - delta) / 4F;
}
return -(eatTimer - 40 - delta) / 4F;
}
public float getHeadAngle(float delta) {
if (eatTimer > 4 && eatTimer <= 36) {
float f = (eatTimer - 4 - delta) / 32F;
return 0.62831855f + 0.21991149f * MathHelper.sin(f * 28.7F);
}
if (eatTimer > 0) {
return 0.62831855f;
}
return entity.getPitch() * ((float)Math.PI / 180);
}
2020-01-16 12:35:46 +01:00
@Override
public Race getSpecies() {
return Race.HUMAN;
}
@Override
public Physics getPhysics() {
return physics;
}
2020-01-16 12:35:46 +01:00
@Override
public void setSpecies(Race race) {
}
@Override
2020-10-02 09:39:00 +02:00
public LevelStore getLevel() {
return LEVELS;
2020-01-16 12:35:46 +01:00
}
2022-09-01 22:57:19 +02:00
@Override
public LevelStore getCorruption() {
return LEVELS;
}
@Override
public boolean subtractEnergyCost(double amount) {
getMaster().damage(DamageSource.MAGIC, (int)amount/2);
return getMaster().getHealth() > 0;
}
2020-01-16 12:35:46 +01:00
@Override
public Affinity getAffinity() {
if (getMaster() instanceof Affine) {
return ((Affine)getMaster()).getAffinity();
2020-01-16 12:35:46 +01:00
}
return Affinity.NEUTRAL;
}
@Override
2021-08-04 15:38:03 +02:00
public void toNBT(NbtCompound compound) {
super.toNBT(compound);
2021-03-03 10:33:23 +01:00
getSpellSlot().get(true).ifPresent(effect -> {
2021-12-29 17:11:32 +01:00
compound.put("effect", Spell.writeNbt(effect));
2021-03-03 10:33:23 +01:00
});
2021-12-29 23:59:17 +01:00
compound.put("master", master.toNBT());
physics.toNBT(compound);
2020-01-16 12:35:46 +01:00
}
@Override
2021-08-04 15:38:03 +02:00
public void fromNBT(NbtCompound compound) {
super.fromNBT(compound);
2020-04-22 16:28:20 +02:00
if (compound.contains("effect")) {
2021-12-29 17:11:32 +01:00
getSpellSlot().put(Spell.readNbt(compound.getCompound("effect")));
2020-01-16 12:35:46 +01:00
}
2021-12-29 23:59:17 +01:00
if (compound.contains("master", NbtElement.COMPOUND_TYPE)) {
master.fromNBT(compound.getCompound("master"));
2022-06-23 16:24:45 +02:00
if (master.isPresent(getReferenceWorld()) && targets != null) {
2021-12-29 23:59:17 +01:00
initMinionAi();
}
}
physics.fromNBT(compound);
2020-01-16 12:35:46 +01:00
}
}