Repository navigation
Expand file tree
/
Copy pathStage.java
More file actions
167 lines (148 loc) · 6.79 KB
/
Copy pathStage.java
File metadata and controls
167 lines (148 loc) · 6.79 KB
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
/* Stage.java
* Scratching -- Scratch for Processing
*
* This file seeks to implement Scratch blocks and sprites in
* Processing, in order to facilitate a transition from Scratch
* into p.
* See: http://wiki.scratch.mit.edu/wiki/Blocks
*
* This Stage class has just a few simple functions for handling
* the background.
*
* switchToBackdrop(#); can replace the background(#);
* command at the top of your draw() loop.
*
* The backdrop size should match your stage size.
* Who knows what might happen if it does not?!
*
*/
import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PFont;
import java.util.ArrayList;
public class Stage {
// without this, built-in functions are broken. use p.whatever to access functionality
PApplet p;
// listing our backgrounds here lets us access them by name instead of number in our main program
// ie, switchToBackdrop(bg_title); instead of switchToBackdrop(1).
//
// You may use your own art for your own project by adding PNG or JPG art to the file folder,
// and changing the "addDefaultBackdrops()" function below.
//
// Backdrop 0 should always the X/Y grid, for debugging movement
public static final int bg_grid=0;
public static final int bg_title=1;
public static final int bg_highway=2;
public static final int bg_gameover=3;
public int startTime;
public int backdropNumber, numberOfBackdrops;
public ArrayList<PImage> backdrops = new ArrayList<PImage>();
int scrollX, scrollY;
Stage (PApplet parent) {
p = parent;
backdropNumber=0;
numberOfBackdrops=0;
startTime=0;
resetTimer();
scrollX = 0; scrollY = 0;
}
// the timer returns seconds, in whole numbers (integer)
public int timer() {
int temp = p.millis()/1000;
return temp-startTime;
}
// reset the stage timer
public void resetTimer() {
startTime = p.millis()/1000;
}
public void update() {
draw();
}
public void draw() {
int scrollXmod = scrollX % p.width;
int scrollYmod = scrollY % p.height;
// current logic doesn't check direction of scroll & draws unnecessary off-screen backdrops!
if ( (scrollXmod) != 0 && (scrollYmod) == 0) {
// scrolling X only. draw stages Y center
p.image(backdrops.get(backdropNumber), (p.width/2)+scrollXmod, (p.height/2), backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
// for scrolling right, draw to the left of stage
p.image(backdrops.get(backdropNumber), 0-(p.width/2)+scrollXmod, (p.height/2), backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
// for scrolling left, draw to the right of stage
p.image(backdrops.get(backdropNumber), p.width+(p.width/2)+scrollXmod, (p.height/2), backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
} else if ( (scrollXmod) == 0 && (scrollYmod) != 0) {
// scrolling Y only. draw center stage
p.image(backdrops.get(backdropNumber), (p.width/2), (p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
// for scrolling right, draw to the left of stage
p.image(backdrops.get(backdropNumber), (p.width/2), 0-(p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
// for scrolling left, draw to the right of stage
p.image(backdrops.get(backdropNumber), (p.width/2), p.height+(p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
} else if ( (scrollXmod) != 0 && (scrollYmod) != 0) {
//*************** scrolling X and Y. draw stage Y top
p.image(backdrops.get(backdropNumber), (p.width/2)+scrollXmod, (p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
// for scrolling right, draw to the left of stage
p.image(backdrops.get(backdropNumber), 0-(p.width/2)+scrollXmod, (p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
// for scrolling left, draw to the right of stage
p.image(backdrops.get(backdropNumber), p.width+(p.width/2)+scrollXmod, (p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
//********** scrolling X and Y. draw center stages,
p.image(backdrops.get(backdropNumber), (p.width/2)+scrollXmod, 0-(p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
// for scrolling right, draw to the left of stage
p.image(backdrops.get(backdropNumber), 0-(p.width/2)+scrollXmod, 0-(p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
// for scrolling left, draw to the right of stage
p.image(backdrops.get(backdropNumber), p.width+(p.width/2)+scrollXmod, 0-(p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
//********** scrolling X and Y. draw bottom stages
p.image(backdrops.get(backdropNumber), (p.width/2)+scrollXmod, p.height+(p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
// for scrolling right, draw to the left of stage
p.image(backdrops.get(backdropNumber), 0-(p.width/2)+scrollXmod, p.height+(p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
// for scrolling left, draw to the right of stage
p.image(backdrops.get(backdropNumber), p.width+(p.width/2)+scrollXmod, p.height+(p.height/2)-scrollYmod, backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
} else {
p.image(backdrops.get(backdropNumber), (p.width/2), (p.height/2), backdrops.get(backdropNumber).width,
backdrops.get(backdropNumber).height);
}
}
// load xy grid as backdrop 0
public void loadDefaultBackdrop() {
addBackdrop("images/xy-grid.png");
}
// add costume from bitmap image file
public void addBackdrop(String filePath) {
numberOfBackdrops++;
backdrops.add(p.loadImage(filePath));
}
// change to next backdrop
public void nextBackdrop() {
backdropNumber++;
if (backdropNumber > numberOfBackdrops + 1) backdropNumber=0;
draw();
}
// change to previous backdrop
public void previousCostume() {
backdropNumber--;
if (backdropNumber < 0) backdropNumber=backdropNumber;
draw();
}
// switch to specific costume
public void setBackdrop(int newBackdropNumber) {
backdropNumber=newBackdropNumber;
draw();
}
public void scrollBackdrop(float x, float y) {
scrollX += x;
scrollY += y;
}
}