1 package erland.game.tileadventure;
2
3 import erland.util.*;
4 import erland.game.tileadventure.rect.RectBlockContainerData;
5
6 import javax.imageio.ImageIO;
7 import java.awt.*;
8 import java.awt.image.BufferedImage;
9 import java.io.File;
10 import java.io.IOException;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class RoomEditorLevelManager extends LevelManager {
32 private Image paletteImage;
33 protected int getDefaultSizeX() {
34 return 10;
35 }
36
37 protected int getDefaultSizeY() {
38 return 10;
39 }
40
41 protected int getDefaultSizeZ() {
42 return 10;
43 }
44
45 protected String getGroupName() {
46 return "rooms";
47 }
48
49 protected String getObjectName() {
50 return "room";
51 }
52
53 protected String getPartName() {
54 return "object";
55 }
56
57 public Image getImage() {
58 TileGameEnvironmentInterface env = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment());
59 SubImageHandler subImageHandler = env.createSubImageHandler(null);
60 boolean bEnd = false;
61 int i=0;
62 DrawMap drawMap = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment()).createBlockMap();
63 while(!bEnd) {
64 LevelInfoInterface level = getLevel(++i);
65 if(level!=null) {
66 RoomExtendedLevelInfo extendedInfo = (RoomExtendedLevelInfo) level.getExtendedInfo();
67 IrregularBlockContainerInterface cont = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment()).createBlockContainer(0,0,extendedInfo.getSizeX(),extendedInfo.getSizeY(),extendedInfo.getSizeZ());
68 Image image = getEnvironment().getImageCreator().createCompatibleImage(cont.getDrawingSizeX(),cont.getDrawingSizeY(),Transparency.BITMASK);
69 MapObjectContainerInterface map = level.getObjects();
70 for (int x = 0; x < map.getSizeX(); x++) {
71 for (int y = 0; y < map.getSizeY(); y++) {
72 for (int z = 0; z < map.getSizeZ(); z++) {
73 MapObjectInterface o = map.getBlock(x,y,z);
74 if(o!=null) {
75 o.setContainer(cont);
76 }
77 }
78 }
79 }
80 drawMap.setContainer(cont);
81 drawMap.removeAllObjectMaps();
82 drawMap.addObjectMap(map);
83 drawMap.draw(image.getGraphics());
84 Graphics g = subImageHandler.getGraphics(i-1);
85 int height = (env.getSubImageWidth()*image.getHeight(null))/image.getWidth(null);
86 int offsetY = env.getSubImageHeight()-height;
87 g.drawImage(image,0,offsetY,env.getSubImageWidth(),offsetY+height,0,0,image.getWidth(null),image.getHeight(null),null);
88 g.dispose();
89 }else {
90 bEnd = true;
91 }
92 }
93 return subImageHandler.getImage();
94 }
95
96 public LevelInfoInterface createLevel(int level) {
97 LevelInfoInterface levelInfo = getLevel(level);
98 if(levelInfo==null) {
99 RoomExtendedLevelInfo info = new RoomExtendedLevelInfo();
100 info.setSizeX(getDefaultSizeX());
101 info.setSizeY(getDefaultSizeY());
102 info.setSizeZ(getDefaultSizeZ());
103 MapObjectContainerInterface map = new MapBlockContainer(info.getSizeX(), info.getSizeY(), info.getSizeZ());
104 levelInfo = new LevelInfo(map, info);
105 }
106 return levelInfo;
107 }
108 public LevelInfoInterface getLevel(int level) {
109 StorageInterface roomStorage = LevelManagerHelper.load(getEnvironment().getStorage(),getGroupName(),getObjectName(),""+level);
110 if (roomStorage != null) {
111 RoomExtendedLevelInfo info = new RoomExtendedLevelInfo();
112 info.setSizeX(getDefaultSizeX());
113 info.setSizeY(getDefaultSizeY());
114 info.setSizeZ(getDefaultSizeZ());
115 MapObjectContainerInterface map = new MapBlockContainer(info.getSizeX(), info.getSizeY(), info.getSizeZ());
116 String prefix = ((TileGameEnvironmentInterface) getEnvironment().getCustomEnvironment()).getTileType();
117 ParameterValueStorageExInterface roomParameters = new ParameterStorageGroupWithId(roomStorage, null, getObjectName(), getPartName());
118 info.read(roomParameters);
119 map = new MapBlockContainer(info.getSizeX(), info.getSizeY(), info.getSizeZ());
120 boolean bEnd = false;
121 int i = 1;
122 while (!bEnd) {
123 StorageInterface objectStorage = roomParameters.getParameterAsStorage(getPartName() + "." + (i++));
124 if (objectStorage != null) {
125 ParameterValueStorageExInterface objectParameters = new ParameterStorageString(objectStorage, null, getPartName());
126 MapEditorObject obj = new MapEditorObject();
127 obj.init(getEnvironment());
128 obj.setContainer(getContainer());
129 obj.read(objectParameters);
130 obj.setImage(getPaletteImage(), obj.getBlockType()-1);
131 map.setBlock(obj, (int)obj.getPosX(), (int)obj.getPosY(), (int)obj.getPosZ());
132 } else {
133 bEnd = true;
134 }
135 }
136 return new LevelInfo(map, info);
137 }
138 return null;
139 }
140
141 public void setLevel(int level, MapObjectContainerInterface blocks, ParameterSerializable extendedInfo) {
142
143 StringStorage roomStorage = new StringStorage();
144 ParameterValueStorageExInterface roomParameters = new ParameterStorageGroupWithId(roomStorage, null, null, getPartName());
145 if (extendedInfo != null) {
146 extendedInfo.write(roomParameters);
147 }
148 int i = 1;
149 for (int x = 0; x < blocks.getSizeX(); x++) {
150 for (int y = 0; y < blocks.getSizeY(); y++) {
151 for (int z = 0; z < blocks.getSizeZ(); z++) {
152 MapEditorObject o = (MapEditorObject) blocks.getBlock(x, y, z);
153 if (o != null) {
154 StringStorage objectStorage = new StringStorage();
155 ParameterValueStorageExInterface objectParameters = new ParameterStorageStringEx(objectStorage, null, null);
156 o.write(objectParameters);
157 roomParameters.setParameter(getPartName() + "." + (i++), objectStorage.load());
158 }
159 }
160 }
161 }
162 LevelManagerHelper.store(getEnvironment().getStorage(),getGroupName(), getObjectName(), "" + level, roomStorage.load());
163 }
164 private Image getPaletteImage() {
165 if(paletteImage==null) {
166 LevelManager levelManager = new MapObjectEditorLevelManager();
167 levelManager.init(getEnvironment());
168 paletteImage = levelManager.getImage();
169 }
170 return paletteImage;
171 }
172 }