Skip to content
This repository was archived by the owner on May 8, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions src/files/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class Controller {
this.model = model;

this.view = view;
this.input = new Input(model,view);
this.updateView();
this.input = new Input(model,view,this);
this.update();
}

private void onlyOneInstance (){
Expand All @@ -32,25 +32,21 @@ private void onlyOneInstance (){
}
}

// ON Start
private void run(){
//Graph graph = model.getGraph();
//view.getUI().getGraphic().setGraph(graph);
view.getUI().getGraphic().draw();
}

private void updateView(){
ArrayList<Shape> shapes = model.getEnvironment().getShapes();
public void update(){
model.getGraph().recalculate(model.getEnvironment());
Graph graph = model.getGraph();
ArrayList<Vertex> resultingPath = model.getPathfinding().getResult();
ArrayList<Shape> shapes = model.getEnvironment().getShapes();
view.getUI().getGraphic().setEnvironment(shapes);
view.getUI().getGraphic().setGraph(graph);
view.getUI().getGraphic().setResultPath(resultingPath);

run();
updatePath();
}

private void updateModel(){

public void updatePath(){
Graph graph = model.getGraph();
model.getPathfinding().run(graph);
ArrayList<Vertex> resultPath = model.getPathfinding().getResult();
view.getUI().getGraphic().setResultPath(resultPath);
view.getUI().getGraphic().draw();
}
}
27 changes: 23 additions & 4 deletions src/files/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public class Model {

public Model(){
this.onlyOneInstance(); // Limit to only 1 instance of the Model class
environment = new Environment();
//graph = new Graph("waypoint",environment);
graph = new Graph("navmesh",environment);
pathfinding = new Pathfinding(graph);
}

private void onlyOneInstance (){
Expand All @@ -45,5 +41,28 @@ public Environment getEnvironment() {

public Pathfinding getPathfinding() {
return pathfinding;
}
public void setPathfinding(String algorithm) {
if(algorithm.equals("A*")){
pathfinding = new Pathfinding(algorithm,graph);
} else if(algorithm.equals("Dijkstra")){
pathfinding = new Pathfinding(algorithm,graph);
}

}

public void setGraphEnvironment(String algorithm){
if(algorithm.equals("NavMesh")){
environment = new Environment();
environment.setMaxSize(150);
graph = new Graph("navmesh",environment);
} else if(algorithm.equals("WayPoint")){
environment = new Environment();
environment.setMaxSize(150);
graph = new Graph("waypoint",environment);
}



}
}
5 changes: 0 additions & 5 deletions src/files/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import files.views.UserInterface;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand Down Expand Up @@ -55,8 +54,4 @@ private void setScene(Parent fxml){
public UserInterface getUI(){
return UI;
}

public void setMaximized(Boolean bool){
this.stage.setMaximized(bool);
}
}
96 changes: 63 additions & 33 deletions src/files/controllers/Input.java
Original file line number Diff line number Diff line change
@@ -1,69 +1,99 @@
package files.controllers;

import files.Controller;
import files.Model;
import files.View;
import files.models.Environment;
import files.models.Shape;
import files.models.graph.Vertex;
import javafx.scene.Node;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;

import java.util.ArrayList;

public class Input {

private boolean fullScreen;
private final Model model;
private final View view;
private final Controller controller;

public Input(Model model, View view){
public Input(Model model, View view, Controller controller){
this.model = model;
this.view = view;
this.controller = controller;

addEventListeners(view.getUI().getEventNodes());
}

private void addEventListeners(ArrayList<Node> nodes){
nodes.forEach((node)->{
String nodeName = node.getClass().getSimpleName().toLowerCase();
String nodeName = node.getId();

switch(nodeName){
case "button":
node.setOnMouseClicked(event -> {
model.getEnvironment().generateEnvironment();
model.getGraph().recalculate(model.getEnvironment());
model.getPathfinding().run(model.getGraph());
ArrayList<Vertex> resultPath = model.getPathfinding().getResult();

ArrayList<Shape> shape = model.getEnvironment().getShapes();
view.getUI().getGraphic().setEnvironment(shape);
view.getUI().getGraphic().setResultPath(resultPath);
case "showNodes":
CheckBox showNodes = (CheckBox) node;
view.getUI().getGraphic().showNodes = showNodes.isSelected();
showNodes.setOnAction(event -> {
boolean bool = showNodes.isSelected();
view.getUI().getGraphic().showNodes = bool;
view.getUI().getGraphic().draw();
});

break;
case "showEdges":
CheckBox showEdges = (CheckBox) node;
view.getUI().getGraphic().showEdges = showEdges.isSelected();
showEdges.setOnAction(event -> {
boolean bool = showEdges.isSelected();
view.getUI().getGraphic().showEdges = bool;
view.getUI().getGraphic().draw();
});
/*
node.setOnKeyPressed(event -> {
System.out.println(event.getCode());
if(event.getCode().equals(KeyCode.ENTER)) {
// do something
System.out.println("You Pressed Enter");
}
break;
case "selectField1":
ComboBox selectGraph = (ComboBox) node;
selectGraph.getItems().addAll(
"NavMesh",
"WayPoint"
);
selectGraph.setValue("NavMesh");
model.setGraphEnvironment((String) selectGraph.getValue());
selectGraph.setOnAction(event -> {
model.setGraphEnvironment((String) selectGraph.getValue());
controller.update();
});

break;
case "selectField2":
ComboBox selectPathfinding = (ComboBox) node;
selectPathfinding.getItems().addAll(
"A*",
"Dijkstra"
);
selectPathfinding.setValue("A*");
model.setPathfinding((String) selectPathfinding.getValue());
selectPathfinding.setOnAction(event -> {
model.setPathfinding((String) selectPathfinding.getValue());
controller.updatePath();
});
*/
break;
case "fullscreenButton":
node.setOnMouseClicked(event -> {
System.out.println("fullscreen "+fullScreen);
fullScreen = !fullScreen;
view.setMaximized(fullScreen);
case "canvas":
Canvas canvas = (Canvas) node;
canvas.setOnMouseClicked(event -> {
double x = event.getX();
double y = event.getY();
System.out.println("x:"+x+",y:"+y);
});
break;
case "button":
Button button = (Button) node;
button.setOnAction(event -> {
model.getEnvironment().generateEnvironment();
controller.update();

node.setOnKeyPressed(event -> {
System.out.println(event.getCode());
if(event.getCode().equals(KeyCode.ENTER)) {
// do something
System.out.println("You Pressed Enter");
}
});
break;
default:
Expand Down
1 change: 1 addition & 0 deletions src/files/interfaces/Overlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

public interface Overlay {
void draw(GraphicsContext graphicsContext);
void drawEdges(GraphicsContext graphicsContext);
}
106 changes: 1 addition & 105 deletions src/files/models/AStar.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,111 +14,7 @@ public class AStar {

public AStar(Graph graph) {

//graph = this.makeSmallGraphA();
/*
// Create graph
//graph = this.makeDijkstraGraph();
graph = this.makeSmallGraphB();
// A
//Vertex startNode = graph.getVertex("A");
//Vertex endNode = graph.getVertex("E");

// B
Vertex startNode = graph.getVertex("J");
Vertex endNode = graph.getVertex("F");

ArrayList<Vertex> result = aStar(startNode, endNode);

for(Vertex v : result) {
System.out.print( v.name + " Dist:" + v.distance + " ");
if (v!=endNode)
System.out.print("-> ");
}

}


public Graph makeSmallGraphB() {
Graph myGraph = new Graph();
final Vertex A = myGraph.addVertex("A");
final Vertex B = myGraph.addVertex("B");
final Vertex C = myGraph.addVertex("C");
final Vertex D = myGraph.addVertex("D");
final Vertex E = myGraph.addVertex("E");
final Vertex F = myGraph.addVertex("F");
final Vertex G = myGraph.addVertex("G");
final Vertex H = myGraph.addVertex("H");
final Vertex I = myGraph.addVertex("I");
final Vertex J = myGraph.addVertex("J");

myGraph.newEdge(A, B, 10, 0);
myGraph.newEdge(A, D, 20, 0);
myGraph.newEdge(A, E, 20, 0);
myGraph.newEdge(A, F, 5, 0);
myGraph.newEdge(A, G, 15, 0);
myGraph.newEdge(B, C, 5, 0);
myGraph.newEdge(B, D, 10, 0);
myGraph.newEdge(C, B, 15, 0);
myGraph.newEdge(C, D, 5, 0);
myGraph.newEdge(D, E, 10, 0);
myGraph.newEdge(E, F, 5, 0);
myGraph.newEdge(G, F, 10, 0);
myGraph.newEdge(H, A, 5, 0);
myGraph.newEdge(H, B, 20, 0);
myGraph.newEdge(H, G, 5, 0);
myGraph.newEdge(I, B, 15, 0);
myGraph.newEdge(I, H, 20, 0);
myGraph.newEdge(I, J, 10, 0);
myGraph.newEdge(J, B, 5, 0);
myGraph.newEdge(J, C, 15, 0);

return myGraph;
}
*/
// A
//Vertex startNode = graph.getVertex("A");
//Vertex endNode = graph.getVertex("E");

// B
//Vertex startNode = graph.getVertex("J");
//Vertex endNode = graph.getVertex("F");

//ArrayList<Vertex> result = start(graph, startNode, endNode);

//for(Vertex v : result) {
// System.out.print( v.name + " Dist:" + v.distance + " ");
// if (v!=endNode)
// System.out.print("-> ");
//}


}

public Graph makeSmallGraphA() {
// TODO: take this graph
/*
Graph myGraph= new Graph();
final Vertex A= myGraph.addVertex("A");
final Vertex B= myGraph.addVertex("B");
final Vertex C = myGraph.addVertex("C");
final Vertex D = myGraph.addVertex("D");
final Vertex E = myGraph.addVertex("E");

myGraph.newEdge(A,B, 5, 3);
myGraph.newEdge(A,C, 10, 3);
myGraph.newEdge(B,C, 3, 3);
myGraph.newEdge(B,D, 2, 3);
myGraph.newEdge(B,E, 9, 3);
myGraph.newEdge(C,B, 2, 3);
myGraph.newEdge(C,E, 1, 3);
myGraph.newEdge(D,E, 6, 3);
myGraph.newEdge(E,D, 4, 3);

return myGraph;

*/ return null;
}


public ArrayList<Vertex> start(Graph graph, Vertex startNode, Vertex endNode) {

Expand All @@ -131,7 +27,7 @@ public ArrayList<Vertex> start(Graph graph, Vertex startNode, Vertex endNode) {
}

startNode.setDistance(0);
startNode.setF(startNode.distance + heuristic(startNode, endNode)); // TODO: temporary...?
startNode.setF(startNode.distance + heuristic(startNode, endNode));
openTreeSet.add(startNode);
Vertex current;

Expand Down
Loading