2
2
3
3
import java .util .ArrayList ;
4
4
import java .util .Iterator ;
5
+ import java .util .List ;
5
6
6
7
import processing .core .PApplet ;
7
8
import processing .core .PConstants ;
15
16
import project_16x16 .Audio .BGM ;
16
17
import project_16x16 .Constants ;
17
18
import project_16x16 .SideScroller .GameScenes ;
18
- import project_16x16 .scene .PScene ;
19
19
import project_16x16 .ui .Button ;
20
20
21
21
/**
@@ -41,7 +41,7 @@ public MainMenu(SideScroller a) {
41
41
background = game .createGraphics ((int ) game .gameResolution .x , (int ) game .gameResolution .x );
42
42
background .noSmooth ();
43
43
Particles .assignApplet (a );
44
- Particles .populate (1000 );
44
+ Particles .populate (1250 );
45
45
46
46
pressStart = new Button (a );
47
47
pressMultiplayer = new Button (a );
@@ -78,11 +78,11 @@ public void switchTo() {
78
78
@ Override
79
79
public void drawUI () {
80
80
game .fill (Constants .Colors .MENU_GREY , 40 );
81
- game .rectMode (CORNER );
82
81
game .noStroke ();
82
+ game .rectMode (CORNER );
83
83
game .rect (0 , 0 , game .gameResolution .x , game .gameResolution .y );
84
- Particles .run ();
85
84
game .rectMode (CENTER );
85
+ Particles .run ();
86
86
87
87
pressStart .manDisplay ();
88
88
pressMultiplayer .manDisplay ();
@@ -121,11 +121,11 @@ void mouseReleased(MouseEvent e) {
121
121
@ Override
122
122
void keyReleased (KeyEvent e ) {
123
123
switch (e .getKeyCode ()) {
124
- case 8 : // BACKSPACE
125
- case PConstants .ESC : // Pause
124
+ case 8 : // BACKSPACE
125
+ case PConstants .ESC : // Pause
126
126
game .returnScene ();
127
127
break ;
128
- default :
128
+ default :
129
129
break ;
130
130
}
131
131
}
@@ -140,14 +140,14 @@ private static class Particles {
140
140
141
141
private static SideScroller game ;
142
142
143
- private static ArrayList <Particle > particles ;
143
+ private static List <Particle > particles ;
144
144
145
145
private static int function = 0 ;
146
146
private static int centerX , centerY ;
147
147
private static int scaleX , scaleY ;
148
148
149
- private static final float STEP = 0.05f ;
150
- private static final int TRANSITION_TIME = 360 ;
149
+ private static long timeAccumulator = 0 ;
150
+ private static final int TRANSITION_INTERVAL = 5000 ; // 5000 milliseconds = 5 seconds
151
151
152
152
static void assignApplet (SideScroller s ) {
153
153
particles = new ArrayList <>();
@@ -160,35 +160,33 @@ static void assignApplet(SideScroller s) {
160
160
161
161
static void populate (int n ) {
162
162
for (int i = 0 ; i < n ; i ++) {
163
- particles .add (new Particle (getXPos (game .random (0 , game .gameResolution .x )),
164
- getYPos (game .random (0 , game .gameResolution .y )), (int ) game .random (2 , 8 ), Utility .colorToRGB (
165
- (int ) game .random (0 , 50 ), (int ) game .random (150 , 255 ), (int ) game .random (150 , 255 ))));
163
+ float x = getXPos (game .random (0 , game .gameResolution .x ));
164
+ float y = getYPos (game .random (0 , game .gameResolution .y ));
165
+ int color = Utility .colorToRGB ((int ) game .random (0 , 50 ), (int ) game .random (150 , 255 ),
166
+ (int ) game .random (150 , 255 ));
167
+ Particle p = new Particle (x , y , (int ) game .random (2 , 8 ), color );
168
+ particles .add (p );
166
169
}
167
170
}
168
171
169
172
static void run () {
170
-
171
- if (game .frameCount % TRANSITION_TIME == 0 ) {
173
+ if (timeAccumulator >= TRANSITION_INTERVAL ) {
172
174
function ++;
173
- function %= 12 ;
175
+ function %= 12 ; // cycle movement function
176
+ timeAccumulator %= TRANSITION_INTERVAL ;
174
177
}
175
178
176
179
int repopulate = 0 ;
177
180
for (Iterator <Particle > iterator = particles .iterator (); iterator .hasNext ();) {
178
181
Particle p = iterator .next ();
179
- float x = (float ) getSlopeX (p .x , p .y );
180
- float y = (float ) getSlopeY (p .x , p .y );
181
- p .x += p .direction * x * STEP ;
182
- p .y += p .direction * y * STEP ;
183
-
184
- x = getXPrint (p .x );
185
- y = getYPrint (p .y );
186
-
187
- if (game .frameCount - p .start > 1 ) {
188
- game .stroke (p .color );
189
- game .strokeWeight (p .size );
190
- game .line (PApplet .lerp (x , p .lastX , 0.15f ), PApplet .lerp (y , p .lastY , 0.15f ), p .lastX , p .lastY );
191
- }
182
+ p .update (3 / game .targetFramerate );
183
+ float x = getXPrint (p .x );
184
+ float y = getYPrint (p .y );
185
+
186
+ game .stroke (p .color );
187
+ game .strokeWeight (p .size );
188
+ game .line (PApplet .lerp (x , p .lastX , 0.15f ), PApplet .lerp (y , p .lastY , 0.15f ), p .lastX , p .lastY );
189
+
192
190
p .lastX = x ;
193
191
p .lastY = y ;
194
192
if (!Utility .withinRegion (p .lastX , p .lastY , -100 , -100 , game .gameResolution .x + 100 ,
@@ -198,6 +196,8 @@ static void run() {
198
196
}
199
197
}
200
198
populate (repopulate );
199
+
200
+ timeAccumulator += 1000 / game .frameRate ;
201
201
}
202
202
203
203
private static double getSlopeX (float x , float y ) {
@@ -263,17 +263,23 @@ static class Particle {
263
263
private final int size ;
264
264
private final int color ;
265
265
private final float direction ;
266
- private final int start ;
267
266
268
267
public Particle (float x , float y , int size , int color ) {
269
268
this .x = x ;
270
269
this .y = y ;
271
270
this .color = color ;
272
271
this .size = size ;
273
- this .lastX = x ;
274
- this .lastY = y ;
272
+ this .lastX = getXPrint ( x ) ;
273
+ this .lastY = getYPrint ( y ) ;
275
274
this .direction = (game .random (0.1f , 1 ) * (game .random (1 ) > 0.5f ? 1 : -1 ));
276
- start = game .frameCount ;
275
+
276
+ }
277
+
278
+ void update (float step ) {
279
+ float xDelta = (float ) getSlopeX (x , y );
280
+ float yDelta = (float ) getSlopeY (x , y );
281
+ x += direction * xDelta * step ;
282
+ y += direction * yDelta * step ;
277
283
}
278
284
}
279
285
}
0 commit comments