Skip to content

Commit

Permalink
Make changes and fixes to controlify support
Browse files Browse the repository at this point in the history
- Stop vibrations when leaving the vehicle
- Other people's vehicles won't vibrate your controller
- More actions cause vibrations, and they are less aggressive
- Normal actions bound to the same button as drift/accelerate/brake don't happen while riding a vehicle when using controlify, matching MidnightControls
  • Loading branch information
FoundationGames committed Aug 19, 2023
1 parent 88c75ba commit f73ae7c
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.github.foundationgames.automobility.Automobility;
import io.github.foundationgames.automobility.block.entity.AutomobileAssemblerBlockEntity;
import io.github.foundationgames.automobility.item.DashPanelItem;
import io.github.foundationgames.automobility.item.CreativeTabQueue;
import io.github.foundationgames.automobility.item.DashPanelItem;
import io.github.foundationgames.automobility.item.SlopeBlockItem;
import io.github.foundationgames.automobility.item.SteepSlopeBlockItem;
import io.github.foundationgames.automobility.item.TooltipBlockItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ public interface AutomobileController {

default void crashRumble() {}

default void updateDriftRumbleState(boolean drifting) {};
default void groundThudRumble() {}

default void driftChargeRumble() {}

default void updateMaxChargeRumbleState(boolean maxCharge) {}

default void updateBoostingRumbleState(boolean boosting, float boostPower) {}

default void updateOffRoadRumbleState(boolean inOffRoad) {}

boolean inControllerMode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.github.foundationgames.automobility.block.AutomobileAssemblerBlock;
import io.github.foundationgames.automobility.block.LaunchGelBlock;
import io.github.foundationgames.automobility.block.OffRoadBlock;
import io.github.foundationgames.automobility.controller.AutomobileController;
import io.github.foundationgames.automobility.item.AutomobileInteractable;
import io.github.foundationgames.automobility.item.AutomobilityItems;
import io.github.foundationgames.automobility.particle.AutomobilityParticles;
Expand All @@ -25,6 +26,7 @@
import io.github.foundationgames.automobility.util.duck.CollisionArea;
import io.github.foundationgames.automobility.util.network.ClientPackets;
import io.github.foundationgames.automobility.util.network.CommonPackets;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Cursor3D;
Expand Down Expand Up @@ -316,6 +318,14 @@ public void recreateFromPacket(ClientboundAddEntityPacket packet) {
}
}

private void controllerAction(Consumer<AutomobileController> action) {
if (this.level().isClientSide()) {
if (this.getControllingPassenger() == Minecraft.getInstance().player) {
action.accept(Platform.get().controller());
}
}
}

@Override
public AutomobileFrame getFrame() {
return frame;
Expand Down Expand Up @@ -420,10 +430,6 @@ private void setDrifting(boolean drifting) {
if (!this.drifting && drifting) {
skidSound.accept(this);
}

if (this.drifting != drifting) {
Platform.get().controller().updateDriftRumbleState(drifting);
}
}

this.drifting = drifting;
Expand All @@ -434,6 +440,10 @@ private void setBurningOut(boolean burningOut) {
skidSound.accept(this);
}

if (this.burningOut != burningOut || (this.turboCharge >= LARGE_TURBO_TIME) != burningOut) {
controllerAction(c -> c.updateMaxChargeRumbleState(burningOut));
}

this.burningOut = burningOut;
}

Expand Down Expand Up @@ -736,6 +746,10 @@ public void movementTick() {
engineSpeed += 0.012f;
}
markDirty();

if (boostTimer == 0) {
controllerAction(c -> c.updateBoostingRumbleState(false, 0));
}
} else {
boostSpeed = AUtils.zero(boostSpeed, 0.09f);
}
Expand Down Expand Up @@ -810,15 +824,21 @@ public void movementTick() {
slopeStickingTimer = Math.max(0, slopeStickingTimer--);
}

boolean wasOffRoad = this.offRoad && this.hSpeed > 0.01;

