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.Vector;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class AnimationEditorLevelManager extends LevelManager {
31 private Image paletteImage;
32 protected int getDefaultSizeX() {
33 return 10;
34 }
35
36 protected int getDefaultSizeY() {
37 return 5;
38 }
39
40 protected int getDefaultSizeZ() {
41 return 1;
42 }
43
44 protected String getGroupName() {
45 return "animations";
46 }
47
48 protected String getObjectName() {
49 return "animation";
50 }
51 protected String getPartName() {
52 return null;
53 }
54
55 public Image getImage() {
56 TileGameEnvironmentInterface env = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment());
57 SubImageHandler subImageHandler = env.createSubImageHandler(null);
58 IrregularBlockContainerInterface cont = env.createRectBlockContainer(0,0,1,1,1);
59 String prefix = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment()).getTileType();
60 boolean bEnd = false;
61 int i=0;
62 while(!bEnd) {
63 LevelInfoInterface level = getLevel(++i);
64 if(level!=null) {
65 AnimationExtendedLevelInfo info = (AnimationExtendedLevelInfo) level.getExtendedInfo();
66 int[] images = info.getImages();
67 MapEditorObject o = new MapEditorObject();
68 o.init(getEnvironment());
69 o.setContainer(cont);
70 o.setPos(0,0,0);
71 o.setImage(prefix+"ground.gif",images[0]);
72 o.draw(subImageHandler.getGraphics(i-1));
73 }else {
74 bEnd = true;
75 }
76 }
77 return subImageHandler.getImage();
78 }
79
80 public LevelInfoInterface createLevel(int level) {
81 LevelInfoInterface levelInfo = getLevel(level);
82 if(levelInfo==null) {
83 MapObjectContainerInterface map = new MapBlockContainer(getDefaultSizeX(),getDefaultSizeY(),getDefaultSizeZ());
84 MapObjectExtendedLevelInfo info = new MapObjectExtendedLevelInfo();
85 levelInfo = new LevelInfo(map,info);
86 }
87 return levelInfo;
88 }
89
90 public LevelInfoInterface getLevel(int level) {
91 StorageInterface animationStorage = LevelManagerHelper.load(getEnvironment().getStorage(),getGroupName(),getObjectName(),""+level);
92 String prefix = ((TileGameEnvironmentInterface)getEnvironment().getCustomEnvironment()).getTileType();
93 if(animationStorage!=null) {
94 MapObjectContainerInterface map = new MapBlockContainer(getDefaultSizeX(),getDefaultSizeY(),getDefaultSizeZ());
95 AnimationExtendedLevelInfo info = new AnimationExtendedLevelInfo();
96 ParameterValueStorageExInterface animationParameters = new ParameterStorageStringEx(animationStorage,null,getObjectName());
97 info.read(animationParameters);
98 int[] images = info.getImages();
99 for (int i = 0; i < images.length; i++) {
100 int image = images[i];
101 MapEditorObject obj = new MapEditorObject();
102 obj.init(getEnvironment());
103 obj.setContainer(getContainer());
104 obj.setBlockType(image+1);
105 obj.setImage(prefix+"ground.gif", obj.getBlockType()-1);
106 obj.setPos((i)%map.getSizeX(), (i)/map.getSizeX(), 0);
107 map.setBlock(obj, (int)obj.getPosX(), (int)obj.getPosY(), (int)obj.getPosZ());
108 }
109 return new LevelInfo(map,info);
110 }else {
111 return null;
112 }
113 }
114
115 public void setLevel(int level, MapObjectContainerInterface blocks, ParameterSerializable extendedInfo) {
116 StringStorage animationStorage = new StringStorage();
117 ParameterValueStorageExInterface animationParameters = new ParameterStorageStringEx(animationStorage, null, null);
118 Vector images = new Vector();
119 for (int x = 0; x < blocks.getSizeX(); x++) {
120 for (int y = 0; y < blocks.getSizeY(); y++) {
121 for (int z = 0; z < blocks.getSizeZ(); z++) {
122 MapEditorObject o = (MapEditorObject) blocks.getBlock(x, y, z);
123 if (o != null) {
124 images.add(new Integer(o.getBlockType()-1));
125 }
126 }
127 }
128 }
129 if (extendedInfo != null) {
130 int[] intImages = new int[images.size()];
131 for (int i = 0; i < intImages.length; i++) {
132 intImages[i] = ((Integer)images.elementAt(i)).intValue();
133 }
134 ((AnimationExtendedLevelInfo)extendedInfo).setImages(intImages);
135 extendedInfo.write(animationParameters);
136 }
137 LevelManagerHelper.store(getEnvironment().getStorage(),getGroupName(), getObjectName(), "" + level, animationStorage.load());
138 }
139 }