优化无人机的飞控

This commit is contained in:
Atsuihsio 2024-07-28 20:06:28 +08:00
parent f4fee211e9
commit ec88c89e92
6 changed files with 161 additions and 96 deletions

View file

@ -71,10 +71,13 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
public static final EntityDataAccessor<String> CONTROLLER = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.STRING);
public static final EntityDataAccessor<Integer> AMMO = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.INT);
public static final EntityDataAccessor<Boolean> KAMIKAZE = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.BOOLEAN);
public static final EntityDataAccessor<Float> MOVEX = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> MOVEY = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> MOVEZ = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> ROTX = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> ROTZ = SynchedEntityData.defineId(DroneEntity.class, EntityDataSerializers.FLOAT);
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private float moveX = 0;
private float moveY = 0;
private float moveZ = 0;
private boolean move = false;
public String animationprocedure = "empty";
@ -92,9 +95,9 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
public DroneEntity(EntityType<? extends DroneEntity> type, Level world, float moveX, float moveY, float moveZ) {
super(type, world);
this.moveX = moveX;
this.moveY = moveY;
this.moveZ = moveZ;
// this.moveX = moveX;
// this.moveY = moveY;
// this.moveZ = moveZ;
}
@Override
@ -105,6 +108,11 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
this.entityData.define(LINKED, false);
this.entityData.define(AMMO, 0);
this.entityData.define(KAMIKAZE, false);
this.entityData.define(MOVEX, 0f);
this.entityData.define(MOVEY, 0f);
this.entityData.define(MOVEZ, 0f);
this.entityData.define(ROTX, 0f);
this.entityData.define(ROTZ, 0f);
}
@Override
@ -145,6 +153,11 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
compound.putString("Controller", this.entityData.get(CONTROLLER));
compound.putInt("ammo", this.entityData.get(AMMO));
compound.putBoolean("Kamikaze", this.entityData.get(KAMIKAZE));
compound.putFloat("moveX", this.entityData.get(MOVEX));
compound.putFloat("moveY", this.entityData.get(MOVEY));
compound.putFloat("moveZ", this.entityData.get(MOVEZ));
compound.putFloat("rotX", this.entityData.get(ROTX));
compound.putFloat("rotZ", this.entityData.get(ROTZ));
}
@Override
@ -159,6 +172,16 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
this.entityData.set(AMMO, compound.getInt("ammo"));
if (compound.contains("Kamikaze"))
this.entityData.set(KAMIKAZE, compound.getBoolean("Kamikaze"));
if (compound.contains("moveX"))
this.entityData.set(MOVEX, compound.getFloat("moveX"));
if (compound.contains("moveY"))
this.entityData.set(MOVEY, compound.getFloat("moveY"));
if (compound.contains("moveZ"))
this.entityData.set(MOVEZ, compound.getFloat("moveZ"));
if (compound.contains("rotX"))
this.entityData.set(ROTX, compound.getFloat("rotX"));
if (compound.contains("rotZ"))
this.entityData.set(ROTZ, compound.getFloat("rotZ"));
}
@Override
@ -166,52 +189,67 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
super.baseTick();
if (this.getPersistentData().getBoolean("left")) {
this.moveX = -1.5f;
this.entityData.set(MOVEX,-1.5f);
this.entityData.set(ROTX,this.entityData.get(ROTX) + 0.13f);
}
if (this.getPersistentData().getBoolean("right")) {
this.moveX = 1.5f;
this.entityData.set(MOVEX,1.5f);
this.entityData.set(ROTX,this.entityData.get(ROTX) - 0.13f);
}
if (this.entityData.get(ROTX) > 0) {
this.entityData.set(ROTX, Mth.clamp(this.entityData.get(ROTX) - 0.55f * (float) Math.pow(this.entityData.get(ROTX),2), 0, 1f));
} else {
this.entityData.set(ROTX, Mth.clamp(this.entityData.get(ROTX) + 0.55f * (float) Math.pow(this.entityData.get(ROTX),2), -1f, 0));
}
if (!this.getPersistentData().getBoolean("left") && !this.getPersistentData().getBoolean("right")) {
if (this.moveX >= 0) {
this.moveX = Mth.clamp(this.moveX - 0.3f, 0, 1);
if (this.entityData.get(MOVEX) >= 0) {
this.entityData.set(MOVEX,Mth.clamp(this.entityData.get(MOVEX) - 0.3f, 0, 1));
} else {
this.moveX = Mth.clamp(this.moveX + 0.3f, -1, 0);
this.entityData.set(MOVEX,Mth.clamp(this.entityData.get(MOVEX) + 0.3f, -1, 0));
}
}
if (this.getPersistentData().getBoolean("forward")) {
this.moveZ = -1.5f;
this.entityData.set(MOVEZ,this.entityData.get(MOVEZ) - 0.15f);
this.entityData.set(ROTZ,this.entityData.get(ROTZ) - 0.13f);
}
if (this.getPersistentData().getBoolean("backward")) {
this.moveZ = 1.5f;
this.entityData.set(MOVEZ,this.entityData.get(MOVEZ) + 0.15f);
this.entityData.set(ROTZ,this.entityData.get(ROTZ) + 0.13f);
}
if (!this.getPersistentData().getBoolean("forward") && !this.getPersistentData().getBoolean("backward")) {
if (this.moveZ >= 0) {
this.moveZ = Mth.clamp(this.moveZ - 0.3f, 0, 1);
} else {
this.moveZ = Mth.clamp(this.moveZ + 0.3f, -1, 0);
}
if (this.entityData.get(ROTZ) > 0) {
this.entityData.set(ROTZ, Mth.clamp(this.entityData.get(ROTZ) - 0.55f * (float) Math.pow(this.entityData.get(ROTZ),2), 0, 1f));
} else {
this.entityData.set(ROTZ, Mth.clamp(this.entityData.get(ROTZ) + 0.55f * (float) Math.pow(this.entityData.get(ROTZ),2), -1f, 0));
}
if (this.entityData.get(MOVEZ) >= 0) {
this.entityData.set(MOVEZ,Mth.clamp(this.entityData.get(MOVEZ) - 0.1f, 0, 1));
} else {
this.entityData.set(MOVEZ,Mth.clamp(this.entityData.get(MOVEZ) + 0.1f, -1, 0));
}
if (this.getPersistentData().getBoolean("up")) {
this.moveY = -1.5f;
this.entityData.set(MOVEY,-1.5f);
}
if (this.getPersistentData().getBoolean("down")) {
this.moveY = 1.5f;
this.entityData.set(MOVEY,1.5f);
}
if (this.entityData.get(MOVEY) >= 0) {
this.entityData.set(MOVEY,Mth.clamp(this.entityData.get(MOVEY) - 0.3f, 0, 1));
} else {
this.entityData.set(MOVEY,Mth.clamp(this.entityData.get(MOVEY)+ 0.3f, -1, 0));
}
if (!this.getPersistentData().getBoolean("up") && !this.getPersistentData().getBoolean("down")) {
if (this.moveY >= 0) {
this.moveY = Mth.clamp(this.moveY - 0.3f, 0, 1);
} else {
this.moveY = Mth.clamp(this.moveY + 0.3f, -1, 0);
}
}
LivingEntity control = this.level().getEntitiesOfClass(Player.class, this.getBoundingBox().inflate(512))
.stream().filter(e -> e.getStringUUID().equals(this.entityData.get(CONTROLLER))).findFirst().orElse(null);
this.setDeltaMovement(new Vec3(
this.getDeltaMovement().x + -this.entityData.get(MOVEZ) * 0.1f * this.getLookAngle().x,
this.getDeltaMovement().y + -this.entityData.get(MOVEY) * 0.05f,
this.getDeltaMovement().z + -this.entityData.get(MOVEZ) * 0.1f * this.getLookAngle().z
));
this.move = this.getPersistentData().getBoolean("left")
|| this.getPersistentData().getBoolean("right")
@ -220,6 +258,16 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
|| this.getPersistentData().getBoolean("up")
|| this.getPersistentData().getBoolean("down");
Vec3 vec = this.getDeltaMovement();
if (this.getDeltaMovement().horizontalDistanceSqr() < 0.75) {
if (this.move) {
this.setDeltaMovement(vec.multiply(1.04, 1, 1.04));
}
}
LivingEntity control = this.level().getEntitiesOfClass(Player.class, this.getBoundingBox().inflate(512))
.stream().filter(e -> e.getStringUUID().equals(this.entityData.get(CONTROLLER))).findFirst().orElse(null);
if (!this.onGround()) {
this.level().playSound(null, this.getOnPos(), ModSounds.DRONE_SOUND.get(), SoundSource.AMBIENT, 3, 1);
if (control != null) {
@ -230,12 +278,6 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
}
}
Vec3 vec = this.getDeltaMovement();
if (this.getDeltaMovement().horizontalDistanceSqr() < 1) {
if (this.move) {
this.setDeltaMovement(vec.multiply(1.04, 1, 1.04));
}
}
if (this.getPersistentData().getBoolean("firing")) {
if (control instanceof Player player) {
@ -349,23 +391,21 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
this.setRot(this.getYRot(), this.getXRot());
this.yBodyRot = control.getYRot();
this.yHeadRot = control.getYRot();
this.setMaxUpStep(1.0F);
this.setSpeed(4 * (float) this.getAttributeValue(Attributes.MOVEMENT_SPEED));
float forward = -moveZ;
float upDown = -moveY;
float strafe = -moveX;
super.travel(new Vec3(2 * strafe, 2 * upDown, 2 * forward));
this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED));
float strafe = -this.entityData.get(MOVEX);
super.travel(new Vec3(2 * strafe, -this.entityData.get(MOVEY), -this.entityData.get(MOVEZ)));
Vec3 vec3 = this.getDeltaMovement();
if (this.onGround()) {
this.setDeltaMovement(vec3.multiply(0.7, 0.98, 0.7));
} else {
this.setDeltaMovement(vec3.multiply(1.04, 0.98, 1.04));
}
if (!this.move) {
this.setDeltaMovement(vec3.multiply(0.9, 0.8, 0.9));
} else {
this.setDeltaMovement(vec3.multiply(1.05, 0.99, 1.05));
}
return;
}
}
this.setMaxUpStep(0.5F);
super.travel(dir);
}
@ -441,12 +481,12 @@ public class DroneEntity extends PathfinderMob implements GeoEntity {
public static AttributeSupplier.Builder createAttributes() {
AttributeSupplier.Builder builder = Mob.createMobAttributes();
builder = builder.add(Attributes.MOVEMENT_SPEED, 0.1);
builder = builder.add(Attributes.MOVEMENT_SPEED, 0);
builder = builder.add(Attributes.MAX_HEALTH, 4);
builder = builder.add(Attributes.ARMOR, 0);
builder = builder.add(Attributes.ATTACK_DAMAGE, 0);
builder = builder.add(Attributes.FOLLOW_RANGE, 64);
builder = builder.add(Attributes.FLYING_SPEED, 0.1);
builder = builder.add(Attributes.FLYING_SPEED, 10);
return builder;
}

View file

@ -4,15 +4,12 @@ import net.mcreator.superbwarfare.ModUtils;
import software.bernie.geckolib.core.animatable.model.CoreGeoBone;
import software.bernie.geckolib.core.animation.AnimationState;
import software.bernie.geckolib.model.GeoModel;
import net.minecraft.resources.ResourceLocation;
import net.mcreator.superbwarfare.entity.DroneEntity;
import static net.mcreator.superbwarfare.entity.DroneEntity.AMMO;
import static net.mcreator.superbwarfare.entity.DroneEntity.KAMIKAZE;
import static net.mcreator.superbwarfare.entity.DroneEntity.*;
public class DroneModel extends GeoModel<DroneEntity> {
@Override
public ResourceLocation getAnimationResource(DroneEntity entity) {
return new ResourceLocation(ModUtils.MODID, "animations/drone.animation.json");
@ -37,6 +34,7 @@ public class DroneModel extends GeoModel<DroneEntity> {
CoreGeoBone ammo5 = getAnimationProcessor().getBone("ammo5");
CoreGeoBone ammo6 = getAnimationProcessor().getBone("ammo6");
CoreGeoBone shell = getAnimationProcessor().getBone("shell");
CoreGeoBone body = getAnimationProcessor().getBone("0");
ammo6.setHidden(animatable.getEntityData().get(AMMO) <= 5);
ammo5.setHidden(animatable.getEntityData().get(AMMO) <= 4);
@ -45,5 +43,9 @@ public class DroneModel extends GeoModel<DroneEntity> {
ammo2.setHidden(animatable.getEntityData().get(AMMO) <= 1);
ammo1.setHidden(animatable.getEntityData().get(AMMO) <= 0);
shell.setHidden(!animatable.getEntityData().get(KAMIKAZE));
body.setRotZ(animatable.getEntityData().get(ROTX));
body.setRotX(animatable.getEntityData().get(ROTZ));
}
}

View file

@ -28,6 +28,9 @@ import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import static net.mcreator.superbwarfare.entity.DroneEntity.ROTX;
import static net.mcreator.superbwarfare.entity.DroneEntity.ROTZ;
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class ClientEventHandler {
@ -48,6 +51,9 @@ public class ClientEventHandler {
data.putDouble("Cannon_xRot", Mth.clamp(0.2 * xRot, -3, 3));
data.putDouble("Cannon_yRot", Mth.clamp(1 * yRot, -15, 15));
data.putDouble("droneCameraRotX", Mth.clamp(0.5f * xRot, -10, 10));
data.putDouble("droneCameraRotY", Mth.clamp(0.25f * yRot, -20, 10));
}
@SubscribeEvent
@ -57,6 +63,12 @@ public class ClientEventHandler {
if (level != null && entity instanceof LivingEntity living && entity.isPassenger() && entity.getVehicle() instanceof Mk42Entity) {
handleCannonCamera(event, living);
}
if (level != null && entity instanceof LivingEntity living
&& living.getMainHandItem().is(ModItems.MONITOR.get())
&& living.getMainHandItem().getOrCreateTag().getBoolean("Using")
&& living.getMainHandItem().getOrCreateTag().getBoolean("Linked")) {
handleDroneCamera(event, living);
}
if (level != null && entity instanceof LivingEntity living && living.getMainHandItem().is(ModTags.Items.GUN)) {
handleWeaponCrossHair(living);
handleWeaponSway(living);
@ -69,6 +81,36 @@ public class ClientEventHandler {
}
}
private static void handleDroneCamera(ViewportEvent.ComputeCameraAngles event, LivingEntity entity) {
var data = entity.getPersistentData();
ItemStack stack = entity.getMainHandItem();
double pitch = event.getPitch();
double roll = event.getRoll();
DroneEntity drone = entity.level().getEntitiesOfClass(DroneEntity.class, entity.getBoundingBox().inflate(512))
.stream().filter(e -> e.getStringUUID().equals(stack.getOrCreateTag().getString("LinkedDrone"))).findFirst().orElse(null);
if (drone != null) {
if (data.getDouble("droneRotZ") > drone.getEntityData().get(ROTZ)) {
data.putDouble("droneRotZ", Mth.clamp(data.getDouble("droneRotZ") - 0.3 * Math.pow(drone.getEntityData().get(ROTZ) - data.getDouble("droneRotZ"), 2),drone.getEntityData().get(ROTZ),Double.POSITIVE_INFINITY));
} else {
data.putDouble("droneRotZ", Mth.clamp(data.getDouble("droneRotZ") + 0.3 * Math.pow(drone.getEntityData().get(ROTZ) - data.getDouble("droneRotZ"), 2),Double.NEGATIVE_INFINITY,drone.getEntityData().get(ROTZ)));
}
if (data.getDouble("droneRotX") > drone.getEntityData().get(ROTX)) {
data.putDouble("droneRotX", Mth.clamp(data.getDouble("droneRotX") - 0.2 * Math.pow(drone.getEntityData().get(ROTX) - data.getDouble("droneRotX"), 2),drone.getEntityData().get(ROTX),Double.POSITIVE_INFINITY));
} else {
data.putDouble("droneRotX", Mth.clamp(data.getDouble("droneRotX") + 0.2 * Math.pow(drone.getEntityData().get(ROTX) - data.getDouble("droneRotX"), 2),Double.NEGATIVE_INFINITY,drone.getEntityData().get(ROTX)));
}
event.setPitch((float) (pitch + data.getDouble("droneCameraRotX") - 0.45f * Mth.RAD_TO_DEG * data.getDouble("droneRotZ")));
event.setRoll((float) (roll + data.getDouble("droneCameraRotY") - 0.8f * Mth.RAD_TO_DEG * data.getDouble("droneRotX")));
}
}
private static void handleCannonCamera(ViewportEvent.ComputeCameraAngles event, LivingEntity entity) {
var data = entity.getPersistentData();
double yaw = event.getYaw();

View file

@ -10,42 +10,26 @@
"bones": {
"wing": {
"rotation": {
"0.0": {
"vector": [0, 0, 0]
},
"0.125": {
"vector": [0, 360, 0]
}
"0.0": [0, 0, 0],
"0.125": [0, 360, 0]
}
},
"wing2": {
"rotation": {
"0.0": {
"vector": [0, 0, 0]
},
"0.125": {
"vector": [0, -360, 0]
}
"0.0": [0, 0, 0],
"0.125": [0, -360, 0]
}
},
"wing3": {
"rotation": {
"0.0": {
"vector": [0, 0, 0]
},
"0.125": {
"vector": [0, -360, 0]
}
"0.0": [0, 0, 0],
"0.125": [0, -360, 0]
}
},
"wing4": {
"rotation": {
"0.0": {
"vector": [0, 0, 0]
},
"0.125": {
"vector": [0, 360, 0]
}
"0.0": [0, 0, 0],
"0.125": [0, 360, 0]
}
}
}

View file

@ -1366,10 +1366,11 @@
"0": {
"rotation": {
"0.125": {
"vector": [0, 0, 0]
"vector": [0, 0, 0],
"easing": "easeInOutSine"
},
"0.2": {
"vector": [2.2309, 1.32745, 5.98455]
"0.225": {
"vector": [-1.07, -0.74, 6.37]
},
"0.4": {
"vector": [-21.00421, 2.1987, -9.76969],
@ -1447,12 +1448,12 @@
}
},
"position": {
"0.125": {
"0.0": {
"vector": [0, 0, 0]
},
"0.35": {
"vector": [-1.28, 1.51, 2.08],
"easing": "easeInElastic"
"0.075": {
"vector": [0, 0, -1],
"easing": "easeInOutSine"
},
"0.425": {
"vector": [-1.36248, 1.69031, 2.10621]
@ -1534,11 +1535,10 @@
"0.0": {
"vector": [0, 0, 0]
},
"0.1": {
"vector": [28.632, -17.75227, 3.13551],
"easing": "linear"
"0.125": {
"vector": [16.99, -1.98, 2.69]
},
"0.4": {
"0.325": {
"vector": [-3.22206, -3.30648, 2.38196],
"easing": "linear"
},
@ -1572,11 +1572,10 @@
"0.0": {
"vector": [0, 0, 0]
},
"0.1": {
"vector": [-0.26, 1.67, 7.31],
"easing": "linear"
"0.125": {
"vector": [-0.06, 1.39, 2.39]
},
"0.4": {
"0.325": {
"vector": [-0.9, -1.5, 4.8],
"easing": "linear"
},

View file

@ -18,7 +18,7 @@
{
"name": "0",
"parent": "bone",
"pivot": [0, 0.5, 0]
"pivot": [0, 2.2, 0]
},
{
"name": "body",
@ -3320,8 +3320,6 @@
{
"origin": [-0.15062, -0.11639, 3.50804],
"size": [0.30349, 0.9159, 1.97267],
"pivot": [0.00112, 0.34156, 4.49437],
"rotation": [0, 0, 0],
"uv": {
"north": {"uv": [37, 28], "uv_size": [0.25, 1]},
"east": {"uv": [18, 13], "uv_size": [2, 1]},