mirror of
https://github.com/Stefan-5422/School-Programming-Projekt-022022.git
synced 2026-09-06 07:36:05 +02:00
Add building code; Fix some bugs
This commit is contained in:
@@ -12,7 +12,6 @@ public class TestBehaviour extends Component {
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
System.out.println(EntityRegistry.getEntity("test").toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.yes.yes.entities.machines;
|
||||
|
||||
import com.yes.yes.behaviours.TestBehaviour;
|
||||
import com.yes.yes.utils.Entity;
|
||||
import javafx.scene.shape.Circle;
|
||||
import javafx.scene.shape.Rectangle;
|
||||
|
||||
public class TestMachine2 extends Entity {
|
||||
|
||||
public TestMachine2() {
|
||||
super();
|
||||
this.getChildren().add(new Rectangle(25,25));
|
||||
|
||||
this.addBehaviour(new TestBehaviour(this));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.yes.yes.managers;
|
||||
|
||||
import com.yes.yes.entities.machines.TestMachine;
|
||||
import com.yes.yes.entities.machines.TestMachine2;
|
||||
import com.yes.yes.utils.Coordinate;
|
||||
import com.yes.yes.utils.EntityRegistry;
|
||||
import com.yes.yes.utils.RegistryEntry;
|
||||
@@ -24,9 +25,11 @@ public class GameManager {
|
||||
world = new World();
|
||||
|
||||
root.getChildren().add(world);
|
||||
world.toBack();
|
||||
|
||||
try {
|
||||
EntityRegistry.register(new RegistryEntry("test","test machine", TestMachine.class));
|
||||
EntityRegistry.register(new RegistryEntry("test2","test machine", TestMachine2.class));
|
||||
} catch (AlreadyExistsException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.yes.yes.managers;
|
||||
|
||||
import com.yes.yes.entities.machines.TestMachine;
|
||||
import com.yes.yes.ui.BuildBox;
|
||||
import com.yes.yes.utils.Coordinate;
|
||||
import com.yes.yes.utils.Entity;
|
||||
import com.yes.yes.utils.EntityRegistry;
|
||||
import com.yes.yes.world.Chunk;
|
||||
import com.yes.yes.world.World;
|
||||
import javafx.scene.Scene;
|
||||
@@ -10,6 +12,8 @@ import javafx.scene.input.KeyCodeCombination;
|
||||
import javafx.scene.input.KeyCombination;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class PlayerManager {
|
||||
|
||||
final KeyCombination leftKey = new KeyCodeCombination(KeyCode.A);
|
||||
@@ -18,16 +22,33 @@ public class PlayerManager {
|
||||
final KeyCombination downKey = new KeyCodeCombination(KeyCode.S);
|
||||
|
||||
World world;
|
||||
BuildBox buildBox;
|
||||
Coordinate chunkPos = new Coordinate(0, 0);
|
||||
|
||||
public PlayerManager(World world) {
|
||||
public PlayerManager(World world, BuildBox buildBox) {
|
||||
this.world = world;
|
||||
this.buildBox = buildBox;
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
Scene scene = world.getScene();
|
||||
scene.setOnKeyPressed(this::ProcessKeyPress);
|
||||
|
||||
world.setOnMouseClicked((e) ->
|
||||
{
|
||||
System.out.println("X:" + e.getX() + " Y:" + e.getY());
|
||||
Coordinate mouseCoordinate = new Coordinate((int)e.getX(), (int)e.getY());
|
||||
Coordinate chunkCoordinate = Coordinate.WorldToChunkCoordinate(mouseCoordinate);
|
||||
Coordinate blockCoordinate = Coordinate.WorldToChunkBlock(mouseCoordinate);
|
||||
System.out.println("Chunk: X:" + chunkCoordinate.x + " Y:" + chunkCoordinate.y);
|
||||
System.out.println("Block: X:" + blockCoordinate.x + " Y:" + blockCoordinate.y);
|
||||
|
||||
try {
|
||||
world.getChunk(chunkCoordinate).setEntity((Entity) EntityRegistry.getEntity(buildBox.getSelectedEntity()).getEntity().getConstructor().newInstance(),blockCoordinate);
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
);
|
||||
|
||||
world.setTranslateX(Integer.MAX_VALUE / -5000);
|
||||
world.setTranslateY(Integer.MAX_VALUE / -5000);
|
||||
}
|
||||
@@ -47,7 +68,7 @@ public class PlayerManager {
|
||||
world.setTranslateY(world.getTranslateY() - 45);
|
||||
}
|
||||
|
||||
double offset = +Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE;
|
||||
double offset = Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE;
|
||||
|
||||
|
||||
//NOTE: WHAT???
|
||||
|
||||
@@ -31,15 +31,15 @@ public class Coordinate {
|
||||
}
|
||||
|
||||
public static Coordinate WorldToChunkCoordinate(Coordinate worldPos) {
|
||||
int chunksize = Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE;
|
||||
int chunkSize = Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE;
|
||||
|
||||
return new Coordinate(worldPos.x / chunksize, worldPos.y / chunksize);
|
||||
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;
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.yes.yes.utils;
|
||||
|
||||
import com.yes.yes.utils.exceptions.AlreadyExistsException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public abstract class EntityRegistry {
|
||||
@@ -19,5 +20,8 @@ public abstract class EntityRegistry {
|
||||
return entities.get(name);
|
||||
}
|
||||
|
||||
public static ArrayList<String> getKeys() {
|
||||
return entities.keySet().stream().collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@ package com.yes.yes.world;
|
||||
|
||||
import com.yes.yes.utils.Coordinate;
|
||||
import com.yes.yes.utils.Entity;
|
||||
import javafx.geometry.HPos;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.geometry.VPos;
|
||||
import javafx.scene.layout.*;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.shape.Rectangle;
|
||||
@@ -34,8 +37,11 @@ public class Chunk extends GridPane {
|
||||
}
|
||||
|
||||
public void setEntity(Entity component, Coordinate pos) {
|
||||
removeEntity(pos);
|
||||
data[pos.x + pos.y * CHUNK_SIZE] = component;
|
||||
this.add(component, pos.x, pos.y);
|
||||
GridPane.setValignment(component, VPos.CENTER);
|
||||
GridPane.setHalignment(component, HPos.CENTER);
|
||||
}
|
||||
|
||||
public void removeEntity(Coordinate pos) {
|
||||
|
||||
Reference in New Issue
Block a user