Skip to content

Commit

Permalink
Improve: Add boost mode to marting car.
Browse files Browse the repository at this point in the history
Bump Version to 1.4.50

Took 2 hours 36 minutes
  • Loading branch information
xkball committed Jan 4, 2025
1 parent b775b8b commit 28b48ea
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 17 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod_name=Power Tool
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPL-3.0
# The mod version. See https://semver.org/
mod_version=1.4.49
mod_version=1.4.50
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static void onRegGuiLayerDef(RegisterGuiLayersEvent event) {
}
}
});
event.registerAbove(VanillaGuiLayers.HOTBAR,VanillaUtils.modRL("marting_car_info"),MartingCarOverlay::renderBoostBar);
if(SharedConstants.IS_RUNNING_WITH_JDWP){
event.registerAboveAll(VanillaUtils.modRL("debug_charts"),(guiGraphics, partialTicks) -> {
var y = 80;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.teacon.powertool.client.overlay;

import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.util.Mth;
import org.teacon.powertool.entity.MartingCarEntity;
import org.teacon.powertool.utils.VanillaUtils;

import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
public class MartingCarOverlay {

public static void renderBoostBar(GuiGraphics guiGraphics, DeltaTracker deltaTracker) {
var player = Minecraft.getInstance().player;
if (player == null || !player.isPassenger()) return;
var vehicle = player.getVehicle();
if(!(vehicle instanceof MartingCarEntity martingCar)) return;
var font = Minecraft.getInstance().font;
float energy = martingCar.getEntityData().get(MartingCarEntity.ENERGY);
//VanillaUtils.recordDebugData("energy",(long) energy);
float ratio = Mth.clamp(energy/(float)MartingCarEntity.MAX_ENERGY,0f,1f);
guiGraphics.renderOutline(guiGraphics.guiWidth()/2-100,guiGraphics.guiHeight()-50,200,10,-1);
guiGraphics.fill(guiGraphics.guiWidth()/2-99,guiGraphics.guiHeight()-49, guiGraphics.guiWidth()/2-99+(int) (198*ratio),guiGraphics.guiHeight()-49 + 8,ratio == 1 ? VanillaUtils.getColor(0,255,0,255) : -1);
guiGraphics.drawString(font, "boost",guiGraphics.guiWidth()/2-102-font.width("boost"),guiGraphics.guiHeight()-48,-1,true);
}
}
45 changes: 32 additions & 13 deletions src/main/java/org/teacon/powertool/entity/MartingCarEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.teacon.powertool.PowerTool;
import org.teacon.powertool.client.overlay.ClientDebugCharts;
import org.teacon.powertool.item.PowerToolItems;
import org.teacon.powertool.utils.VanillaUtils;

Expand All @@ -49,6 +48,7 @@
@ParametersAreNonnullByDefault
public class MartingCarEntity extends LivingEntity {

public static final int MAX_ENERGY = 100;
// Rotate radians of the steering wheel, negative for left, positive for right.
public static final EntityDataAccessor<Float> DATA_ID_STEERING_ROTATE_RADIAN = SynchedEntityData.defineId(MartingCarEntity.class, EntityDataSerializers.FLOAT);
// Rotate radians of the wheels.
Expand All @@ -57,7 +57,8 @@ public class MartingCarEntity extends LivingEntity {
public static final EntityDataAccessor<Float> DATA_ID_DAMAGE = SynchedEntityData.defineId(MartingCarEntity.class, EntityDataSerializers.FLOAT);

public static final EntityDataAccessor<Integer> VARIANT = SynchedEntityData.defineId(MartingCarEntity.class, EntityDataSerializers.INT);


public static final EntityDataAccessor<Float> ENERGY = SynchedEntityData.defineId(MartingCarEntity.class, EntityDataSerializers.FLOAT);
// Something definition with radians.
public static final float WHEEL_ROTATE_RADIAN_BASE = (float) Math.toRadians(90.0);
public static final float STEERING_ROTATE_RADIAN_LIMIT = (float) Math.toRadians(45); // Both positive limit and negative limit
Expand All @@ -70,6 +71,8 @@ public class MartingCarEntity extends LivingEntity {
private AttributeMap attributeMap;
private float xxaSum;
private float zzaSum;
private int boost;
private Vec3 lastPos = Vec3.ZERO;
// </editor-fold>

public MartingCarEntity(EntityType<MartingCarEntity> entityType, Level level) {
Expand Down Expand Up @@ -117,19 +120,20 @@ public void setItemSlot(@NotNull EquipmentSlot equipmentSlot, @NotNull ItemStack
@Override
public void tick() {
super.tick();
if(boost > 0) {
boost-=1;
}
var f2 = this.level().getBlockState(this.getBlockPosBelowThatAffectsMyMovement()).getFriction(level(), this.getBlockPosBelowThatAffectsMyMovement(), this);
f2 = this.onGround() ? f2 * 0.1F : 0.05F;
var facing = getYRot();
var v = getInputVector(getDeltaMovement(),1,-facing);
this.setDeltaMovement(getInputVector(new Vec3(v.x*0.9,v.y,v.z),0.9f+f2,facing));

setYHeadRot(facing);
setYBodyRot(facing);
if (!level().isClientSide()) {
if (remainingLifeTimeTicks < 0) {
discard();
}

if (!getPassengers().isEmpty()) {
remainingLifeTimeTicks = MAX_REMAINING_LIFE_TIME_TICKS;
} else {
Expand Down Expand Up @@ -221,13 +225,15 @@ protected void updateWheelsRotate() {
if (!getPassengers().isEmpty()) {
var delta = WHEEL_ROTATE_RADIAN_BASE;
delta *= (float) Mth.clamp(Math.log(getDeltaMovement().length()+0.9),0,4);
if(!movingForward()) delta = -delta;
var movingForward = movingForward();
if(!movingForward) delta = -delta;
float original = this.entityData.get(DATA_ID_WHEEL_ROTATE_RADIAN);
original += Mth.PI;
original += delta;
original %= Mth.TWO_PI;
original -= Mth.PI;
ClientDebugCharts.recordDebugData("Speed", (long) (Mth.sign(delta)*getDeltaMovement().length()*100));
//VanillaUtils.recordDebugData("Speed", (long) (Mth.sign(delta)*getDeltaMovement().length()*100));
//VanillaUtils.recordDebugData("Speed", (long) (getDeltaMovement().length()*100*(movingForward?1:-1)));
this.entityData.set(DATA_ID_WHEEL_ROTATE_RADIAN, original);
} else {
this.entityData.set(DATA_ID_WHEEL_ROTATE_RADIAN, 0F);
Expand All @@ -236,7 +242,7 @@ protected void updateWheelsRotate() {

@Override
protected float getBlockSpeedFactor() {
return 1;
return boost>0? 1.2f:1f;
}

protected boolean movingForward(){
Expand Down Expand Up @@ -280,11 +286,23 @@ protected void positionRider(Entity passenger, MoveFunction callback) {
protected Vec3 getRiddenInput(Player player, Vec3 travelVector) {
//setYRot(player.getYHeadRot());
updateSteeringRotate(player.xxa);
xxaSum = Mth.clamp((xxaSum + player.xxa*5f)*0.75f,-30f,30f);
var yRot = (360 + getYRot() - xxaSum) % 360;
setYRot(yRot);
zzaSum = Mth.clamp((zzaSum + player.zza*0.04f)*0.75f,-0.25f,0.25f);
if(getDeltaMovement().lengthSqr() < 2) this.moveRelative(1,new Vec3(0f, 0f, zzaSum));
if(player.level().isClientSide){
this.entityData.set(ENERGY,(float)(this.entityData.get(ENERGY)+this.getDeltaMovement().length()));
if(this.entityData.get(ENERGY) > MAX_ENERGY && player.jumping){
this.entityData.set(ENERGY, 0f);
boost = 30;
}
xxaSum = Mth.clamp((xxaSum + player.xxa*5f)*0.75f,-30f,30f);
var yRot = (360 + getYRot() - xxaSum) % 360;
setYRot(yRot);
var maxSpeed = 0.25f;
zzaSum = Mth.clamp((zzaSum + player.zza*0.04f)*0.75f,-maxSpeed,maxSpeed);
if(boost>0 || getDeltaMovement().lengthSqr() < 2) this.moveRelative(1f,new Vec3(0f, 0f, zzaSum));
if(boost>0) this.setDeltaMovement(this.getDeltaMovement().scale(1.8));
var dp = new Vec3(this.position().x-lastPos.x, this.position().y-lastPos.y, this.position().z-lastPos.z);
lastPos = this.position();
//VanillaUtils.recordDebugData("Speed_", (long) (dp.length()*100));
}
return Vec3.ZERO;
}

Expand Down Expand Up @@ -317,6 +335,7 @@ protected void defineSynchedData(SynchedEntityData.Builder builder) {
builder.define(DATA_ID_WHEEL_ROTATE_RADIAN, 0F);
builder.define(DATA_ID_DAMAGE, 0F);
builder.define(VARIANT,0);
builder.define(ENERGY,0f);
}

public float getDamage() {
Expand All @@ -340,7 +359,7 @@ public static AttributeSupplier createAttributes() {
return LivingEntity.createLivingAttributes()
.add(Attributes.MAX_HEALTH, 1)
.add(Attributes.STEP_HEIGHT, 1.5)
.add(Attributes.MOVEMENT_SPEED, 0.7)
.add(Attributes.MOVEMENT_SPEED, 2.7)
.add(Attributes.SCALE)
.add(Attributes.GRAVITY)
.add(Attributes.MOVEMENT_EFFICIENCY)
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/teacon/powertool/item/MartingCarItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import java.util.List;

public class MartingCarItem extends Item {
public static final String TOOLTIP = "tooltip.powertool.marting";
public static final String TOOLTIP1 = "tooltip.powertool.marting";
public static final String TOOLTIP2 = "tooltip.powertool.marting2";

private final MartingCarEntity.Variant variant;

Expand All @@ -25,8 +26,8 @@ public MartingCarItem(Properties properties, MartingCarEntity.Variant variant) {
@Override
public void appendHoverText(@NotNull ItemStack stack, @NotNull TooltipContext context,
@NotNull List<Component> tooltipComponents, @NotNull TooltipFlag tooltipFlag) {
tooltipComponents.add(Component.translatable(TOOLTIP));

tooltipComponents.add(Component.translatable(TOOLTIP1));
tooltipComponents.add(Component.translatable(TOOLTIP2));
super.appendHoverText(stack, context, tooltipComponents, tooltipFlag);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public net.minecraft.client.renderer.RenderType$CompositeRenderType state()Lnet/
public net.minecraft.client.renderer.RenderType$CompositeState outputState
public net.minecraft.client.renderer.RenderType$CompositeState textureState
public net.minecraft.world.entity.Entity getInputVector(Lnet/minecraft/world/phys/Vec3;FF)Lnet/minecraft/world/phys/Vec3; # getInputVector
public net.minecraft.world.entity.LivingEntity jumping

1 change: 1 addition & 0 deletions src/main/resources/assets/powertool/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
"item.powertool.marting_car_green": "Marting Car (Green)",
"item.powertool.marting_car_blue": "Marting Car (Blue)",
"tooltip.powertool.marting": "MalayP asked me to write a karting car",
"tooltip.powertool.marting2": "Press space to speed up.",
"powertool.gui.register.rev": "Require Item:",
"powertool.gui.register.sup": "Supply Item (Optional):",
"powertool.gui.register.display_supply": "Display Supply Item On HUD",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/powertool/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
"item.powertool.marting_car_green": "马丁车(绿色)",
"item.powertool.marting_car_blue": "马丁车(蓝色)",
"tooltip.powertool.marting": "马雷让写的卡丁车,简称马丁车",
"tooltip.powertool.marting2": "按下空格加速",
"powertool.gui.register.rev": "需求物品:",
"powertool.gui.register.sup": "提供物品(可选):",
"powertool.gui.register.display_supply": "在HUD显示提供的物品",
Expand Down

0 comments on commit 28b48ea

Please sign in to comment.