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   import java.util.Map;
10  import java.util.HashMap;
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 MapObjectGameLevelManager extends LevelManager {
32      private Map cacheMap = new HashMap();
33      private AnimationGameLevelManager animationLevelManager;
34      private Image paletteImage;
35      protected int getDefaultSizeX() {
36          return 1;
37      }
38  
39      protected int getDefaultSizeY() {
40          return 1;
41      }
42  
43      protected int getDefaultSizeZ() {
44          return 1;
45      }
46  
47      protected String getGroupName() {
48          return "objects";
49      }
50  
51      protected String getObjectName() {
52          return "object";
53      }
54      public Image getImage() {
55          TileGameEnvironmentInterface env = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment());
56          SubImageHandler subImageHandler = env.createSubImageHandler(null);
57          IrregularBlockContainerInterface cont = env.createRectBlockContainer(0,0,1,1,1);
58          boolean bEnd = false;
59          int i=0;
60          while(!bEnd) {
61              LevelInfoInterface level = getLevel(++i);
62              if(level!=null) {
63                  MapObjectContainerInterface map = level.getObjects();
64                  MapEditorObject o = (MapEditorObject) map.getBlock(0,0,0);
65                  o.init(getEnvironment());
66                  o.setContainer(cont);
67                  o.setPos(0,0,0);
68                  o.draw(subImageHandler.getGraphics(i-1));
69              }else {
70                  bEnd = true;
71              }
72          }
73          return subImageHandler.getImage();
74      }
75  
76      public LevelInfoInterface createLevel(int level) {
77          // This cannot create new levels
78          return null;
79      }
80  
81      public LevelInfoInterface getLevel(int level) {
82          LevelInfo levelInfo = getFromCache(level);
83          if(levelInfo!=null) {
84              return cloneLevel(levelInfo);
85          }
86          StorageInterface objectStorage = LevelManagerHelper.load(getEnvironment().getStorage(),getGroupName(),getObjectName(),""+level);
87          String prefix = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment()).getTileType();
88          if(objectStorage!=null) {
89              MapObjectContainerInterface map = new MapBlockContainer(1,1,1);
90              MapObjectExtendedLevelInfo info = new MapObjectExtendedLevelInfo();
91              ParameterValueStorageExInterface objectParameters = new ParameterStorageStringEx(objectStorage,null,getObjectName());
92              info.read(objectParameters);
93              MapEditorObject obj = new MapEditorObject();
94              obj.init(getEnvironment());
95              obj.setContainer(getContainer());
96              obj.read(objectParameters);
97  
98              GameObject o = getObject(obj,info);
99              o.setContainer(getContainer());
100             o.setObjectMap(map);
101             o.setPos(obj.getPosX(),obj.getPosY(),obj.getPosZ());
102             map.setBlock(o,(int)o.getPosX(),(int)o.getPosY(),(int)o.getPosZ());
103             levelInfo = new LevelInfo(map, null);
104             setInCache(level,levelInfo);
105             return levelInfo;
106         }else {
107             return null;
108         }
109     }
110 
111     private LevelInfo getFromCache(int level) {
112         return (LevelInfo) cacheMap.get(new Integer(level));
113     }
114     private void setInCache(int level, LevelInfo data) {
115         cacheMap.put(new Integer(level),data);
116     }
117 
118     private LevelInfo cloneLevel(LevelInfo level) {
119         MapObjectContainerInterface newBlock = cloneObjects(level.getObjects());
120         ParameterSerializable newLevelInfo = null;
121         ParameterValueStorageExInterface storage = new ParameterStorageString(new StringStorage(),null);
122         if(level.getExtendedInfo()!=null) {
123             level.getExtendedInfo().read(storage);
124             newLevelInfo = new MapObjectExtendedLevelInfo();
125             newLevelInfo.write(storage);
126         }
127         return new LevelInfo(newBlock,newLevelInfo);
128     }
129     private MapObjectContainerInterface cloneObjects(MapObjectContainerInterface objects) {
130         MapObjectContainerInterface newMap = new MapBlockContainer(objects.getSizeX(),objects.getSizeY(),objects.getSizeZ());
131         for (int x = 0; x < newMap.getSizeX(); x++) {
132             for (int y = 0; y < newMap.getSizeY(); y++) {
133                 for (int z = 0; z < newMap.getSizeZ(); z++) {
134                     MapObjectInterface o = objects.getBlock(x,y,z);
135                     if(o!=null) {
136                         GameObject newObj = (GameObject) o.clone();
137                         newObj.setObjectMap(newMap);
138                         newMap.setBlock(newObj,x,y,z);
139                     }
140                 }
141             }
142         }
143         return newMap;
144     }
145 
146     public void setLevel(int level, MapObjectContainerInterface blocks, ParameterSerializable extendedInfo) {
147 
148         StringStorage objectStorage = new StringStorage();
149         ParameterValueStorageExInterface objectParameters = new ParameterStorageStringEx(objectStorage,null,null);
150         MapEditorObject obj = (MapEditorObject) blocks.getBlock(0,0,0);
151         if(obj!=null) {
152             obj.write(objectParameters);
153         }
154         if(extendedInfo!=null) {
155             extendedInfo.write(objectParameters);
156         }
157 
158         LevelManagerHelper.store(getEnvironment().getStorage(),getGroupName(),getObjectName(),""+level,objectStorage.load() );
159     }
160 
161     public GameObject getObject(MapEditorObject obj, MapObjectExtendedLevelInfo info) {
162         GameObject o = null;
163         if(animationLevelManager==null) {
164             animationLevelManager = new AnimationGameLevelManager();
165             animationLevelManager.init(getEnvironment());
166         }
167         LevelInfoInterface levelInfo = animationLevelManager.getLevel(obj.getBlockType());
168         AnimationExtendedLevelInfo animationInfo = (AnimationExtendedLevelInfo) levelInfo.getExtendedInfo();
169         String prefix = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment()).getTileType();
170         if(info.getPushable().booleanValue()) {
171             MovableObject tmp = new MovableObject();
172             tmp.init(getEnvironment());
173             tmp.setAnimation(prefix+"ground.gif",new Animation(animationInfo.getSpeed(),animationInfo.getImages()));
174             o = tmp;
175         }else if(animationInfo.getImages().length>1) {
176             AnimatedObject tmp = new AnimatedObject();
177             tmp.init(getEnvironment());
178             tmp.setAnimation(prefix+"ground.gif",new Animation(animationInfo.getSpeed(),animationInfo.getImages()));
179             o = tmp;
180         }else {
181             BitmapObject tmp = new BitmapObject();
182             tmp.init(getEnvironment());
183             tmp.setImage(prefix+"ground.gif",obj.getBlockType()-1);
184             o = tmp;
185         }
186         return o;
187     }
188 }