Skip to content

Commit

Permalink
kamikaze drone ?
Browse files Browse the repository at this point in the history
  • Loading branch information
wefcdse committed Dec 9, 2024
1 parent 1f37451 commit e44e697
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 12 deletions.
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20.4+build.3
loader_version=0.15.7

# Mod Properties
mod_version=1.1.9
mod_version=1.1.10
maven_group=com.iung.fpv20
archives_base_name=fpv20

Expand All @@ -21,7 +21,7 @@ fabric_version=0.96.11+1.20.4
owo_version=0.11.2+1.20

#
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=28081
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=28081
#systemProp.http.proxyHost=127.0.0.1
#systemProp.http.proxyPort=28081
#systemProp.https.proxyHost=127.0.0.1
#systemProp.https.proxyPort=28081
63 changes: 63 additions & 0 deletions src/client/java/com/iung/fpv20/Explosion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.iung.fpv20;

import com.iung.fpv20.config.Fpv20ConfigClientManual;
import com.iung.fpv20.flying.GlobalFlying;
import com.iung.fpv20.network.DroneExplosionPacket;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.util.math.Vec3d;

import java.util.Objects;

import static com.iung.fpv20.flying.GlobalFlying.ZERO_SPEED;

public class Explosion {
public static boolean explosion_trig() {
if (controller() || Fpv20Client.config1.explosion_config.always_explode) {
if (Fpv20Client.config1.explosion_config.stop_fly_after_explosion) {
GlobalFlying.setFlying(false);
}
// GlobalFlying.G.drone.set_speed(new Vec3d(0,0,0));
ClientPlayNetworking.send(
new DroneExplosionPacket(
Fpv20Client.config1.explosion_config.power,
Fpv20Client.config1.explosion_config.stop_fly_after_explosion,
GlobalFlying.start_fly_pos(),
Fpv20Client.config1.explosion_config.teleport_after_explosion
));
return true;
}
return false;
}

public static boolean controller() {
float e = Objects.requireNonNull(Fpv20Client.controller).get_value_by_name("expl");
return e > 0.5;
}

private static Fpv20ConfigClientManual.ExplosionConfig.Vec3d min_speed() {
return Fpv20Client.config1.explosion_config.min_speed;
}

public static boolean handle_explosion(Vec3d last_speed, Vec3d now_speed) {
boolean trig = false;
if (Math.abs(last_speed.x) > min_speed().x() && Math.abs(now_speed.x) < ZERO_SPEED) {
trig = true;
}
if (Math.abs(last_speed.y) > min_speed().y() && Math.abs(now_speed.y) < ZERO_SPEED) {
trig = true;
}
if (Math.abs(last_speed.z) > min_speed().z() && Math.abs(now_speed.z) < ZERO_SPEED) {
trig = true;
}
// Fpv20.LOGGER.info("he {} / {}", last_speed, now_speed);

if (trig) {
Fpv20.LOGGER.info("trigged");

return explosion_trig();
}
return false;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,22 @@ public static class AngularVelocity_DegSec {

public boolean disable_player_render_when_flying = true;

public boolean throttle_display_in_center = true;
public boolean throttle_display_in_center = false;


public static class ExplosionConfig {
public float power = 8.0f;
public Vec3d min_speed = new Vec3d(1,1,1);
public boolean always_explode = false;
public boolean stop_fly_after_explosion = true;
public boolean teleport_after_explosion = true;
public record Vec3d(double x, double y, double z) {
}

}

public ExplosionConfig explosion_config = new ExplosionConfig();


/////////////////////////////////
public float getCamera_angle() {
Expand Down
31 changes: 25 additions & 6 deletions src/client/java/com/iung/fpv20/flying/GlobalFlying.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.iung.fpv20.flying;

import com.iung.fpv20.Explosion;
import com.iung.fpv20.Fpv20;
import com.iung.fpv20.Fpv20Client;
import com.iung.fpv20.input.Controller;
Expand All @@ -23,6 +24,14 @@
import static com.iung.fpv20.utils.LocalMath.DEG_TO_RAD;

public class GlobalFlying {
public static double ZERO_SPEED = 0.0000001;
private static Vec3d fly_pos = new Vec3d(0, 128, 0);

public static Vec3d start_fly_pos() {
return new Vec3d(fly_pos.x, fly_pos.y, fly_pos.z);
}


public static GlobalFlying G = new GlobalFlying(0);
// private float lastCamRoll;
// private float camRoll;
Expand All @@ -39,7 +48,7 @@ public class GlobalFlying {
private boolean last_tick_flying;

// private float cam_angel_deg;
private Drone drone;
public Drone drone;

private Vec3d last_pos;

Expand Down Expand Up @@ -94,6 +103,9 @@ public static void setFlying(boolean if_fly) {
ClientPlayerEntity player = client.player;
IsFlying p = (IsFlying) player;
if (p != null) {
if (!p.get_is_flying() && if_fly) {
fly_pos = player.getPos();
}

if (ClientPlayNetworking.canSend(DroneFlyPacket.TYPE)) {
ClientPlayNetworking.send(new DroneFlyPacket(if_fly));
Expand Down Expand Up @@ -227,19 +239,19 @@ private void _handle_flying_inner(MinecraftClient client, float dt) {
boolean to_set_z = false;


if (Math.abs(v0.x) < 0.0001) {
if (Math.abs(v0.x) < ZERO_SPEED) {
Fpv20.LOGGER.debug("process hit:x");
vd.x = 0;
to_set_y = true;
to_set_z = true;
}
if (Math.abs(v0.y) < 0.0001) {
if (Math.abs(v0.y) < ZERO_SPEED) {
Fpv20.LOGGER.debug("process hit:y");
vd.y = 0;
to_set_x = true;
to_set_z = true;
}
if (Math.abs(v0.z) < 0.0001) {
if (Math.abs(v0.z) < ZERO_SPEED) {
Fpv20.LOGGER.debug("process hit:z");
vd.z = 0;
to_set_y = true;
Expand Down Expand Up @@ -277,6 +289,8 @@ private void _handle_flying_inner(MinecraftClient client, float dt) {
}
}

Explosion.handle_explosion(drone.get_speed(), new Vec3d(vd));

drone.set_speed(new Vec3d(vd));
// // process hit

Expand Down Expand Up @@ -352,7 +366,8 @@ public void handle_flying_phy(ClientPlayerEntity player, float dt) {
float aaa = 0.5f;
boolean hit = false;

final float ZERO = 0.000001f;
// final float ZERO = 0.000001f;
final double ZERO = ZERO_SPEED;

if (Math.abs(v0.x) < ZERO) {
// hit = true;
Expand All @@ -370,17 +385,21 @@ public void handle_flying_phy(ClientPlayerEntity player, float dt) {
vd.z = 0;
}
if (hit) {
if (vd.length() > aaa * dt && vd.length() > 0.0000001) {
if (vd.length() > aaa * dt && vd.length() > ZERO_SPEED) {
Vector3f vd1 = new Vector3f(vd).normalize().mul(-1f * aaa * dt);
vd.add(vd1);
} else {
vd.zero();
}
}

boolean expl = Explosion.handle_explosion(drone.get_speed(), new Vec3d(vd));

drone.set_speed(new Vec3d(vd));
// // process hit
if (expl) {
drone.set_speed(new Vec3d(0, 0, 0));
}


drone.update_physics(input_t, dt);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/iung/fpv20/Fpv20.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.iung.fpv20.globals.AllChannels;
import com.iung.fpv20.mixin_utils.IsFlying;
import com.iung.fpv20.network.ChannelUpdatePacket;
import com.iung.fpv20.network.DroneExplosionPacket;
import com.iung.fpv20.network.DroneFlyPacket;
import com.iung.fpv20.network.SetReceiverPacket;
import net.fabricmc.api.ModInitializer;
Expand All @@ -16,6 +17,8 @@
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.world.World;
import net.minecraft.world.explosion.ExplosionBehavior;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -81,5 +84,22 @@ public void onInitialize() {
Fpv20.LOGGER.info("updated");
});

ServerPlayNetworking.registerGlobalReceiver(DroneExplosionPacket.TYPE, (packet, player, responseSender) -> {
((IsFlying) player).set_is_flying(!packet.stop_fly);
var ds = player.getDamageSources();
player.getWorld().createExplosion(
player,
ds.explosion(player, player),
new ExplosionBehavior(),
player.getPos(),
packet.power,
false,
World.ExplosionSourceType.MOB
);
if (packet.if_tele) {
player.teleport(packet.tele_pos.x, packet.tele_pos.y, packet.tele_pos.z);
}
});

}
}
49 changes: 49 additions & 0 deletions src/main/java/com/iung/fpv20/network/DroneExplosionPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.iung.fpv20.network;

import com.iung.fpv20.Fpv20;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketType;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.GlobalPos;
import net.minecraft.util.math.Vec3d;

public class DroneExplosionPacket implements FabricPacket {

public static final PacketType<DroneExplosionPacket> TYPE = PacketType.create(new Identifier(Fpv20.MOD_ID, "drone_explosion_packet"), DroneExplosionPacket::new);

public float power;
public boolean stop_fly;
public Vec3d tele_pos;
public boolean if_tele;

private DroneExplosionPacket(PacketByteBuf buf) {
this.power = buf.readFloat();
this.stop_fly = buf.readBoolean();
this.tele_pos = new Vec3d(buf.readDouble(), buf.readDouble(), buf.readDouble());
this.if_tele = buf.readBoolean();
}

public DroneExplosionPacket(float power, boolean stop_fly, Vec3d tele_pos, boolean if_tele) {
this.power = power;
this.stop_fly = stop_fly;
this.tele_pos = tele_pos;
this.if_tele = if_tele;
}


@Override
public void write(PacketByteBuf buf) {
buf.writeFloat(this.power);
buf.writeBoolean(this.stop_fly);
buf.writeDouble(this.tele_pos.x);
buf.writeDouble(this.tele_pos.y);
buf.writeDouble(this.tele_pos.z);
buf.writeBoolean(this.if_tele);
}

@Override
public PacketType<?> getType() {
return TYPE;
}
}

0 comments on commit e44e697

Please sign in to comment.