Add tie in off global event system; Add test offerer and reciever

This commit is contained in:
Stefan-5422
2022-04-21 12:56:23 +02:00
parent f4d884753c
commit 0dd786f812
9 changed files with 66 additions and 12 deletions
@@ -3,7 +3,9 @@ package com.yes.yes;
import com.yes.yes.managers.GameManager;
import com.yes.yes.managers.PlayerManager;
import com.yes.yes.managers.UiManager;
import com.yes.yes.utils.GlobalEventHandler;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.layout.VBox;
@@ -1,5 +1,8 @@
package com.yes.yes;
import com.yes.yes.utils.GlobalEventHandler;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
@@ -11,6 +14,12 @@ public class YesApplication extends javafx.application.Application {
launch();
}
@Override
public void stop() {
System.out.println("closing bro");
GlobalEventHandler.trigger("closing",null);
}
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(YesApplication.class.getResource("main-view.fxml"));
@@ -22,7 +22,7 @@ public class TestBehaviour extends Component {
@Override
public void update() {
System.out.println("Updating " + parent.getClass().getSimpleName());
/*System.out.println("Updating " + parent.getClass().getSimpleName());
for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) {
try {
@@ -36,6 +36,6 @@ public class TestBehaviour extends Component {
ex.printStackTrace();
}
}
}
}*/
}
}
@@ -1,13 +1,10 @@
package com.yes.yes.behaviours;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Component;
import com.yes.yes.utils.Coordinate;
import com.yes.yes.utils.Entity;
import com.yes.yes.utils.*;
public class TestOffererBehaviour extends Component {
private Entity reciever;
private Entity receiver;
public TestOffererBehaviour(Entity entity, BlockContainer blockContainer) {
super(entity, blockContainer);
@@ -16,7 +13,7 @@ public class TestOffererBehaviour extends Component {
private void placed(Entity entity) {
try {
if (blockContainer.getBlock(new Coordinate(1, 0)) == entity) {
this.reciever = entity;
this.receiver = entity;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
@@ -26,7 +23,7 @@ public class TestOffererBehaviour extends Component {
@Override
public void initialize() {
try {
this.reciever = blockContainer.getBlock(new Coordinate(1, 0));
this.receiver = blockContainer.getBlock(new Coordinate(1, 0));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
@@ -36,8 +33,10 @@ public class TestOffererBehaviour extends Component {
@Override
public void update() {
if(reciever == null) return;
if(receiver == null) return;
reciever.trigger("offerItem", someItem);
Item someItem = new Item();
receiver.trigger("offerItem", someItem);
}
}
@@ -0,0 +1,25 @@
package com.yes.yes.behaviours;
import com.yes.yes.utils.*;
public class TestRecieveBeahaviour extends Component {
public TestRecieveBeahaviour(Entity entity, BlockContainer blockContainer) {
super(entity, blockContainer);
}
@Override
public void initialize() {
this.parent.addListener("offerItem", this::recieve);
}
void recieve(Item item) {
System.out.println("Received Item");
}
@Override
public void update() {
}
}
@@ -3,6 +3,7 @@ package com.yes.yes.entities.machines;
import com.yes.yes.behaviours.ItemBehaviour;
import com.yes.yes.behaviours.PlaceBehaviour;
import com.yes.yes.behaviours.TestBehaviour;
import com.yes.yes.behaviours.TestRecieveBeahaviour;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Entity;
import javafx.scene.shape.Circle;
@@ -21,5 +22,6 @@ public class TestMachine extends Entity {
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new TestBehaviour(this, blockContainer));
this.addBehaviour(new ItemBehaviour(this, blockContainer));
this.addBehaviour(new TestRecieveBeahaviour(this, blockContainer));
}
}
@@ -3,6 +3,7 @@ package com.yes.yes.entities.machines;
import com.yes.yes.behaviours.ItemBehaviour;
import com.yes.yes.behaviours.PlaceBehaviour;
import com.yes.yes.behaviours.TestBehaviour;
import com.yes.yes.behaviours.TestOffererBehaviour;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Entity;
import javafx.scene.shape.Rectangle;
@@ -18,5 +19,6 @@ public class TestMachine2 extends Entity {
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new TestBehaviour(this, blockContainer));
this.addBehaviour(new ItemBehaviour(this, blockContainer));
this.addBehaviour(new TestOffererBehaviour(this, blockContainer));
}
}
@@ -3,11 +3,16 @@ package com.yes.yes.managers;
import com.yes.yes.entities.machines.TestMachine;
import com.yes.yes.entities.machines.TestMachine2;
import com.yes.yes.utils.EntityRegistry;
import com.yes.yes.utils.GlobalEventHandler;
import com.yes.yes.utils.RegistryEntry;
import com.yes.yes.utils.exceptions.AlreadyExistsException;
import com.yes.yes.world.World;
import javafx.scene.layout.VBox;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class GameManager {
private final VBox root;
private World world;
@@ -32,5 +37,11 @@ public class GameManager {
} catch (AlreadyExistsException e) {
e.printStackTrace();
}
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(() -> GlobalEventHandler.trigger("TimerTick", null), 0, 3, TimeUnit.SECONDS);
GlobalEventHandler.addListener("closing",(__)->executor.shutdown());
}
}
+5 -1
View File
@@ -11,6 +11,10 @@ public abstract class Entity extends javafx.scene.Group {
private ArrayList<Component> behaviours = new ArrayList<>();
private int rotation;
public Entity() {
GlobalEventHandler.addListener("TimerTick",(__)->update());
}
public final void addBehaviour(Component behaviour) {
behaviours.add(behaviour);
behaviour.initialize();
@@ -28,7 +32,7 @@ public abstract class Entity extends javafx.scene.Group {
public final void update() {
for (Component behaviour : behaviours) {
System.out.println("Updating " + behaviour.getClass().getSimpleName());
//System.out.println("Updating " + behaviour.getClass().getSimpleName());
behaviour.update();
}
}