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
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
+1 -1
View File
@@ -60,7 +60,7 @@
<!-- Default configuration for running with: mvn clean javafx:run --> <!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id> <id>default-cli</id>
<configuration> <configuration>
<mainClass>com.yes.yes/com.yes.yes.HelloApplication</mainClass> <mainClass>com.yes.yes/com.yes.yes.YesApplication</mainClass>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>
@@ -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; package com.yes.yes;
import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.IOException; import java.io.IOException;
public class HelloApplication extends Application { public class YesApplication extends javafx.application.Application {
@Override @Override
public void start(Stage stage) throws IOException { 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); Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!"); stage.setTitle("Hello!");
stage.setScene(scene); 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);
}
}
@@ -6,11 +6,5 @@
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml" <VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml"
fx:controller="com.yes.yes.HelloController"> fx:controller="com.yes.yes.MainController" fx:id="root">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</padding>
<Label fx:id="welcomeText"/>
<Button text="Hello!" onAction="#onHelloButtonClick"/>
</VBox> </VBox>
+10
View File
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml"
fx:controller="com.yes.yes.MainController" fx:id="root">
</VBox>
+23 -4
View File
@@ -1,9 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4"> <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_17">
<exclude-output /> <output url="file://$MODULE_DIR$/target/classes" />
<content url="file://$MODULE_DIR$" /> <output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-controls:17-ea+11" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-controls:win:17-ea+11" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:17-ea+11" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:win:17-ea+11" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-base:17-ea+11" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-base:win:17-ea+11" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:17-ea+11" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:win:17-ea+11" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.7.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.7.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.7.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.7.1" level="project" />
</component> </component>
</module> </module>