generate .we maze

- export maze as .we ( still hlf boundaries missing )
This commit is contained in:
philippe lhardy
2020-11-03 17:49:58 +01:00
parent 8211147b28
commit 41067707bc
2 changed files with 15 additions and 11 deletions

View File

@@ -0,0 +1,114 @@
package org.artisanlogiciel.games.maze.persist;
import org.artisanlogiciel.games.maze.Brick;
import org.artisanlogiciel.games.maze.LabyModel;
import org.artisanlogiciel.games.minetest.Node;
import org.artisanlogiciel.games.minetest.WorlEditGenerator;
import java.io.*;
public class MazePersistWorldEdit {
private LabyModel model;
private WorlEditGenerator generator;
private static final String GRASS_MATERIAL = "default:dirt_with_grass";
public MazePersistWorldEdit(LabyModel model) {
this.model = model;
}
void addNode(Node node)
{
if (node != null )
{
generator.writeNode(node);
}
}
void addWalls(int pX, int pY) {
short walls = model.getWalls(pX, pY);
short wdrawn = 0;
// todo
int w = 2;
int h = 2;
int ox = 0;
int oy = 0;
int z = 0;
String material = GRASS_MATERIAL;
int x = ox + (int) (pX * w);
int y = oy + (int) (pY * h);
// copied from drawing, where order did matter, might not be the case here...
//if ((pY == 0) && LabyModel.isFlagSet(walls, Brick.UP)) {
if (LabyModel.isFlagSet(walls, Brick.UP)) {
for ( int dx = 0; dx < w; dx++) {
addNode(new Node(x + dx, y, z, material));
}
wdrawn |= Brick.UP;
}
/*
if (LabyModel.isFlagSet(walls, Brick.RIGHT)) {
if (!LabyModel.isFlagSet(wdrawn, Brick.UP)) {
addNode(new Node(x + (int) w, y, 0, material));
}
addNode(new Node(x + (int) w, y + (int) h, z, material));
wdrawn |= Brick.RIGHT;
}
if (LabyModel.isFlagSet(walls, Brick.DOWN)) {
if (!LabyModel.isFlagSet(wdrawn, Brick.RIGHT)) {
addNode(new Node(x + (int) w, y + (int) h, z, material));
}
addNode(new Node(x, y + (int) h, z, material));
wdrawn |= Brick.DOWN;
}
*/
//if ((pX == 0) && LabyModel.isFlagSet(walls, Brick.LEFT)) {
if (LabyModel.isFlagSet(walls, Brick.LEFT)) {
for (int dy = 0; dy < h; dy ++) {
addNode(new Node(x, y + dy, z, material));
}
wdrawn |= Brick.LEFT;
}
}
public void streamOut(String pFormat, OutputStream pOut) throws IOException {
// WIP using WorldEditGenerator
if ((pFormat == null) || (pFormat.equals("we"))) {
Node refNode = new Node(0,0,0,"default:dirt_with_grass");
StringBuilder builder = new StringBuilder();
generator = new WorlEditGenerator(builder,refNode);
generator.writeStart();
// first raw format, not smart.
DataOutputStream dataOut = new DataOutputStream(pOut);
dataOut.flush();
for (int y = 0; y < model.getHeight(); y++) {
for (int x = 0; x < model.getWidth(); x++) {
addWalls(x,y);
}
}
generator.writeEnd();
dataOut.write(builder.toString().getBytes());
dataOut.flush();
} else {
throw new IOException("Format " + pFormat + " Not yet implemented.");
}
}
public LabyModel parseInputStream(String pFormat, InputStream pIn) throws IOException {
// TODO using WorldEditGenerator
if ((pFormat == null) || (pFormat.equals("we"))) {
// DataInputStream in = new DataInputStream(pIn);
throw new IOException("Format " + pFormat + " Not yet implemented.");
// should be at end of stream ? Not necessary can stream multiple
// labs ( or tiling ).
} else {
throw new IOException("Format " + pFormat + " Not yet implemented.");
}
}
}