Skip to content

Commit

Permalink
Cool button. note to self: pls test
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Nov 20, 2023
1 parent d59b8fa commit 60cace4
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 19 deletions.
14 changes: 11 additions & 3 deletions src/main/java/dev/enjarai/minitardis/block/ModBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.fabricmc.fabric.api.object.builder.v1.world.poi.PointOfInterestHelper;
import net.minecraft.block.Block;
import net.minecraft.block.BlockSetType;
import net.minecraft.block.Blocks;
import net.minecraft.block.MapColor;
import net.minecraft.block.entity.BlockEntity;
Expand Down Expand Up @@ -52,13 +53,16 @@ public class ModBlocks {
public static final ConsoleScreenBlock CONSOLE_SCREEN =
register("console_screen", new ConsoleScreenBlock(FabricBlockSettings.create()));
public static final ConsoleButtonBlock RESET_DESTINATION_BUTTON =
register("reset_destination_button", new ConsoleButtonBlock(FabricBlockSettings.create(), Blocks.DARK_OAK_BUTTON,
register("reset_destination_button", new ConsoleButtonBlock(FabricBlockSettings.create(),
BlockSetType.DARK_OAK, Blocks.DARK_OAK_BUTTON, true,
(controls, facing) -> controls.resetDestination()));
public static final ConsoleButtonBlock NUDGE_DESTINATION_BUTTON_1 =
register("nudge_destination_button_1", new ConsoleButtonBlock(FabricBlockSettings.create(), Blocks.OAK_BUTTON,
register("nudge_destination_button_1", new ConsoleButtonBlock(FabricBlockSettings.create(),
BlockSetType.OAK, Blocks.OAK_BUTTON, true,
TardisControl::nudgeDestination));
public static final ConsoleButtonBlock NUDGE_DESTINATION_BUTTON_2 =
register("nudge_destination_button_2", new ConsoleButtonBlock(FabricBlockSettings.create(), Blocks.SPRUCE_BUTTON,
register("nudge_destination_button_2", new ConsoleButtonBlock(FabricBlockSettings.create(),
BlockSetType.SPRUCE, Blocks.SPRUCE_BUTTON, true,
TardisControl::nudgeDestination));
public static final ConsoleRepeaterBlock COORDINATE_SCALE_SELECTOR =
register("coordinate_scale_selector", new ConsoleRepeaterBlock(FabricBlockSettings.create(),
Expand All @@ -75,6 +79,10 @@ public class ModBlocks {
public static final ConsoleDaylightDetectorBlock FUEL_CONTROL =
register("fuel_control", new ConsoleDaylightDetectorBlock(FabricBlockSettings.create(),
(controls, value) -> true)); // TODO
public static final ConsoleToggleButtonBlock COORDINATE_LOCK =
register("coordinate_lock", new ConsoleToggleButtonBlock(FabricBlockSettings.create(),
BlockSetType.STONE, Blocks.STONE_BUTTON, false,
TardisControl::setDestinationLocked));

public static final BlockEntityType<TardisExteriorBlockEntity> TARDIS_EXTERIOR_ENTITY =
registerEntity("tardis_exterior", TardisExteriorBlockEntity::new, TARDIS_EXTERIOR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class ConsoleButtonBlock extends ButtonBlock implements PolymerBlock, Tar
private final Block polymerBlock;
private final BiFunction<TardisControl, Direction, Boolean> controlInput;

public ConsoleButtonBlock(Settings settings, Block polymerBlock, BiFunction<TardisControl, Direction, Boolean> controlInput) {
super(settings, BlockSetType.OAK, 2, true);
public ConsoleButtonBlock(Settings settings, BlockSetType buttonType, Block polymerBlock, boolean wooden, BiFunction<TardisControl, Direction, Boolean> controlInput) {
super(settings, buttonType, 2, wooden);
this.polymerBlock = polymerBlock;
this.controlInput = controlInput;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package dev.enjarai.minitardis.block.console;

import dev.enjarai.minitardis.component.TardisControl;
import net.minecraft.block.Block;
import net.minecraft.block.BlockSetType;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;

import java.util.function.BiFunction;

@SuppressWarnings("deprecation")
public class ConsoleToggleButtonBlock extends ConsoleButtonBlock {
private final BiFunction<TardisControl, Boolean, Boolean> controlInput;

public ConsoleToggleButtonBlock(Settings settings, BlockSetType buttonType, Block polymerBlock, boolean wooden, BiFunction<TardisControl, Boolean, Boolean> controlInput) {
super(settings, buttonType, polymerBlock, wooden, null);
this.controlInput = controlInput;
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
var isPowered = !state.get(POWERED);
world.setBlockState(pos, state.with(POWERED, isPowered));

if (!getTardis(world).map(tardis -> controlInput.apply(tardis.getControls(), isPowered)).orElse(false)) {
inputFailure(world, pos, SoundEvents.BLOCK_IRON_TRAPDOOR_OPEN, 0);
}

return ActionResult.SUCCESS;
}

@Override
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
getTardis(world).ifPresent(tardis -> {
var locked = tardis.getControls().isDestinationLocked();
if (state.get(POWERED) != locked) {
world.setBlockState(pos, state.with(POWERED, locked));
}
});
world.scheduleBlockTick(pos, this, 10);
}

@Override
public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) {
world.scheduleBlockTick(pos, this, 10);
}
}
47 changes: 39 additions & 8 deletions src/main/java/dev/enjarai/minitardis/component/TardisControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,32 @@
public class TardisControl {
public static final Codec<TardisControl> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("coordinate_scale").forGetter(c -> c.coordinateScale),
ScreenApp.CODEC.listOf().fieldOf("screen_apps").forGetter(c -> ImmutableList.copyOf(c.screenApps.values()))
ScreenApp.CODEC.listOf().fieldOf("screen_apps").forGetter(c -> ImmutableList.copyOf(c.screenApps.values())),
Codec.BOOL.optionalFieldOf("destination_locked", false).forGetter(c -> c.destinationLocked)
).apply(instance, TardisControl::new));

private int coordinateScale;
private final Map<Identifier, ScreenApp> screenApps;
private boolean destinationLocked;

Tardis tardis;

private TardisControl(int coordinateScale, Collection<ScreenApp> screenApps) {
private TardisControl(int coordinateScale, Collection<ScreenApp> screenApps, boolean destinationLocked) {
this.coordinateScale = coordinateScale;
var builder = ImmutableMap.<Identifier, ScreenApp>builder();
ScreenApp.CONSTRUCTORS.forEach((key, value) -> builder.put(key, value.get()));
screenApps.forEach(app -> builder.put(app.id(), app));
this.screenApps = builder.buildKeepingLast();
this.destinationLocked = destinationLocked;
}

@SuppressWarnings("CopyConstructorMissesField")
public TardisControl(TardisControl copyFrom) {
this(copyFrom.coordinateScale, copyFrom.screenApps.values());
this(copyFrom.coordinateScale, copyFrom.screenApps.values(), copyFrom.destinationLocked);
}

public TardisControl() {
this(1, List.of());
this(1, List.of(), false);
}


Expand All @@ -50,7 +53,12 @@ public boolean resetDestination() {
return false;
}

return tardis.setDestination(tardis.getCurrentLocation(), false);
var success = tardis.setDestination(tardis.getCurrentLocation(), false);

if (success) {
destinationLocked = false;
}
return success;
}

public boolean updateCoordinateScale(int scale) {
Expand All @@ -59,7 +67,7 @@ public boolean updateCoordinateScale(int scale) {
}

public boolean nudgeDestination(Direction direction) {
return tardis.setDestination(tardis.getDestination()
var success = tardis.setDestination(tardis.getDestination()
.map(d -> {
if (direction.getAxis().isVertical()) {
return snapLocationVertically(d, direction);
Expand All @@ -68,6 +76,10 @@ public boolean nudgeDestination(Direction direction) {
}
}), false)
&& tardis.getDestination().isPresent();
if (success) {
destinationLocked = false;
}
return success;
}

private TardisLocation snapLocationVertically(TardisLocation location, Direction direction) {
Expand All @@ -84,13 +96,32 @@ private TardisLocation snapLocationVertically(TardisLocation location, Direction
}

public boolean rotateDestination(Direction direction) {
return tardis.setDestination(tardis.getDestination()
var success = tardis.setDestination(tardis.getDestination()
.map(d -> d.with(direction)), false)
&& tardis.getDestination().isPresent();
if (success) {
destinationLocked = false;
}
return success;
}

public boolean handbrake(boolean state) {
return tardis.suggestStateTransition(state ? new TakingOffState() : new SearchingForLandingState(false));
if (isDestinationLocked()) {
return tardis.suggestStateTransition(state ? new TakingOffState() : new SearchingForLandingState(false));
}
return false;
}

public boolean isDestinationLocked() {
return destinationLocked;
}

public boolean setDestinationLocked(boolean destinationLocked) {
if (tardis.getState().tryChangeCourse(tardis)) {
this.destinationLocked = destinationLocked;
return true;
}
return false;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public FlightState tick(Tardis tardis) {
if (maybeDestination.isPresent()) {
var destination = maybeDestination.get();

// Shuffle the landing location if we're crashing
if (crashing) {
var random = tardis.getInteriorWorld().getRandom();
destination = destination.with(destination.pos().add(random.nextBetween(-1000, 1000), 0, random.nextBetween(-1000, 1000)));
}

if (searchIterator == null) {
// Shuffle the landing location if we're crashing
if (crashing) {
var random = tardis.getInteriorWorld().getRandom();
destination = destination.with(destination.pos().add(random.nextBetween(-1000, 1000), 0, random.nextBetween(-1000, 1000)));
}

searchIterator = BlockPos.iterateOutwards(destination.pos(), MAX_SEARCH_RANGE, MAX_SEARCH_RANGE, MAX_SEARCH_RANGE).iterator();
}

Expand Down

0 comments on commit 60cace4

Please sign in to comment.