添加范围锁定工具类
This commit is contained in:
parent
e4d854cf19
commit
5c0d14d38f
2 changed files with 52 additions and 0 deletions
|
@ -6,6 +6,7 @@ import net.mcreator.superbwarfare.init.ModSounds;
|
|||
import net.mcreator.superbwarfare.init.ModTags;
|
||||
import net.mcreator.superbwarfare.network.ModVariables;
|
||||
import net.mcreator.superbwarfare.network.message.SimulationDistanceMessage;
|
||||
import net.mcreator.superbwarfare.tools.SeekTool;
|
||||
import net.mcreator.superbwarfare.tools.SoundTool;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
|
@ -17,11 +18,14 @@ import net.minecraft.util.Mth;
|
|||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.effect.MobEffectInstance;
|
||||
import net.minecraft.world.effect.MobEffects;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.level.ClipContext;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.LevelAccessor;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraftforge.event.TickEvent;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||
|
@ -92,6 +96,7 @@ public class PlayerEventHandler {
|
|||
handleChangeFireRate(player);
|
||||
handleBocekPulling(player);
|
||||
handleGunRecoil(player);
|
||||
handleWeaponSeek(player, event.player.level());
|
||||
}
|
||||
|
||||
handleGround(player);
|
||||
|
@ -103,6 +108,16 @@ public class PlayerEventHandler {
|
|||
}
|
||||
}
|
||||
|
||||
//测试用
|
||||
private static void handleWeaponSeek(Player player, LevelAccessor level) {
|
||||
if (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).zooming) {
|
||||
Entity seekingEntity = SeekTool.seekEntity(player, level, 256,30);
|
||||
if (seekingEntity instanceof LivingEntity _entity && !_entity.level().isClientSide())
|
||||
_entity.addEffect(new MobEffectInstance(MobEffects.GLOWING, 2, 0));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static void handleWeaponSway(Player player) {
|
||||
if (player.getMainHandItem().is(ModTags.Items.GUN)) {
|
||||
float pose;
|
||||
|
|
37
src/main/java/net/mcreator/superbwarfare/tools/SeekTool.java
Normal file
37
src/main/java/net/mcreator/superbwarfare/tools/SeekTool.java
Normal file
|
@ -0,0 +1,37 @@
|
|||
package net.mcreator.superbwarfare.tools;
|
||||
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.level.LevelAccessor;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class SeekTool {
|
||||
|
||||
// TODO 固体方块阻挡搜寻,以及目标锁定为夹角最小的实体
|
||||
public static Entity seekEntity(Entity entity, LevelAccessor world, double seekRange, double seekAngle) {
|
||||
final Vec3 _center = new Vec3(entity.getX(), entity.getY(), entity.getZ());
|
||||
List<Entity> _entfound = world.getEntitiesOfClass(Entity.class, new AABB(_center, _center).inflate(seekRange), e -> true).stream().sorted(Comparator.comparingDouble(_entcnd -> _entcnd.distanceToSqr(_center))).toList();
|
||||
for (Entity entityiterator : _entfound) {
|
||||
if ((new Object() {
|
||||
public double angle(Vec3 _start, Vec3 _end) {
|
||||
double _d0 = _start.length();
|
||||
double _d1 = _end.length();
|
||||
if (_d0 > 0.0D && _d1 > 0.0D) {
|
||||
return Math.toDegrees(Math.acos(_start.dot(_end) / (_d0 * _d1)));
|
||||
} else {
|
||||
return 0.0D;
|
||||
}
|
||||
}
|
||||
}).angle((new Vec3((entityiterator.getX() - entity.getX()), (entityiterator.getY() - entity.getY()), (entityiterator.getZ() - entity.getZ()))), (entity.getLookAngle())) < seekAngle && entityiterator instanceof LivingEntity) {
|
||||
if (entityiterator != entity) {
|
||||
return entityiterator;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue