WaveFunctionCollapse editor

This commit is contained in:
Grimm
2022-08-02 17:59:30 +02:00
parent 42ae9dda06
commit 95fba3d0cb
21 changed files with 417 additions and 114 deletions

View File

@@ -2,11 +2,7 @@ package forge.adventure;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Clipboard;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3WindowListener;
import com.badlogic.gdx.backends.lwjgl3.*;
import com.badlogic.gdx.graphics.glutils.HdpiMode;
import forge.Forge;
import forge.adventure.util.Config;

View File

@@ -46,8 +46,8 @@ public class BiomeEdit extends JComponent {
center.add(new JLabel("enemies:")); center.add(enemies);
center.add(new JLabel("pointsOfInterest:")); center.add(pointsOfInterest);
center.add(new JLabel("color:")); center.add(color);
center.add(new JLabel("terrain/structures:"));
BorderLayout layout=new BorderLayout();
center.add(new JLabel("terrain/structures:"));center.add(new JLabel(""));
BoxLayout layout=new BoxLayout(this, BoxLayout.Y_AXIS);
setLayout(layout);
add(center,BorderLayout.NORTH);
add(terrain,BorderLayout.CENTER);

View File

@@ -0,0 +1,169 @@
package forge.adventure.editor;
import forge.adventure.data.BiomeStructureData;
import forge.adventure.util.Config;
import forge.adventure.world.BiomeStructure;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class BiomeStructureDataMappingEditor extends JComponent {
DefaultListModel<BiomeStructureData.BiomeStructureDataMapping> model = new DefaultListModel<>();
JList<BiomeStructureData.BiomeStructureDataMapping> list = new JList<>(model);
JToolBar toolBar = new JToolBar("toolbar");
BiomeStructureDataMappingEdit edit=new BiomeStructureDataMappingEdit();
private BiomeStructureData data;
public void setCurrent(BiomeStructureData data) {
this.data=data;
model.clear();
for(int i=0;data.mappingInfo!=null&&i<data.mappingInfo.length;i++)
model.addElement(data.mappingInfo[i]);
}
public BiomeStructureData.BiomeStructureDataMapping[] getCurrent()
{
BiomeStructureData.BiomeStructureDataMapping[] array=new BiomeStructureData.BiomeStructureDataMapping[model.size()];
for(int i=0;i<array.length;i++)
array[i]=model.get(i);
return array;
}
public class BiomeStructureDataMappingRenderer extends DefaultListCellRenderer {
private final BiomeStructureDataMappingEditor editor;
public BiomeStructureDataMappingRenderer(BiomeStructureDataMappingEditor biomeStructureDataMappingEditor) {
this.editor=biomeStructureDataMappingEditor;
}
@Override
public Component getListCellRendererComponent(
JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if(!(value instanceof BiomeStructureData.BiomeStructureDataMapping))
return label;
BiomeStructureData.BiomeStructureDataMapping data=(BiomeStructureData.BiomeStructureDataMapping) value;
// Get the renderer component from parent class
label.setText(data.name);
if(editor.data!=null)
{
SwingAtlas itemAtlas=new SwingAtlas(Config.instance().getFile(editor.data.structureAtlasPath));
if(itemAtlas.has(data.name))
label.setIcon(itemAtlas.get(data.name));
else
{
ImageIcon img=itemAtlas.getAny();
if(img!=null)
label.setIcon(img);
}
}
return label;
}
}
public void addButton(String name, ActionListener action)
{
JButton newButton=new JButton(name);
newButton.addActionListener(action);
toolBar.add(newButton);
}
public BiomeStructureDataMappingEditor()
{
list.setCellRenderer(new BiomeStructureDataMappingEditor.BiomeStructureDataMappingRenderer(this));
list.addListSelectionListener(e -> BiomeStructureDataMappingEditor.this.updateEdit());
addButton("add", e -> BiomeStructureDataMappingEditor.this.add());
addButton("remove", e -> BiomeStructureDataMappingEditor.this.remove());
addButton("copy", e -> BiomeStructureDataMappingEditor.this.copy());
BorderLayout layout=new BorderLayout();
setLayout(layout);
add(new JScrollPane(list), BorderLayout.WEST);
add(toolBar, BorderLayout.NORTH);
add(edit,BorderLayout.CENTER);
}
private void copy() {
int selected=list.getSelectedIndex();
if(selected<0)
return;
BiomeStructureData.BiomeStructureDataMapping data=new BiomeStructureData.BiomeStructureDataMapping(model.get(selected));
model.add(model.size(),data);
}
private void updateEdit() {
int selected=list.getSelectedIndex();
if(selected<0)
return;
edit.setCurrent(model.get(selected));
}
void add()
{
BiomeStructureData.BiomeStructureDataMapping data=new BiomeStructureData.BiomeStructureDataMapping();
data.name="Structure "+model.getSize();
model.add(model.size(),data);
}
void remove()
{
int selected=list.getSelectedIndex();
if(selected<0)
return;
model.remove(selected);
}
private class BiomeStructureDataMappingEdit extends JComponent{
BiomeStructureData.BiomeStructureDataMapping currentData;
public JTextField name=new JTextField();
public JTextField color=new JTextField();
public JCheckBox collision=new JCheckBox();
private boolean updating=false;
public BiomeStructureDataMappingEdit()
{
setLayout(new GridLayout(3,2));
add(new JLabel("name:")); add(name);
add(new JLabel("color:")); add(color);
add(new JLabel("collision:")); add(collision);
name.getDocument().addDocumentListener(new DocumentChangeListener(() -> BiomeStructureDataMappingEdit.this.update()));
color.getDocument().addDocumentListener(new DocumentChangeListener(() -> BiomeStructureDataMappingEdit.this.update()));
collision.addChangeListener(e -> BiomeStructureDataMappingEdit.this.update());
refresh();
}
private void update() {
if(currentData==null||updating)
return;
currentData.name = name.getText();
currentData.color = color.getText();
currentData.collision = collision.isSelected();
}
public void setCurrent(BiomeStructureData.BiomeStructureDataMapping data)
{
currentData=data;
refresh();
}
private void refresh() {
setEnabled(currentData!=null);
if(currentData==null)
{
return;
}
updating=true;
name.setText(currentData.name);
color.setText(currentData.color);
collision.setSelected(currentData.collision);
updating=false;
}
}
}

View File

@@ -7,43 +7,61 @@ import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.image.BufferedImage;
public class BiomeStructureEdit extends JComponent {
SwingAtlasPreview preview=new SwingAtlasPreview(128);
private boolean updating=false;
BiomeStructureData currentData;
BiomeData currentBiomeData;
public JTextField structureAtlasPath=new JTextField();
public FloatSpinner x= new FloatSpinner();
public FloatSpinner y= new FloatSpinner();
public FloatSpinner size= new FloatSpinner();
public FloatSpinner width= new FloatSpinner();
public FloatSpinner height= new FloatSpinner();
public JCheckBox randomPosition=new JCheckBox();
public JCheckBox collision=new JCheckBox();
public IntSpinner N= new IntSpinner();
public JTextField sourcePath= new JTextField();
public JCheckBox periodicInput= new JCheckBox();
public IntSpinner ground= new IntSpinner();
public IntSpinner symmetry= new IntSpinner();
public JCheckBox periodicOutput= new JCheckBox();
public BiomeStructureDataMappingEditor data=new BiomeStructureDataMappingEditor();
public BiomeStructureEdit()
{
JComponent center=new JComponent() { };
center.setLayout(new GridLayout(6,2));
center.setLayout(new GridLayout(11,2));
center.add(new JLabel("structureAtlasPath:")); center.add(structureAtlasPath);
center.add(new JLabel("x:")); center.add(x);
center.add(new JLabel("y:")); center.add(y);
center.add(new JLabel("size:")); center.add(size);
center.add(new JLabel("randomPosition:")); center.add(randomPosition);
center.add(new JLabel("collision:")); center.add(collision);
center.add(new JLabel("width:")); center.add(width);
center.add(new JLabel("height:")); center.add(height);
center.add(new JLabel("N:")); center.add(N);
center.add(new JLabel("sourcePath:")); center.add(sourcePath);
center.add(new JLabel("periodicInput:")); center.add(periodicInput);
center.add(new JLabel("ground:")); center.add(ground);
center.add(new JLabel("symmetry:")); center.add(symmetry);
center.add(new JLabel("periodicOutput:")); center.add(periodicOutput);
BorderLayout layout=new BorderLayout();
setLayout(layout);
add(preview,BorderLayout.LINE_START);
add(center,BorderLayout.CENTER);
add(data,BorderLayout.SOUTH);
structureAtlasPath.getDocument().addDocumentListener(new DocumentChangeListener(() -> BiomeStructureEdit.this.updateStructure()));
x.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
y.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
size.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
width.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
height.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
randomPosition.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
collision.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
N.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
sourcePath.getDocument().addDocumentListener(new DocumentChangeListener(() -> BiomeStructureEdit.this.updateStructure()));
periodicInput.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
ground.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
symmetry.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
periodicOutput.addChangeListener(e -> BiomeStructureEdit.this.updateStructure());
refresh();
}
private void refresh() {
@@ -56,10 +74,17 @@ public class BiomeStructureEdit extends JComponent {
structureAtlasPath.setText(currentData.structureAtlasPath);
x.setValue(currentData.x);
y.setValue(currentData.y);
size.setValue(currentData.size);
width.setValue(currentData.width);
height.setValue(currentData.height);
randomPosition.setSelected(currentData.randomPosition);
collision.setSelected(currentData.collision);
preview.setSpritePath(currentBiomeData.tilesetAtlas,currentData.structureAtlasPath);
N.setValue(currentData.N);
sourcePath.setText(currentData.sourcePath);
periodicInput.setSelected(currentData.periodicInput);
ground.setValue(currentData.ground);
symmetry.setValue(currentData.symmetry);
periodicOutput.setSelected(currentData.periodicOutput);
data.setCurrent(currentData);
updating=false;
}
public void updateStructure()
@@ -71,10 +96,17 @@ public class BiomeStructureEdit extends JComponent {
currentData.x= x.floatValue();
currentData.y= y.floatValue();
currentData.size= size.floatValue();
currentData.width= width.floatValue();
currentData.height= height.floatValue();
currentData.randomPosition=randomPosition.isSelected();
currentData.collision=collision.isSelected();
preview.setSpritePath(currentBiomeData.tilesetAtlas,currentData.structureAtlasPath);
currentData.mappingInfo= data.getCurrent();
currentData.N= N.intValue();
currentData.sourcePath= sourcePath.getText();
currentData.periodicInput= periodicInput.isSelected();
currentData.ground= ground.intValue();
currentData.symmetry= symmetry.intValue();
currentData.periodicOutput= periodicOutput.isSelected();
emitChanged();
}
public void setCurrentStructure(BiomeStructureData biomeTerrainData, BiomeData data) {
@@ -95,4 +127,5 @@ public class BiomeStructureEdit extends JComponent {
}
}
}
}

View File

@@ -29,7 +29,7 @@ public class BiomeTerrainEdit extends JComponent {
center.add(new JLabel("resolution:")); center.add(resolution);
BorderLayout layout=new BorderLayout();
setLayout(layout);
add(preview,BorderLayout.LINE_START);
add(preview,BorderLayout.WEST);
add(center,BorderLayout.CENTER);
spriteName.getDocument().addDocumentListener(new DocumentChangeListener(() -> BiomeTerrainEdit.this.updateTerrain()));
@@ -52,7 +52,8 @@ public class BiomeTerrainEdit extends JComponent {
min.setValue(currentData.min);
max.setValue(currentData.max);
resolution.setValue(currentData.resolution);
preview.setSpritePath(currentBiomeData.tilesetAtlas,currentData.spriteName);
if(currentBiomeData!=null&&currentData!= null)
preview.setSpritePath(currentBiomeData.tilesetAtlas,currentData.spriteName);
updating=false;
}
public void updateTerrain()

View File

@@ -7,6 +7,7 @@ import java.awt.*;
* Editor class to edit configuration, maybe moved or removed
*/
public class EditorMainWindow extends JFrame {
public final static WorldEditor worldEditor = new WorldEditor();
JTabbedPane tabs =new JTabbedPane();
public EditorMainWindow()
@@ -14,7 +15,7 @@ public class EditorMainWindow extends JFrame {
BorderLayout layout=new BorderLayout();
setLayout(layout);
add(tabs);
tabs.addTab("World",new WorldEditor());
tabs.addTab("World",worldEditor);
tabs.addTab("POI",new PointOfInterestEditor());
tabs.addTab("Items",new ItemsEditor());
tabs.addTab("Enemies",new EnemyEditor());

View File

@@ -2,13 +2,18 @@ package forge.adventure.editor;
import forge.adventure.data.BiomeData;
import forge.adventure.data.BiomeStructureData;
import forge.adventure.data.BiomeTerrainData;
import forge.adventure.data.WorldData;
import forge.adventure.util.Config;
import forge.adventure.world.BiomeStructure;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
/**
* Editor class to edit configuration, maybe moved or removed
@@ -27,14 +32,11 @@ public class StructureEditor extends JComponent{
JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if(!(value instanceof BiomeTerrainData))
if(!(value instanceof BiomeStructureData))
return label;
BiomeTerrainData structureData=(BiomeTerrainData) value;
StringBuilder builder=new StringBuilder();
builder.append("Structure");
builder.append(" ");
builder.append(structureData.spriteName);
label.setText(builder.toString());
BiomeStructureData structureData=(BiomeStructureData) value;
label.setText("Structure");
label.setIcon(new ImageIcon(Config.instance().getFilePath(structureData.sourcePath)));
return label;
}
}
@@ -54,10 +56,11 @@ public class StructureEditor extends JComponent{
addButton("add", e -> StructureEditor.this.addStructure());
addButton("remove", e -> StructureEditor.this.remove());
addButton("copy", e -> StructureEditor.this.copy());
addButton("test", e -> StructureEditor.this.test());
BorderLayout layout=new BorderLayout();
setLayout(layout);
add(list, BorderLayout.LINE_START);
add(toolBar, BorderLayout.PAGE_START);
add(list, BorderLayout.WEST);
add(toolBar, BorderLayout.NORTH);
add(edit,BorderLayout.CENTER);
@@ -77,6 +80,45 @@ public class StructureEditor extends JComponent{
}
}
}
private void test() {
if (list.isSelectionEmpty())
return;
BiomeStructureData data = model.get(list.getSelectedIndex());
try {
BiomeStructure struct = new BiomeStructure(data, System.currentTimeMillis(),
(int) (currentData.width * EditorMainWindow.worldEditor.width.intValue() * data.width),
(int) (currentData.width * EditorMainWindow.worldEditor.height.intValue() * data.height));
struct.initialize();
JLabel label = new JLabel();
BufferedImage image = struct.image;
if (image.getWidth() < 640 | image.getHeight() < 640) {
if (image.getHeight() > image.getWidth()) {
BufferedImage nimage = new BufferedImage(640, 640 * (image.getWidth() / image.getHeight()), BufferedImage.TYPE_INT_ARGB);
AffineTransform at = new AffineTransform();
at.scale(640 / image.getHeight(), 640 / image.getHeight());
AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = scaleOp.filter(image, nimage);
} else {
BufferedImage nimage = new BufferedImage(640 * (image.getHeight() / image.getWidth()), 640, BufferedImage.TYPE_INT_ARGB);
AffineTransform at = new AffineTransform();
at.scale(640 / image.getWidth(), 640 / image.getWidth());
AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = scaleOp.filter(image, nimage);
}
}
label.setIcon(new ImageIcon(image));
label.setSize(640, 640);
JOptionPane.showMessageDialog(this, label);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, "WaveFunctionCollapse was not successful");
}
}
private void copy() {
int selected=list.getSelectedIndex();

View File

@@ -4,6 +4,7 @@ import forge.adventure.util.Config;
import org.apache.commons.lang3.tuple.Pair;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
@@ -40,7 +41,6 @@ public class SwingAtlasPreview extends Box {
setSpritePath(sprite,null);
}
public void setSpritePath(String sprite,String name) {
if(this.sprite==null||name==null||sprite==null||(this.sprite.equals(sprite)&&(spriteName==null&&spriteName.equals(name))))
return;
removeAll();

View File

@@ -16,6 +16,7 @@ import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class WorldEditor extends JComponent {
@@ -23,15 +24,15 @@ public class WorldEditor extends JComponent {
WorldData currentData;
JSpinner width= new JSpinner(new SpinnerNumberModel(0, 0, 100000, 1));
JSpinner height= new JSpinner(new SpinnerNumberModel(0, 0, 100000, 1));
JSpinner playerStartPosX= new JSpinner(new SpinnerNumberModel(0, 0, 1, .1));
JSpinner playerStartPosY= new JSpinner(new SpinnerNumberModel(0, 0, 1, .1));
JSpinner noiseZoomBiome= new JSpinner(new SpinnerNumberModel(0, 0, 1000f, 1f));
JSpinner tileSize= new JSpinner(new SpinnerNumberModel(0, 0, 100000, 1));
IntSpinner width= new IntSpinner( 0, 100000, 1);
IntSpinner height= new IntSpinner( 0, 100000, 1);
FloatSpinner playerStartPosX= new FloatSpinner( 0, 1, .1f);
FloatSpinner playerStartPosY= new FloatSpinner(0, 1, .1f);
FloatSpinner noiseZoomBiome= new FloatSpinner( 0, 1000f, 1f);
IntSpinner tileSize= new IntSpinner( 0, 100000, 1);
JTextField biomesSprites = new JTextField();
JSpinner maxRoadDistance = new JSpinner(new SpinnerNumberModel(0, 0, 100000f, 1f));
FloatSpinner maxRoadDistance = new FloatSpinner( 0, 100000f, 1f);
TextListEdit biomesNames = new TextListEdit();
DefaultListModel<BiomeData> model = new DefaultListModel<>();
@@ -92,7 +93,7 @@ public class WorldEditor extends JComponent {
setLayout(layout);
add(tabs);
JPanel worldPanel=new JPanel();
JPanel biomeData=new JPanel();
JSplitPane biomeData=new JSplitPane();
tabs.addTab("BiomeData", biomeData);
tabs.addTab("WorldData", worldPanel);
@@ -116,8 +117,8 @@ public class WorldEditor extends JComponent {
worldPanel.add(new Box.Filler(new Dimension(0,0),new Dimension(0,Integer.MAX_VALUE),new Dimension(0,Integer.MAX_VALUE)));
biomeData.setLayout(new GridLayout(1,2)) ;
biomeData.add(list); biomeData.add(edit);
JScrollPane pane = new JScrollPane(edit);
biomeData.setLeftComponent(list); biomeData.setRightComponent(pane);
load();
@@ -166,6 +167,16 @@ public class WorldEditor extends JComponent {
void save()
{
currentData.width=width.intValue();
currentData.height=height.intValue();
currentData.playerStartPosX=playerStartPosX.floatValue();
currentData.playerStartPosY=playerStartPosY.floatValue();
currentData.noiseZoomBiome=noiseZoomBiome.floatValue();
currentData.tileSize=tileSize.intValue();
currentData.biomesSprites=biomesSprites.getText();
currentData.maxRoadDistance=maxRoadDistance.floatValue();
currentData.biomesNames= Arrays.asList(biomesNames.getList());
Json json = new Json(JsonWriter.OutputType.json);
FileHandle handle = Config.instance().getFile(Paths.WORLD);
handle.writeString(json.prettyPrint(json.toJson(currentData,Array.class, WorldData.class)),false);
@@ -173,6 +184,9 @@ public class WorldEditor extends JComponent {
}
void load()
{
model.clear();
Json json = new Json();
FileHandle handle = Config.instance().getFile(Paths.WORLD);

View File

@@ -888,6 +888,8 @@ public class Forge implements ApplicationListener {
//check if sentry is enabled, if not it will call the gui interface but here we end the graphics so we only send it via sentry..
if (BugReporter.isSentryEnabled())
BugReporter.reportException(ex);
else
ex.printStackTrace();
}
if (showFPS)
frameRate.render();

View File

@@ -1,34 +1,62 @@
package forge.adventure.data;
import java.awt.*;
import java.awt.image.BufferedImage;
public class BiomeStructureData {
static public class BiomeStructureDataMapping
{
public int getColor() {
return 0xff000000 |(Integer.parseInt(color,16));
}
public String name;
public String color;
public boolean collision;
public BiomeStructureDataMapping() {
}
public BiomeStructureDataMapping(BiomeStructureDataMapping biomeStructureDataMapping) {
this.name=biomeStructureDataMapping.name;
this.color=biomeStructureDataMapping.color;
this.collision=biomeStructureDataMapping.collision;
}
}
public int N = 3;
public float x;
public float y;
public float size;
public boolean randomPosition;
public boolean collision;
public String structureAtlasPath;
public boolean periodicInput;
public String sourcePath;
public boolean periodicInput=true;
public float height;
public float width;
public int ground;
public int symmetry;
public boolean periodicOutput;
public int symmetry=2;
public boolean periodicOutput=true;
public BiomeStructureDataMapping[] mappingInfo;
public BiomeStructureData( )
{
public BiomeStructureData( ) {
}
public BiomeStructureData(BiomeStructureData biomeStructureData) {
this.structureAtlasPath=biomeStructureData.structureAtlasPath;
this.sourcePath=biomeStructureData.sourcePath;
this.x=biomeStructureData.x;
this.y=biomeStructureData.y;
this.size=biomeStructureData.size;
this.width=biomeStructureData.width;
this.height=biomeStructureData.height;
this.randomPosition=biomeStructureData.randomPosition;
this.collision=biomeStructureData.collision;
if(biomeStructureData.mappingInfo!=null)
{
this.mappingInfo=new BiomeStructureDataMapping[ biomeStructureData.mappingInfo.length];
for(int i=0;i<biomeStructureData.mappingInfo.length;i++)
this.mappingInfo[i]=new BiomeStructureDataMapping(biomeStructureData.mappingInfo[i]);
}
else
this.mappingInfo=null;
}
public BufferedImage sourceImage() {

View File

@@ -3,6 +3,7 @@ package forge.adventure.data;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.SerializationException;
import forge.adventure.util.Config;
import forge.adventure.util.Paths;
import forge.adventure.world.BiomeSprites;
@@ -80,10 +81,16 @@ public class WorldData implements Serializable {
public List<BiomeData> GetBiomes() {
if (biomes == null) {
biomes = new ArrayList<BiomeData>();
Json json = new Json();
for (String name : biomesNames) {
biomes.add(json.fromJson(BiomeData.class, Config.instance().getFile(name)));
try
{
biomes = new ArrayList<BiomeData>();
Json json = new Json();
for (String name : biomesNames) {
biomes.add(json.fromJson(BiomeData.class, Config.instance().getFile(name)));
}
}
catch (SerializationException ex) {
ex.printStackTrace();
}
}
return biomes;

View File

@@ -20,6 +20,8 @@ public class BiomeStructure {
private int dataMap[][];
boolean init=false;
private TextureAtlas structureAtlas;
public BufferedImage image;
public BiomeStructure(BiomeStructureData data,long seed,int width,int height)
{
this.data=data;
@@ -35,59 +37,42 @@ public class BiomeStructure {
return structureAtlas;
}
public int structureObjectCount() {
int count=0;
for(TextureAtlas.AtlasRegion region:atlas ().getRegions())
{
if(region.name.startsWith("structure"))
{
count++;
}
}
return count;
return data.mappingInfo.length;
}
public int objectID(int x, int y) {
if(!init)
{
init=true;
initialize();
}
if(x>biomeWidth*data.width)
return -1;
if(y>biomeHeight*data.height)
return -1;
if(x<biomeWidth*data.x)
return -1;
if(y<biomeHeight*data.y)
if(x>=dataMap.length||x<0||y<0||y>=dataMap[0].length)
return -1;
return dataMap[x][y];
}
private void initialize() {
public void initialize() {
init=true;
OverlappingModel model= new OverlappingModel(sourceImage(),data.N, (int) (data.width* biomeWidth), (int) (data.height*biomeHeight),data.periodicInput,data.periodicOutput,data.symmetry,data.ground);
HashMap<Integer,Integer> colorIdMap=new HashMap<>();
int counter=0;
for(TextureAtlas.AtlasRegion region:atlas ().getRegions())
for(int i=0;i<data.mappingInfo.length;i++)
{
if(region.name.startsWith("structure"))
{
String[] split= region.name.split("_");
if(split.length<2)
continue;
int rgb=Integer.parseInt(split[1],16);
colorIdMap.put(rgb,counter);
counter++;
}
colorIdMap.put(Integer.parseInt(data.mappingInfo[i].color,16),i);
}
BufferedImage image=model.graphics();
boolean suc=model.run((int) seed,0);
if(!suc)
{
dataMap=new int[(int) (data.width* biomeWidth)][ (int) (data.height*biomeHeight)];
return;
}
image=model.graphics();
dataMap=new int[image.getWidth()][image.getHeight()];
for(int x=0;x<image.getWidth();x++)
{
for(int y=0;y<image.getHeight();y++)
{
int rgb=image.getRGB(x,y);
int rgb=image.getRGB(x,y) & 0xffffff;
if(!colorIdMap.containsKey(rgb))
{
dataMap[x][y]=-1;
@@ -101,14 +86,24 @@ public class BiomeStructure {
}
private BufferedImage sourceImage() {
TextureAtlas.AtlasRegion region=atlas().findRegion("Source");
if(region==null)
return null;
try {
return ImageIO.read(new File(Config.instance().getFilePath(data.structureAtlasPath))).getSubimage((int) region.offsetX, (int) region.offsetY,region.originalWidth,region.originalHeight);
return ImageIO.read(new File(Config.instance().getFilePath(data.sourcePath)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public int x() {
return (int) ((data.x*biomeWidth)-(data.width*biomeWidth)/2);
}
public int y() {
return (int) ((data.y*biomeHeight)-(data.height*biomeHeight)/2);
}
public BiomeStructureData.BiomeStructureDataMapping[] mapping() {
return data.mappingInfo;
}
}

View File

@@ -95,13 +95,12 @@ public class BiomeTexture implements Serializable {
for(BiomeStructureData structureData:data.structures)
{
BiomeStructure structure=new BiomeStructure(structureData,0,0,0);
for(TextureAtlas.AtlasRegion region:structure.atlas ().getRegions())
TextureAtlas atlas=structure.atlas ();
for(BiomeStructureData.BiomeStructureDataMapping mapping:structure.mapping())
{
if(region.name.startsWith("structure"))
{
regions.add(region);
source.add(structure.atlas());
}
regions.add(atlas.findRegion(mapping.name));
source.add(atlas);
}
}
}

View File

@@ -327,9 +327,15 @@ public class World implements Disposable, SaveFileContent {
structureDataMap.put(data,new BiomeStructure(data,seed,biomeWidth,biomeHeight));
}
structure=structureDataMap.get(data);
int structureIndex=structure.objectID(x-biomeXStart,y-biomeYStart);
int structureXStart= structure.x()+beginX;
int structureYStart= structure.y()+beginY;
int structureIndex=structure.objectID(x-structureXStart,y-structureYStart);
if(structureIndex>=0)
{
pix.setColor(data.mappingInfo[structureIndex].getColor());
pix.drawPixel(x, y);
terrainMap[x][y]=terrainCounter+structureIndex;
}
terrainCounter+=structure.structureObjectCount();
}
@@ -395,6 +401,9 @@ public class World implements Disposable, SaveFileContent {
foundSolution=true;
x=x+xi*data.tileSize;
y=y+yi*data.tileSize;
}
}
}

View File

@@ -67,6 +67,12 @@
<version>5.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.jetopto1.cling</groupId>
<artifactId>cling-core</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -21,10 +21,18 @@
],
"structures":[
{
"structureAtlasPath":"world/tilesets/forest.atlas",
"sourcePath" : "world/tilesets/forestSource.png",
"structureAtlasPath":"world/tilesets/structures.atlas",
"mappingInfo":[
{
"name":"Forest",
"color":"007000"
}
],
"x": 0.5,
"y": 0.5,
"size": 0.3
"width": 0.3 ,
"height": 0.3
}
],
"width": 0.7,

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@@ -1,19 +1,12 @@
forest.png
structures.png
size: 96,64
format: RGBA8888
filter: Nearest,Nearest
repeat: none
Source
Forest
rotate: false
xy: 0, 0
size: 16, 16
orig: 0, 0
offset: 0, 0
index: 0
structure_000000
rotate: false
xy: 48, 0
size: 48, 64
orig: 0, 0
offset: 0, 0

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB