Skip to content

Commit 42f5fa2

Browse files
committed
make menu background framerate-independent
prevent particles from occasionally jumping introduce framerateTarget variable
1 parent bd479df commit 42f5fa2

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

src/main/java/project_16x16/SideScroller.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@ public static DebugType get(int value) {
5656

5757
public static final boolean SNAP = true; // snap objects to grid when moving; TODO move to options
5858
public static int snapSize;
59+
public static long startTime;
5960

6061
// Game Rendering
6162
private PVector windowSize = new PVector(1280, 720); // Game window size -- to be set via options
6263
public PVector gameResolution = new PVector(1280, 720); // Game rendering resolution
64+
/** Framerate target/cap; the actual framerate's limit.*/
65+
public static float targetFramerate;
6366
// Font Resources
6467
private static PFont font_pixel;
6568

@@ -183,15 +186,15 @@ public void setup() {
183186

184187
snapSize = SNAP ? Options.snapSize : 1; // global snap step
185188

186-
// Set frame rate limit
187-
frameRate(Options.targetFrameRate);
189+
// Load framerate target from user settings (default = 60)
190+
targetFramerate = Options.targetFrameRate;
188191

189192
// Setup modes
190193
imageMode(CENTER);
191194
rectMode(CENTER);
192195
strokeCap(SQUARE);
193196

194-
// Create ArrayList
197+
// create set to manage current key(s) pressed down
195198
keysDown = new HashSet<Integer>();
196199

197200
// Main Load
@@ -200,7 +203,7 @@ public void setup() {
200203
Notifications.assignApplet(this);
201204
Audio.assignApplet(this);
202205

203-
// Create scene
206+
// Create scenes
204207
sceneHistory = new ArrayDeque<>();
205208
game = new GameplayScene(this, Constants.DEV_LEVEL);
206209
menu = new MainMenu(this);
@@ -214,14 +217,16 @@ public void setup() {
214217
controlsSettings = new ControlsSettings(this);
215218
swapToScene(GameScenes.MAIN_MENU);
216219

217-
// Camera
220+
// Init camera
218221
camera = new Camera(this);
219222
camera.setMouseMask(CONTROL);
220223
camera.setMinZoomScale(Constants.CAMERA_ZOOM_MIN);
221224
camera.setMaxZoomScale(Constants.CAMERA_ZOOM_MAX);
222225

223226
scaleResolution();
224-
launchIntoMultiplayer();
227+
launchIntoMultiplayer(); // multi is conditional on program args
228+
229+
startTime = System.currentTimeMillis(); // game starttime occurs at setup end
225230
}
226231

227232
/**
@@ -272,7 +277,7 @@ public void returnScene() {
272277
*/
273278
@Override
274279
public void draw() {
275-
280+
frameRate(targetFramerate);
276281
camera.hook();
277282
drawBelowCamera();
278283
camera.release();
@@ -296,6 +301,9 @@ public void draw() {
296301
* @see {@link Camera#hook()}
297302
*/
298303
private void drawBelowCamera() {
304+
if (sceneHistory.isEmpty()) {
305+
return;
306+
}
299307
sceneHistory.peek().getScene().draw(); // Handle Draw Scene Method - draws world, etc.
300308
if (debug == DebugType.ALL) {
301309
sceneHistory.peek().getScene().debug();
@@ -312,6 +320,9 @@ private void drawBelowCamera() {
312320
* @see {@link Camera#release()}
313321
*/
314322
private void drawAboveCamera() {
323+
if (sceneHistory.isEmpty()) {
324+
return;
325+
}
315326
sceneHistory.peek().getScene().drawUI();
316327
Notifications.run();
317328
if (debug == DebugType.ALL) {
@@ -348,11 +359,11 @@ public void keyReleased(processing.event.KeyEvent event) {
348359

349360
final int keyCode = event.getKeyCode();
350361
if (keyCode == Options.frameRateHighKey) {
351-
frameRate(5000);
362+
targetFramerate = 1000;
352363
} else if (keyCode == Options.frameRateLowKey) {
353-
frameRate(20);
364+
targetFramerate = 20;
354365
} else if (keyCode == Options.frameRateDefaultKey) {
355-
frameRate(60);
366+
targetFramerate = 60;
356367
} else if (keyCode == Options.toggleDeadzoneKey) {
357368
camera.toggleDeadZone();
358369
} else if (keyCode == Options.cameraToMouseKey) {

src/main/java/project_16x16/scene/MainMenu.java

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.Iterator;
5+
import java.util.List;
56

67
import processing.core.PApplet;
78
import processing.core.PConstants;
@@ -15,7 +16,6 @@
1516
import project_16x16.Audio.BGM;
1617
import project_16x16.Constants;
1718
import project_16x16.SideScroller.GameScenes;
18-
import project_16x16.scene.PScene;
1919
import project_16x16.ui.Button;
2020

2121
/**
@@ -41,7 +41,7 @@ public MainMenu(SideScroller a) {
4141
background = game.createGraphics((int) game.gameResolution.x, (int) game.gameResolution.x);
4242
background.noSmooth();
4343
Particles.assignApplet(a);
44-
Particles.populate(1000);
44+
Particles.populate(1250);
4545

4646
pressStart = new Button(a);
4747
pressMultiplayer = new Button(a);
@@ -78,11 +78,11 @@ public void switchTo() {
7878
@Override
7979
public void drawUI() {
8080
game.fill(Constants.Colors.MENU_GREY, 40);
81-
game.rectMode(CORNER);
8281
game.noStroke();
82+
game.rectMode(CORNER);
8383
game.rect(0, 0, game.gameResolution.x, game.gameResolution.y);
84-
Particles.run();
8584
game.rectMode(CENTER);
85+
Particles.run();
8686

8787
pressStart.manDisplay();
8888
pressMultiplayer.manDisplay();
@@ -121,11 +121,11 @@ void mouseReleased(MouseEvent e) {
121121
@Override
122122
void keyReleased(KeyEvent e) {
123123
switch (e.getKeyCode()) {
124-
case 8 : // BACKSPACE
125-
case PConstants.ESC : // Pause
124+
case 8: // BACKSPACE
125+
case PConstants.ESC: // Pause
126126
game.returnScene();
127127
break;
128-
default :
128+
default:
129129
break;
130130
}
131131
}
@@ -140,14 +140,14 @@ private static class Particles {
140140

141141
private static SideScroller game;
142142

143-
private static ArrayList<Particle> particles;
143+
private static List<Particle> particles;
144144

145145
private static int function = 0;
146146
private static int centerX, centerY;
147147
private static int scaleX, scaleY;
148148

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
151151

152152
static void assignApplet(SideScroller s) {
153153
particles = new ArrayList<>();
@@ -160,35 +160,33 @@ static void assignApplet(SideScroller s) {
160160

161161
static void populate(int n) {
162162
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);
166169
}
167170
}
168171

169172
static void run() {
170-
171-
if (game.frameCount % TRANSITION_TIME == 0) {
173+
if (timeAccumulator >= TRANSITION_INTERVAL) {
172174
function++;
173-
function %= 12;
175+
function %= 12; // cycle movement function
176+
timeAccumulator %= TRANSITION_INTERVAL;
174177
}
175178

176179
int repopulate = 0;
177180
for (Iterator<Particle> iterator = particles.iterator(); iterator.hasNext();) {
178181
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+
192190
p.lastX = x;
193191
p.lastY = y;
194192
if (!Utility.withinRegion(p.lastX, p.lastY, -100, -100, game.gameResolution.x + 100,
@@ -198,6 +196,8 @@ static void run() {
198196
}
199197
}
200198
populate(repopulate);
199+
200+
timeAccumulator += 1000 / game.frameRate;
201201
}
202202

203203
private static double getSlopeX(float x, float y) {
@@ -263,17 +263,23 @@ static class Particle {
263263
private final int size;
264264
private final int color;
265265
private final float direction;
266-
private final int start;
267266

268267
public Particle(float x, float y, int size, int color) {
269268
this.x = x;
270269
this.y = y;
271270
this.color = color;
272271
this.size = size;
273-
this.lastX = x;
274-
this.lastY = y;
272+
this.lastX = getXPrint(x);
273+
this.lastY = getYPrint(y);
275274
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;
277283
}
278284
}
279285
}

0 commit comments

Comments
 (0)