-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tilemapmgr.java
151 lines (147 loc) · 6 KB
/
Tilemapmgr.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import java.io.*;
import java.util.HashMap;
import javax.imageio.ImageIO;
public class Tilemapmgr {
static HashMap<String,Tilemap> tilemaps = new HashMap<>();
public static final char[] tilechars = {' ','.','~','-','|','/'};
static boolean active = false;
public static void loadFromImage(String name, String imagePath){
try{
// Load the image
java.awt.image.BufferedImage image = ImageIO.read(new File(imagePath));
int width = image.getWidth();
int height = image.getHeight();
int[][] rg = new int[height][width];
int[][] b = new int[height][width];
// Calculate pixel brightness
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pixel = image.getRGB(x, y);
// Extract RGB components
int red = (pixel >> 16) & 0xFF;
int green = (pixel >> 8) & 0xFF;
int blue = pixel & 0xFF;
// Sum RGB values
int brightness = red + green;
rg[y][x] = brightness; // Store in the array
b[y][x] = blue; // Store in the array
}
}
// Create tilemap
Tilemap map = new Tilemap(rg,b);
tilemaps.put(name, map);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void loadMap(String name, String path){
try(DataInputStream dis = new DataInputStream(new FileInputStream(path))){
int width = dis.readInt();
int height = dis.readInt();
int[][] tileids = new int[width][height];
int[][] tiledata = new int[width][height];
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
tileids[x][y] = dis.readInt();
tiledata[x][y] = dis.readInt();
}
if(dis.readInt()!=0){
throw new IOException("Failed to load map: "+name+" (Invalid file format or alignment)");
}
}
if(dis.available()!=0){
throw new IOException("Failed to load map: "+name+" (Invalid file format or alignment)");
}
Tilemap map = new Tilemap(tileids, tiledata);
tilemaps.put(name, map);
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
public static void loadMap(String name, Tilemap map){
tilemaps.put(name, map);
}
public static void saveMap(String name, String path){
try(DataOutputStream dos = new DataOutputStream(new FileOutputStream(path))){
Tilemap map = tilemaps.get(name);
dos.writeInt(map.tileids.length);
dos.writeInt(map.tileids[0].length);
for(int x = 0; x < map.tileids.length; x++){
for(int y = 0; y < map.tileids[0].length; y++){
dos.writeInt(map.tileids[x][y]);
dos.writeInt(map.tiledata[x][y]);
}
}
dos.writeInt(0);
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
public static void makeMap(){
Reader in = new Reader();
java.io.PrintWriter out = new java.io.PrintWriter(System.out);
out.println("Enter map name:");
out.flush();
String name = "";
try{name = in.readLine();}catch(IOException e){e.printStackTrace();}
out.println("Enter map width:");
out.flush();
int width = 0;
try{width = in.nextInt();}catch(IOException e){e.printStackTrace();}
out.println("Enter map height:");
out.flush();
int height = 0;
try{height = in.nextInt();}catch(IOException e){e.printStackTrace();}
if(name.equals("empty")){
out.println("Enter tile id and data for empty tile:");
out.flush();
try{tilemaps.put(name, new Tilemap(width, height, in.nextInt(), in.nextInt()));}catch(IOException e){e.printStackTrace();}
return;
}
int[][] tileids = new int[width][height];
int[][] tiledata = new int[width][height];
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
out.println("Enter tile id and data for tile at "+x+","+y+":");
out.flush();
try{tileids[x][y] = in.nextInt();}catch(IOException e){e.printStackTrace();}
try{tiledata[x][y] = in.nextInt();}catch(IOException e){e.printStackTrace();}
}
}
tilemaps.put(name, new Tilemap(tileids, tiledata));
}
public static RenderedMap draw(String name){
char[][] ret = new char[tilemaps.get(name).tileids.length][tilemaps.get(name).tileids[0].length];
Tilemap map = tilemaps.get(name);
for(int x=0;x<map.tileids.length;x++){
for(int y=0;y<map.tileids[0].length;y++){
ret[x][y] = tilechars[map.tileids[x][y]];
}
}
return new RenderedMap(ret);
}
public static RenderedMap draw(String name, int x, int y, int width, int height){
char[][] ret = new char[height][width];
int mapwidth = tilemaps.get(name).tileids.length, mapheight = tilemaps.get(name).tileids[0].length;
Tilemap map = tilemaps.get(name);
for(int ox=0;ox<width;ox++){
for(int oy=0;oy<height;oy++){
ret[oy][ox] = y+oy<mapheight && x+ox<mapwidth && x+ox>=0 && y+oy>=0? tilechars[map.tileids[y+oy][x+ox]] : ' ';
}
}
return new RenderedMap(ret);
}
public static Tilemap getTilemap(String name){
return tilemaps.get(name);
}
public static void activate(){
active = true;
}
public static void deactivate(){
active = false;
}
}