superb-warfare/src/main/java/com/atsuishio/superbwarfare/entity/WheelChairEntity.java
2025-01-02 10:57:28 +08:00

297 lines
12 KiB
Java

package com.atsuishio.superbwarfare.entity;
import com.atsuishio.superbwarfare.config.server.ExplosionDestroyConfig;
import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModEntities;
import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.tools.CustomExplosion;
import com.atsuishio.superbwarfare.tools.EntityFindUtil;
import com.atsuishio.superbwarfare.tools.ParticleTool;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.*;
import net.minecraft.world.entity.animal.WaterAnimal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.network.NetworkHooks;
import net.minecraftforge.network.PlayMessages;
import org.joml.Math;
import software.bernie.geckolib.animatable.GeoEntity;
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.core.animation.AnimatableManager;
import software.bernie.geckolib.util.GeckoLibUtil;
import java.util.List;
public class WheelChairEntity extends MobileVehicleEntity implements GeoEntity {
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
public static final float MAX_HEALTH = 50;
public static final int MAX_ENERGY = 24000;
public float leftWheelRot;
public float rightWheelRot;
public float leftWheelRotO;
public float rightWheelRotO;
public int jumpCoolDown;
public int handBusyTime;
public WheelChairEntity(PlayMessages.SpawnEntity packet, Level world) {
this(ModEntities.WHEEL_CHAIR.get(), world);
}
public WheelChairEntity(EntityType<WheelChairEntity> type, Level world) {
super(type, world);
this.setMaxUpStep(1.1f);
}
@Override
public void playerTouch(Player pPlayer) {
if (this.position().distanceTo(pPlayer.position()) > 1.4) return;
if (!this.level().isClientSide) {
double entitySize = pPlayer.getBbWidth() * pPlayer.getBbHeight();
double thisSize = this.getBbWidth() * this.getBbHeight();
double f = Math.min(entitySize / thisSize, 2);
this.setDeltaMovement(this.getDeltaMovement().add(new Vec3(pPlayer.position().vectorTo(this.position()).toVector3f()).scale(0.5 * f * pPlayer.getDeltaMovement().length())));
this.setYRot(pPlayer.getYHeadRot());
}
}
@Override
protected void defineSynchedData() {
super.defineSynchedData();
}
@Override
public void addAdditionalSaveData(CompoundTag compound) {
super.addAdditionalSaveData(compound);
}
@Override
public void readAdditionalSaveData(CompoundTag compound) {
super.readAdditionalSaveData(compound);
}
@Override
protected float getEyeHeight(Pose pPose, EntityDimensions pSize) {
return 0.75F;
}
@Override
public Packet<ClientGamePacketListener> getAddEntityPacket() {
return NetworkHooks.getEntitySpawningPacket(this);
}
@Override
public boolean hurt(DamageSource source, float amount) {
super.hurt(source, amount);
this.level().playSound(null, this.getOnPos(), ModSounds.HIT.get(), SoundSource.PLAYERS, 1, 1);
this.hurt(amount);
return true;
}
@Override
public void baseTick() {
leftWheelRotO = this.getLeftWheelRot();
rightWheelRotO = this.getRightWheelRot();
if (jumpCoolDown > 0 && onGround()) {
jumpCoolDown--;
}
if (handBusyTime > 0) {
handBusyTime--;
}
super.baseTick();
this.setDeltaMovement(this.getDeltaMovement().add(0.0, -0.048, 0.0));
if (this.onGround()) {
float f = (float) Mth.clamp(0.85f + 0.05f * Mth.abs(90 - (float) calculateAngle(this.getDeltaMovement(), this.getViewVector(1))) / 90, 0.01, 0.99);
this.setDeltaMovement(this.getDeltaMovement().multiply(f, 0.95, f));
} else {
this.setDeltaMovement(this.getDeltaMovement().multiply(0.99, 0.95, 0.99));
}
this.setSprinting(this.getDeltaMovement().horizontalDistance() > 0.15);
attractEntity();
this.refreshDimensions();
}
public boolean hasEnoughSpaceFor(Entity pEntity) {
return pEntity.getBbWidth() < this.getBbWidth();
}
public void attractEntity() {
List<Entity> list = this.level().getEntities(this, this.getBoundingBox().inflate(0.2F, -0.01F, 0.2F));
if (!list.isEmpty()) {
boolean flag = !this.level().isClientSide && !(this.getControllingPassenger() instanceof Player);
for (Entity entity : list) {
if (!entity.hasPassenger(this) && flag && !entity.isPassenger() && this.hasEnoughSpaceFor(entity) && (entity instanceof LivingEntity || entity instanceof MortarEntity) && !(entity instanceof WaterAnimal) && !(entity instanceof Player)) {
entity.startRiding(this);
}
}
}
}
@Override
public void travel() {
Entity passenger = this.getPassengers().isEmpty() ? null : this.getPassengers().get(0);
float diffY = 0;
if (passenger == null) {
this.leftInputDown = false;
this.rightInputDown = false;
this.forwardInputDown = false;
this.backInputDown = false;
} else if (passenger instanceof Player) {
if (level().isClientSide && this.getEnergy() > 0) {
level().playLocalSound(this.getX(), this.getY() + this.getBbHeight() * 0.5, this.getZ(), this.getEngineSound(), this.getSoundSource(), Math.min((this.forwardInputDown || this.backInputDown ? 7.5f : 5f) * 2 * Mth.abs(this.entityData.get(POWER)), 0.25f), (random.nextFloat() * 0.1f + 1f), false);
}
diffY = Math.clamp(-90f, 90f, Mth.wrapDegrees(passenger.getYHeadRot() - this.getYRot()));
this.setYRot(this.getYRot() + Mth.clamp(0.4f * diffY, -5f, 5f));
}
if (this.forwardInputDown) {
this.entityData.set(POWER, this.entityData.get(POWER) + 0.01f);
if (this.getEnergy() <= 0 && passenger instanceof Player player) {
moveWithOutPower(player, true);
}
}
if (this.backInputDown) {
this.entityData.set(POWER, this.entityData.get(POWER) - 0.01f);
if (this.getEnergy() <= 0 && passenger instanceof Player player) {
moveWithOutPower(player, false);
}
}
if (this.upInputDown && this.onGround() && this.getEnergy() > 400 && jumpCoolDown == 0) {
if (passenger instanceof ServerPlayer serverPlayer) {
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.WHEEL_CHAIR_JUMP.get(), SoundSource.PLAYERS, 1, 1);
}
this.extraEnergy(400);
this.setDeltaMovement(this.getDeltaMovement().add(0, 0.48, 0));
jumpCoolDown = 3;
}
if (this.forwardInputDown || this.backInputDown) {
this.extraEnergy(1);
}
if (passenger instanceof Player player && player.level().isClientSide && this.handBusyTime > 0) {
var localPlayer = Minecraft.getInstance().player;
if (localPlayer != null && player.getUUID().equals(localPlayer.getUUID())) {
localPlayer.handsBusy = true;
}
}
this.entityData.set(POWER, this.entityData.get(POWER) * 0.87f);
float angle = (float) calculateAngle(this.getDeltaMovement(), this.getViewVector(1));
double s0;
if (Mth.abs(angle) < 90) {
s0 = this.getDeltaMovement().horizontalDistance();
} else {
s0 = -this.getDeltaMovement().horizontalDistance();
}
this.setLeftWheelRot((float) (this.getLeftWheelRot() - 1.25 * s0) - 0.015f * Mth.clamp(0.4f * diffY, -5f, 5f));
this.setRightWheelRot((float) (this.getRightWheelRot() - 1.25 * s0) + 0.015f * Mth.clamp(0.4f * diffY, -5f, 5f));
this.setDeltaMovement(this.getDeltaMovement().add(Mth.sin(-this.getYRot() * 0.017453292F) * (this.onGround() ? 1 : 0.1) * this.entityData.get(POWER), 0.0, Mth.cos(this.getYRot() * 0.017453292F) * (this.onGround() ? 1 : 0.1) * this.entityData.get(POWER)));
}
public void moveWithOutPower(Player player, boolean forward) {
this.entityData.set(POWER, this.entityData.get(POWER) + (forward ? 0.015f : -0.015f));
if (player instanceof ServerPlayer serverPlayer) {
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), SoundEvents.BOAT_PADDLE_LAND, SoundSource.PLAYERS, 1, 1);
}
player.causeFoodExhaustion(0.03F);
this.handBusyTime = 4;
this.forwardInputDown = false;
this.backInputDown = false;
}
@Override
public SoundEvent getEngineSound() {
return ModSounds.WHEEL_CHAIR_ENGINE.get();
}
public float getLeftWheelRot() {
return this.leftWheelRot;
}
public void setLeftWheelRot(float pLeftWheelRot) {
this.leftWheelRot = pLeftWheelRot;
}
public float getRightWheelRot() {
return this.rightWheelRot;
}
public void setRightWheelRot(float pRightWheelRot) {
this.rightWheelRot = pRightWheelRot;
}
protected void clampRotation(Entity entity) {
entity.setYBodyRot(this.getYRot());
float f2 = Mth.wrapDegrees(entity.getYRot() - this.getYRot());
float f3 = Mth.clamp(f2, -90F, 90.0F);
entity.yRotO += f3 - f2;
entity.setYRot(entity.getYRot() + f3 - f2);
entity.setYHeadRot(entity.getYRot());
}
@Override
public void onPassengerTurned(Entity entity) {
this.clampRotation(entity);
}
@Override
public void destroy() {
if (level() instanceof ServerLevel) {
Entity attacker = EntityFindUtil.findEntity(this.level(), this.entityData.get(LAST_ATTACKER_UUID));
CustomExplosion explosion = new CustomExplosion(this.level(), attacker == null ? this : attacker,
ModDamageTypes.causeCustomExplosionDamage(this.level().registryAccess(), attacker == null ? this : attacker, attacker == null ? this : attacker), 25.0f,
this.getX(), this.getY(), this.getZ(), 5f, ExplosionDestroyConfig.EXPLOSION_DESTROY.get() ? Explosion.BlockInteraction.DESTROY : Explosion.BlockInteraction.KEEP).setDamageMultiplier(1);
explosion.explode();
net.minecraftforge.event.ForgeEventFactory.onExplosionStart(this.level(), explosion);
explosion.finalizeExplosion(false);
ParticleTool.spawnMediumExplosionParticles(this.level(), this.position());
this.discard();
}
}
@Override
public void registerControllers(AnimatableManager.ControllerRegistrar data) {
}
@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.cache;
}
@Override
public float getMaxHealth() {
return MAX_HEALTH;
}
@Override
public int getMaxEnergy() {
return MAX_ENERGY;
}
}