-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.java
311 lines (246 loc) · 6.97 KB
/
Scene.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import java.awt.*;
import java.util.*;
import java.net.*;
import java.io.*;
/**
* this class was originally meant to simply hold a reference to a map
* and a collection of sprites, which it delegated all the work to in the
* logic and render methods of these objects...<p>
*
* so it is reasonably fitting that this class also manage the loading of the
* maps and sprites it uses from files or URLs.<p>
*
* Each scene consists of a Map, and a collection of sprites, as intended,
* but may also be loaded or saved.
*/
public class Scene
{
int offsetX = 0;
int offsetY = 0;
float effect_rScale = 1;
float effect_gScale = 1;
float effect_bScale = 1;
float effect_hue = 0;
float effect_sat = 1;
Map map;
ArrayList sprites;
GraphicsBank tileset;
/**
* creates a scene using the given map and sprites.
*/
public Scene(Map m, ArrayList s, GraphicsBank gfx)
{
this.map = m;
sprites = s;
this.tileset = gfx;
}
/* Create a new empty scene */
public Scene() {
map = new Map(10, 10, 32, 32);
tileset = new GraphicsBank();
}
public GraphicsBank getTileset()
{
return tileset;
}
public void setTileset(GraphicsBank gfx) {
tileset = gfx;
map.setTileset(gfx);
}
/**
* loads a scene from the given URL. takes tiles from the given GraphicsBank.
*/
static Scene loadScene(File f) throws IOException
{
boolean hasColourEffect = false;
float r = 1;
float g = 1;
float b = 1;
float h = 0;
float s = 1;
BufferedReader reader = new BufferedReader(new FileReader(f));
String line = reader.readLine();
StringTokenizer tokens = new StringTokenizer(line);
int width = Integer.parseInt(tokens.nextToken());
int height = Integer.parseInt(tokens.nextToken());
String tileset = tokens.nextToken();
GraphicsBank gfx = new GraphicsBank();
System.out.println("Attempt to load tileset "+tileset);
System.out.println("Working path is "+f.getParentFile());
File ts = new File(f.getParentFile(), tileset);
System.out.println("Attempt to load tileset "+ts.getAbsoluteFile());
gfx.loadTileset(ts);
Map map = new Map(width, height);
line = reader.readLine();
tokens = new StringTokenizer(line);
if(tokens.nextToken().equalsIgnoreCase("colorization")) {
hasColourEffect = true;
r = Float.parseFloat(tokens.nextToken());
g = Float.parseFloat(tokens.nextToken());
b = Float.parseFloat(tokens.nextToken());
h = Float.parseFloat(tokens.nextToken());
s = Float.parseFloat(tokens.nextToken());
}
while(! line.equals("."))
{
line = reader.readLine();
}
for(int z=0; z<3; z++)
{
line = reader.readLine();
tokens = new StringTokenizer(line);
for(int y=0; y<height; y++)
{
for(int x=0; x<width; x++)
{
String code = tokens.nextToken();
map.setTile(x, y, z, gfx.getTile(Integer.parseInt(code)));
}
}
}
reader.close();
Scene scene = new Scene(map, new ArrayList(), gfx);
scene.tileset = gfx;
if(hasColourEffect) {
System.out.println("Calling setEffect on scene recently loaded.");
scene.setEffect(r, g, b, h, s, 1f);
}
return scene;
}
static Scene loadScene(String filename) throws IOException {
Scene scene = loadScene(new File(filename));
return scene;
}
/**
* writes the map only (at the moment) to a file.
*/
public void saveScene(File file)
{
if(tileset.isUnsaved()) {
throw new RuntimeException("Tileset is unsaved. Cannot save the scene");
}
try
{
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
String line = "";
int width = map.getWidth();
int height = map.getHeight();
File wd = new File(file.getParentFile().getCanonicalFile().toString());
File ts = new File(tileset.getFile().getCanonicalFile().toString());
String relativePath = RelativePath.getRelativePath(wd, ts);
line = width + " " + height + " " + relativePath;
writer.println(line);
line = "colorization " + effect_rScale +
" " + effect_gScale +
" " + effect_bScale +
" " + effect_hue +
" " + effect_sat;
writer.println(line);
System.out.println("Colorization red in save is "+effect_rScale);
writer.println(".");
for(int z=0; z<3; z++)
{
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
Tile t = map.getTile(j, i, z);
if(t != null)
writer.print(t.getNumber()+ " ");
else writer.print("0 ");
}
}
writer.println();
}
writer.flush();
writer.close();
}
catch(IOException e)
{
throw new RuntimeException("Could not save the level");
}
System.err.println("Saved");
}
/**
* calls each sprites logic method.
*/
void logic()
{
for(int i=0; i<sprites.size(); i++)
{
Sprite s = (Sprite)sprites.get(i);
s.logic();
}
}
/**
* renders the scene to the graphics context.
* at the moment, sprites appear above everything else.
*
* TODO: Fix this up.
*/
void render(Graphics g)
{
//System.out.println("Render Scene");
map.render(g, offsetX, offsetY);
for(int i=0; i<sprites.size(); i++)
{
Sprite s = (Sprite)sprites.get(i);
//s.render(g, offsetX, offsetY);
}
}
public void render(Graphics g, int offX, int offY)
{
map.render(g, offX, offY);
}
public void render(Graphics g, Camera c)
{
map.render(g, c);
}
public void render(Graphics g, Point origin, Dimension size)
{
map.render(g, origin, size);
}
public void render(Graphics g, Point origin, Dimension size, int layer)
{
map.render(g, origin, size, layer);
}
/**
* sets the visible area of the scene.
* This will probably not be used once the game is finished.
*/
void setViewSize(int width, int height)
{
map.setViewSize(width, height);
}
/**
* sets the screen offset. Screen offset can be anywhere, not just
* following a character around. This is good for cut scenes.
*/
void setOffset(int x, int y)
{
offsetX = x;
offsetY = y;
}
/**
* Apply RGB scalars and hue/sat adjustments to the tiles and characters
* in the scene. The hue and sat are always applied first
**/
public void setEffect(float r, float g, float b, float h, float s, float z) {
System.out.println("Scene setEffect called. will call for the gfx bank...r" +r+" g"+g+" b"+b+" z"+z);
effect_rScale = r;
effect_gScale = g;
effect_bScale = b;
effect_hue = h;
effect_sat = s;
tileset.setEffect(r, g, b, h, s, z);
map.setZoom(z);
}
/**
* returns the map. not really any reason to want to do this except for the
* level editor.
*/
public Map getMap()
{
return map;
}
}