Skip to content

Commit

Permalink
Cool stuff tho!!
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Sep 16, 2024
1 parent 895c6b4 commit d8cd235
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 42 deletions.
50 changes: 50 additions & 0 deletions src/main/java/dev/enjarai/minitardis/component/FlightWave.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dev.enjarai.minitardis.component;

import net.minecraft.util.math.random.Random;

public class FlightWave {
private double magnitude;
private double period;
private double offset;

public FlightWave(double magnitude, double period, double offset) {
this.magnitude = magnitude;
this.period = period;
this.offset = offset;
}

public FlightWave(Random random) {
this(0, 0, 0);
shuffle(random);
}

public void shuffle(Random random) {
magnitude = 0.2 + random.nextDouble() * 0.8;
period = 0.2 + random.nextDouble() * 0.8;
offset = random.nextDouble();
}

public double getMagnitude() {
return magnitude;
}

public double getPeriod() {
return period;
}

public double getOffset() {
return offset;
}

public void setMagnitude(double magnitude) {
this.magnitude = magnitude;
}

public void setPeriod(double period) {
this.period = period;
}

public void setOffset(double offset) {
this.offset = offset;
}
}
12 changes: 12 additions & 0 deletions src/main/java/dev/enjarai/minitardis/component/Tardis.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class Tardis {
private int stability;
private int fuel;
private boolean doorOpen;
private FlightWave flightWave;

private Tardis(UUID uuid, boolean interiorPlaced, Identifier interior, Either<TardisLocation, PartialTardisLocation> currentLocation, Optional<TardisLocation> destination, BlockPos interiorDoorPosition, TardisControl controls, FlightState state, int stability, int fuel, boolean doorOpen) {
this.uuid = uuid;
Expand Down Expand Up @@ -143,6 +144,10 @@ public void tick() {
world.getChunkManager().addTicket(INTERIOR_TICKET_TYPE, new ChunkPos(interiorDoorPosition), 1, interiorDoorPosition);
}

if (flightWave == null) {
flightWave = new FlightWave(getRandom());
}

var newState = state.tick(this);
var shake = state.getScreenShakeIntensity(this);
applyScreenShake(shake, shake > 0 ? 1 : 0);
Expand Down Expand Up @@ -496,6 +501,13 @@ public boolean setDoorOpen(boolean open, boolean force) {
return true;
}

public FlightWave getFlightWave() {
if (flightWave == null) {
return new FlightWave(0, 0, 0);
}
return flightWave;
}

public void createInteriorSparks(boolean damage) {
findSparkPos().ifPresent(pos -> {
var world = getInteriorWorld();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public boolean rotateDestination(Direction direction) {
public boolean handbrake(boolean state) {
if (!state && tardis.getState() instanceof FlyingState && !isDestinationLocked()) {
return tardis.getDestinationTardis()
.map(otherTardis -> tardis.suggestStateTransition(new InterceptingState(otherTardis.uuid())))
.map(otherTardis -> tardis.suggestStateTransition(new InterdictingState(otherTardis.uuid())))
.orElseGet(() -> tardis.suggestStateTransition(new DriftingState()));
} else if (tardis.getState() instanceof RespondsToFlyLever respondingState) {
return respondingState.toggleFlyLever(tardis, state) || tardis.suggestStateTransition(new FlyingState(tardis.getRandom().nextInt()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import xyz.nucleoid.fantasy.Fantasy;

import java.util.*;
import java.util.stream.Collectors;

public class TardisHolder implements ServerTickingComponent {
private MinecraftServer server;
Expand Down Expand Up @@ -54,6 +55,10 @@ public Set<Tardis> getAllTardii() {
return ImmutableSet.copyOf(tardii.values());
}

public Set<Tardis> getInterdictableTardii() {
return tardii.values().stream().filter(t -> t.getState().canBeInterdicted(t)).collect(Collectors.toSet());
}

@Override
public void serverTick() {
tardii.values().forEach(Tardis::tick);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.enjarai.minitardis.MiniTardis;
import dev.enjarai.minitardis.ModSounds;
import dev.enjarai.minitardis.component.PartialTardisLocation;
import dev.enjarai.minitardis.component.Tardis;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.Identifier;
import net.minecraft.util.Uuids;

import java.util.UUID;

public class BeingInterceptedState extends InterceptState {
public static final MapCodec<BeingInterceptedState> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
public class BeingInterdictedState extends InterdictState {
public static final MapCodec<BeingInterdictedState> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Uuids.CODEC.fieldOf("other_tardis").forGetter(s -> s.otherTardis),
Codec.INT.fieldOf("flying_ticks").forGetter(s -> s.flyingTicks),
Codec.INT.fieldOf("phases_complete").forGetter(s -> s.phasesComplete),
Expand All @@ -23,19 +22,19 @@ public class BeingInterceptedState extends InterceptState {
Codec.INT.fieldOf("offset_y").forGetter(s -> s.offsetY),
Codec.INT.fieldOf("target_x").forGetter(s -> s.targetX),
Codec.INT.fieldOf("target_y").forGetter(s -> s.targetY)
).apply(instance, BeingInterceptedState::new));
public static final Identifier ID = MiniTardis.id("being_intercepted");
).apply(instance, BeingInterdictedState::new));
public static final Identifier ID = MiniTardis.id("being_interdicted");

private int targetX;
private int targetY;

protected BeingInterceptedState(UUID otherTardis, int flyingTicks, int phasesComplete, int phaseTicks, int offsetX, int offsetY, int targetX, int targetY) {
protected BeingInterdictedState(UUID otherTardis, int flyingTicks, int phasesComplete, int phaseTicks, int offsetX, int offsetY, int targetX, int targetY) {
super(otherTardis, flyingTicks, phasesComplete, phaseTicks, offsetX, offsetY);
this.targetX = targetX;
this.targetY = targetY;
}

public BeingInterceptedState(UUID otherTardis) {
public BeingInterdictedState(UUID otherTardis) {
super(otherTardis);
}

Expand All @@ -58,7 +57,7 @@ public FlightState tick(Tardis tardis) {
}

var other = tardis.getHolder().getTardis(otherTardis);
if (other.flatMap(t -> t.getState(InterceptState.class)).isEmpty()) {
if (other.flatMap(t -> t.getState(InterdictState.class)).isEmpty()) {
tardis.getControls().moderateMalfunction();
return new FlyingState(tardis.getRandom().nextInt());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import dev.enjarai.minitardis.ModCCAComponents;
import dev.enjarai.minitardis.block.console.ScreenBlockEntity;
import dev.enjarai.minitardis.component.Tardis;
import dev.enjarai.minitardis.component.TardisControl;
import dev.enjarai.minitardis.component.screen.canvas.patbox.DrawableCanvas;
import net.minecraft.network.packet.s2c.play.PositionFlag;
import net.minecraft.network.packet.s2c.play.StopSoundS2CPacket;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
Expand All @@ -31,8 +29,8 @@ public interface FlightState {
Map.entry(BootingUpState.ID, BootingUpState.CODEC),
Map.entry(CrashedState.ID, CrashedState.CODEC),
Map.entry(SuspendedFlightState.ID, SuspendedFlightState.CODEC),
Map.entry(InterceptingState.ID, InterceptingState.CODEC),
Map.entry(BeingInterceptedState.ID, BeingInterceptedState.CODEC)
Map.entry(InterdictingState.ID, InterdictingState.CODEC),
Map.entry(BeingInterdictedState.ID, BeingInterdictedState.CODEC)
);
Map<Identifier, Supplier<? extends FlightState>> CONSTRUCTORS = Map.of(
LandedState.ID, LandedState::new,
Expand Down Expand Up @@ -98,6 +96,10 @@ default boolean overrideScreenImage(Tardis tardis) {
return false;
}

default boolean canBeInterdicted(Tardis tardis) {
return false;
}

/**
* The function that will be used to draw the custom screen image if `overrideScreenImage` returns true.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.enjarai.minitardis.MiniTardis;
import dev.enjarai.minitardis.ModSounds;
import dev.enjarai.minitardis.component.PartialTardisLocation;
import dev.enjarai.minitardis.component.Tardis;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
Expand Down Expand Up @@ -119,10 +118,10 @@ public boolean suggestTransition(Tardis tardis, FlightState newState) {
} else if (newState instanceof DriftingState driftingState) {
driftingState.flyingTicks = flyingTicks;
return true;
} else if (newState instanceof InterceptingState interceptingState) {
interceptingState.flyingTicks = flyingTicks;
} else if (newState instanceof InterdictingState interdictingState) {
interdictingState.flyingTicks = flyingTicks;
return true;
} else if (newState instanceof BeingInterceptedState interceptedState) {
} else if (newState instanceof BeingInterdictedState interceptedState) {
interceptedState.flyingTicks = flyingTicks;
return true;
} else if (newState instanceof SuspendedFlightState suspendedFlightState) {
Expand Down Expand Up @@ -175,6 +174,11 @@ public boolean isSolid(Tardis tardis) {
return false;
}

@Override
public boolean canBeInterdicted(Tardis tardis) {
return true;
}

@Override
public boolean isInteriorLightEnabled(int order) {
return spinnyLighting(order, flyingTicks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import static dev.enjarai.minitardis.component.flight.FlyingState.SOUND_LOOP_LENGTH;

public abstract class InterceptState implements FlightState, RespondsToFlyLever, RespondsToNudging {
public abstract class InterdictState implements FlightState, RespondsToFlyLever, RespondsToNudging {
public static final int PHASES = 6;
public static final int MAX_X_OFFSET = 4;
public static final int MAX_Y_OFFSET = 2;
Expand All @@ -23,7 +23,7 @@ public abstract class InterceptState implements FlightState, RespondsToFlyLever,
protected int offsetX;
protected int offsetY;

protected InterceptState(UUID otherTardis, int flyingTicks, int phasesComplete, int phaseTicks, int offsetX, int offsetY) {
protected InterdictState(UUID otherTardis, int flyingTicks, int phasesComplete, int phaseTicks, int offsetX, int offsetY) {
this.otherTardis = otherTardis;
this.flyingTicks = flyingTicks;
this.phasesComplete = phasesComplete;
Expand All @@ -32,7 +32,7 @@ protected InterceptState(UUID otherTardis, int flyingTicks, int phasesComplete,
this.offsetY = offsetY;
}

public InterceptState(UUID otherTardis) {
public InterdictState(UUID otherTardis) {
this(otherTardis, 0, 0, 0, 0, 0);
}

Expand Down Expand Up @@ -134,8 +134,8 @@ public int getPhasesComplete() {
return phasesComplete;
}

public Optional<InterceptState> getLinkedState(Tardis tardis) {
return tardis.getHolder().getTardis(otherTardis).flatMap(t -> t.getState(InterceptState.class));
public Optional<InterdictState> getLinkedState(Tardis tardis) {
return tardis.getHolder().getTardis(otherTardis).flatMap(t -> t.getState(InterdictState.class));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@

import java.util.UUID;

public class InterceptingState extends InterceptState {
public static final MapCodec<InterceptingState> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
public class InterdictingState extends InterdictState {
public static final MapCodec<InterdictingState> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Uuids.CODEC.fieldOf("other_tardis").forGetter(s -> s.otherTardis),
Codec.INT.fieldOf("flying_ticks").forGetter(s -> s.flyingTicks),
Codec.INT.fieldOf("phases_complete").forGetter(s -> s.phasesComplete),
Codec.INT.fieldOf("phase_ticks").forGetter(s -> s.phaseTicks),
Codec.INT.fieldOf("offset_x").forGetter(s -> s.offsetX),
Codec.INT.fieldOf("offset_y").forGetter(s -> s.offsetY)
).apply(instance, InterceptingState::new));
public static final Identifier ID = MiniTardis.id("intercepting");
).apply(instance, InterdictingState::new));
public static final Identifier ID = MiniTardis.id("interdicting");

protected InterceptingState(UUID otherTardis, int flyingTicks, int phasesComplete, int phaseTicks, int offsetX, int offsetY) {
protected InterdictingState(UUID otherTardis, int flyingTicks, int phasesComplete, int phaseTicks, int offsetX, int offsetY) {
super(otherTardis, flyingTicks, phasesComplete, phaseTicks, offsetX, offsetY);
}

public InterceptingState(UUID otherTardis) {
public InterdictingState(UUID otherTardis) {
super(otherTardis);
}

Expand All @@ -38,8 +38,8 @@ public FlightState tick(Tardis tardis) {
return new SearchingForLandingState(true, tardis.getRandom().nextInt());
}

if (other.get().getState(BeingInterceptedState.class).isEmpty()) {
if (!other.get().suggestStateTransition(new BeingInterceptedState(tardis.uuid()))) {
if (other.get().getState(BeingInterdictedState.class).isEmpty()) {
if (!other.get().suggestStateTransition(new BeingInterdictedState(tardis.uuid()))) {
tardis.getControls().moderateMalfunction();
return new FlyingState(tardis.getRandom().nextInt());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public boolean suggestTransition(Tardis tardis, FlightState newState) {
} else if (newState instanceof DriftingState driftingState) {
driftingState.flyingTicks = flyingTicks;
return true;
} else if (newState instanceof BeingInterceptedState interceptedState) {
} else if (newState instanceof BeingInterdictedState interceptedState) {
interceptedState.flyingTicks = flyingTicks;
return true;
} else if (newState instanceof TakingOffState) {
Expand All @@ -100,6 +100,11 @@ public boolean isSolid(Tardis tardis) {
return false;
}

@Override
public boolean canBeInterdicted(Tardis tardis) {
return true;
}

@Override
public boolean isInteriorLightEnabled(int order) {
return FlyingState.spinnyLighting(order, flyingTicks);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.enjarai.minitardis.component.screen.app;

import com.mojang.serialization.Codec;
import dev.enjarai.minitardis.block.console.ScreenBlockEntity;
import dev.enjarai.minitardis.canvas.TardisCanvasUtils;
import dev.enjarai.minitardis.component.FlightWave;
import dev.enjarai.minitardis.component.TardisControl;
import dev.enjarai.minitardis.component.screen.canvas.patbox.DrawableCanvas;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ClickType;

public class InterdictorApp implements ScreenApp {
public static final Codec<InterdictorApp> CODEC = Codec.unit(InterdictorApp::new);

@Override
public AppView getView(TardisControl controls) {
return new AppView() {
FlightWave selectedWave = new FlightWave(controls.getTardis().getRandom());

@Override
public void draw(ScreenBlockEntity blockEntity, DrawableCanvas canvas) {
var timeyTimey = controls.getTardis().getInteriorWorld().getTime() * 2;

drawWave(canvas, selectedWave, timeyTimey, (short) 0xf383);
}

void drawWave(DrawableCanvas canvas, FlightWave wave, double offset, short color) {
for (int x = 3; x < 126; x++) {
int y1 = 55 + (int) (Math.sin((x + offset + wave.getOffset() * 35.0 - 1) * wave.getPeriod() * 0.2) * 35.0 * wave.getMagnitude());
int y2 = 55 + (int) (Math.sin((x + offset + wave.getOffset() * 35.0) * wave.getPeriod() * 0.2) * 35.0 * wave.getMagnitude());
canvas.bresenhamLine(x - 1, y1, x, y2, color);
}
}

@Override
public void drawBackground(ScreenBlockEntity blockEntity, DrawableCanvas canvas) {
canvas.draw(0, 0, TardisCanvasUtils.getSprite("interdictor_background"));
}

@Override
public boolean onClick(ScreenBlockEntity blockEntity, ServerPlayerEntity player, ClickType type, int x, int y) {
return false;
}
};
}

@Override
public ScreenAppType<?> getType() {
return ScreenAppTypes.INTERDICTOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ScreenAppTypes {
public static final ScreenAppType<WaypointsApp> WAYPOINTS = register("waypoints", WaypointsApp.CODEC, WaypointsApp::new, true);
public static final ScreenAppType<LookAndFeelApp> LOOK_AND_FEEL = register("look_and_feel", LookAndFeelApp.CODEC, LookAndFeelApp::new, true);
public static final ScreenAppType<FloppyBirdApp> FLOPPY_BIRD = register("floppy_bird", FloppyBirdApp.CODEC, FloppyBirdApp::new, true);
public static final ScreenAppType<InterdictorApp> INTERDICTOR = register("interdictor", InterdictorApp.CODEC, InterdictorApp::new, true);

private static <T extends ScreenApp> ScreenAppType<T> register(String name, Codec<T> codec, Supplier<T> constructor, boolean spawnsAsDungeonLoot) {
return Registry.register(ScreenAppType.REGISTRY, MiniTardis.id(name), new ScreenAppType<>(MapCodec.assumeMapUnsafe(codec), constructor, spawnsAsDungeonLoot));
Expand Down
Loading

0 comments on commit d8cd235

Please sign in to comment.