// Handle being in off-road
if (boostSpeed < 0.4f && level().getBlockState(blockPosition()).getBlock() instanceof OffRoadBlock offRoad) {
if (boostSpeed < 0.4f && level().getBlockState(blockPosition()).getBlock() instanceof OffRoadBlock block) {
int layers = level().getBlockState(blockPosition()).getValue(OffRoadBlock.LAYERS);
float cap = stats.getComfortableSpeed() * (1 - ((float)layers / 3.5f));
engineSpeed = Math.min(cap, engineSpeed);
this.debrisColor = offRoad.color;
this.debrisColor = block.color;
this.offRoad = true;
} else this.offRoad = false;

if ((this.offRoad && this.hSpeed > 0.01) != wasOffRoad) {
controllerAction(c -> c.updateOffRoadRumbleState(this.offRoad));
}

// Set the horizontal speed
if (!burningOut()) hSpeed = engineSpeed + boostSpeed;

Expand Down Expand Up @@ -904,7 +924,7 @@ public void postMovementTick() {

if (isVehicle() && level().isClientSide()) {
if (getPassengers().stream().anyMatch(p -> p instanceof LocalPlayer)) {
Platform.get().controller().crashRumble();
controllerAction(c -> c.crashRumble());
}
}

Expand Down Expand Up @@ -1124,6 +1144,8 @@ public void boost(float power, int time) {
if (this.isControlledByLocalInstance()) {
this.engineSpeed = Math.max(this.engineSpeed, this.stats.getComfortableSpeed() * 0.5f);
}

controllerAction(c -> c.updateBoostingRumbleState(true, power));
}

private void steeringTick() {
Expand Down Expand Up @@ -1152,10 +1174,13 @@ private void consumeTurboCharge() {
}

private void driftingTick() {
int prevTurboCharge = turboCharge;

// Handles starting a drift
if (!prevHoldDrift && holdingDrift) {
if (steering != 0 && !drifting && hSpeed > 0.4f && automobileOnGround) {
setDrifting(true);
controllerAction(AutomobileController::driftChargeRumble);
driftDir = steering > 0 ? 1 : -1;
// Reduce speed when a drift starts, based on how long the last drift was for
// This allows you to do a series of short drifts without tanking all your speed, while still reducing your speed when you begin the drift(s)
Expand All @@ -1171,15 +1196,27 @@ private void driftingTick() {
// Ending a drift successfully, giving you a turbo boost
if (prevHoldDrift && !holdingDrift) {
setDrifting(false);
controllerAction(c -> c.updateMaxChargeRumbleState(false));
consumeTurboCharge();
// Ending a drift unsuccessfully, not giving you a boost
} else if (hSpeed < 0.33f) {
setDrifting(false);
controllerAction(c -> c.updateMaxChargeRumbleState(false));
turboCharge = 0;
}
if (automobileOnGround) turboCharge += ((steeringLeft && driftDir < 0) || (steeringRight && driftDir > 0)) ? 2 : 1;
}

if (turboCharge == SMALL_TURBO_TIME || turboCharge == MEDIUM_TURBO_TIME || turboCharge == LARGE_TURBO_TIME) {
controllerAction(AutomobileController::driftChargeRumble);
}

if (turboCharge >= LARGE_TURBO_TIME && prevTurboCharge < LARGE_TURBO_TIME) {
controllerAction(c -> c.updateMaxChargeRumbleState(true));
} else if (prevTurboCharge >= LARGE_TURBO_TIME && turboCharge < LARGE_TURBO_TIME) {
controllerAction(c -> c.updateMaxChargeRumbleState(false));
}

this.prevHoldDrift = this.holdingDrift;
}

Expand Down Expand Up @@ -1472,6 +1509,7 @@ public float getTrackedFrontAttachmentAnimation() {
public void bounce() {
suspensionBounceTimer = 3;
level().playLocalSound(this.getX(), this.getY(), this.getZ(), AutomobilitySounds.LANDING.require(), SoundSource.AMBIENT, 1, 1.5f + (0.15f * (this.level().random.nextFloat() - 0.5f)), true);
controllerAction(AutomobileController::groundThudRumble);
}

public static final class Displacement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import com.mojang.blaze3d.vertex.PoseStack;
import io.github.foundationgames.automobility.automobile.render.AutomobileRenderer;
import io.github.foundationgames.automobility.entity.AutomobileEntity;
import org.joml.Quaternionf;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.resources.ResourceLocation;
import org.joml.Quaternionf;

public class AutomobileEntityRenderer extends EntityRenderer<AutomobileEntity> {
public AutomobileEntityRenderer(EntityRendererProvider.Context ctx) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.github.foundationgames.automobility.item;

import io.github.foundationgames.automobility.block.AutomobilityBlocks;
import io.github.foundationgames.automobility.block.SlopeBlock;
import io.github.foundationgames.automobility.block.SteepSlopeBlock;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.foundationgames.automobility.recipe;

import io.github.foundationgames.automobility.Automobility;

import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.Container;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package io.github.foundationgames.automobility.screen;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import io.github.foundationgames.automobility.Automobility;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.screens.inventory.MenuAccess;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.github.foundationgames.automobility.Automobility;
import io.github.foundationgames.automobility.util.Eventual;
import io.github.foundationgames.automobility.util.RegistryQueue;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.sounds.SoundEvent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import io.github.foundationgames.automobility.automobile.AutomobilePrefab;
import io.github.foundationgames.automobility.automobile.AutomobileWheel;
import io.github.foundationgames.automobility.item.AutomobilityItems;
import org.joml.Vector3f;

import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.Direction;
Expand All @@ -18,6 +16,7 @@
import net.minecraft.world.Container;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3f;

import java.text.DecimalFormat;

Expand Down
2 changes: 1 addition & 1 deletion fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies {
}

// Controlify Support
modImplementation "dev.isxander:controlify:${project.controlify_version}"
modCompileOnly "dev.isxander:controlify:${project.controlify_version}"

// Json entity models
modImplementation "maven.modrinth:jsonem:${project.jsonem_version}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import io.github.foundationgames.automobility.fabric.block.render.FabricSlopeBakedModel;
import io.github.foundationgames.automobility.particle.AutomobilityParticles;
import io.github.foundationgames.automobility.particle.DriftSmokeParticle;
import io.github.foundationgames.automobility.platform.Platform;
import io.github.foundationgames.automobility.screen.AutomobileHud;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
Expand All @@ -19,6 +21,8 @@
import net.minecraft.client.renderer.RenderType;

public class AutomobilityClientFabric implements ClientModInitializer {
private static boolean wasRidingAutomobile = false;

@Override
public void onInitializeClient() {
FabricPlatform.init();
Expand All @@ -41,6 +45,21 @@ public void onInitializeClient() {
ModelLoadingRegistry.INSTANCE.registerResourceProvider(manager -> (location, context) ->
SlopeUnbakedModel.DEFAULT_MODELS.containsKey(location) ? SlopeUnbakedModel.DEFAULT_MODELS.get(location).get() : null);

ClientTickEvents.START_WORLD_TICK.register(world -> {
boolean isRidingAutomobile = Minecraft.getInstance().player != null &&
Minecraft.getInstance().player.getVehicle() instanceof AutomobileEntity;

if (wasRidingAutomobile && !isRidingAutomobile) {
var con = Platform.get().controller();

con.updateMaxChargeRumbleState(false);
con.updateOffRoadRumbleState(false);
con.updateBoostingRumbleState(false, 0);
}

wasRidingAutomobile = isRidingAutomobile;
});

BlockRenderLayerMap.INSTANCE.putBlock(AutomobilityBlocks.LAUNCH_GEL.require(), RenderType.translucent());
BlockRenderLayerMap.INSTANCE.putBlock(AutomobilityBlocks.AUTOMOBILE_ASSEMBLER.require(), RenderType.cutout());
BlockRenderLayerMap.INSTANCE.putBlock(AutomobilityBlocks.SLOPE.require(), RenderType.translucent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder;
import net.fabricmc.fabric.api.particle.v1.FabricParticleTypes;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.client.color.block.BlockColor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.foundationgames.automobility.fabric.controller.controlify;

import dev.isxander.controlify.api.ControlifyApi;
import dev.isxander.controlify.api.bind.BindingSupplier;
import dev.isxander.controlify.api.bind.ControlifyBindingsApi;
import dev.isxander.controlify.api.entrypoint.ControlifyEntrypoint;
import dev.isxander.controlify.api.event.ControlifyEvents;
Expand All @@ -12,10 +13,13 @@
import io.github.foundationgames.automobility.entity.AutomobileEntity;
import net.minecraft.network.chat.Component;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

public class ControlifyCompat implements ControlifyEntrypoint {
public static final Set<BindingSupplier> AUTOMOBILITY_BINDINGS = new HashSet<>();

@Override
public void onControlifyPreInit(ControlifyApi controlify) {
BindContext drivingCtx = new BindContext(Automobility.rl("driving"), Set.of());
Expand All @@ -34,6 +38,11 @@ public void onControlifyPreInit(ControlifyApi controlify) {
.context(drivingCtx)
.category(category));

AUTOMOBILITY_BINDINGS.clear();
AUTOMOBILITY_BINDINGS.add(ControlifyController.accelerateBinding);
AUTOMOBILITY_BINDINGS.add(ControlifyController.brakeBinding);
AUTOMOBILITY_BINDINGS.add(ControlifyController.driftBinding);

ControlifyEvents.INGAME_GUIDE_REGISTRY.register((bindings, registry) -> {
var accelerate = bindings.get(Automobility.rl("accelerate_automobile"));
var brake = bindings.get(Automobility.rl("brake_automobile"));
Expand Down
Loading

0 comments on commit f73ae7c

Please sign in to comment.