re-implemented moving of tiles

This commit is contained in:
Stephan Richter
2020-12-04 14:14:32 +01:00
parent 33d596f5c3
commit 9bfe0d56dd
12 changed files with 109 additions and 101 deletions

View File

@@ -36,7 +36,7 @@ public class BlockH extends Block{
@Override
public int width() {
return stretch;
return stretch();
}
@Override

View File

@@ -36,7 +36,7 @@ public class BlockV extends Block{
@Override
public int height() {
return stretch;
return stretch();
}
@Override

View File

@@ -20,11 +20,11 @@ public class Shadow extends Tile{
return super.connections(from);
}
public Shadow(Tile overlay, int x, int y) {
public Shadow(StretchableTile overlay, int x, int y) {
this.overlay = overlay;
this.x = x;
this.y = y;
overlay.addShadow(this);
overlay.add(this);
}
public Tile overlay() {

View File

@@ -35,6 +35,6 @@ public class StraightH extends StretchableTile{
@Override
public int width() {
return stretch;
return stretch();
}
}

View File

@@ -25,7 +25,7 @@ public class StraightV extends StretchableTile{
@Override
public int height() {
return stretch;
return stretch();
}
@Override

View File

@@ -3,17 +3,24 @@ package de.srsoftware.web4rail.tiles;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Vector;
import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.tags.Fieldset;
import de.srsoftware.web4rail.tags.Input;
public abstract class StretchableTile extends Tile {
private static final String STRETCH_LENGTH = "stretch";
public int stretch = 1;
private int stretch = 1;
private Vector<Id> shadows = new Vector<Id>();
public void add(Shadow shadow) {
shadows.add(shadow.id());
}
@Override
public JSONObject config() {
@@ -35,12 +42,57 @@ public abstract class StretchableTile extends Tile {
return super.load(json);
}
@Override
public boolean move(int dx, int dy) {
for (int destX=1; destX<width(); destX++) {
int destY = dy > 0 ? height() : dy;
Tile tileAtDest = plan.get(Tile.id(x+destX, y+destY), true);
if (isNull(tileAtDest) || tileAtDest == this) continue;
if (!tileAtDest.move(dx, dy)) return false;
}
for (int destY=1; destY<height(); destY++) {
int destX = dx > 0 ? width() : dx;
Tile tileAtDest = plan.get(Tile.id(x+destX, y+destY), true);
if (isNull(tileAtDest) || tileAtDest == this) continue;
if (!tileAtDest.move(dx, dy)) return false;
}
boolean moved = super.move(dx, dy);
if (moved) placeShadows();
return moved;
}
public void placeShadows() {
removeShadows();
for (int dx=1; dx<width(); dx++) plan.place(new Shadow(this, x+dx, y));
for (int dy=1; dy<height(); dy++) plan.place(new Shadow(this, x, y+dy));
}
@Override
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
formInputs.add(stretchType(),new Input(STRETCH_LENGTH, stretch).numeric().addTo(new Tag("span")).content(NBSP+t("Tile(s)")));
return super.properties(preForm, formInputs, postForm);
}
@Override
public BaseClass remove() {
super.remove();
removeShadows();
return this;
}
private void removeShadows() {
while (!shadows.isEmpty()) {
Tile tile = BaseClass.get(shadows.remove(0));
if (tile instanceof Shadow) tile.remove();
}
}
public int stretch() {
return stretch;
}
private void stretch(String value) {
try {
stretch(Integer.parseInt(value));
@@ -49,10 +101,14 @@ public abstract class StretchableTile extends Tile {
}
}
public void stretch(int len) {
this.stretch = Math.max(1, len);
public void stretch(int newStretch) {
newStretch = Math.max(1, newStretch);
if (newStretch != stretch) {
stretch = newStretch;
placeShadows();
}
}
protected abstract String stretchType();
@Override

View File

@@ -81,6 +81,6 @@ public class TextDisplay extends StretchableTile {
@Override
public int width() {
return stretch;
return stretch();
}
}

View File

@@ -56,7 +56,6 @@ public abstract class Tile extends BaseClass implements Comparable<Tile>{
protected Direction oneWay = null;
protected Route route = null;
private TreeSet<Route> routes = new TreeSet<>();
protected TreeSet<Shadow> shadows = new TreeSet<>();
protected Train train = null;
public Integer x = null;
public Integer y = null;
@@ -65,10 +64,6 @@ public abstract class Tile extends BaseClass implements Comparable<Tile>{
this.routes.add(route);
}
public void addShadow(Shadow shadow) {
shadows.add(shadow);
}
protected Vector<String> classes(){
Vector<String> classes = new Vector<String>();
classes.add("tile");
@@ -113,8 +108,9 @@ public abstract class Tile extends BaseClass implements Comparable<Tile>{
private static void inflate(String clazz, JSONObject json, Plan plan) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, IOException {
clazz = Tile.class.getName().replace(".Tile", "."+clazz);
Tile tile = (Tile) Tile.class.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance();
tile.load(json).parent(plan);
plan.set(tile.x, tile.y, tile);
tile.load(json).register();
if (tile instanceof StretchableTile) ((StretchableTile)tile).placeShadows();
plan.place(tile);
}
public boolean isFreeFor(Train newTrain) {
@@ -382,14 +378,12 @@ public abstract class Tile extends BaseClass implements Comparable<Tile>{
public BaseClass remove() {
super.remove();
while (!routes.isEmpty()) routes.first().remove();
while (!shadows.isEmpty()) shadows.first().remove();
return this;
}
@Override
public void removeChild(BaseClass child) {
routes.remove(child);
if (child instanceof Shadow) shadows.remove(child);
if (child == train) train = null;
if (child == route) route = null;
plan.place(this);
@@ -421,4 +415,17 @@ public abstract class Tile extends BaseClass implements Comparable<Tile>{
public int width() {
return 1;
}
public boolean move(int dx, int dy) {
int destX = x+(dx > 0 ? width() : dx);
int destY = y+(dy > 0 ? height() : dy);
if (destX < 0 || destY < 0) return false;
Tile tileAtDestination = plan.get(id(destX, destY),true);
if (isSet(tileAtDestination) && !tileAtDestination.move(dx, dy)) return false;
plan.drop(this);
position(x+dx, y+dy);
plan.place(this);
return true;
}
}