mirror of
https://github.com/Sollace/Unicopia.git
synced 2025-02-01 11:36:43 +01:00
Move physics logic into the physics class and properly reset navigation/eye level when gravity changes
This commit is contained in:
parent
8f75122d79
commit
ca1fda7de2
7 changed files with 98 additions and 62 deletions
|
@ -31,7 +31,7 @@ public class Creature extends Living<LivingEntity> {
|
|||
|
||||
public static void boostrap() {}
|
||||
|
||||
private final Physics physics = new EntityPhysics<>(this, GRAVITY);
|
||||
private final EntityPhysics<Creature> physics = new EntityPhysics<>(this, GRAVITY);
|
||||
|
||||
public Creature(LivingEntity entity) {
|
||||
super(entity, EFFECT);
|
||||
|
@ -52,6 +52,13 @@ public class Creature extends Living<LivingEntity> {
|
|||
builder.add(PlayerAttributes.ENTITY_GRAVTY_MODIFIER);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
super.tick();
|
||||
physics.tick();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Race getSpecies() {
|
||||
return Race.HUMAN;
|
||||
|
|
|
@ -10,21 +10,26 @@ import net.minecraft.block.BlockState;
|
|||
import net.minecraft.block.FenceGateBlock;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.entity.damage.DamageSource;
|
||||
import net.minecraft.entity.data.TrackedData;
|
||||
import net.minecraft.entity.mob.MobEntity;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.particle.BlockStateParticleEffect;
|
||||
import net.minecraft.particle.ParticleTypes;
|
||||
import net.minecraft.tag.BlockTags;
|
||||
import net.minecraft.util.Tickable;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
|
||||
public class EntityPhysics<T extends Owned<? extends Entity>> implements Physics, Copieable<EntityPhysics<T>> {
|
||||
public class EntityPhysics<T extends Owned<? extends Entity>> implements Physics, Copieable<EntityPhysics<T>>, Tickable {
|
||||
|
||||
private final TrackedData<Float> gravity;
|
||||
|
||||
protected final T pony;
|
||||
|
||||
private float lastGravity = 1;
|
||||
|
||||
public EntityPhysics(T pony, TrackedData<Float> gravity) {
|
||||
this(pony, gravity, true);
|
||||
}
|
||||
|
@ -38,6 +43,37 @@ public class EntityPhysics<T extends Owned<? extends Entity>> implements Physics
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
Entity entity = pony.getMaster();
|
||||
|
||||
if (isGravityNegative()) {
|
||||
if (entity.getY() > entity.world.getHeight() + 64) {
|
||||
entity.damage(DamageSource.OUT_OF_WORLD, 4.0F);
|
||||
}
|
||||
|
||||
entity.setOnGround(!entity.world.isAir(new BlockPos(entity.getX(), Math.floor(entity.getBoundingBox().maxY + 0.25), entity.getZ())));
|
||||
}
|
||||
|
||||
float gravity = this.getGravityModifier();
|
||||
if (gravity != lastGravity) {
|
||||
lastGravity = gravity;
|
||||
|
||||
onGravitychanged();
|
||||
}
|
||||
}
|
||||
|
||||
protected void onGravitychanged() {
|
||||
Entity entity = pony.getMaster();
|
||||
|
||||
entity.calculateDimensions();
|
||||
|
||||
if (!entity.world.isClient && entity instanceof MobEntity) {
|
||||
((MobEntity)entity).getNavigation().stop();
|
||||
((MobEntity)entity).setTarget(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFlying() {
|
||||
return false;
|
||||
|
@ -135,4 +171,5 @@ public class EntityPhysics<T extends Owned<? extends Entity>> implements Physics
|
|||
public void fromNBT(CompoundTag compound) {
|
||||
setBaseGravityModifier(compound.getFloat("gravity"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ import com.minelittlepony.unicopia.item.enchantment.UEnchantments;
|
|||
import com.minelittlepony.unicopia.util.VecHelper;
|
||||
|
||||
import net.minecraft.enchantment.EnchantmentHelper;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.ItemEntity;
|
||||
import net.minecraft.entity.MovementType;
|
||||
import net.minecraft.entity.data.DataTracker;
|
||||
|
@ -21,16 +20,15 @@ import net.minecraft.nbt.CompoundTag;
|
|||
import net.minecraft.particle.ParticleEffect;
|
||||
import net.minecraft.particle.ParticleTypes;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.Tickable;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
|
||||
public class ItemImpl implements Equine<ItemEntity>, Owned<ItemEntity> {
|
||||
private static final TrackedData<Integer> ITEM_RACE = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.INTEGER);
|
||||
private static final TrackedData<Float> ITEM_GRAVITY = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
||||
static final TrackedData<Float> ITEM_GRAVITY = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
||||
|
||||
private final ItemEntity owner;
|
||||
|
||||
private final ItemPhysics physics = new ItemPhysics();
|
||||
private final ItemPhysics physics = new ItemPhysics(this);
|
||||
|
||||
private Race serverRace;
|
||||
|
||||
|
@ -141,49 +139,6 @@ public class ItemImpl implements Equine<ItemEntity>, Owned<ItemEntity> {
|
|||
ActionResult onGroundTick(IItemEntity entity);
|
||||
}
|
||||
|
||||
class ItemPhysics extends EntityPhysics<ItemImpl> implements Tickable {
|
||||
|
||||
private float serverGravity;
|
||||
|
||||
public ItemPhysics() {
|
||||
super(ItemImpl.this, ITEM_GRAVITY, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
if (!owner.world.isClient) {
|
||||
float gravity = getBaseGravityModifier();
|
||||
if (gravity != serverGravity) {
|
||||
serverGravity = gravity;
|
||||
setBaseGravityModifier(gravity == 0 ? 1 : gravity * 2);
|
||||
setBaseGravityModifier(gravity);
|
||||
}
|
||||
}
|
||||
|
||||
if (isGravityNegative() && !owner.getStack().isEmpty()) {
|
||||
owner.setNoGravity(true);
|
||||
owner.addVelocity(
|
||||
0,
|
||||
0.04
|
||||
+ calcGravity(-0.04D), // apply our own
|
||||
0
|
||||
);
|
||||
|
||||
if (!owner.isOnGround()
|
||||
|| Entity.squaredHorizontalLength(owner.getVelocity()) > 9.999999747378752E-6D) {
|
||||
|
||||
float above = 0.98f;
|
||||
if (owner.verticalCollision) {
|
||||
above *= owner.world.getBlockState(owner.getBlockPos().up()).getBlock().getSlipperiness();
|
||||
//above /= 9;
|
||||
}
|
||||
|
||||
owner.setVelocity(owner.getVelocity().multiply(above, 1, above));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface ClingyItem {
|
||||
ClingyItem DEFAULT = stack -> {
|
||||
return EnchantmentHelper.getLevel(UEnchantments.CLINGY, stack) > 0;
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
package com.minelittlepony.unicopia.entity;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.ItemEntity;
|
||||
|
||||
class ItemPhysics extends EntityPhysics<ItemImpl> {
|
||||
public ItemPhysics(ItemImpl itemImpl) {
|
||||
super(itemImpl, ItemImpl.ITEM_GRAVITY, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
super.tick();
|
||||
|
||||
ItemEntity owner = pony.getMaster();
|
||||
|
||||
if (isGravityNegative() && !owner.getStack().isEmpty()) {
|
||||
owner.setNoGravity(true);
|
||||
owner.addVelocity(
|
||||
0,
|
||||
0.04
|
||||
+ calcGravity(-0.04D), // apply our own
|
||||
0
|
||||
);
|
||||
|
||||
if (!owner.isOnGround()
|
||||
|| Entity.squaredHorizontalLength(owner.getVelocity()) > 9.999999747378752E-6D) {
|
||||
|
||||
float above = 0.98f;
|
||||
if (owner.verticalCollision) {
|
||||
above *= owner.world.getBlockState(owner.getBlockPos().up()).getBlock().getSlipperiness();
|
||||
//above /= 9;
|
||||
}
|
||||
|
||||
owner.setVelocity(owner.getVelocity().multiply(above, 1, above));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onGravitychanged() {
|
||||
if (!pony.getMaster().world.isClient) {
|
||||
float gravity = this.getBaseGravityModifier();
|
||||
setBaseGravityModifier(gravity == 0 ? 1 : gravity * 2);
|
||||
setBaseGravityModifier(gravity);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,7 +19,6 @@ import net.minecraft.entity.projectile.ProjectileEntity;
|
|||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
public abstract class Living<T extends LivingEntity> implements Equine<T>, Caster<T> {
|
||||
|
||||
|
@ -97,13 +96,6 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
|
|||
prevSneaking = entity.isSneaking();
|
||||
prevLanded = entity.isOnGround();
|
||||
|
||||
if (getPhysics().isGravityNegative() && entity.getY() > entity.world.getHeight() + 64) {
|
||||
entity.damage(DamageSource.OUT_OF_WORLD, 4.0F);
|
||||
}
|
||||
if (getPhysics().isGravityNegative()) {
|
||||
entity.setOnGround(!entity.world.isAir(new BlockPos(entity.getX(), Math.floor(entity.getBoundingBox().maxY + 0.25), entity.getZ())));
|
||||
}
|
||||
|
||||
enchants.tick();
|
||||
}
|
||||
|
||||
|
|
|
@ -64,6 +64,8 @@ public class PlayerPhysics extends EntityPhysics<Pony> implements Tickable, Moti
|
|||
|
||||
@Override
|
||||
public void tick() {
|
||||
super.tick();
|
||||
|
||||
if (wallHitCooldown > 0) {
|
||||
wallHitCooldown--;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import java.util.Map;
|
|||
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.minelittlepony.unicopia.AwaitTickQueue;
|
||||
import com.minelittlepony.unicopia.entity.Living;
|
||||
|
||||
import net.minecraft.enchantment.EnchantmentTarget;
|
||||
|
@ -44,7 +43,6 @@ public class AttributedEnchantment extends SimpleEnchantment {
|
|||
instance.removeModifier(modifier.getId());
|
||||
instance.addPersistentModifier(modifier);
|
||||
});
|
||||
entity.calculateDimensions();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,9 +55,6 @@ public class AttributedEnchantment extends SimpleEnchantment {
|
|||
instance.tryRemoveModifier(modifierSupplier.get(user, 1).getId());
|
||||
});
|
||||
user.getEnchants().remove(this);
|
||||
AwaitTickQueue.scheduleTask(entity.world, w -> {
|
||||
entity.calculateDimensions();
|
||||
}, 1);
|
||||
}
|
||||
|
||||
public void getModifiers(Living<?> user, int level, Map<EquipmentSlot, Multimap<EntityAttribute, EntityAttributeModifier>> output) {
|
||||
|
|
Loading…
Reference in a new issue