Skip to content

Commit

Permalink
make menu background framerate-independent
Browse files Browse the repository at this point in the history
prevent particles from occasionally jumping
introduce framerateTarget variable
  • Loading branch information
micycle1 committed Dec 27, 2024
1 parent bd479df commit 42f5fa2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
31 changes: 21 additions & 10 deletions src/main/java/project_16x16/SideScroller.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ public static DebugType get(int value) {

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

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

Expand Down Expand Up @@ -183,15 +186,15 @@ public void setup() {

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

// Set frame rate limit
frameRate(Options.targetFrameRate);
// Load framerate target from user settings (default = 60)
targetFramerate = Options.targetFrameRate;

// Setup modes
imageMode(CENTER);
rectMode(CENTER);
strokeCap(SQUARE);

// Create ArrayList
// create set to manage current key(s) pressed down
keysDown = new HashSet<Integer>();

// Main Load
Expand All @@ -200,7 +203,7 @@ public void setup() {
Notifications.assignApplet(this);
Audio.assignApplet(this);

// Create scene
// Create scenes
sceneHistory = new ArrayDeque<>();
game = new GameplayScene(this, Constants.DEV_LEVEL);
menu = new MainMenu(this);
Expand All @@ -214,14 +217,16 @@ public void setup() {
controlsSettings = new ControlsSettings(this);
swapToScene(GameScenes.MAIN_MENU);

// Camera
// Init camera
camera = new Camera(this);
camera.setMouseMask(CONTROL);
camera.setMinZoomScale(Constants.CAMERA_ZOOM_MIN);
camera.setMaxZoomScale(Constants.CAMERA_ZOOM_MAX);

scaleResolution();
launchIntoMultiplayer();
launchIntoMultiplayer(); // multi is conditional on program args

startTime = System.currentTimeMillis(); // game starttime occurs at setup end
}

/**
Expand Down Expand Up @@ -272,7 +277,7 @@ public void returnScene() {
*/
@Override
public void draw() {

frameRate(targetFramerate);
camera.hook();
drawBelowCamera();
camera.release();
Expand All @@ -296,6 +301,9 @@ public void draw() {
* @see {@link Camera#hook()}
*/
private void drawBelowCamera() {
if (sceneHistory.isEmpty()) {
return;
}
sceneHistory.peek().getScene().draw(); // Handle Draw Scene Method - draws world, etc.
if (debug == DebugType.ALL) {
sceneHistory.peek().getScene().debug();
Expand All @@ -312,6 +320,9 @@ private void drawBelowCamera() {
* @see {@link Camera#release()}
*/
private void drawAboveCamera() {
if (sceneHistory.isEmpty()) {
return;
}
sceneHistory.peek().getScene().drawUI();
Notifications.run();
if (debug == DebugType.ALL) {
Expand Down Expand Up @@ -348,11 +359,11 @@ public void keyReleased(processing.event.KeyEvent event) {

final int keyCode = event.getKeyCode();
if (keyCode == Options.frameRateHighKey) {
frameRate(5000);
targetFramerate = 1000;
} else if (keyCode == Options.frameRateLowKey) {
frameRate(20);
targetFramerate = 20;
} else if (keyCode == Options.frameRateDefaultKey) {
frameRate(60);
targetFramerate = 60;
} else if (keyCode == Options.toggleDeadzoneKey) {
camera.toggleDeadZone();
} else if (keyCode == Options.cameraToMouseKey) {
Expand Down
72 changes: 39 additions & 33 deletions src/main/java/project_16x16/scene/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import processing.core.PApplet;
import processing.core.PConstants;
Expand All @@ -15,7 +16,6 @@
import project_16x16.Audio.BGM;
import project_16x16.Constants;
import project_16x16.SideScroller.GameScenes;
import project_16x16.scene.PScene;
import project_16x16.ui.Button;

/**
Expand All @@ -41,7 +41,7 @@ public MainMenu(SideScroller a) {
background = game.createGraphics((int) game.gameResolution.x, (int) game.gameResolution.x);
background.noSmooth();
Particles.assignApplet(a);
Particles.populate(1000);
Particles.populate(1250);

pressStart = new Button(a);
pressMultiplayer = new Button(a);
Expand Down Expand Up @@ -78,11 +78,11 @@ public void switchTo() {
@Override
public void drawUI() {
game.fill(Constants.Colors.MENU_GREY, 40);
game.rectMode(CORNER);
game.noStroke();
game.rectMode(CORNER);
game.rect(0, 0, game.gameResolution.x, game.gameResolution.y);
Particles.run();
game.rectMode(CENTER);
Particles.run();

pressStart.manDisplay();
pressMultiplayer.manDisplay();
Expand Down Expand Up @@ -121,11 +121,11 @@ void mouseReleased(MouseEvent e) {
@Override
void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case 8 : // BACKSPACE
case PConstants.ESC : // Pause
case 8: // BACKSPACE
case PConstants.ESC: // Pause
game.returnScene();
break;
default :
default:
break;
}
}
Expand All @@ -140,14 +140,14 @@ private static class Particles {

private static SideScroller game;

private static ArrayList<Particle> particles;
private static List<Particle> particles;

private static int function = 0;
private static int centerX, centerY;
private static int scaleX, scaleY;

private static final float STEP = 0.05f;
private static final int TRANSITION_TIME = 360;
private static long timeAccumulator = 0;
private static final int TRANSITION_INTERVAL = 5000; // 5000 milliseconds = 5 seconds

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

static void populate(int n) {
for (int i = 0; i < n; i++) {
particles.add(new Particle(getXPos(game.random(0, game.gameResolution.x)),
getYPos(game.random(0, game.gameResolution.y)), (int) game.random(2, 8), Utility.colorToRGB(
(int) game.random(0, 50), (int) game.random(150, 255), (int) game.random(150, 255))));
float x = getXPos(game.random(0, game.gameResolution.x));
float y = getYPos(game.random(0, game.gameResolution.y));
int color = Utility.colorToRGB((int) game.random(0, 50), (int) game.random(150, 255),
(int) game.random(150, 255));
Particle p = new Particle(x, y, (int) game.random(2, 8), color);
particles.add(p);
}
}

static void run() {

if (game.frameCount % TRANSITION_TIME == 0) {
if (timeAccumulator >= TRANSITION_INTERVAL) {
function++;
function %= 12;
function %= 12; // cycle movement function
timeAccumulator %= TRANSITION_INTERVAL;
}

int repopulate = 0;
for (Iterator<Particle> iterator = particles.iterator(); iterator.hasNext();) {
Particle p = iterator.next();
float x = (float) getSlopeX(p.x, p.y);
float y = (float) getSlopeY(p.x, p.y);
p.x += p.direction * x * STEP;
p.y += p.direction * y * STEP;

x = getXPrint(p.x);
y = getYPrint(p.y);

if (game.frameCount - p.start > 1) {
game.stroke(p.color);
game.strokeWeight(p.size);
game.line(PApplet.lerp(x, p.lastX, 0.15f), PApplet.lerp(y, p.lastY, 0.15f), p.lastX, p.lastY);
}
p.update(3 / game.targetFramerate);
float x = getXPrint(p.x);
float y = getYPrint(p.y);

game.stroke(p.color);
game.strokeWeight(p.size);
game.line(PApplet.lerp(x, p.lastX, 0.15f), PApplet.lerp(y, p.lastY, 0.15f), p.lastX, p.lastY);

p.lastX = x;
p.lastY = y;
if (!Utility.withinRegion(p.lastX, p.lastY, -100, -100, game.gameResolution.x + 100,
Expand All @@ -198,6 +196,8 @@ static void run() {
}
}
populate(repopulate);

timeAccumulator += 1000 / game.frameRate;
}

private static double getSlopeX(float x, float y) {
Expand Down Expand Up @@ -263,17 +263,23 @@ static class Particle {
private final int size;
private final int color;
private final float direction;
private final int start;

public Particle(float x, float y, int size, int color) {
this.x = x;
this.y = y;
this.color = color;
this.size = size;
this.lastX = x;
this.lastY = y;
this.lastX = getXPrint(x);
this.lastY = getYPrint(y);
this.direction = (game.random(0.1f, 1) * (game.random(1) > 0.5f ? 1 : -1));
start = game.frameCount;

}

void update(float step) {
float xDelta = (float) getSlopeX(x, y);
float yDelta = (float) getSlopeY(x, y);
x += direction * xDelta * step;
y += direction * yDelta * step;
}
}
}
Expand Down

0 comments on commit 42f5fa2

Please sign in to comment.