Skip to content

Commit

Permalink
Merge pull request #40 from S010MON/humans!
Browse files Browse the repository at this point in the history
adds movement component to Human
  • Loading branch information
S010MON authored Feb 6, 2022
2 parents 8189429 + 9d814d5 commit 3df0596
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/main/java/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
35 changes: 22 additions & 13 deletions src/main/java/app/controller/GameEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/app/controller/Human.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
15 changes: 0 additions & 15 deletions src/main/java/app/controller/ManualAgent.java

This file was deleted.

19 changes: 9 additions & 10 deletions src/main/java/app/model/MapTemp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.model;

import app.controller.AgentImp;
import app.controller.Human;
import app.controller.Vector;

import java.util.ArrayList;
Expand All @@ -9,6 +10,7 @@ public class MapTemp
{
private ArrayList<Placeable> objects;
private ArrayList<Agent> agents;
private Human human;

/**
* Temporary map for testing ray drawing, will be swapped out for proper one once made
Expand All @@ -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<Placeable> createObjects()
{
ArrayList<Placeable> 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);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/app/view/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3df0596

Please sign in to comment.