add world interface
This commit is contained in:
@@ -5,12 +5,17 @@ import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.system.*;
|
||||
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.commands.GetPieceAtCommand;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Piece;
|
||||
import chess.view.DDDrender.world.Entity;
|
||||
import chess.view.DDDrender.world.World;
|
||||
import common.Signal1;
|
||||
|
||||
import java.nio.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.lwjgl.glfw.Callbacks.*;
|
||||
import static org.lwjgl.glfw.GLFW.*;
|
||||
@@ -24,13 +29,32 @@ public class Window {
|
||||
private long window;
|
||||
|
||||
private Renderer renderer;
|
||||
private Camera cam;
|
||||
private final CommandExecutor commandExecutor;
|
||||
private final Camera cam;
|
||||
private final World world;
|
||||
|
||||
public Window(CommandExecutor commandExecutor, Renderer renderer) {
|
||||
private final Queue<Runnable> tasks;
|
||||
private final List<Consumer<Float>> regularTasks;
|
||||
|
||||
public final Signal1<Coordinate> OnCellClick = new Signal1<>();
|
||||
|
||||
public Window(Renderer renderer, World world) {
|
||||
this.renderer = new Renderer();
|
||||
this.cam = new Camera();
|
||||
this.commandExecutor = commandExecutor;
|
||||
this.cam = world.getCamera();
|
||||
this.tasks = new ConcurrentLinkedDeque<>();
|
||||
this.world = world;
|
||||
this.regularTasks = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addRegularTask(Consumer<Float> task) {
|
||||
this.regularTasks.add(task);
|
||||
}
|
||||
|
||||
public synchronized void scheduleTask(Runnable runnable) {
|
||||
this.tasks.add(runnable);
|
||||
}
|
||||
|
||||
public synchronized Runnable getNextTask() {
|
||||
return this.tasks.poll();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
@@ -95,29 +119,27 @@ public class Window {
|
||||
}
|
||||
|
||||
private void render(float delta, float aspectRatio) {
|
||||
final float angle = 1f;
|
||||
cam.setRotateAngle(cam.getRotateAngle() + angle * delta);
|
||||
|
||||
cam.setAspectRatio(aspectRatio);
|
||||
renderer.Render(cam);
|
||||
renderPieces();
|
||||
renderer.Update(cam);
|
||||
renderer.RenderBoard();
|
||||
renderWorld();
|
||||
// renderPieces();
|
||||
}
|
||||
|
||||
private Piece pieceAt(Coordinate pos) {
|
||||
GetPieceAtCommand cmd = new GetPieceAtCommand(pos);
|
||||
this.commandExecutor.executeCommand(cmd);
|
||||
return cmd.getPiece();
|
||||
private void renderWorld() {
|
||||
for (Entity entity : this.world.getEntites()) {
|
||||
entity.render(this.renderer);
|
||||
}
|
||||
}
|
||||
|
||||
private void renderPieces() {
|
||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||
Coordinate pos = new Coordinate(i, j);
|
||||
Piece piece = pieceAt(pos);
|
||||
if (piece == null)
|
||||
continue;
|
||||
this.renderer.RenderPiece(pieceAt(pos), pos);
|
||||
}
|
||||
private void executeTasks(float delta) {
|
||||
Runnable task = getNextTask();
|
||||
while (task != null) {
|
||||
task.run();
|
||||
task = getNextTask();
|
||||
}
|
||||
for (Consumer<Float> consumer : regularTasks) {
|
||||
consumer.accept(delta);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,7 +176,8 @@ public class Window {
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
|
||||
|
||||
double currentTime = glfwGetTime();
|
||||
render((float) (currentTime - lastTime), (float) width[0] / (float) height[0]);
|
||||
float deltaTime = (float) (currentTime - lastTime);
|
||||
render(deltaTime, (float) width[0] / (float) height[0]);
|
||||
lastTime = glfwGetTime();
|
||||
|
||||
glfwSwapBuffers(window); // swap the color buffers
|
||||
@@ -163,6 +186,8 @@ public class Window {
|
||||
// invoked during this call.
|
||||
glfwPollEvents();
|
||||
|
||||
executeTasks(deltaTime);
|
||||
|
||||
glfwGetWindowSize(window, width, height);
|
||||
glViewport(0, 0, width[0], height[0]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user