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 WorldGameLevelManager 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          // No new levels can be created with this level manager
52          return null;
53      }
54  
55      public LevelInfoInterface getLevel(int level) {
56          StorageInterface roomStorage = LevelManagerHelper.load(getEnvironment().getStorage(),getGroupName(),getObjectName(),""+level);
57          if (roomStorage != null) {
58              WorldExtendedLevelInfo info = new WorldExtendedLevelInfo();
59              info.setSizeX(getDefaultSizeX());
60              info.setSizeY(getDefaultSizeY());
61              info.setSizeZ(getDefaultSizeZ());
62              MapObjectContainerInterface map = new MapBlockContainer(info.getSizeX(), info.getSizeY(), info.getSizeZ());
63              String prefix = ((TileGameEnvironmentInterface) getEnvironment().getCustomEnvironment()).getTileType();
64              ParameterValueStorageExInterface roomParameters = new ParameterStorageGroupWithId(roomStorage, null, getObjectName(), getPartName());
65              info.read(roomParameters);
66              map = new MapBlockContainer(info.getSizeX(), info.getSizeY(), info.getSizeZ());
67  
68              LevelManager roomLevelManager = new RoomGameLevelManager();
69              roomLevelManager.init(getEnvironment());
70              roomLevelManager.setContainer(getContainer());
71  
72              boolean bEnd = false;
73              int i = 1;
74              while (!bEnd) {
75                  StorageInterface objectStorage = roomParameters.getParameterAsStorage(getPartName() + "." + (i++));
76                  if (objectStorage != null) {
77                      ParameterValueStorageExInterface objectParameters = new ParameterStorageString(objectStorage, null, getPartName());
78                      MapEditorObject obj = new MapEditorObject();
79                      obj.init(getEnvironment());
80                      obj.setContainer(getContainer());
81                      obj.read(objectParameters);
82  
83                      LevelInfoInterface objectInfo = roomLevelManager.getLevel(obj.getBlockType());
84                      RoomObject room = new RoomObject();
85                      room.init(getEnvironment());
86                      room.setBlocks(objectInfo.getObjects());
87                      room.setExtendedLevelInfo(objectInfo.getExtendedInfo());
88                      room.setPos(obj.getPosX(),obj.getPosY(),obj.getPosZ());
89                      map.setBlock(room,(int)obj.getPosX(),(int)obj.getPosY(),(int)obj.getPosZ());
90                  } else {
91                      bEnd = true;
92                  }
93              }
94              return new LevelInfo(map, info);
95          }
96          return null;
97      }
98  
99      public void setLevel(int level, MapObjectContainerInterface blocks, ParameterSerializable extendedInfo) {
100 
101         StringStorage roomStorage = new StringStorage();
102         ParameterValueStorageExInterface roomParameters = new ParameterStorageGroupWithId(roomStorage, null, null, getPartName());
103         if (extendedInfo != null) {
104             extendedInfo.write(roomParameters);
105         }
106         int i = 1;
107         for (int x = 0; x < blocks.getSizeX(); x++) {
108             for (int y = 0; y < blocks.getSizeY(); y++) {
109                 for (int z = 0; z < blocks.getSizeZ(); z++) {
110                     MapEditorObject o = (MapEditorObject) blocks.getBlock(x, y, z);
111                     if (o != null) {
112                         StringStorage objectStorage = new StringStorage();
113                         ParameterValueStorageExInterface objectParameters = new ParameterStorageStringEx(objectStorage, null, null);
114                         o.write(objectParameters);
115                         roomParameters.setParameter(getPartName() + "." + (i++), objectStorage.load());
116                     }
117                 }
118             }
119         }
120         LevelManagerHelper.store(getEnvironment().getStorage(),getGroupName(), getObjectName(), "" + level, roomStorage.load());
121     }
122 }