From b5e7bf673daba6eb1e1fa9537424b9cc92a8cc76 Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 29 Jan 2019 16:23:42 +0200 Subject: [PATCH] Fixed cloud movement --- .../unicopia/entity/EntityCloud.java | 93 ++++++++++++------- .../unicopia/entity/EntityWildCloud.java | 26 ++++-- 2 files changed, 79 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/entity/EntityCloud.java b/src/main/java/com/minelittlepony/unicopia/entity/EntityCloud.java index 44b8fd56..04ff3142 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/EntityCloud.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/EntityCloud.java @@ -9,6 +9,7 @@ import com.minelittlepony.unicopia.UItems; import com.minelittlepony.unicopia.UParticles; import com.minelittlepony.unicopia.particle.Particles; import com.minelittlepony.unicopia.player.PlayerSpeciesList; + import net.minecraft.block.BlockCrops; import net.minecraft.block.BlockFarmland; import net.minecraft.block.BlockFire; @@ -22,6 +23,7 @@ import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityFlying; import net.minecraft.entity.EntityList; +import net.minecraft.entity.IEntityLivingData; import net.minecraft.entity.effect.EntityLightningBolt; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.passive.IAnimals; @@ -51,36 +53,27 @@ import net.minecraft.util.SoundEvent; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; +import net.minecraft.world.DifficultyInstance; import net.minecraft.world.World; public class EntityCloud extends EntityFlying implements IAnimals { - private double altitude; - - private final double baseWidth = 3f; - private final double baseHeight = 0.8f; - - public EntityCloud(World world) { - super(world); - preventEntitySpawning = false; - ignoreFrustumCheck = true; - - if (world.rand.nextInt(20) == 0 && canRainHere()) { - setRaining(); - if (world.rand.nextInt(20) == 0) { - setIsThundering(true); - } - } - - setCloudSize(1 + rand.nextInt(3)); - } - private static final DataParameter RAINTIMER = EntityDataManager.createKey(EntityCloud.class, DataSerializers.VARINT); private static final DataParameter THUNDERING = EntityDataManager.createKey(EntityCloud.class, DataSerializers.BOOLEAN); private static final DataParameter SCALE = EntityDataManager.createKey(EntityCloud.class, DataSerializers.VARINT); private static final DataParameter STATIONARY = EntityDataManager.createKey(EntityCloud.class, DataSerializers.BOOLEAN); + protected double altitude; + + private final double baseWidth = 3f; + private final double baseHeight = 0.8f; + + public EntityCloud(World world) { + super(world); + ignoreFrustumCheck = true; + } + @Override protected void entityInit() { super.entityInit(); @@ -122,7 +115,7 @@ public class EntityCloud extends EntityFlying implements IAnimals { @Override public int getBrightnessForRender() { - return 15728640; + return 0xF00000; } @Override @@ -140,6 +133,20 @@ public class EntityCloud extends EntityFlying implements IAnimals { } + @Override + public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, IEntityLivingData livingdata) { + if (world.rand.nextInt(20) == 0 && canRainHere()) { + setRaining(); + if (world.rand.nextInt(20) == 0) { + setIsThundering(true); + } + } + + setCloudSize(1 + rand.nextInt(4)); + + return super.onInitialSpawn(difficulty, livingdata); + } + @Override protected void collideWithEntity(Entity other) { if (other instanceof EntityCloud) { @@ -322,26 +329,50 @@ public class EntityCloud extends EntityFlying implements IAnimals { protected void updateAITasks() { if (!getStationary()) { super.updateAITasks(); + if (!isBeingRidden()) { - double diffY = altitude - posZ; + double distance = altitude - posY; - if (rand.nextInt(700) == 0 || diffY*diffY < 3f) { - altitude += rand.nextInt(10) - 5; + if (Math.abs(distance) < 1 && rand.nextInt(7000) == 0) { + altitude = getRandomFlyingHeight(); + distance = altitude - posY; } - if (altitude > world.provider.getCloudHeight() - 8) { - altitude = world.provider.getCloudHeight() - 18; - } else if (altitude < 70) { - altitude = posY + 10; + if (distance < 1) { + distance = 0; } - double var3 = altitude + 0.1D - posY; - motionX -= 0.001; - motionY += (Math.signum(var3) * 0.699999988079071D - motionY) * 0.10000000149011612D; + motionX -= 0.01; + motionZ -= (Math.signum(distance) * 0.699999988079071D - motionZ) * 0.10000000149011612D; + motionY += (Math.signum(distance) * 0.699999988079071D - motionY) * 0.10000000149011612D; } } } + protected float getRandomFlyingHeight() { + float a = getMaximumFlyingHeight(); + float b = getMinimumFlyingHeight(); + + float min = Math.min(a, b); + float max = Math.max(a, b); + + return min + world.rand.nextFloat() * (max - min); + } + + protected float getMinimumFlyingHeight() { + float ground = world.provider.getAverageGroundLevel(); + float cloud = world.provider.getCloudHeight(); + + float min = Math.min(ground, cloud); + float max = Math.max(ground, cloud); + + return min + (max - min)/2; + } + + protected float getMaximumFlyingHeight() { + return world.provider.getActualHeight() - 5; + } + @Override public EnumActionResult applyPlayerInteraction(EntityPlayer player, Vec3d vec, EnumHand hand) { if (!(isBeingRidden() || isRidingOrBeingRiddenBy(player)) && hand == EnumHand.MAIN_HAND) { diff --git a/src/main/java/com/minelittlepony/unicopia/entity/EntityWildCloud.java b/src/main/java/com/minelittlepony/unicopia/entity/EntityWildCloud.java index ba5607a5..6395ed03 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/EntityWildCloud.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/EntityWildCloud.java @@ -1,18 +1,17 @@ package com.minelittlepony.unicopia.entity; -import java.util.List; - import net.minecraft.entity.Entity; import net.minecraft.entity.IEntityLivingData; import net.minecraft.util.math.AxisAlignedBB; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.DifficultyInstance; import net.minecraft.world.World; import net.minecraft.world.biome.Biome.SpawnListEntry; public class EntityWildCloud extends EntityCloud { - public static final SpawnListEntry SPAWN_ENTRY_LAND = new SpawnListEntry(EntityWildCloud.class, 3, 2, 5); - public static final SpawnListEntry SPAWN_ENTRY_OCEAN = new SpawnListEntry(EntityWildCloud.class, 3, 1, 2); + public static final SpawnListEntry SPAWN_ENTRY_LAND = new SpawnListEntry(EntityWildCloud.class, 1, 1, 3); + public static final SpawnListEntry SPAWN_ENTRY_OCEAN = new SpawnListEntry(EntityWildCloud.class, 1, 1, 2); public EntityWildCloud(World world) { super(world); @@ -30,21 +29,30 @@ public class EntityWildCloud extends EntityCloud { * @ref World.checkNoEntityCollision(AxisAlignedBB area, Entity entity) */ public boolean checkNoEntityCollision(AxisAlignedBB area, Entity entity) { - List list = world.getEntitiesWithinAABBExcludingEntity(null, area); - for (Entity i : list) { - if (!i.isDead && (i.preventEntitySpawning || i instanceof EntityCloud) && i != entity && (!entity.isRiding() || !entity.isRidingOrBeingRiddenBy(i))) { + + for (Entity i : world.getEntitiesWithinAABBExcludingEntity(entity, area)) { + if (!i.isDead && (i.preventEntitySpawning || i instanceof EntityCloud) && (!entity.isRiding() || !entity.isRidingOrBeingRiddenBy(i))) { return false; } } return true; } + @Override + public boolean getCanSpawnHere() { + BlockPos pos = new BlockPos(this).down(); + + return world.getBlockState(pos).canEntitySpawn(this); + } + @Override public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, IEntityLivingData livingdata) { - float minSpawnHeight = world.provider.getAverageGroundLevel() + 18; + float minSpawnHeight = getMinimumFlyingHeight(); + + altitude = getRandomFlyingHeight(); if (posY < minSpawnHeight) { - minSpawnHeight += world.rand.nextInt(Math.max(1, world.provider.getActualHeight() - (int)minSpawnHeight)); + minSpawnHeight += world.rand.nextInt(Math.max(1, (int)getMaximumFlyingHeight() - (int)minSpawnHeight)); setLocationAndAngles(posX, minSpawnHeight - 1, posZ, rotationYaw, rotationPitch); collideWithNearbyEntities();