View Javadoc

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