mirror of
https://github.com/fazo96/AIrium.git
synced 2025-01-10 09:34:20 +01:00
reimplemented frame limiter
This commit is contained in:
parent
b7dd099fdb
commit
6ee54d8d8f
@ -6,6 +6,7 @@ import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import logic.Element;
|
||||
import logic.World;
|
||||
|
||||
@ -25,6 +26,10 @@ public class Game extends ApplicationAdapter {
|
||||
shaper = new ShapeRenderer();
|
||||
shaper.setAutoShapeType(true);
|
||||
font = new BitmapFont();
|
||||
Thread worldThread = new Thread(world);
|
||||
worldThread.setName("Worker");
|
||||
worldThread.setPriority(Thread.MAX_PRIORITY);
|
||||
worldThread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -54,15 +59,19 @@ public class Game extends ApplicationAdapter {
|
||||
if (Gdx.input.isKeyJustPressed(Input.Keys.P)) {
|
||||
paused = !paused;
|
||||
}
|
||||
// Update
|
||||
if (!paused) {
|
||||
world.update();
|
||||
if (Gdx.input.isKeyJustPressed(Input.Keys.L)) {
|
||||
if (world.getFpsLimit() == 60) {
|
||||
world.setFpsLimit(0);
|
||||
} else {
|
||||
world.setFpsLimit(60);
|
||||
}
|
||||
}
|
||||
// Draw
|
||||
Gdx.gl.glClearColor(0, 0, 0, 1);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
shaper.setColor(1, 1, 1, 1);
|
||||
shaper.begin(ShapeRenderer.ShapeType.Line);
|
||||
try {
|
||||
for (Element e : world.getElements()) {
|
||||
try {
|
||||
e.render(shaper);
|
||||
@ -71,6 +80,8 @@ public class Game extends ApplicationAdapter {
|
||||
//Log.log(Log.ERROR, ex+"");
|
||||
}
|
||||
}
|
||||
} catch (ConcurrentModificationException ex) {
|
||||
}
|
||||
shaper.setColor(0.3f, 0.3f, 0.3f, 1);
|
||||
// draw borders
|
||||
shaper.rect(0, 0, world.getWidth(), world.getHeight());
|
||||
@ -84,4 +95,8 @@ public class Game extends ApplicationAdapter {
|
||||
public static Game get() {
|
||||
return game;
|
||||
}
|
||||
|
||||
public boolean isPaused() {
|
||||
return paused;
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ public class Creature extends Element {
|
||||
fov = (float) Math.PI / 2.5f;
|
||||
fitness = 0;
|
||||
brain = new Brain(10, 5, 2, 10);
|
||||
sights = new Sight[2];
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -146,18 +147,18 @@ public class Creature extends Element {
|
||||
double relX = Math.cos(dir), relY = Math.sin(dir);
|
||||
float c = 0;
|
||||
float eyeX = (float) (relX * getSize() * 0.6f), eyeY = (float) (relY * getSize() * 0.6f);
|
||||
for (int i = 0; i < sights.length; i++) {
|
||||
if (sights[i] != null) {
|
||||
c = sights[i].getDistance() / sightRange * 2 + sightRange;
|
||||
for (Sight sight : sights) {
|
||||
if (sight != null) {
|
||||
c = sight.getDistance() / sightRange * 2 + sightRange;
|
||||
} else {
|
||||
}
|
||||
if (sights[i] != null) {
|
||||
if (sights[i].getElement() instanceof Creature) {
|
||||
if (sight != null) {
|
||||
if (sight.getElement() instanceof Creature) {
|
||||
s.setColor(c, 0, 0, 1);
|
||||
} else if (sights[i].getElement() instanceof Vegetable) {
|
||||
} else if (sight.getElement() instanceof Vegetable) {
|
||||
s.setColor(0, c, 0, 1);
|
||||
}
|
||||
s.line(eyeX + getX(), getY() + eyeY, sights[i].getElement().getX(), sights[i].getElement().getY());
|
||||
s.line(eyeX + getX(), getY() + eyeY, sight.getElement().getX(), sight.getElement().getY());
|
||||
}
|
||||
}
|
||||
if (sights[0] == null && sights[1] == null) {
|
||||
|
@ -5,9 +5,11 @@
|
||||
*/
|
||||
package logic;
|
||||
|
||||
import com.mygdx.game.Game;
|
||||
import com.mygdx.game.Log;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -15,10 +17,11 @@ import java.util.logging.Logger;
|
||||
*
|
||||
* @author fazo
|
||||
*/
|
||||
public class World {
|
||||
public class World implements Runnable {
|
||||
|
||||
private final int width, height, nPlants, creatPerGen;
|
||||
private int generation = 1;
|
||||
private float fpsLimit = 60;
|
||||
public ArrayList<Element> elements;
|
||||
public ArrayList<Element> toAdd;
|
||||
public ArrayList<Creature> creatures;
|
||||
@ -40,6 +43,36 @@ public class World {
|
||||
newGen(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Date d;
|
||||
long time;
|
||||
float target;
|
||||
for (;;) {
|
||||
if (!Game.get().isPaused()) {
|
||||
d = new Date();
|
||||
update();
|
||||
if (fpsLimit > 0) {
|
||||
time = new Date().getTime() - d.getTime();
|
||||
target = 1000 / fpsLimit;
|
||||
if (time < target) {
|
||||
try {
|
||||
Thread.sleep((long) (target - time));
|
||||
} catch (InterruptedException ex) {
|
||||
Logger.getLogger(World.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException ex) {
|
||||
Logger.getLogger(World.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void update() {
|
||||
for (Element e : toAdd) {
|
||||
elements.add(e);
|
||||
@ -81,7 +114,7 @@ public class World {
|
||||
};
|
||||
if (graveyard.isEmpty() || restart) { // First gen
|
||||
generation = 1;
|
||||
Log.log(Log.INFO, "Starting from generation 1: spawning "+creatPerGen+" creatures.");
|
||||
Log.log(Log.INFO, "Starting from generation 1: spawning " + creatPerGen + " creatures.");
|
||||
for (int i = 0; i < creatPerGen; i++) {
|
||||
spawnCreature();
|
||||
}
|
||||
@ -206,4 +239,12 @@ public class World {
|
||||
return plants;
|
||||
}
|
||||
|
||||
public float getFpsLimit() {
|
||||
return fpsLimit;
|
||||
}
|
||||
|
||||
public void setFpsLimit(float fpsLimit) {
|
||||
this.fpsLimit = fpsLimit;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,9 +12,6 @@ public class DesktopLauncher {
|
||||
config.height = 600;
|
||||
config.width = 800;
|
||||
config.resizable = false;
|
||||
config.vSyncEnabled = false; // Setting to false disables vertical sync
|
||||
config.foregroundFPS = 60; // Setting to 0 disables foreground fps throttling
|
||||
config.backgroundFPS = 0; // Setting to 0 disables background fps throttling
|
||||
new LwjglApplication(new Game(), config);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user