Wave 3, simplify trigger pixel handling

This commit is contained in:
Sollace 2018-04-25 13:00:18 +02:00
parent 52e2c84b67
commit 5da88c8551
7 changed files with 136 additions and 98 deletions

View file

@ -1,37 +1,13 @@
package com.minelittlepony;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableBiMap;
import com.minelittlepony.pony.data.TriggerPixels;
import java.awt.image.BufferedImage;
import java.util.Map;
import javax.annotation.concurrent.Immutable;
@Immutable
public class PonyData implements IPonyData {
private static final Map<Integer, PonyRace> RACE_COLORS = ImmutableBiMap.<Integer, PonyRace>builder()
.put(0xf9b131, PonyRace.EARTH)
.put(0xd19fe4, PonyRace.UNICORN)
.put(0x88caf0, PonyRace.PEGASUS)
.put(0xfef9fc, PonyRace.ALICORN)
.put(0xd0cccf, PonyRace.ZEBRA)
.put(0x282b29, PonyRace.CHANGELING)
.put(0xcaed5a, PonyRace.REFORMED_CHANGELING)
.put(0xae9145, PonyRace.GRIFFIN)
.put(0xd6ddac, PonyRace.HIPPOGRIFF)
.build();
private static final Map<Integer, TailLengths> TAIL_COLORS = ImmutableBiMap.<Integer, TailLengths>builder()
.put(0x425844, TailLengths.STUB)
.put(0xd19fe4, TailLengths.QUARTER)
.put(0x534b76, TailLengths.HALF)
.put(0x8a6b7f, TailLengths.THREE_QUARTERS).build();
private static final Map<Integer, PonySize> SIZE_COLORS = ImmutableBiMap.<Integer, PonySize>builder()
.put(0xffbe53, PonySize.FOAL)
.put(0xce3254, PonySize.LARGE)
.put(0x534b76, PonySize.TALL)
.build();
private final PonyRace race;
private final TailLengths tailSize;
private final PonyGender gender;
@ -39,15 +15,19 @@ public class PonyData implements IPonyData {
private final int glowColor;
public PonyData() {
this(PonyRace.HUMAN, TailLengths.FULL, PonyGender.MARE, PonySize.NORMAL, 0x4444aa);
this.race = PonyRace.HUMAN;
this.tailSize = TailLengths.FULL;
this.gender = PonyGender.MARE;
this.size = PonySize.NORMAL;
this.glowColor = 0x4444aa;
}
private PonyData(PonyRace race, TailLengths tailSize, PonyGender gender, PonySize size, int glowColor) {
this.race = race;
this.tailSize = tailSize;
this.gender = gender;
this.size = size;
this.glowColor = glowColor;
private PonyData(BufferedImage image) {
this.race = TriggerPixels.RACE.readValue(image);
this.tailSize = TriggerPixels.TAIL.readValue(image);
this.size = TriggerPixels.SIZE.readValue(image);
this.gender = TriggerPixels.GENDER.readValue(image);
this.glowColor = TriggerPixels.GLOW.readColor(image, -1);
}
@Override
@ -92,43 +72,6 @@ public class PonyData implements IPonyData {
}
static IPonyData parse(BufferedImage image) {
int racePx = TriggerPixels.RACE.readColor(image);
PonyRace race = RACE_COLORS.getOrDefault(racePx, PonyRace.HUMAN);
int tailPx = TriggerPixels.TAIL.readColor(image);
TailLengths tail = TAIL_COLORS.getOrDefault(tailPx, TailLengths.FULL);
int sizePx = TriggerPixels.SIZE.readColor(image);
PonySize size = SIZE_COLORS.getOrDefault(sizePx, PonySize.NORMAL);
int genderPx = TriggerPixels.GENDER.readColor(image);
PonyGender gender = genderPx == 0xffffff ? PonyGender.STALLION : PonyGender.MARE;
int glowColor = TriggerPixels.GLOW.readColor(image, -1);
return new PonyData(race, tail, gender, size, glowColor);
}
private enum TriggerPixels {
RACE(0, 0),
TAIL(1, 0),
GENDER(2, 0),
SIZE(3, 0),
GLOW(0, 1);
private int x, y;
TriggerPixels(int x, int y) {
this.x = x;
this.y = y;
}
private int readColor(BufferedImage image) {
return readColor(image, 0xffffff);
}
private int readColor(BufferedImage image, int mask) {
return image.getRGB(x, y) & mask;
}
return new PonyData(image);
}
}

View file

@ -1,6 +1,19 @@
package com.minelittlepony;
public enum PonyGender {
MARE,
STALLION
import com.minelittlepony.pony.data.ITriggerPixelMapped;
public enum PonyGender implements ITriggerPixelMapped<PonyGender> {
MARE(0),
STALLION(0xffffff);
int triggerValue;
PonyGender(int pixel) {
triggerValue = pixel;
}
@Override
public int getTriggerPixel() {
return triggerValue;
}
}

View file

@ -1,25 +1,30 @@
package com.minelittlepony;
import com.minelittlepony.model.PlayerModels;
import com.minelittlepony.pony.data.ITriggerPixelMapped;
public enum PonyRace {
public enum PonyRace implements ITriggerPixelMapped<PonyRace> {
HUMAN(PlayerModels.HUMAN, false, false),
EARTH(PlayerModels.PONY,false, false),
PEGASUS(PlayerModels.PONY, true, false),
UNICORN(PlayerModels.PONY, false, true),
ALICORN(PlayerModels.PONY, true, true),
CHANGELING(PlayerModels.PONY, true, true),
ZEBRA(PlayerModels.PONY, false, false),
REFORMED_CHANGELING(PlayerModels.PONY, true, true),
GRIFFIN(PlayerModels.PONY, true, false),
HIPPOGRIFF(PlayerModels.PONY, true, false);
HUMAN(0, PlayerModels.HUMAN, false, false),
EARTH(0xf9b131, PlayerModels.PONY,false, false),
PEGASUS(0x88caf0, PlayerModels.PONY, true, false),
UNICORN(0xd19fe4, PlayerModels.PONY, false, true),
ALICORN(0xfef9fc, PlayerModels.PONY, true, true),
CHANGELING(0x282b29, PlayerModels.PONY, true, true),
ZEBRA(0xd0cccf, PlayerModels.PONY, false, false),
REFORMED_CHANGELING(0xcaed5a, PlayerModels.PONY, true, true),
GRIFFIN(0xae9145, PlayerModels.PONY, true, false),
HIPPOGRIFF(0xd6ddac, PlayerModels.PONY, true, false);
private boolean wings, horn;
private int triggerPixel;
private PlayerModels model;
PonyRace(PlayerModels model, boolean wings, boolean horn) {
PonyRace(int triggerPixel, PlayerModels model, boolean wings, boolean horn) {
this.triggerPixel = triggerPixel;
this.wings = wings;
this.horn = horn;
this.model = model;
@ -45,4 +50,9 @@ public enum PonyRace {
if (level == PonyLevel.HUMANS) return HUMAN;
return this;
}
@Override
public int getTriggerPixel() {
return triggerPixel;
}
}

View file

@ -1,14 +1,19 @@
package com.minelittlepony;
public enum PonySize {
NORMAL(0.4f, 1f),
LARGE(0.5f, 0.8f),
FOAL(0.25f, 0.8f),
TALL(0.45f, 1f);
import com.minelittlepony.pony.data.ITriggerPixelMapped;
public enum PonySize implements ITriggerPixelMapped<PonySize> {
NORMAL(0, 0.4f, 1f),
LARGE(0xce3254, 0.5f, 0.8f),
FOAL(0xffbe53, 0.25f, 0.8f),
TALL(0x534b76, 0.45f, 1f);
private int triggerValue;
private float shadowSize, scale;
PonySize(float shadowSz, float scaleF) {
PonySize(int pixel, float shadowSz, float scaleF) {
triggerValue = pixel;
shadowSize = shadowSz;
scale = scaleF;
}
@ -20,4 +25,9 @@ public enum PonySize {
public float getScaleFactor() {
return scale;
}
@Override
public int getTriggerPixel() {
return triggerValue;
}
}

View file

@ -1,10 +1,23 @@
package com.minelittlepony;
public enum TailLengths {
import com.minelittlepony.pony.data.ITriggerPixelMapped;
STUB,
QUARTER,
HALF,
THREE_QUARTERS,
FULL;
public enum TailLengths implements ITriggerPixelMapped<TailLengths> {
STUB(0x425844),
QUARTER(0xd19fe4),
HALF(0x534b76),
THREE_QUARTERS(0x8a6b7f),
FULL(0);
private int triggerValue;
TailLengths(int pixel) {
triggerValue = pixel;
}
@Override
public int getTriggerPixel() {
return triggerValue;
}
}

View file

@ -0,0 +1,14 @@
package com.minelittlepony.pony.data;
public interface ITriggerPixelMapped<T extends Enum<T> & ITriggerPixelMapped<T>> {
int getTriggerPixel();
@SuppressWarnings("unchecked")
public static <T extends Enum<T> & ITriggerPixelMapped<T>> T getByTriggerPixel(T type, int pixelValue) {
for (T i : (T[])type.getClass().getEnumConstants()) {
if (i.getTriggerPixel() == pixelValue) return i;
}
return type;
}
}

View file

@ -0,0 +1,35 @@
package com.minelittlepony.pony.data;
import java.awt.image.BufferedImage;
import com.minelittlepony.PonyGender;
import com.minelittlepony.PonyRace;
import com.minelittlepony.PonySize;
import com.minelittlepony.TailLengths;
public enum TriggerPixels {
RACE(PonyRace.HUMAN, 0, 0),
TAIL(TailLengths.FULL, 1, 0),
GENDER(PonyGender.MARE, 2, 0),
SIZE(PonySize.NORMAL, 3, 0),
GLOW(null, 0, 1);
private int x, y;
ITriggerPixelMapped<?> def;
TriggerPixels(ITriggerPixelMapped<?> def, int x, int y) {
this.def = def;
this.x = x;
this.y = y;
}
public int readColor(BufferedImage image, int mask) {
return image.getRGB(x, y) & mask;
}
@SuppressWarnings("unchecked")
public <T extends Enum<T> & ITriggerPixelMapped<T>> T readValue(BufferedImage image) {
return ITriggerPixelMapped.getByTriggerPixel((T)def, readColor(image, 0xffffff));
}
}