重构雷达旋转
This commit is contained in:
parent
d66ea35d76
commit
a0259dd5e3
4 changed files with 63 additions and 56 deletions
|
@ -132,6 +132,6 @@ public class Mod {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerDataTickets() {
|
private void registerDataTickets() {
|
||||||
FuMO25BlockEntity.FUMO25_TICK = GeckoLibUtil.addDataTicket(SerializableDataTicket.ofDouble(loc("fumo25_tick")));
|
FuMO25BlockEntity.FUMO25_TICK = GeckoLibUtil.addDataTicket(SerializableDataTicket.ofInt(loc("fumo25_tick")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import software.bernie.geckolib.animatable.GeoBlockEntity;
|
import software.bernie.geckolib.animatable.GeoBlockEntity;
|
||||||
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
|
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
|
||||||
import software.bernie.geckolib.animation.*;
|
import software.bernie.geckolib.animation.AnimatableManager;
|
||||||
import software.bernie.geckolib.constant.dataticket.SerializableDataTicket;
|
import software.bernie.geckolib.constant.dataticket.SerializableDataTicket;
|
||||||
import software.bernie.geckolib.util.GeckoLibUtil;
|
import software.bernie.geckolib.util.GeckoLibUtil;
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ import java.util.List;
|
||||||
|
|
||||||
public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoBlockEntity {
|
public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoBlockEntity {
|
||||||
|
|
||||||
public static SerializableDataTicket<Double> FUMO25_TICK;
|
public static SerializableDataTicket<Integer> FUMO25_TICK;
|
||||||
|
|
||||||
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
|
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
|
||||||
|
|
||||||
|
@ -66,6 +66,7 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
||||||
public int time = 0;
|
public int time = 0;
|
||||||
public boolean powered = false;
|
public boolean powered = false;
|
||||||
public int tick = 0;
|
public int tick = 0;
|
||||||
|
public float yRot0 = 0;
|
||||||
|
|
||||||
protected final ContainerEnergyData dataAccess = new ContainerEnergyData() {
|
protected final ContainerEnergyData dataAccess = new ContainerEnergyData() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -106,14 +107,17 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
||||||
return this.energyStorage;
|
return this.energyStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends BlockEntity> void serverTick(Level pLevel, BlockPos pPos, BlockState pState, T blockEntity) {
|
public static void serverTick(Level pLevel, BlockPos pPos, BlockState pState, FuMO25BlockEntity blockEntity) {
|
||||||
var radar = (FuMO25BlockEntity) blockEntity;
|
if (pState.getValue(FuMO25Block.POWERED)) {
|
||||||
|
blockEntity.tick++;
|
||||||
|
blockEntity.setAnimData(FUMO25_TICK, blockEntity.tick);
|
||||||
|
}
|
||||||
|
|
||||||
var energyStorage = ((FuMO25BlockEntity) blockEntity).getEnergyStorage(null);
|
var energyStorage = blockEntity.getEnergyStorage(null);
|
||||||
var energy = energyStorage.getEnergyStored();
|
var energy = energyStorage.getEnergyStored();
|
||||||
radar.tick++;
|
blockEntity.tick++;
|
||||||
|
|
||||||
FuncType funcType = radar.type;
|
FuncType funcType = blockEntity.type;
|
||||||
int energyCost;
|
int energyCost;
|
||||||
if (funcType == FuncType.WIDER) {
|
if (funcType == FuncType.WIDER) {
|
||||||
energyCost = MAX_ENERGY_COST;
|
energyCost = MAX_ENERGY_COST;
|
||||||
|
@ -125,45 +129,44 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
||||||
if (pState.getValue(FuMO25Block.POWERED)) {
|
if (pState.getValue(FuMO25Block.POWERED)) {
|
||||||
pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, false));
|
pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, false));
|
||||||
pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_END.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_END.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
||||||
radar.powered = false;
|
blockEntity.powered = false;
|
||||||
setChanged(pLevel, pPos, pState);
|
setChanged(pLevel, pPos, pState);
|
||||||
}
|
}
|
||||||
if (radar.time > 0) {
|
if (blockEntity.time > 0) {
|
||||||
radar.time = 0;
|
blockEntity.time = 0;
|
||||||
radar.setChanged();
|
blockEntity.setChanged();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!pState.getValue(FuMO25Block.POWERED)) {
|
if (!pState.getValue(FuMO25Block.POWERED)) {
|
||||||
if (energy >= DEFAULT_MIN_ENERGY) {
|
if (energy >= DEFAULT_MIN_ENERGY) {
|
||||||
pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, true));
|
pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, true));
|
||||||
pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_START.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_START.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
||||||
radar.powered = true;
|
blockEntity.powered = true;
|
||||||
setChanged(pLevel, pPos, pState);
|
setChanged(pLevel, pPos, pState);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
energyStorage.extractEnergy(energyCost, false);
|
energyStorage.extractEnergy(energyCost, false);
|
||||||
if (radar.tick == 200) {
|
if (blockEntity.tick == 200) {
|
||||||
pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_IDLE.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_IDLE.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (radar.time > 0) {
|
if (blockEntity.time > 0) {
|
||||||
if (radar.time % 100 == 0) {
|
if (blockEntity.time % 100 == 0) {
|
||||||
radar.setGlowEffect();
|
blockEntity.setGlowEffect();
|
||||||
}
|
}
|
||||||
radar.time--;
|
blockEntity.time--;
|
||||||
radar.setChanged();
|
blockEntity.setChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (radar.tick >= 200) {
|
if (blockEntity.tick >= 200) {
|
||||||
radar.tick = 0;
|
blockEntity.tick = 0;
|
||||||
radar.setAnimData(FUMO25_TICK, (double) radar.tick);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (radar.time <= 0 && radar.type != FuncType.NORMAL) {
|
if (blockEntity.time <= 0 && blockEntity.type != FuncType.NORMAL) {
|
||||||
radar.type = FuncType.NORMAL;
|
blockEntity.type = FuncType.NORMAL;
|
||||||
radar.setChanged();
|
blockEntity.setChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,16 +225,8 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
||||||
return ClientboundBlockEntityDataPacket.create(this);
|
return ClientboundBlockEntityDataPacket.create(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private PlayState predicate(AnimationState<FuMO25BlockEntity> event) {
|
|
||||||
if (this.getBlockState().getValue(FuMO25Block.POWERED)) {
|
|
||||||
return event.setAndContinue(RawAnimation.begin().thenLoop("animation.fumo_25.rot"));
|
|
||||||
}
|
|
||||||
return PlayState.STOP;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerControllers(AnimatableManager.ControllerRegistrar data) {
|
public void registerControllers(AnimatableManager.ControllerRegistrar data) {
|
||||||
data.add(new AnimationController<>(this, "controller", 0, this::predicate));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -3,9 +3,13 @@ package com.atsuishio.superbwarfare.client.model.block;
|
||||||
import com.atsuishio.superbwarfare.Mod;
|
import com.atsuishio.superbwarfare.Mod;
|
||||||
import com.atsuishio.superbwarfare.block.entity.FuMO25BlockEntity;
|
import com.atsuishio.superbwarfare.block.entity.FuMO25BlockEntity;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
import net.minecraft.util.Mth;
|
||||||
|
import software.bernie.geckolib.animation.AnimationState;
|
||||||
|
import software.bernie.geckolib.cache.object.GeoBone;
|
||||||
import software.bernie.geckolib.model.GeoModel;
|
import software.bernie.geckolib.model.GeoModel;
|
||||||
|
|
||||||
public class FuMO25Model extends GeoModel<FuMO25BlockEntity> {
|
public class FuMO25Model extends GeoModel<FuMO25BlockEntity> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResourceLocation getAnimationResource(FuMO25BlockEntity animatable) {
|
public ResourceLocation getAnimationResource(FuMO25BlockEntity animatable) {
|
||||||
return Mod.loc("animations/fumo_25.animation.json");
|
return Mod.loc("animations/fumo_25.animation.json");
|
||||||
|
@ -20,4 +24,32 @@ public class FuMO25Model extends GeoModel<FuMO25BlockEntity> {
|
||||||
public ResourceLocation getTextureResource(FuMO25BlockEntity animatable) {
|
public ResourceLocation getTextureResource(FuMO25BlockEntity animatable) {
|
||||||
return Mod.loc("textures/block/fumo_25.png");
|
return Mod.loc("textures/block/fumo_25.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCustomAnimations(FuMO25BlockEntity animatable, long instanceId, AnimationState<FuMO25BlockEntity> animationState) {
|
||||||
|
GeoBone bone = this.getAnimationProcessor().getBone("mian");
|
||||||
|
if (bone == null) return;
|
||||||
|
|
||||||
|
float targetDeg = getTick(animatable) * 1.8f; // 目标角度(0~360°)
|
||||||
|
float currentDeg = animatable.yRot0 * Mth.RAD_TO_DEG; // 当前角度(弧度转角度)
|
||||||
|
|
||||||
|
// 计算最短路径角度差(处理360°跳变)
|
||||||
|
float diffDeg = Mth.wrapDegrees(targetDeg - currentDeg);
|
||||||
|
|
||||||
|
// 应用插值
|
||||||
|
float newDeg = currentDeg + diffDeg * 0.1f;
|
||||||
|
|
||||||
|
// 转换为弧度并更新
|
||||||
|
float newRad = newDeg * Mth.DEG_TO_RAD;
|
||||||
|
animatable.yRot0 = newRad;
|
||||||
|
bone.setRotY(newRad);
|
||||||
|
}
|
||||||
|
|
||||||
|
private float getTick(FuMO25BlockEntity animatable) {
|
||||||
|
Integer tick = animatable.getAnimData(FuMO25BlockEntity.FUMO25_TICK);
|
||||||
|
if (tick != null) {
|
||||||
|
return tick.floatValue();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,25 +1,5 @@
|
||||||
{
|
{
|
||||||
"format_version": "1.8.0",
|
"format_version": "1.8.0",
|
||||||
"animations": {
|
"animations": {
|
||||||
"animation.fumo_25.rot": {
|
}
|
||||||
"loop": true,
|
|
||||||
"animation_length": 8,
|
|
||||||
"bones": {
|
|
||||||
"mian": {
|
|
||||||
"rotation": {
|
|
||||||
"0.0": [
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
],
|
|
||||||
"8.0": [
|
|
||||||
0,
|
|
||||||
360,
|
|
||||||
0
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue