Skip to content

Commit

Permalink
Merge pull request #217 from S010MON/trails
Browse files Browse the repository at this point in the history
Trails
  • Loading branch information
S010MON authored Mar 11, 2022
2 parents 5078e65 + 822717c commit 588c062
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 46 deletions.
9 changes: 7 additions & 2 deletions src/main/java/app/controller/GameEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import app.controller.graphicsEngine.GraphicsEngine;
import app.controller.linAlg.Intersection;
import app.controller.linAlg.Vector;
import app.model.agents.ACO.AcoAgent;
import app.model.Trail;
import app.model.agents.Agent;
import app.model.boundary.Boundary;
import app.model.Map;
Expand All @@ -14,15 +14,18 @@
import javafx.animation.Timeline;
import javafx.scene.input.KeyEvent;
import javafx.util.Duration;
import lombok.Getter;

public class GameEngine
{
@Getter private long tics;
private Map map;
private Renderer renderer;
private GraphicsEngine graphicsEngine;

public GameEngine(Map map, Renderer renderer)
{
this.tics = 0;
this.map = map;
this.renderer = renderer;
this.graphicsEngine = new GraphicsEngine();
Expand All @@ -44,16 +47,18 @@ public void tick()
if(teleportTo != null)
{
a.updateLocation(teleportTo);
renderer.addTrail(new Trail(teleportTo, tics));
}

if (legalMove(startPoint, endPoint) &&
legalMove(a, endPoint) &&
legalMove(a, startPoint) && legalMove(a, startPoint,endPoint))
{
a.updateLocation(endPoint);
renderer.addTrail(new Trail(endPoint, tics));
}
}

tics++;
renderer.render();
}

Expand Down
10 changes: 3 additions & 7 deletions src/main/java/app/model/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import app.controller.settings.Settings;
import app.controller.settings.SettingsObject;
import app.model.agents.ACO.AcoAgent;
import app.model.agents.*;
import app.model.boundary.Boundary;
import app.model.furniture.*;
import app.model.soundFurniture.SoundFurniture;
import app.model.soundSource.SoundSource;
import app.model.agents.Agent;
import app.model.agents.Human;
import app.model.agents.WallFollowAgent;
import app.model.boundary.Boundary;
import app.model.furniture.Furniture;
import app.model.furniture.FurnitureFactory;
import app.model.furniture.FurnitureType;
import app.model.soundFurniture.SoundFurnitureFactory;
import app.model.soundSource.SoundSourceFactory;
import app.model.soundSource.SoundSourceType;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/app/model/Trail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package app.model;

import app.controller.linAlg.Vector;
import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class Trail implements Comparable
{
private Vector loc;
private long time;

@Override public int compareTo(Object o)
{
if(!(o instanceof Trail))
throw new ClassCastException();

Trail other = (Trail) o;

if(this.time < other.time)
return -1;
else if(this.time > other.time)
return 1;
return 0;
}
}
33 changes: 11 additions & 22 deletions src/main/java/app/model/agents/ACO/AcoAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import app.controller.graphicsEngine.Ray;
import app.controller.linAlg.Vector;
import app.model.Grid.AcoGrid;
import app.model.agents.AgentImp;
import app.model.agents.Cells.PheromoneCell;
import app.model.map.Move;
Expand All @@ -11,7 +10,7 @@
import java.util.HashMap;
import java.util.Random;

//TODO Accomodate for agents clashes
//TODO Accommodate for agents clashes
public class AcoAgent extends AgentImp
{
private static AcoGrid world = new AcoGrid();
Expand Down Expand Up @@ -142,11 +141,10 @@ public Vector selectRandomEquivalentMove(ArrayList<Vector> equivalentMoves)
}
else
{
ArrayList<Vector> rememberedMoves = new ArrayList<Vector>(shortTermMoveMemory.values());
ArrayList<Vector> rememberedMoves = new ArrayList<>(shortTermMoveMemory.values());
int randomIndex = randomGenerator.nextInt(rememberedMoves.size());
return rememberedMoves.get(randomIndex);
}

}

public ArrayList<Double> accessAvaliableCellPheromones(ArrayList<Vector> possibleMovements)
Expand Down Expand Up @@ -179,19 +177,14 @@ public ArrayList<Vector> determineAvailableMovements(Ray[] cardinalRays)

public Vector angleToGridMovementLink(int angle)
{
switch(angle)
{
case 0:
return new Vector(0, cellSize);
case 90:
return new Vector(cellSize, 0);
case 180:
return new Vector(0, -cellSize);
case 270:
return new Vector(-cellSize, 0);
default:
return null;
}
return switch (angle)
{
case 0 -> new Vector(0, cellSize);
case 90 -> new Vector(cellSize, 0);
case 180 -> new Vector(0, -cellSize);
case 270 -> new Vector(-cellSize, 0);
default -> null;
};
}

public Ray[] detectCardinalRays()
Expand Down Expand Up @@ -239,11 +232,7 @@ private int calculateMidPoint(int upperBound, int lowerBound)

public boolean approximateAngleRange(double detectedAngle, double targetAngle)
{
if(detectedAngle < (targetAngle + epsilon) && detectedAngle > (targetAngle - epsilon))
{
return true;
}
return false;
return detectedAngle < (targetAngle + epsilon) && detectedAngle > (targetAngle - epsilon);
}

public double releaseMaxPheromone()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.model.Grid;
package app.model.agents.ACO;

import app.model.agents.ACO.AcoAgent;
import app.model.agents.Grid;
import app.model.agents.Cells.Cell;
import app.model.agents.Cells.CellType;
import app.model.agents.Cells.PheromoneCell;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.model.Grid;
package app.model.agents;

import app.controller.linAlg.Vector;
import app.model.agents.Cells.*;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/app/view/simulation/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import app.controller.graphicsEngine.Ray;
import app.model.Map;
import app.model.Trail;
import app.view.ScreenSize;
import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
Expand All @@ -11,18 +12,21 @@
import javafx.scene.paint.Color;

import java.util.ArrayList;
import java.util.PriorityQueue;

public class Renderer extends Canvas
{
private Map map;
private Color backgroundColour, outlineColor;
private double zoomRate = 0.2d;
private Point2D click;
private PriorityQueue<Trail> trails;

public Renderer(Map map)
{
super(ScreenSize.width, ScreenSize.height);
this.map = map;
this.trails = new PriorityQueue<>();
this.setInitialZoom(map.getSettings().getHeight(),map.getSettings().getWidth());
backgroundColour = Color.WHITE;
outlineColor = Color.rgb(191, 191, 191);
Expand All @@ -42,10 +46,16 @@ public void render()
map.drawIntruderSpawn(gc);
map.drawGuardSpawn(gc);
map.getFurniture().forEach(e -> e.draw(gc));
trails.forEach(e -> drawTrail(gc, e));
map.getAgents().forEach(e -> drawRays(gc, e.getView()));
map.getAgents().forEach(e -> e.draw(gc));
}

public void addTrail(Trail t)
{
trails.add(t);
}

private void drawRays(GraphicsContext gc, ArrayList<Ray> rays)
{
rays.forEach(e -> e.draw(gc));
Expand All @@ -63,6 +73,15 @@ private void drawBackground(GraphicsContext gc)
map.getSettings().getHeight() * Info.getInfo().zoom);
}

private void drawTrail(GraphicsContext gc, Trail t)
{
gc.setFill(Color.GRAY);
gc.fillOval(t.getLoc().getX() * Info.getInfo().getZoom() + Info.getInfo().offsetX,
t.getLoc().getY() * Info.getInfo().getZoom() + Info.getInfo().offsetY,
2 * Info.getInfo().getZoom(),
2 * Info.getInfo().getZoom());
}

private void handleScroll(ScrollEvent e)
{
double dy = e.getDeltaY();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/testing/AcoAgentTesting.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import app.controller.io.FileManager;
import app.controller.linAlg.Vector;
import app.controller.settings.Settings;
import app.model.Grid.AcoGrid;
import app.model.agents.ACO.AcoGrid;
import app.model.Map;
import app.model.agents.Cells.PheromoneCell;
import app.model.map.Move;
Expand Down
15 changes: 6 additions & 9 deletions src/test/java/testing/AcoGridTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import app.controller.linAlg.Vector;
import app.model.agents.ACO.AcoAgent;
import app.model.Grid.AcoGrid;
import app.model.agents.ACO.AcoGrid;
import app.model.agents.Cells.PheromoneCell;
import org.junit.jupiter.api.Test;

Expand All @@ -12,6 +12,7 @@
public class AcoGridTest
{
private final boolean display = false;

@Test
void testUpdateAgent()
{
Expand Down Expand Up @@ -54,17 +55,13 @@ void testAgentMovementWithinGrid()

Vector agentPosition = new Vector(10, 10);
AcoAgent agent = new AcoAgent(agentPosition, new Vector(1, 0), 10);
if(display)
{
AcoAgent.accessWorld().print();
}

if(display) AcoAgent.accessWorld().print();

Vector newPosition = new Vector(9, 9);
agent.updateLocation(newPosition);
if(display)
{
AcoAgent.accessWorld().print();
}

if(display) AcoAgent.accessWorld().print();

PheromoneCell currentCell = (PheromoneCell) AcoAgent.accessWorld().getCellAt(newPosition);
assertTrue(currentCell.currentPheromoneValue() < agent.releaseMaxPheromone());
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/testing/GridTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package testing;

import app.controller.linAlg.Vector;
import app.model.Grid.Grid;
import app.model.Grid.AcoGrid;
import app.model.agents.Grid;
import app.model.agents.ACO.AcoGrid;
import app.model.agents.Cells.BooleanCell;
import app.model.agents.Cells.Cell;
import app.model.agents.Cells.CellType;
Expand Down

0 comments on commit 588c062

Please sign in to comment.