mirror of
https://github.com/fazo96/AIrium.git
synced 2025-04-19 01:28:38 +02:00
unhooked rendering from logic
This commit is contained in:
parent
4638bdab3e
commit
ace1c3434a
@ -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,8 +26,8 @@ public class Game extends ApplicationAdapter {
|
||||
shaper = new ShapeRenderer();
|
||||
shaper.setAutoShapeType(true);
|
||||
font = new BitmapFont();
|
||||
new Thread(world).start();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
@ -55,22 +56,21 @@ public class Game extends ApplicationAdapter {
|
||||
if (Gdx.input.isKeyJustPressed(Input.Keys.P)) {
|
||||
paused = !paused;
|
||||
}
|
||||
// Update
|
||||
if (!paused && world.isBusy()) {
|
||||
world.update();
|
||||
}
|
||||
// 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);
|
||||
for (Element e : world.getElements()) {
|
||||
try {
|
||||
e.render(shaper);
|
||||
} catch (ArrayIndexOutOfBoundsException ex) {
|
||||
try {
|
||||
for (Element e : world.getElements()) {
|
||||
try {
|
||||
e.render(shaper);
|
||||
} catch (ArrayIndexOutOfBoundsException ex) {
|
||||
// No idea why it happens, but it's rendering so meh
|
||||
//Log.log(Log.ERROR, ex+"");
|
||||
//Log.log(Log.ERROR, ex+"");
|
||||
}
|
||||
}
|
||||
} catch (ConcurrentModificationException e) {
|
||||
}
|
||||
shaper.setColor(0.3f, 0.3f, 0.3f, 1);
|
||||
// draw borders
|
||||
@ -85,4 +85,8 @@ public class Game extends ApplicationAdapter {
|
||||
public static Game get() {
|
||||
return game;
|
||||
}
|
||||
|
||||
public boolean isPaused() {
|
||||
return paused;
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
*/
|
||||
package logic;
|
||||
|
||||
import com.mygdx.game.Game;
|
||||
import com.mygdx.game.Log;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
@ -15,7 +16,7 @@ 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;
|
||||
@ -63,9 +64,25 @@ public class World {
|
||||
newGen(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (;;) {
|
||||
// Add speed limiter here
|
||||
if (!Game.get().isPaused()) {
|
||||
update();
|
||||
} else {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException ex) {
|
||||
Logger.getLogger(World.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void update() {
|
||||
busy = true;
|
||||
Log.log(Log.DEBUG,"Started.");
|
||||
Log.log(Log.DEBUG, "Started.");
|
||||
for (Element e : toAdd) {
|
||||
elements.add(e);
|
||||
if (e instanceof Creature) {
|
||||
@ -82,7 +99,7 @@ public class World {
|
||||
creatures.removeAll(graveyard);
|
||||
if (creatures.isEmpty()) {
|
||||
// All dead, next gen
|
||||
Log.log(Log.DEBUG,"Newgen.");
|
||||
Log.log(Log.DEBUG, "Newgen.");
|
||||
newGen(false);
|
||||
}
|
||||
while (plants.size() < nPlants) {
|
||||
@ -93,7 +110,7 @@ public class World {
|
||||
}
|
||||
workerDone = false;
|
||||
busy = false;
|
||||
Log.log(Log.DEBUG,"Launching Creature workers");
|
||||
Log.log(Log.DEBUG, "Launching Creature workers");
|
||||
int readyCount = 0;
|
||||
Thread.yield();
|
||||
do {
|
||||
@ -103,9 +120,9 @@ public class World {
|
||||
readyCount++;
|
||||
}
|
||||
}
|
||||
Log.log(Log.DEBUG,"DoneCount: " + readyCount + " out of " + creatures.size());
|
||||
Log.log(Log.DEBUG, "DoneCount: " + readyCount + " out of " + creatures.size());
|
||||
} while (readyCount < creatures.size());
|
||||
Log.log(Log.DEBUG,"Done creatures, awaiting veg worker...");
|
||||
Log.log(Log.DEBUG, "Done creatures, awaiting veg worker...");
|
||||
while (!workerDone) {
|
||||
Thread.yield();
|
||||
/*try {
|
||||
@ -115,11 +132,11 @@ public class World {
|
||||
}*/
|
||||
}
|
||||
busy = true;
|
||||
Log.log(Log.DEBUG,"Paused workers. Applying modifications to world... ");
|
||||
Log.log(Log.DEBUG, "Paused workers. Applying modifications to world... ");
|
||||
for (Creature c : creatures) {
|
||||
c.applyToWorld();
|
||||
}
|
||||
Log.log(Log.DEBUG,"Done.");
|
||||
Log.log(Log.DEBUG, "Done.");
|
||||
}
|
||||
|
||||
public void newGen(boolean restart) {
|
||||
|
@ -11,10 +11,7 @@ public class DesktopLauncher {
|
||||
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
|
||||
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
|
||||
config.resizable = false; // add resizeability to game
|
||||
new LwjglApplication(new Game(), config);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user