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  import java.util.Vector;
12  
13  /*
14   * Copyright (C) 2004 Erland Isaksson (erland_i@hotmail.com)
15   *
16   * This program is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU General Public License
18   * as published by the Free Software Foundation; either version 2
19   * of the License, or (at your option) any later version.
20   *
21   * This program is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24   * GNU General Public License for more details.
25   *
26   * You should have received a copy of the GNU General Public License
27   * along with this program; if not, write to the Free Software
28   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29   * 
30   */
31  
32  public class AnimationGameLevelManager extends LevelManager {
33      private Map cacheMap = new HashMap();
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 "animations";
49      }
50  
51      protected String getObjectName() {
52          return "animation";
53      }
54      protected String getPartName() {
55          return "image";
56      }
57  
58      public Image getImage() {
59          // It is not possible to create an image with this level manager
60          return null;
61      }
62  
63      public LevelInfoInterface createLevel(int level) {
64          // It is not possible to create new levels with this level manager
65          return null;
66      }
67  
68      public LevelInfoInterface getLevel(int level) {
69          LevelInfo levelInfo = getFromCache(level);
70          if(levelInfo!=null) {
71              return cloneLevel(levelInfo);
72          }
73          StorageInterface animationStorage = LevelManagerHelper.load(getEnvironment().getStorage(),getGroupName(),getObjectName(),""+level);
74          String prefix = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment()).getTileType();
75          if(animationStorage!=null) {
76              AnimationExtendedLevelInfo info = new AnimationExtendedLevelInfo();
77              ParameterValueStorageExInterface animationParameters = new ParameterStorageStringEx(animationStorage,null,getObjectName());
78              info.read(animationParameters);
79              levelInfo = new LevelInfo(null,info);
80              setInCache(level,levelInfo);
81              return levelInfo;
82          }else {
83              return null;
84          }
85      }
86  
87      private LevelInfo getFromCache(int level) {
88          return (LevelInfo) cacheMap.get(new Integer(level));
89      }
90      private void setInCache(int level, LevelInfo data) {
91          cacheMap.put(new Integer(level),data);
92      }
93  
94      private LevelInfo cloneLevel(LevelInfo level) {
95          MapObjectContainerInterface newBlock = cloneObjects(level.getObjects());
96          ParameterSerializable newLevelInfo = null;
97          ParameterValueStorageExInterface storage = new ParameterStorageString(new StringStorage(),null);
98          if(level.getExtendedInfo()!=null) {
99              level.getExtendedInfo().read(storage);
100             newLevelInfo = new AnimationExtendedLevelInfo();
101             newLevelInfo.write(storage);
102         }
103         return new LevelInfo(newBlock,newLevelInfo);
104     }
105     private MapObjectContainerInterface cloneObjects(MapObjectContainerInterface objects) {
106         if(objects!=null) {
107             MapObjectContainerInterface newMap = new MapBlockContainer(objects.getSizeX(),objects.getSizeY(),objects.getSizeZ());
108             for (int x = 0; x < newMap.getSizeX(); x++) {
109                 for (int y = 0; y < newMap.getSizeY(); y++) {
110                     for (int z = 0; z < newMap.getSizeZ(); z++) {
111                         MapObjectInterface o = objects.getBlock(x,y,z);
112                         if(o!=null) {
113                             GameObject newObj = (GameObject) o.clone();
114                             newObj.setObjectMap(newMap);
115                             newMap.setBlock(newObj,x,y,z);
116                         }
117                     }
118                 }
119             }
120             return newMap;
121         }
122         return null;
123     }
124 
125     public void setLevel(int level, MapObjectContainerInterface blocks, ParameterSerializable extendedInfo) {
126         StringStorage animationStorage = new StringStorage();
127         ParameterValueStorageExInterface animationParameters = new ParameterStorageGroupWithId(animationStorage, null, null, getPartName());
128         int i = 1;
129         for (int x = 0; x < blocks.getSizeX(); x++) {
130             for (int y = 0; y < blocks.getSizeY(); y++) {
131                 for (int z = 0; z < blocks.getSizeZ(); z++) {
132                     MapEditorObject o = (MapEditorObject) blocks.getBlock(x, y, z);
133                     if (o != null) {
134                         MapEditorAnimationObject tmpObj = new MapEditorAnimationObject();
135                         tmpObj.init(getEnvironment());
136                         tmpObj.setBlockType(o.getBlockType());
137                         StringStorage imageStorage = new StringStorage();
138                         ParameterValueStorageExInterface imageParameters = new ParameterStorageStringEx(imageStorage, null, null);
139                         tmpObj.write(imageParameters);
140                         animationParameters.setParameter(getPartName() + "." + (i++), imageStorage.load());
141                     }
142                 }
143             }
144         }
145         if (extendedInfo != null) {
146             extendedInfo.write(animationParameters);
147         }
148         LevelManagerHelper.store(getEnvironment().getStorage(),getGroupName(), getObjectName(), "" + level, animationStorage.load());
149     }
150 }