mirror of
https://github.com/Stefan-5422/School-Programming-Projekt-022022.git
synced 2026-09-04 08:56:01 +02:00
Added Chunk Loading and player movement code
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package com.yes.yes;
|
package com.yes.yes;
|
||||||
|
|
||||||
import com.yes.yes.managers.GameManager;
|
import com.yes.yes.managers.GameManager;
|
||||||
|
import com.yes.yes.managers.PlayerManager;
|
||||||
|
import com.yes.yes.utils.Coordinate;
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
|
|
||||||
@@ -12,5 +15,11 @@ public class MainController {
|
|||||||
public void initialize() {
|
public void initialize() {
|
||||||
GameManager gameManager = new GameManager(root);
|
GameManager gameManager = new GameManager(root);
|
||||||
gameManager.setup();
|
gameManager.setup();
|
||||||
|
|
||||||
|
Platform.runLater(() -> {
|
||||||
|
PlayerManager playerManager = new PlayerManager(gameManager.getWorld());
|
||||||
|
playerManager.setup();
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.yes.yes;
|
|||||||
|
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
|
import javafx.scene.transform.Scale;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -13,6 +14,11 @@ public class YesApplication extends javafx.application.Application {
|
|||||||
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
|
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
|
||||||
stage.setTitle("Hello!");
|
stage.setTitle("Hello!");
|
||||||
stage.setScene(scene);
|
stage.setScene(scene);
|
||||||
|
|
||||||
|
//Scale scale = new Scale(0.25,0.25, 0, 0);
|
||||||
|
//scene.getRoot().getTransforms().add(scale);
|
||||||
|
|
||||||
|
stage.setMaximized(true);
|
||||||
stage.show();
|
stage.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.yes.yes.entities.machines;
|
package com.yes.yes.entities.machines;
|
||||||
|
|
||||||
|
import com.yes.yes.utils.Coordinate;
|
||||||
import com.yes.yes.utils.Entity;
|
import com.yes.yes.utils.Entity;
|
||||||
import javafx.scene.shape.Circle;
|
import javafx.scene.shape.Circle;
|
||||||
|
|
||||||
@@ -7,8 +8,6 @@ public class TestMachine extends Entity {
|
|||||||
|
|
||||||
public TestMachine() {
|
public TestMachine() {
|
||||||
super();
|
super();
|
||||||
Circle shape = new Circle(25);
|
this.getChildren().add(new Circle(25));
|
||||||
shape.setStyle("-fx-fill: #ff0000");
|
|
||||||
this.getChildren().add(shape);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,17 +7,19 @@ import javafx.scene.layout.VBox;
|
|||||||
|
|
||||||
public class GameManager {
|
public class GameManager {
|
||||||
VBox root;
|
VBox root;
|
||||||
|
World world;
|
||||||
|
|
||||||
|
public World getWorld() {
|
||||||
|
return world;
|
||||||
|
}
|
||||||
|
|
||||||
public GameManager(VBox root) {
|
public GameManager(VBox root) {
|
||||||
this.root = root;
|
this.root = root;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setup() {
|
public void setup() {
|
||||||
World world = new World();
|
world = new World();
|
||||||
|
|
||||||
root.getChildren().add(world);
|
root.getChildren().add(world);
|
||||||
|
|
||||||
world.getChunk(new Coordinate(1, 1)).setEntity(new TestMachine(), new Coordinate(1, 1));
|
|
||||||
world.getChunk(new Coordinate(1, 1)).setEntity(new TestMachine(), new Coordinate(1, 2));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,94 @@
|
|||||||
package com.yes.yes.managers;
|
package com.yes.yes.managers;
|
||||||
|
|
||||||
|
import com.yes.yes.utils.Coordinate;
|
||||||
|
import com.yes.yes.world.Chunk;
|
||||||
|
import com.yes.yes.world.World;
|
||||||
|
import javafx.scene.Scene;
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
import javafx.scene.input.KeyCodeCombination;
|
||||||
|
import javafx.scene.input.KeyCombination;
|
||||||
|
import javafx.scene.input.KeyEvent;
|
||||||
|
|
||||||
public class PlayerManager {
|
public class PlayerManager {
|
||||||
|
|
||||||
|
final KeyCombination leftKey = new KeyCodeCombination(KeyCode.A);
|
||||||
|
final KeyCombination rightKey = new KeyCodeCombination(KeyCode.D);
|
||||||
|
final KeyCombination upKey = new KeyCodeCombination(KeyCode.W);
|
||||||
|
final KeyCombination downKey = new KeyCodeCombination(KeyCode.S);
|
||||||
|
|
||||||
|
World world;
|
||||||
|
Coordinate chunkPos = new Coordinate(0, 0);
|
||||||
|
|
||||||
|
public PlayerManager(World world) {
|
||||||
|
this.world = world;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setup() {
|
||||||
|
Scene scene = world.getScene();
|
||||||
|
scene.setOnKeyPressed(this::ProcessKeyPress);
|
||||||
|
|
||||||
|
world.setTranslateX(Integer.MAX_VALUE / -5000);
|
||||||
|
world.setTranslateY(Integer.MAX_VALUE / -5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProcessKeyPress(KeyEvent key) {
|
||||||
|
if (leftKey.match(key)) {
|
||||||
|
world.setTranslateX(world.getTranslateX() + 45);
|
||||||
|
}
|
||||||
|
if (rightKey.match(key)) {
|
||||||
|
|
||||||
|
world.setTranslateX(world.getTranslateX() - 45);
|
||||||
|
}
|
||||||
|
if (upKey.match(key)) {
|
||||||
|
world.setTranslateY(world.getTranslateY() + 45);
|
||||||
|
}
|
||||||
|
if (downKey.match(key)) {
|
||||||
|
world.setTranslateY(world.getTranslateY() - 45);
|
||||||
|
}
|
||||||
|
|
||||||
|
double offset = +Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE;
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("World pos: X: " + (-world.getTranslateX() + offset) + "; Y: " + (-world.getTranslateY() + offset));
|
||||||
|
|
||||||
|
//NOTE: WHAT???
|
||||||
|
|
||||||
|
|
||||||
|
int chunkX = -(int) Math.floor((world.getTranslateX() + offset) / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE);
|
||||||
|
int chunkY = -(int) Math.floor((world.getTranslateY() + offset) / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE);
|
||||||
|
System.out.println("Chunk pos: X: " + chunkX + "; Y: " + chunkY);
|
||||||
|
|
||||||
|
if (chunkX < chunkPos.x) {
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
world.load(new Coordinate(chunkX, chunkY + i));
|
||||||
|
world.unload(new Coordinate(chunkPos.x + 5, chunkY + i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chunkX > chunkPos.x) {
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
world.load(new Coordinate(chunkX + 5, chunkY + i));
|
||||||
|
world.unload(new Coordinate(chunkPos.x, chunkY + i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chunkY > chunkPos.y) {
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
world.load(new Coordinate(chunkX + i, chunkY + 5));
|
||||||
|
world.unload(new Coordinate(chunkX + i, chunkPos.y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chunkY < chunkPos.y) {
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
world.load(new Coordinate(chunkX + i, chunkY));
|
||||||
|
world.unload(new Coordinate(chunkX + i, chunkPos.y + 5));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
chunkPos = new Coordinate(chunkX, chunkY);
|
||||||
|
|
||||||
|
//world.load(new Coordinate(chunkX, chunkY));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.yes.yes.utils;
|
package com.yes.yes.utils;
|
||||||
|
|
||||||
|
import com.yes.yes.world.Chunk;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class Coordinate {
|
public class Coordinate {
|
||||||
@@ -14,6 +16,7 @@ public class Coordinate {
|
|||||||
public Coordinate() {
|
public Coordinate() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
@@ -26,4 +29,18 @@ public class Coordinate {
|
|||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(x, y);
|
return Objects.hash(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Coordinate WorldToChunkCoordinate(Coordinate worldPos) {
|
||||||
|
int chunksize = Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE;
|
||||||
|
|
||||||
|
return new Coordinate(worldPos.x / chunksize, worldPos.y / chunksize);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Coordinate WorldToChunkBlock(Coordinate worldPos) {
|
||||||
|
int chunksize = Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE;
|
||||||
|
worldPos.x = worldPos.x % chunksize;
|
||||||
|
worldPos.y = worldPos.y % chunksize;
|
||||||
|
|
||||||
|
return new Coordinate(worldPos.x / Chunk.ENTITY_SIZE, worldPos.y / Chunk.ENTITY_SIZE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.yes.yes.utils;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Size {
|
||||||
|
public int x;
|
||||||
|
public int y;
|
||||||
|
|
||||||
|
public Size(int x, int y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Size() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof Size that)) return false;
|
||||||
|
return x == that.x && y == that.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.yes.yes.utils.exceptions;
|
||||||
|
|
||||||
|
public class ChunkAlreadyExistsException extends Exception {
|
||||||
|
public ChunkAlreadyExistsException() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChunkAlreadyExistsException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChunkAlreadyExistsException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChunkAlreadyExistsException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChunkAlreadyExistsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||||
|
super(message, cause, enableSuppression, writableStackTrace);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,6 @@
|
|||||||
<?import javafx.scene.layout.VBox?>
|
<?import javafx.scene.layout.VBox?>
|
||||||
|
|
||||||
<?import javafx.scene.control.Button?>
|
<?import javafx.scene.control.Button?>
|
||||||
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml"
|
<VBox spacing="20.0" xmlns:fx="http://javafx.com/fxml"
|
||||||
fx:controller="com.yes.yes.MainController" fx:id="root">
|
fx:controller="com.yes.yes.MainController" fx:id="root">
|
||||||
</VBox>
|
</VBox>
|
||||||
|
|||||||
Reference in New Issue
Block a user