From 9d814d58f90ce0384cf667f1b9c5328035e6c6ef Mon Sep 17 00:00:00 2001 From: S010MON Date: Sun, 6 Feb 2022 17:17:56 +0100 Subject: [PATCH] adds movement compoent to player -> requires left/right turns and speed to be set --- src/main/java/app/App.java | 1 + src/main/java/app/controller/GameEngine.java | 35 ++++++++++++------- src/main/java/app/controller/Human.java | 24 +++++++++++++ src/main/java/app/controller/ManualAgent.java | 15 -------- src/main/java/app/model/MapTemp.java | 19 +++++----- src/main/java/app/view/Frame.java | 10 +++++- 6 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 src/main/java/app/controller/Human.java delete mode 100644 src/main/java/app/controller/ManualAgent.java diff --git a/src/main/java/app/App.java b/src/main/java/app/App.java index 5929c522..d751bd38 100644 --- a/src/main/java/app/App.java +++ b/src/main/java/app/App.java @@ -17,6 +17,7 @@ public void start(Stage stage) throws IOException this.stage = stage; Frame frame = new Frame(800, 500); Scene scene = new Scene(frame,800, 500); + scene.setOnKeyTyped(e -> frame.handleKey(e)); stage.setTitle("Multi Agent Surveillance"); stage.setScene(scene); stage.show(); diff --git a/src/main/java/app/controller/GameEngine.java b/src/main/java/app/controller/GameEngine.java index 2cb3212b..536fb4e1 100644 --- a/src/main/java/app/controller/GameEngine.java +++ b/src/main/java/app/controller/GameEngine.java @@ -3,31 +3,40 @@ import app.model.Agent; import app.model.MapTemp; import app.view.Renderer; - -import javax.swing.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +import javafx.animation.Animation; +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; +import javafx.scene.input.KeyEvent; +import javafx.util.Duration; public class GameEngine { private MapTemp map; private Renderer renderer; private GraphicsEngine graphicsEngine; - private Timer clock; - private int delay = 100; public GameEngine(MapTemp map, Renderer renderer) { this.map = map; this.renderer = renderer; this.graphicsEngine = new RayTracing(); - clock = new Timer(delay, new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - tick(); - } - }); - clock.start(); + Timeline timeline = new Timeline(new KeyFrame( Duration.millis(100), ae -> tick())); + timeline.setCycleCount(Animation.INDEFINITE); + timeline.play(); + } + + public void handleKey(KeyEvent e) + { + System.out.println(e.getCharacter()); + switch (e.getCharacter()) + { + case "w" -> map.moveHuman(new Vector(0,-10)); + case "s" -> map.moveHuman(new Vector(0,10)); + case "a" -> map.moveHuman(new Vector(-10,0)); + case "d" -> map.moveHuman(new Vector(10,0)); + + } + } public void tick() diff --git a/src/main/java/app/controller/Human.java b/src/main/java/app/controller/Human.java new file mode 100644 index 00000000..73425fe8 --- /dev/null +++ b/src/main/java/app/controller/Human.java @@ -0,0 +1,24 @@ +package app.controller; + +public class Human extends AgentImp +{ + private Vector nextMove; + + public Human(Vector position, Vector direction, double radius) + { + super(position, direction, radius); + nextMove = new Vector(); + } + + @Override + public void move() + { + position = position.add(nextMove); + nextMove = new Vector(); + } + + public void setNextMove(Vector nextMove) + { + this.nextMove = nextMove; + } +} diff --git a/src/main/java/app/controller/ManualAgent.java b/src/main/java/app/controller/ManualAgent.java deleted file mode 100644 index 944ecdad..00000000 --- a/src/main/java/app/controller/ManualAgent.java +++ /dev/null @@ -1,15 +0,0 @@ -package app.controller; - -public class ManualAgent extends AgentImp -{ - public ManualAgent(Vector position, Vector direction, double radius) - { - super(position, direction, radius); - } - - @Override - public void move() - { - - } -} diff --git a/src/main/java/app/model/MapTemp.java b/src/main/java/app/model/MapTemp.java index a4aa125d..f56825b3 100644 --- a/src/main/java/app/model/MapTemp.java +++ b/src/main/java/app/model/MapTemp.java @@ -1,6 +1,7 @@ package app.model; import app.controller.AgentImp; +import app.controller.Human; import app.controller.Vector; import java.util.ArrayList; @@ -9,6 +10,7 @@ public class MapTemp { private ArrayList objects; private ArrayList agents; + private Human human; /** * Temporary map for testing ray drawing, will be swapped out for proper one once made @@ -17,22 +19,19 @@ public MapTemp() { objects = createObjects(); agents = new ArrayList<>(); - agents.add(new AgentImp(new Vector(400, 250), new Vector(1,0), 10)); + human = new Human(new Vector(400, 250), new Vector(1,0), 10); + agents.add(human); agents.add(new AgentImp(new Vector(100, 100), new Vector(1,0), 10)); } + public void moveHuman(Vector v) + { + human.setNextMove(v); + } + private ArrayList createObjects() { ArrayList objects = new ArrayList<>(); - Vector b1 = new Vector(0,0); - Vector b2 = new Vector(800,0); - Vector b3 = new Vector(800,500); - Vector b4 = new Vector(0,500); -// objects.add(new Border(b1, b2)); -// objects.add(new Border(b2, b3)); -// objects.add(new Border(b3, b4)); -// objects.add(new Border(b4, b1)); - Vector p1 = new Vector(200,100); Vector p2 = new Vector(600,100); Vector p3 = new Vector(600,400); diff --git a/src/main/java/app/view/Frame.java b/src/main/java/app/view/Frame.java index f59013b6..5cff5801 100644 --- a/src/main/java/app/view/Frame.java +++ b/src/main/java/app/view/Frame.java @@ -2,15 +2,23 @@ import app.controller.GameEngine; import app.model.MapTemp; +import javafx.scene.input.KeyEvent; import javafx.scene.layout.BorderPane; public class Frame extends BorderPane { + private GameEngine gameEngine; + public Frame(int width, int height) { MapTemp map = new MapTemp(); Renderer renderer = new Renderer(map, width, height); - GameEngine gameEngine = new GameEngine(map, renderer); + gameEngine = new GameEngine(map, renderer); this.setCenter(renderer); } + + public void handleKey(KeyEvent e) + { + gameEngine.handleKey(e); + } }