Skip to content

Commit

Permalink
[#133] Implemented basic flare.
Browse files Browse the repository at this point in the history
=> Currently no cone
=> Can already be implemented via contentpack
=> Added some caching for the flares, should be extended later on
  • Loading branch information
Danielxs01 committed Dec 15, 2023
1 parent 9a691a2 commit 8d91caa
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.landofrails.api.contentpacks.v2.flares.Flare;
import net.landofrails.stellwand.utils.exceptions.ContentPackException;

import java.io.IOException;
Expand All @@ -14,6 +15,11 @@ public class ContentPackSignalPart {
private String name;
private String model;

/**
* FIXME Hack. Remove once LOSBlocks has been reworked with Builder-Pattern.
*/
private Flare[] flares;

private float[] translation;
private float[] itemTranslation;
private float[] scaling;
Expand Down Expand Up @@ -61,6 +67,16 @@ public void setModel(final String model) {
this.model = model;
}

// FIXME Hack. Remove once LOSBlocks has been reworked with Builder-Pattern.
public Flare[] getFlares() {
return flares;
}

// FIXME Hack. Remove once LOSBlocks has been reworked with Builder-Pattern.
public void setFlares(Flare[] flares) {
this.flares = flares;
}

public float[] getTranslation() {
return translation;
}
Expand Down
126 changes: 126 additions & 0 deletions src/main/java/net/landofrails/api/contentpacks/v2/flares/Flare.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package net.landofrails.api.contentpacks.v2.flares;

import java.util.Map;
import java.util.function.Predicate;

public class Flare {

/**
* Required. Needs to be named after the element in the model.
*/
private String id;
/**
* Optional. Will set the color of the flare. Default: OxFFFFFF (white)
*/
private String color = "#FFFFFF";
/**
* Optional. Will set the intensity of the light flare. Range 0-2. Default 2.
*/
private float intensity = 2f;

/**
* Optional. Will always activate light flare. Default: false
*/
private boolean alwaysOn = false;

/**
* Required for: ContentPackSignal. States that will trigger this light flare.
*/
private String[] states;

/**
* Required for: ContentPackComplexSignal. Groups+States that will trigger this light flare.
*/
private Map<String, String> groupStates;

public Flare(String id, String color, float intensity, boolean alwaysOn, String[] states) {
this.id = id;
this.color = color;
this.intensity = intensity;
this.alwaysOn = alwaysOn;
this.states = states;
}

public Flare(String id, String color, float intensity, boolean alwaysOn, Map<String, String> groupStates) {
this.id = id;
this.color = color;
this.intensity = intensity;
this.alwaysOn = alwaysOn;
this.groupStates = groupStates;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public float getIntensity() {
return intensity;
}

public void setIntensity(float intensity) {
this.intensity = intensity;
}

public boolean isAlwaysOn() {
return alwaysOn;
}

public void setAlwaysOn(boolean alwaysOn) {
this.alwaysOn = alwaysOn;
}

public String[] getStates() {
return states;
}

public void setStates(String[] states) {
this.states = states;
}

public Map<String, String> getGroupStates() {
return groupStates;
}

public void setGroupStates(Map<String, String> groupStates) {
this.groupStates = groupStates;
}

public float[] getRenderColor(){
float[] rgb = new float[3];

// 80;160;240 or 010:020:030 or 0-050-230
Predicate<String> isRGB = rawColor -> rawColor.matches("((\\d{1,3}[;:-]){2}\\d{1,3})");
// #123DEF or 0xFFAA00
Predicate<String> isHEX = rawColor -> rawColor.matches("(#|0x)[A-Z\\d]{6}");

if(isRGB.test(color)){
String[] rawColors = color.split("[;:-]");
rgb[0] = Integer.parseInt(rawColors[0]) / 255f;
rgb[1] = Integer.parseInt(rawColors[1]) / 255f;
rgb[2] = Integer.parseInt(rawColors[2]) / 255f;
}else if(isHEX.test(color)){
String rawColors = color.replaceAll("(#|0x)", "");
rgb[0] = Integer.valueOf( rawColors.substring( 0, 2 ), 16 ) / 255f;
rgb[1] = Integer.valueOf( rawColors.substring( 2, 4 ), 16 ) / 255f;
rgb[2] = Integer.valueOf( rawColors.substring( 4, 6 ), 16 ) / 255f;
}else{
String error = "Issues with flare \"%s\": \"%s\" is not rgb or hex!";
throw new RuntimeException(String.format(error, id, color));
}

return rgb;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.Gson;
import net.landofrails.api.contentpacks.v2.ContentPack;
import net.landofrails.api.contentpacks.v2.ContentPackException;
import net.landofrails.api.contentpacks.v2.flares.Flare;
import net.landofrails.landofsignals.LOSTabs;

import java.io.IOException;
Expand All @@ -23,12 +24,14 @@ public class ContentPackSignal {
private String base;
private String[] states;
private String itemState;
private Flare[] flares;

private float[] translation;
private float[] itemTranslation;
private float[] scaling;
private float[] itemScaling;


// metadataId : data
private Map<String, Object> metadata;

Expand Down Expand Up @@ -142,10 +145,18 @@ public String getItemState() {
return itemState;
}

public void setItemGroupStates(String itemState) {
public void setItemState(String itemState) {
this.itemState = itemState;
}

public Flare[] getFlares() {
return flares;
}

public void setFlares(Flare[] flares) {
this.flares = flares;
}

public float[] getTranslation() {
return translation;
}
Expand Down Expand Up @@ -255,6 +266,10 @@ private void defaultMissing() {
itemState = states[0];
}

if(flares == null){
flares = new Flare[0];
}

if (translation == null) {
translation = new float[]{0, 0, 0};
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/net/landofrails/landofsignals/LOSBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.landofrails.api.contentpacks.v2.ContentPackException;
import net.landofrails.api.contentpacks.v2.complexsignal.ContentPackComplexSignal;
import net.landofrails.api.contentpacks.v2.deco.ContentPackDeco;
import net.landofrails.api.contentpacks.v2.flares.Flare;
import net.landofrails.api.contentpacks.v2.lever.ContentPackLever;
import net.landofrails.api.contentpacks.v2.parent.ContentPackModel;
import net.landofrails.api.contentpacks.v2.sign.ContentPackSign;
Expand Down Expand Up @@ -110,10 +111,12 @@ public static void register() {
"an"
}));

registerSignalContentPack(new ContentPackSignalPart("block_signal_part_light_flare", "Ampel (Off, Red)", "models/block/landofsignals/light_flare/lightflare.obj", new float[]{0.5f, 0f, 0.5f}, new float[]{0.5f, 0f, 0.5f}, new float[]{1f, 1f, 1f}, new String[]{
ContentPackSignalPart light_flare = new ContentPackSignalPart("block_signal_part_light_flare", "Ampel (Off, Red)", "models/block/landofsignals/light_flare/lightflare.obj", new float[]{0.5f, 0f, 0.5f}, new float[]{0.5f, 0f, 0.5f}, new float[]{1f, 1f, 1f}, new String[]{
"",
"red"
}));
});
light_flare.setFlares(new Flare[]{new Flare("lightflare_1", "165;32;25", 2f, false, new String[]{"red"})});
registerSignalContentPack(light_flare);

// Animated Signals
LOSBlocks.BLOCK_SIGNAL_PART_ANIMATED.add(MISSING_SIGNAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public static void convertToV2(ContentPackSignalPart contentPackSignalPart, Cont
contentPackSignal.setItemTranslation(contentPackSignalPart.getItemTranslation());
contentPackSignal.setScaling(contentPackSignalPart.getScaling());
contentPackSignal.setItemScaling(contentPackSignalPart.getItemScaling());
// FIXME Hack. Remove once LOSBlocks has been reworked with Builder-Pattern.
contentPackSignal.setFlares(contentPackSignalPart.getFlares());

ModCore.info("Signal (v1->v2): %s", contentPackSignal.getName());
// Validate
Expand Down
Loading

0 comments on commit 8d91caa

Please sign in to comment.