1 package org.molwind.view;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.util.Hashtable;
23
24 import org.molwind.io.MolwindLocator;
25 import org.molwind.model.LayeredPosition;
26 import org.molwind.model.MolwindWorld;
27 import org.molwind.util.MolwindLogger;
28
29
30
31
32
33
34
35 public class Tiles {
36
37 private String worldName;
38 private MolwindWorld world;
39 private Hashtable tileStore;
40
41 private static Hashtable worldStore;
42
43
44 private Tiles(final String newWorldName, final MolwindWorld newWorld) {
45 tileStore = new Hashtable();
46 this.worldName = newWorldName;
47 this.world = newWorld;
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public static synchronized Tiles getInstance(final String worldName)
63 throws IOException {
64 if (worldStore == null) {
65 worldStore = new Hashtable();
66 }
67
68 Tiles tiles = (Tiles) worldStore.get(worldName);
69
70 if (tiles == null) {
71 MolwindWorld world = MolwindLocator.getLocator()
72 .locateWorld(worldName);
73
74 if (world != null) {
75 tiles = new Tiles(worldName, world);
76 worldStore.put(worldName, tiles);
77 MolwindLogger.debug("New tiles store created for: "
78 + worldName);
79 }
80 }
81
82 return tiles;
83 }
84
85
86
87
88
89
90
91 public String getWorldName() {
92 return worldName;
93 }
94
95
96
97
98
99
100
101 public void setWorldName(final String newWorldName) {
102 this.worldName = newWorldName;
103 }
104
105
106
107
108
109
110
111 public MolwindWorld getWorld() {
112 return world;
113 }
114
115
116
117
118
119
120
121 public void setWorld(final MolwindWorld newWorld) {
122 this.world = newWorld;
123 }
124
125
126
127
128
129
130
131
132
133 public Tile getTile(final LayeredPosition position) {
134 return (Tile) tileStore.get(position);
135 }
136
137
138
139
140
141
142
143
144
145 public void addTile(final LayeredPosition position, final Tile tile) {
146 tileStore.put(position, tile);
147 }
148
149
150
151
152
153
154
155
156
157
158
159 public void writeTile(final LayeredPosition position,
160 final OutputStream output)
161 throws IOException {
162 Tile tile = getTile(position);
163
164 if (tile == null) {
165 MolwindLogger.debug("Generating tile " + position);
166
167 TileGenerator generator = world.getTileGenerator();
168 try {
169 tile = generator.generateTile(position);
170 } catch (TileException te) {
171 throw new IOException(te.getMessage());
172 }
173
174 addTile(position, tile);
175 MolwindLogger.debug("Tile generated " + tile);
176 }
177
178 tile.writeTo(output);
179 }
180
181 }