Added basic world and chunk code

-Basic chunk code
-Basic world code
This commit is contained in:
Stefan-5422
2022-02-11 11:30:35 +01:00
parent 3996d6383b
commit a4f5310aa6
13 changed files with 176 additions and 29 deletions
@@ -1,14 +0,0 @@
package com.yes.yes;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class HelloController {
@FXML
private Label welcomeText;
@FXML
protected void onHelloButtonClick() {
welcomeText.setText("Welcome to JavaFX Application!");
}
}
@@ -0,0 +1,17 @@
package com.yes.yes;
import com.yes.yes.world.World;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
public class MainController {
@FXML
VBox root;
public void initialize() {
World chunk = new World();
root.getChildren().add(chunk);
}
}
@@ -1,16 +1,15 @@
package com.yes.yes;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
public class YesApplication extends javafx.application.Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
FXMLLoader fxmlLoader = new FXMLLoader(YesApplication.class.getResource("main-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
@@ -0,0 +1,4 @@
package com.yes.yes.utils;
public abstract class Component extends javafx.scene.shape.Shape {
}
@@ -0,0 +1,27 @@
package com.yes.yes.utils;
import java.util.Objects;
public class Coordinate {
public int x;
public int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public Coordinate() {}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Coordinate)) return false;
Coordinate that = (Coordinate) o;
return x == that.x && y == that.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
@@ -0,0 +1,4 @@
package com.yes.yes.utils;
public abstract class Entity {
}
@@ -0,0 +1,50 @@
package com.yes.yes.world;
import com.yes.yes.utils.Component;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Chunk extends GridPane {
private static final int CHUNK_SIZE = 16;
private static final double COMPONENT_SIZE = 50;
private final Component[] data = new Component[CHUNK_SIZE * CHUNK_SIZE];
public Chunk() {
super();
this.setMaxSize(CHUNK_SIZE * COMPONENT_SIZE, CHUNK_SIZE * COMPONENT_SIZE);
this.setMinSize(CHUNK_SIZE * COMPONENT_SIZE, CHUNK_SIZE * COMPONENT_SIZE);
for (int i = 0; i < CHUNK_SIZE; i++) {
for (int j = 0; j < CHUNK_SIZE; j++) {
this.getColumnConstraints().add(new ColumnConstraints(COMPONENT_SIZE));
this.getRowConstraints().add(new RowConstraints(COMPONENT_SIZE));
Rectangle rect = new Rectangle(COMPONENT_SIZE, COMPONENT_SIZE);
rect.setFill(Color.color((i+j*16)/257d,(i+j*16)/257d, (i+j*16)/257d,1)); //DEBUG CODE
rect.setStroke(Color.BLACK);
rect.strokeWidthProperty().set(0.5);
this.add(rect, i, j);
}
}
}
public void setComponent(Component component, int x, int y) {
data[x + y * CHUNK_SIZE] = component;
this.add(component, x, y);
}
public void removeComponent(int x, int y) {
this.getChildren().remove(data[x + y * CHUNK_SIZE]);
data[x + y * CHUNK_SIZE] = null;
}
public Component getComponent(int x, int y) {
return data[x + y * CHUNK_SIZE];
}
}
@@ -0,0 +1,30 @@
package com.yes.yes.world;
import com.yes.yes.utils.Coordinate;
import javafx.scene.layout.GridPane;
import java.util.HashMap;
public class World extends GridPane {
private final HashMap<Coordinate,Chunk> chunks = new HashMap<>();
public World() {
super();
for (int x = 0; x < 4; x++) {//DEBUG CODE
for (int y = 0; y < 4; y++) {
chunks.put(new Coordinate(x, y), new Chunk());
this.add(chunks.get(new Coordinate(x, y)), x, y);
}
}
this.unload(new Coordinate(2, 2)); //DEBUG CODE
}
public void unload(Coordinate pos) {
this.getChildren().remove(chunks.get(pos));
}
public void load(Coordinate pos) {
this.add(chunks.get(pos), pos.x, pos.y);
}
}