View Javadoc

1   package erland.game.tileadventure;
2   
3   import erland.util.*;
4   
5   import java.awt.*;
6   
7   /*
8    * Copyright (C) 2004 Erland Isaksson (erland_i@hotmail.com)
9    *
10   * This program is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU General Public License
12   * as published by the Free Software Foundation; either version 2
13   * of the License, or (at your option) any later version.
14   *
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   *
20   * You should have received a copy of the GNU General Public License
21   * along with this program; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23   * 
24   */
25  
26  public class WorldEditorLevelManager extends LevelManager {
27      private Image paletteImage;
28      protected String getGroupName() {
29          return "worlds";
30      }
31      protected String getObjectName() {
32          return "world";
33      }
34      protected String getPartName() {
35          return "room";
36      }
37      protected int getDefaultSizeX() {
38          return 10;
39      }
40      protected int getDefaultSizeY() {
41          return 10;
42      }
43      protected int getDefaultSizeZ() {
44          return 1;
45      }
46      public Image getImage() {
47          // No image can be created with this level manager
48          return null;
49      }
50      public LevelInfoInterface createLevel(int level) {
51          LevelInfoInterface levelInfo = getLevel(level);
52          if(levelInfo==null) {
53              RoomExtendedLevelInfo info = new RoomExtendedLevelInfo();
54              info.setSizeX(getDefaultSizeX());
55              info.setSizeY(getDefaultSizeY());
56              info.setSizeZ(getDefaultSizeZ());
57              MapObjectContainerInterface map = new MapBlockContainer(info.getSizeX(), info.getSizeY(), info.getSizeZ());
58              levelInfo = new LevelInfo(map, info);
59          }
60          return levelInfo;
61      }
62  
63      public LevelInfoInterface getLevel(int level) {
64          StorageInterface roomStorage = LevelManagerHelper.load(getEnvironment().getStorage(),getGroupName(),getObjectName(),""+level);
65          if (roomStorage != null) {
66              WorldExtendedLevelInfo info = new WorldExtendedLevelInfo();
67              info.setSizeX(getDefaultSizeX());
68              info.setSizeY(getDefaultSizeY());
69              info.setSizeZ(getDefaultSizeZ());
70              MapObjectContainerInterface map = new MapBlockContainer(info.getSizeX(), info.getSizeY(), info.getSizeZ());
71              String prefix = ((TileGameEnvironmentInterface) getEnvironment().getCustomEnvironment()).getTileType();
72              ParameterValueStorageExInterface roomParameters = new ParameterStorageGroupWithId(roomStorage, null, getObjectName(), getPartName());
73              info.read(roomParameters);
74              map = new MapBlockContainer(info.getSizeX(), info.getSizeY(), info.getSizeZ());
75              boolean bEnd = false;
76              int i = 1;
77              while (!bEnd) {
78                  StorageInterface objectStorage = roomParameters.getParameterAsStorage(getPartName() + "." + (i++));
79                  if (objectStorage != null) {
80                      ParameterValueStorageExInterface objectParameters = new ParameterStorageString(objectStorage, null, getPartName());
81                      MapEditorObject obj = new MapEditorObject();
82                      obj.init(getEnvironment());
83                      obj.setContainer(getContainer());
84                      obj.read(objectParameters);
85                      obj.setImage(getPaletteImage(), obj.getBlockType()-1);
86                      map.setBlock(obj, (int)obj.getPosX(), (int)obj.getPosY(), (int)obj.getPosZ());
87                  } else {
88                      bEnd = true;
89                  }
90              }
91              return new LevelInfo(map, info);
92          }
93          return null;
94      }
95  
96      public void setLevel(int level, MapObjectContainerInterface blocks, ParameterSerializable extendedInfo) {
97  
98          StringStorage roomStorage = new StringStorage();
99          ParameterValueStorageExInterface roomParameters = new ParameterStorageGroupWithId(roomStorage, null, null, getPartName());
100         if (extendedInfo != null) {
101             extendedInfo.write(roomParameters);
102         }
103         int i = 1;
104         for (int x = 0; x < blocks.getSizeX(); x++) {
105             for (int y = 0; y < blocks.getSizeY(); y++) {
106                 for (int z = 0; z < blocks.getSizeZ(); z++) {
107                     MapEditorObject o = (MapEditorObject) blocks.getBlock(x, y, z);
108                     if (o != null) {
109                         StringStorage objectStorage = new StringStorage();
110                         ParameterValueStorageExInterface objectParameters = new ParameterStorageStringEx(objectStorage, null, null);
111                         o.write(objectParameters);
112                         roomParameters.setParameter(getPartName() + "." + (i++), objectStorage.load());
113                     }
114                 }
115             }
116         }
117         LevelManagerHelper.store(getEnvironment().getStorage(),getGroupName(), getObjectName(), "" + level, roomStorage.load());
118     }
119     private Image getPaletteImage() {
120         if(paletteImage==null) {
121             LevelManager levelManager = new RoomEditorLevelManager();
122             levelManager.init(getEnvironment());
123             paletteImage = levelManager.getImage();
124         }
125         return paletteImage;
126     }
127 }