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.GL20;
|
||||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||||
|
import java.util.ConcurrentModificationException;
|
||||||
import logic.Element;
|
import logic.Element;
|
||||||
import logic.World;
|
import logic.World;
|
||||||
|
|
||||||
@ -25,6 +26,10 @@ public class Game extends ApplicationAdapter {
|
|||||||
shaper = new ShapeRenderer();
|
shaper = new ShapeRenderer();
|
||||||
shaper.setAutoShapeType(true);
|
shaper.setAutoShapeType(true);
|
||||||
font = new BitmapFont();
|
font = new BitmapFont();
|
||||||
|
Thread worldThread = new Thread(world);
|
||||||
|
worldThread.setName("Worker");
|
||||||
|
worldThread.setPriority(Thread.MAX_PRIORITY);
|
||||||
|
worldThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -54,15 +59,19 @@ public class Game extends ApplicationAdapter {
|
|||||||
if (Gdx.input.isKeyJustPressed(Input.Keys.P)) {
|
if (Gdx.input.isKeyJustPressed(Input.Keys.P)) {
|
||||||
paused = !paused;
|
paused = !paused;
|
||||||
}
|
}
|
||||||
// Update
|
if (Gdx.input.isKeyJustPressed(Input.Keys.L)) {
|
||||||
if (!paused) {
|
if (world.getFpsLimit() == 60) {
|
||||||
world.update();
|
world.setFpsLimit(0);
|
||||||
|
} else {
|
||||||
|
world.setFpsLimit(60);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Draw
|
// Draw
|
||||||
Gdx.gl.glClearColor(0, 0, 0, 1);
|
Gdx.gl.glClearColor(0, 0, 0, 1);
|
||||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||||
shaper.setColor(1, 1, 1, 1);
|
shaper.setColor(1, 1, 1, 1);
|
||||||
shaper.begin(ShapeRenderer.ShapeType.Line);
|
shaper.begin(ShapeRenderer.ShapeType.Line);
|
||||||
|
try {
|
||||||
for (Element e : world.getElements()) {
|
for (Element e : world.getElements()) {
|
||||||
try {
|
try {
|
||||||
e.render(shaper);
|
e.render(shaper);
|
||||||
@ -71,6 +80,8 @@ public class Game extends ApplicationAdapter {
|
|||||||
//Log.log(Log.ERROR, ex+"");
|
//Log.log(Log.ERROR, ex+"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (ConcurrentModificationException ex) {
|
||||||
|
}
|
||||||
shaper.setColor(0.3f, 0.3f, 0.3f, 1);
|
shaper.setColor(0.3f, 0.3f, 0.3f, 1);
|
||||||
// draw borders
|
// draw borders
|
||||||
shaper.rect(0, 0, world.getWidth(), world.getHeight());
|
shaper.rect(0, 0, world.getWidth(), world.getHeight());
|
||||||
@ -84,4 +95,8 @@ public class Game extends ApplicationAdapter {
|
|||||||
public static Game get() {
|
public static Game get() {
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isPaused() {
|
||||||
|
return paused;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,7 @@ public class Creature extends Element {
|
|||||||
fov = (float) Math.PI / 2.5f;
|
fov = (float) Math.PI / 2.5f;
|
||||||
fitness = 0;
|
fitness = 0;
|
||||||
brain = new Brain(10, 5, 2, 10);
|
brain = new Brain(10, 5, 2, 10);
|
||||||
|
sights = new Sight[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -146,18 +147,18 @@ public class Creature extends Element {
|
|||||||
double relX = Math.cos(dir), relY = Math.sin(dir);
|
double relX = Math.cos(dir), relY = Math.sin(dir);
|
||||||
float c = 0;
|
float c = 0;
|
||||||
float eyeX = (float) (relX * getSize() * 0.6f), eyeY = (float) (relY * getSize() * 0.6f);
|
float eyeX = (float) (relX * getSize() * 0.6f), eyeY = (float) (relY * getSize() * 0.6f);
|
||||||
for (int i = 0; i < sights.length; i++) {
|
for (Sight sight : sights) {
|
||||||
if (sights[i] != null) {
|
if (sight != null) {
|
||||||
c = sights[i].getDistance() / sightRange * 2 + sightRange;
|
c = sight.getDistance() / sightRange * 2 + sightRange;
|
||||||
} else {
|
} else {
|
||||||
}
|
}
|
||||||
if (sights[i] != null) {
|
if (sight != null) {
|
||||||
if (sights[i].getElement() instanceof Creature) {
|
if (sight.getElement() instanceof Creature) {
|
||||||
s.setColor(c, 0, 0, 1);
|
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.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) {
|
if (sights[0] == null && sights[1] == null) {
|
||||||
|
@ -5,9 +5,11 @@
|
|||||||
*/
|
*/
|
||||||
package logic;
|
package logic;
|
||||||
|
|
||||||
|
import com.mygdx.game.Game;
|
||||||
import com.mygdx.game.Log;
|
import com.mygdx.game.Log;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -15,10 +17,11 @@ import java.util.logging.Logger;
|
|||||||
*
|
*
|
||||||
* @author fazo
|
* @author fazo
|
||||||
*/
|
*/
|
||||||
public class World {
|
public class World implements Runnable {
|
||||||
|
|
||||||
private final int width, height, nPlants, creatPerGen;
|
private final int width, height, nPlants, creatPerGen;
|
||||||
private int generation = 1;
|
private int generation = 1;
|
||||||
|
private float fpsLimit = 60;
|
||||||
public ArrayList<Element> elements;
|
public ArrayList<Element> elements;
|
||||||
public ArrayList<Element> toAdd;
|
public ArrayList<Element> toAdd;
|
||||||
public ArrayList<Creature> creatures;
|
public ArrayList<Creature> creatures;
|
||||||
@ -40,6 +43,36 @@ public class World {
|
|||||||
newGen(true);
|
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() {
|
public void update() {
|
||||||
for (Element e : toAdd) {
|
for (Element e : toAdd) {
|
||||||
elements.add(e);
|
elements.add(e);
|
||||||
@ -206,4 +239,12 @@ public class World {
|
|||||||
return plants;
|
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.height = 600;
|
||||||
config.width = 800;
|
config.width = 800;
|
||||||
config.resizable = false;
|
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);
|
new LwjglApplication(new Game(), config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user