View Javadoc

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   * Copyright (C) 2004 Erland Isaksson (erland_i@hotmail.com)
14   *
15   * This program is free software; you can redistribute it and/or
16   * modify it under the terms of the GNU General Public License
17   * as published by the Free Software Foundation; either version 2
18   * of the License, or (at your option) any later version.
19   *
20   * This program is distributed in the hope that it will be useful,
21   * but WITHOUT ANY WARRANTY; without even the implied warranty of
22   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   * GNU General Public License for more details.
24   *
25   * You should have received a copy of the GNU General Public License
26   * along with this program; if not, write to the Free Software
27   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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 }