30 lines
1016 B
Java
30 lines
1016 B
Java
package com.projectsacrifice.game;
|
|
|
|
public final class GameMath {
|
|
private GameMath() {}
|
|
|
|
public static float clamp(float value, float min, float max) {
|
|
return Math.max(min, Math.min(max, value));
|
|
}
|
|
|
|
public static boolean intersects(
|
|
float ax, float ay, float aw, float ah,
|
|
float bx, float by, float bw, float bh
|
|
) {
|
|
return ax < bx + bw && ax + aw > bx && ay < by + bh && ay + ah > by;
|
|
}
|
|
|
|
public static float pingPong(float elapsedSeconds, float periodSeconds) {
|
|
if (periodSeconds <= 0f) return 0f;
|
|
double phase = (elapsedSeconds % periodSeconds) / periodSeconds;
|
|
return (float) ((1d - Math.cos(phase * Math.PI * 2d)) * 0.5d);
|
|
}
|
|
|
|
public static String formatTime(long elapsedMs) {
|
|
long totalSeconds = Math.max(0L, elapsedMs) / 1000L;
|
|
long minutes = totalSeconds / 60L;
|
|
long seconds = totalSeconds % 60L;
|
|
return String.format(java.util.Locale.US, "%02d:%02d", minutes, seconds);
|
|
}
|
|
}
|