hibiscus/engine/source/core/engine.d

62 lines
1.3 KiB
D
Raw Normal View History

2023-05-20 18:37:04 +00:00
module gameengine.core.engine;
2023-05-20 21:27:42 +00:00
import gameengine.graphics.drawable;
import gameengine.renderer.renderer;
2023-05-20 18:37:04 +00:00
import gameengine.node.node;
2023-05-20 21:27:42 +00:00
import std.datetime.stopwatch;
import std.stdio;
import std.format;
2023-05-20 18:37:04 +00:00
class Engine {
2023-05-20 21:27:42 +00:00
this(Renderer renderer) {
2023-05-20 18:37:04 +00:00
this.renderer = renderer;
2023-05-20 21:27:42 +00:00
previousProcessTick = 0.00001f;
previousPhysicsProcessTick = 0.00001f;
2023-05-20 18:37:04 +00:00
2023-05-20 21:27:42 +00:00
watch.start();
}
2023-05-20 18:37:04 +00:00
2023-05-20 21:27:42 +00:00
~this() {
watch.stop();
}
2023-05-20 18:37:04 +00:00
2023-05-20 21:27:42 +00:00
void runNodeProcesses() {
// TODO: fix delta then adapt for runNodePhysicsProcesses
2023-05-21 20:55:02 +00:00
const float current = watch.peek.total!"msecs" / 1_000;
2023-05-20 21:27:42 +00:00
float delta = (current - previousProcessTick);
2023-05-20 18:37:04 +00:00
foreach (Node* node ; nodeList) {
node.process(delta);
}
2023-05-20 21:27:42 +00:00
this.previousProcessTick = watch.peek.total!"msecs" / 1_000;
2023-05-20 18:37:04 +00:00
}
void runNodePhysicsProcesses() {
// Schade!
2023-05-21 20:55:02 +00:00
const float current = watch.peek.total!"msecs" / 1_000;
2023-05-20 21:27:42 +00:00
float delta = (current - previousProcessTick);
foreach (Node* node ; nodeList) {
node.physicsProcess(delta);
}
this.previousPhysicsProcessTick = watch.peek.total!"msecs" / 1_000;
2023-05-20 18:37:04 +00:00
}
2023-05-20 21:27:42 +00:00
void drawNodes() {
foreach (Node* node; nodeList) {
if (Drawable* drawNode = cast(Drawable*) node) {
drawNode.draw(&renderer);
}
}
}
2023-05-20 18:37:04 +00:00
2023-05-20 21:27:42 +00:00
private Renderer renderer;
private StopWatch watch;
private float previousProcessTick;
private float previousPhysicsProcessTick;
2023-05-20 18:37:04 +00:00
private Node*[] nodeList;
}