Skip to content

Commit

Permalink
add pie graph shading style
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Oct 18, 2023
1 parent d74a38c commit 2f15254
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/main/java/net/pixaurora/janerator/shade/method/PieShading.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package net.pixaurora.janerator.shade.method;

import java.util.List;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.minecraft.util.Mth;
import net.pixaurora.janerator.config.SerialType;
import net.pixaurora.janerator.graphing.Coordinate;

public class PieShading implements SimpleShadingMethod {
public static final Codec<PieShading> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
Codec.STRING.listOf().fieldOf("generator_keys").forGetter(PieShading::involvedGeneratorKeys)
).apply(instance, PieShading::new)
);
public static final SerialType<ShadingMethod> TYPE = new SerialType<>("pie", CODEC);

private final List<String> generatorKeys;

private final double stepAmount;
private final int maxSlice;

public PieShading(List<String> generatorKeys) {
this.generatorKeys = generatorKeys;

this.stepAmount = 2 * Math.PI / (generatorKeys.size());
this.maxSlice = generatorKeys.size() - 1;
}

@Override
public SerialType<? extends ShadingMethod> type() {
return TYPE;
}

@Override
public List<String> involvedGeneratorKeys() {
return this.generatorKeys;
}

private int getSlice(Coordinate pos) {
int slice = Mth.floor((pos.angle() + Math.PI) / this.stepAmount);
return Math.min(slice, maxSlice); // Because there is a straight 1-thick line that is out of bounds otherwise
}

@Override
public String getShade(Coordinate pos) {
return this.generatorKeys.get(this.getSlice(pos));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import net.pixaurora.janerator.graphing.Coordinate;

public interface ShadingMethod extends SpecifiesType<ShadingMethod> {
public static final List<SerialType<ShadingMethod>> TYPES = new ArrayList<>(List.of(NormalShading.TYPE));
public static final List<SerialType<ShadingMethod>> TYPES = new ArrayList<>(List.of(NormalShading.TYPE, PieShading.TYPE));
public static final Codec<ShadingMethod> BASE_CODEC = new SerialType.Group<>("Shading method", TYPES).dispatchCodec();
public static final Codec<ShadingMethod> CODEC = Codec.either(
Codec.STRING,
Expand Down

0 comments on commit 2f15254

Please sign in to comment.