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

137 lines
3.6 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.entity;
2019-02-04 14:25:12 +01:00
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.network.Channel;
import com.minelittlepony.unicopia.network.MsgSpawnRainbow;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityPose;
2020-01-17 14:27:26 +01:00
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnType;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Packet;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.math.Box;
2019-02-04 14:25:12 +01:00
import net.minecraft.util.math.MathHelper;
2020-01-17 14:27:26 +01:00
import net.minecraft.world.IWorld;
2019-02-04 14:25:12 +01:00
import net.minecraft.world.World;
2020-01-17 14:27:26 +01:00
import net.minecraft.world.biome.Biome.SpawnEntry;
2019-02-04 14:25:12 +01:00
public class RainbowEntity extends MobEntity implements InAnimate {
2019-02-04 14:25:12 +01:00
public static final SpawnEntry SPAWN_ENTRY = new SpawnEntry(UEntities.RAINBOW, 1, 1, 1);
2019-02-04 14:25:12 +01:00
private int ticksAlive;
private final double radius;
2019-02-04 14:25:12 +01:00
public static final int RAINBOW_MAX_SIZE = 180;
public static final int RAINBOW_MIN_SIZE = 50;
2020-01-16 12:35:46 +01:00
public static final Box SPAWN_COLLISSION_RADIUS = new Box(
-RAINBOW_MAX_SIZE, -RAINBOW_MAX_SIZE, -RAINBOW_MAX_SIZE,
RAINBOW_MAX_SIZE, RAINBOW_MAX_SIZE, RAINBOW_MAX_SIZE
2020-01-17 14:27:26 +01:00
).expand(RAINBOW_MAX_SIZE);
2020-01-16 12:35:46 +01:00
2020-01-17 14:27:26 +01:00
public RainbowEntity(EntityType<RainbowEntity> type, World world) {
super(type, world);
2020-01-16 12:35:46 +01:00
2020-01-17 14:27:26 +01:00
float yaw = (int)MathHelper.nextDouble(random, 0, 360);
2019-02-04 14:25:12 +01:00
2020-04-22 16:28:20 +02:00
updatePositionAndAngles(0, 0, 0, yaw, 0);
2019-02-04 14:25:12 +01:00
2020-01-17 14:27:26 +01:00
radius = MathHelper.nextDouble(random, RAINBOW_MIN_SIZE, RAINBOW_MAX_SIZE);
2019-02-04 14:25:12 +01:00
ticksAlive = 10000;
2020-01-17 14:27:26 +01:00
ignoreCameraFrustum = true;
2019-02-04 14:25:12 +01:00
calculateDimensions();
}
@Override
public boolean canInteract(Race race) {
return false;
}
2019-02-04 14:25:12 +01:00
@Override
2020-04-22 16:28:20 +02:00
public void setPos(double x, double y, double z) {
super.setPos(x, y, z);
2019-02-04 14:25:12 +01:00
2020-01-17 14:27:26 +01:00
float width = getDimensions(getPose()).width;
2020-01-16 12:35:46 +01:00
setBoundingBox(new Box(
2019-02-04 14:25:12 +01:00
x - width, y - radius/2, z,
x + width, y + radius/2, z
));
}
@Override
public EntityDimensions getDimensions(EntityPose pose) {
return EntityDimensions.changing((float)getRadius(), (float)getRadius());
}
2019-02-04 14:25:12 +01:00
@Override
public SoundCategory getSoundCategory() {
return SoundCategory.WEATHER;
}
@Override
2020-04-22 16:28:20 +02:00
public boolean shouldRender(double distance) {
2019-02-04 14:25:12 +01:00
return true;
}
public double getRadius() {
return radius;
}
@Override
2020-01-16 12:35:46 +01:00
public void tick() {
super.tick();
2019-02-04 14:25:12 +01:00
if (ticksAlive-- <= 0) {
2020-01-16 12:35:46 +01:00
remove();
2019-02-04 14:25:12 +01:00
}
2020-01-16 12:35:46 +01:00
if (!removed) {
2020-01-17 14:27:26 +01:00
Box bounds = SPAWN_COLLISSION_RADIUS.offset(getPos());
2020-04-22 16:28:20 +02:00
world.getEntities(RainbowEntity.class, bounds, null).forEach(this::attackCompetitor);
}
}
private void attackCompetitor(Entity other) {
if (other != this) {
2020-01-16 12:35:46 +01:00
other.remove();
}
2019-02-04 14:25:12 +01:00
}
@Override
public boolean canSpawn(IWorld world, SpawnType type) {
Box bounds = SPAWN_COLLISSION_RADIUS.offset(getPos());
return super.canSpawn(world, type)
&& world.getEntities(RainbowEntity.class, bounds, null).isEmpty();
2020-01-16 12:35:46 +01:00
}
@Override
public int getLimitPerChunk() {
return 1;
2019-02-04 14:25:12 +01:00
}
@Override
public void readCustomDataFromTag(CompoundTag var1) {
2019-02-04 14:25:12 +01:00
}
@Override
public void writeCustomDataToTag(CompoundTag var1) {
2019-02-04 14:25:12 +01:00
}
@Override
public Packet<?> createSpawnPacket() {
return Channel.SPAWN_RAINBOW.toPacket(new MsgSpawnRainbow(this));
2019-02-04 14:25:12 +01:00
}
}