Skip to content

Commit 831bad5

Browse files
committed
made clean into its own method and created a method for shifting tiles
1 parent 8db572d commit 831bad5

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/main/java/net/ddns/endercypt/cs2dmap/library/map/sub/extra/map/MapArray.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.ddns.endercypt.cs2dmap.library.map.sub.extra.map;
22

33
import java.awt.Dimension;
4+
import java.util.function.Supplier;
45

56
import net.ddns.endercypt.cs2dmap.library.map.obj.tile.Tile;
67
import net.ddns.endercypt.cs2dmap.library.map.sub.extra.tile.action.TileAction;
@@ -59,7 +60,7 @@ public void resizeMap(int new_width, int new_height)
5960
newMap[x][y] = map[x][y];
6061
}
6162
}
62-
// fill null's
63+
// clean
6364
for (int x = 0; x < new_width; x++)
6465
{
6566
for (int y = 0; y < new_height; y++)
@@ -70,12 +71,67 @@ public void resizeMap(int new_width, int new_height)
7071
}
7172
}
7273
}
74+
// clean
75+
clean(newMap, () -> new Tile(new RawCs2dTile((byte) 0)));
7376
// set
7477
width = new_width;
7578
height = new_height;
7679
map = newMap;
7780
}
7881

82+
private static void clean(Tile[][] map, Supplier<Tile> replacementSupplier)
83+
{
84+
for (int x = 0; x < map.length; x++)
85+
{
86+
Tile[] inMap = map[x];
87+
for (int y = 0; y < inMap.length; y++)
88+
{
89+
Tile tile = inMap[y];
90+
if (tile == null)
91+
{
92+
inMap[y] = replacementSupplier.get();
93+
}
94+
}
95+
}
96+
}
97+
98+
public void shift(int x, int y, TileAction defaultAction)
99+
{
100+
Tile[][] newMap = new Tile[getWidth()][getHeight()];
101+
//
102+
int x1 = -Math.min(0, x);
103+
int y1 = -Math.min(0, y);
104+
int x2 = getWidth() - Math.max(x, 0) - 1;
105+
int y2 = getHeight() - Math.max(y, 0) - 1;
106+
// move
107+
iterate(x1, y1, x2, y2, new MapIterator()
108+
{
109+
@Override
110+
public void process(int xi, int yi, Tile tile)
111+
{
112+
int tx = xi + x;
113+
int ty = yi + y;
114+
if (isInBounds(tx, ty))
115+
{
116+
newMap[xi + x][yi + y] = tile;
117+
}
118+
}
119+
});
120+
// clean
121+
clean(newMap, new Supplier<Tile>()
122+
{
123+
@Override
124+
public Tile get()
125+
{
126+
Tile tile = new Tile(0);
127+
defaultAction.process(tile);
128+
return tile;
129+
}
130+
});
131+
// set
132+
map = newMap;
133+
}
134+
79135
public boolean isInBounds(int x1, int y1, int x2, int y2)
80136
{
81137
return (isInBounds(x1, y1) && isInBounds(x2, y2));

src/main/java/test/CreateMap.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
7+
import java.nio.file.StandardCopyOption;
58

69
import net.ddns.endercypt.cs2dmap.library.file.stream.exception.Cs2dUnexpectedEOFException;
710
import net.ddns.endercypt.cs2dmap.library.file.write.Cs2dMapWriter;
@@ -51,6 +54,8 @@ public static void main(String[] args) throws IOException
5154
MapSection copy = map.copy(0, 0, 5, 5);
5255
copy.pasteInto(map, -1, -1);
5356

57+
map.shift(100, 100, TileActions.setFrame(39));
58+
5459
cs2dMap.update(map);
5560

5661
// entities
@@ -79,6 +84,9 @@ public static void main(String[] args) throws IOException
7984

8085
// save
8186
Cs2dMapWriter.write(cs2dMap, new File("maps/test.map"));
87+
88+
// copy
89+
Files.copy(Paths.get("maps/test.map"), Paths.get("/Users/EnderCrypt/Library/Application Support/Steam/steamapps/common/CS2D/maps/test.map"), StandardCopyOption.REPLACE_EXISTING);
8290
}
8391
catch (Cs2dUnexpectedEOFException e)
8492
{

0 commit comments

Comments
 (0)