-
Notifications
You must be signed in to change notification settings - Fork 53
/
Level.pde
executable file
·143 lines (122 loc) · 3.63 KB
/
Level.pde
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
/**
* This class defines a generic sprite engine level.
* A layer may consist of one or more layers, with
* each layer modeling a 'self-contained' slice.
* For top-down games, these slices yield pseudo-height,
* whereas for side-view games they yield pseudo-depth.
*/
abstract class Level extends Screen {
boolean finished = false;
ArrayList<LevelLayer> layers;
HashMap<String, Integer> layerids;
// current viewbox
ViewBox viewbox;
/**
* Levels have dimensions!
*/
Level(float _width, float _height) {
super(_width,_height);
layers = new ArrayList<LevelLayer>();
layerids = new HashMap<String, Integer>();
viewbox = new ViewBox(_width, _height);
}
/**
* The viewbox only shows part of the level,
* so that we don't waste time computing things
* for parts of the level that we can't even see.
*/
void setViewBox(float _x, float _y, float _w, float _h) {
viewbox.x = _x;
viewbox.y = _y;
viewbox.w = _w;
viewbox.h = _h;
}
void addLevelLayer(String name, LevelLayer layer) {
layerids.put(name,layers.size());
layers.add(layer);
}
LevelLayer getLevelLayer(String name) {
return layers.get(layerids.get(name));
}
void cleanUp() {
for(LevelLayer l: layers) {
l.cleanUp();
}
}
// FIXME: THIS IS A TEST FUNCTION. KEEP? REJECT?
void updatePlayer(Player oldPlayer, Player newPlayer) {
for(LevelLayer l: layers) {
l.updatePlayer(oldPlayer, newPlayer);
}
}
/**
* Change the behaviour when the level finishes
*/
void finish() { setSwappable(); finished = true; }
/**
* What to do on a premature level finish (for instance, a reset-warranting death)
*/
void end() { finish(); }
/**
* draw the level, as seen from the viewbox
*/
void draw() {
translate(-viewbox.x, -viewbox.y);
for(LevelLayer l: layers) {
l.draw();
}
}
// used for statistics
int getActorCount() {
int count = 0;
for(LevelLayer l: layers) { count += l.getActorCount(); }
return count;
}
/**
* passthrough events
*/
void keyPressed(char key, int keyCode) {
for(LevelLayer l: layers) {
l.keyPressed(key, keyCode);
}
}
void keyReleased(char key, int keyCode) {
for(LevelLayer l: layers) {
l.keyReleased(key, keyCode);
}
}
void mouseMoved(int mx, int my) {
for(LevelLayer l: layers) {
l.mouseMoved(mx, my);
}
}
void mousePressed(int mx, int my, int button) {
for(LevelLayer l: layers) {
l.mousePressed(mx, my, button);
}
}
void mouseDragged(int mx, int my, int button) {
for(LevelLayer l: layers) {
l.mouseDragged(mx, my, button);
}
}
void mouseReleased(int mx, int my, int button) {
for(LevelLayer l: layers) {
l.mouseReleased(mx, my, button);
}
}
void mouseClicked(int mx, int my, int button) {
for(LevelLayer l: layers) {
l.mouseClicked(mx, my, button);
}
}
// layer component show/hide methods
void showBackground(boolean b) { for(LevelLayer l: layers) { l.showBackground = b; }}
void showBoundaries(boolean b) { for(LevelLayer l: layers) { l.showBoundaries = b; }}
void showPickups(boolean b) { for(LevelLayer l: layers) { l.showPickups = b; }}
void showDecals(boolean b) { for(LevelLayer l: layers) { l.showDecals = b; }}
void showInteractors(boolean b) { for(LevelLayer l: layers) { l.showInteractors = b; }}
void showActors(boolean b) { for(LevelLayer l: layers) { l.showActors = b; }}
void showForeground(boolean b) { for(LevelLayer l: layers) { l.showForeground = b; }}
void showTriggers(boolean b) { for(LevelLayer l: layers) { l.showTriggers = b; }}
}