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 import java.util.HashMap;
12 import java.util.Map;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 public class RoomGameLevelManager extends LevelManager {
34 private Map cacheMap = new HashMap();
35 protected int getDefaultSizeX() {
36 return 10;
37 }
38
39 protected int getDefaultSizeY() {
40 return 10;
41 }
42
43 protected int getDefaultSizeZ() {
44 return 10;
45 }
46
47 protected String getGroupName() {
48 return "rooms";
49 }
50
51 protected String getObjectName() {
52 return "room";
53 }
54
55 protected String getPartName() {
56 return "object";
57 }
58
59 public Image getImage() {
60
61 return null;
62 }
63
64 public LevelInfoInterface createLevel(int level) {
65
66 return null;
67 }
68 public LevelInfoInterface getLevel(int level) {
69 LevelInfo levelInfo = getFromCache(level);
70 if(levelInfo!=null) {
71 return cloneLevel(levelInfo);
72 }
73 StorageInterface roomStorage = LevelManagerHelper.load(getEnvironment().getStorage(),getGroupName(),getObjectName(),""+level);
74 if (roomStorage != null) {
75 RoomExtendedLevelInfo info = new RoomExtendedLevelInfo();
76 info.setSizeX(getDefaultSizeX());
77 info.setSizeY(getDefaultSizeY());
78 info.setSizeZ(getDefaultSizeZ());
79 MapObjectContainerInterface map = new MapBlockContainer(info.getSizeX(), info.getSizeY(), info.getSizeZ());
80 String prefix = ((TileGameEnvironmentInterface) getEnvironment().getCustomEnvironment()).getTileType();
81 ParameterValueStorageExInterface roomParameters = new ParameterStorageGroupWithId(roomStorage, null, getObjectName(), getPartName());
82 info.read(roomParameters);
83 map = new MapBlockContainer(info.getSizeX(), info.getSizeY(), info.getSizeZ());
84
85 LevelManager objectLevelManager = new MapObjectGameLevelManager();
86 objectLevelManager.init(getEnvironment());
87
88 boolean bEnd = false;
89 int i = 1;
90 while (!bEnd) {
91 StorageInterface objectStorage = roomParameters.getParameterAsStorage(getPartName() + "." + (i++));
92 if (objectStorage != null) {
93 ParameterValueStorageExInterface objectParameters = new ParameterStorageString(objectStorage, null, getPartName());
94 MapEditorObject obj = new MapEditorObject();
95 obj.init(getEnvironment());
96 obj.setContainer(getContainer());
97 obj.read(objectParameters);
98
99 LevelInfoInterface objectInfo = objectLevelManager.getLevel(obj.getBlockType());
100 MapObjectContainerInterface objectMap = objectInfo.getObjects();
101 GameObject o = (GameObject) objectMap.getBlock(0,0,0);
102 o.setContainer(getContainer());
103 o.setObjectMap(map);
104 o.setPos(obj.getPosX(),obj.getPosY(),obj.getPosZ());
105 map.setBlock(o,(int)o.getPosX(),(int)o.getPosY(),(int)o.getPosZ());
106 } else {
107 bEnd = true;
108 }
109 }
110 levelInfo = new LevelInfo(map, info);
111 setInCache(level,levelInfo);
112 return levelInfo;
113 }
114 return null;
115 }
116
117 private LevelInfo getFromCache(int level) {
118 return (LevelInfo) cacheMap.get(new Integer(level));
119 }
120 private void setInCache(int level, LevelInfo data) {
121 cacheMap.put(new Integer(level),data);
122 }
123
124 private LevelInfo cloneLevel(LevelInfo level) {
125 MapObjectContainerInterface newBlock = cloneObjects(level.getObjects());
126 ParameterSerializable newLevelInfo = null;
127 ParameterValueStorageExInterface storage = new ParameterStorageString(new StringStorage(),null);
128 if(level.getExtendedInfo()!=null) {
129 level.getExtendedInfo().read(storage);
130 newLevelInfo = new RoomExtendedLevelInfo();
131 newLevelInfo.write(storage);
132 }
133 return new LevelInfo(newBlock,newLevelInfo);
134 }
135 private MapObjectContainerInterface cloneObjects(MapObjectContainerInterface objects) {
136 MapObjectContainerInterface newMap = new MapBlockContainer(objects.getSizeX(),objects.getSizeY(),objects.getSizeZ());
137 for (int x = 0; x < newMap.getSizeX(); x++) {
138 for (int y = 0; y < newMap.getSizeY(); y++) {
139 for (int z = 0; z < newMap.getSizeZ(); z++) {
140 MapObjectInterface o = objects.getBlock(x,y,z);
141 if(o!=null) {
142 GameObject newObj = (GameObject) o.clone();
143 newObj.setObjectMap(newMap);
144 newMap.setBlock(newObj,x,y,z);
145 }
146 }
147 }
148 }
149 return newMap;
150 }
151 public void setLevel(int level, MapObjectContainerInterface blocks, ParameterSerializable extendedInfo) {
152
153 StringStorage roomStorage = new StringStorage();
154 ParameterValueStorageExInterface roomParameters = new ParameterStorageGroupWithId(roomStorage, null, null, getPartName());
155 if (extendedInfo != null) {
156 extendedInfo.write(roomParameters);
157 }
158 int i = 1;
159 for (int x = 0; x < blocks.getSizeX(); x++) {
160 for (int y = 0; y < blocks.getSizeY(); y++) {
161 for (int z = 0; z < blocks.getSizeZ(); z++) {
162 MapEditorObject o = (MapEditorObject) blocks.getBlock(x, y, z);
163 if (o != null) {
164 StringStorage objectStorage = new StringStorage();
165 ParameterValueStorageExInterface objectParameters = new ParameterStorageStringEx(objectStorage, null, null);
166 o.write(objectParameters);
167 roomParameters.setParameter(getPartName() + "." + (i++), objectStorage.load());
168 }
169 }
170 }
171 }
172 LevelManagerHelper.store(getEnvironment().getStorage(),getGroupName(), getObjectName(), "" + level, roomStorage.load());
173 }
174 }