laatste versie

This commit is contained in:
unknown
2015-04-08 15:56:46 +02:00
parent c2cb07cf3e
commit 69c05da3fd
18 changed files with 726 additions and 215 deletions
+220 -81
View File
@@ -2,7 +2,6 @@ package Applicatie;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
@@ -16,10 +15,12 @@ import java.util.Map;
import Agenda.Agenda;
import Agenda.Event;
import Objects.DrawObject;
import Objects.Entrance;
import Objects.Path;
import Objects.Stage;
public class Visitor {
public class Visitor
{
Point2D position;
double rotation;
@@ -29,35 +30,38 @@ public class Visitor {
ArrayList<Action> actions;
Agenda agenda;
ArrayList<DrawObject> objects;
ArrayList<Waypoint> waypoints;
GregorianCalendar date;
char target;
int target;
int finalTarget;
public Visitor(String filename, Point2D position, Agenda agenda,
ArrayList<DrawObject> objects, GregorianCalendar date) {
public Visitor(String filename, Point2D position, Agenda agenda, ArrayList<DrawObject> objects, ArrayList<Waypoint> waypoints, GregorianCalendar date)
{
this.position = position;
this.filename = filename;
this.rotation = 0;
this.date = date;
this.speed = 1 + Math.random() * 4;
this.waypoints = waypoints;
this.date = date;
scale = 1;
actions = new ArrayList<Action>();
this.agenda = agenda;
this.objects = objects;
fillActions();
target = 'z';
for (Action a : actions){
System.out.println(a.getStartTime());
}
System.out.println(actions.size());
target = 1;
finalTarget = 4;
}
public void draw(Graphics2D g2) {
public void draw(Graphics2D g2)
{
AffineTransform tx = getTransform();
Image image = Images.getImage(filename);
g2.drawImage(image, tx, null);
}
private AffineTransform getTransform() {
private AffineTransform getTransform()
{
Image image = Images.getImage(filename);
AffineTransform tx = new AffineTransform();
tx.scale(scale, scale);
@@ -66,48 +70,151 @@ public class Visitor {
return tx;
}
public void update(ArrayList<DrawObject> objects, int currentTime,
ArrayList<Visitor> visitors, ArrayList<Path> paths) {
Point2D target = new Point(500, 500);
for (Action a : actions) {
if (currentTime >= a.getStartTime()
&& currentTime < a.getStoptime()) {
target = a.getPosition();
public DrawObject getEntrance()
{
for (DrawObject o : objects)
{
if (o instanceof Entrance)
{
return o;
}
}
return null;
}
public void update(ArrayList<DrawObject> objects, int currentTime, ArrayList<Visitor> visitors, ArrayList<Path> paths, Panel panel)
{
DrawObject target = getEntrance();
for (Action a : actions)
{
if (currentTime >= a.getStartTime() && currentTime < a.getStoptime())
{
target = a.getTargetObject();
}
}
moveToTarget(target, objects, visitors, paths);
// moveToTarget(target, objects, visitors, paths);
move(target, panel);
}
public void moveToTarget(Point2D target, ArrayList<DrawObject> objects,
ArrayList<Visitor> visitors, ArrayList<Path> paths) {
Point2D tar = target;
public void move(DrawObject tar, Panel panel)
{
Point2D targetPoint = panel.getWaypoint(target).getPosition();
double newRot = Math.atan2(targetPoint.getY() - position.getY(), targetPoint.getX() - position.getX());
int difx = (int) (targetPoint.getX() - position.getX());
int dify = (int) (targetPoint.getY() - position.getY());
int distance = (int) Math.sqrt((difx * difx) + (dify * dify));
if (rotation > newRot && distance > 10)
{
rotation -= 0.15;
}
else if (rotation < newRot && distance > 10)
{
rotation += 0.15;
}
Point2D oldPosition = position;
// face direction
float directionX = (float) Math.cos(rotation);
float directionY = (float) Math.sin(rotation);
if (distance > 10)
{
position = new Point2D.Double((position.getX() + directionX * speed), (position.getY() + directionY * speed));
}
boolean possible = true;
for (DrawObject object : objects)
{
if (object instanceof Entrance || object instanceof Waypoint)
{
continue;
}
if (hasCollisionDO(object))
{
possible = false;
}
}
if (possible == false)
{
position = oldPosition;
rotation += 0.2;
}
if (checkIfOnWaypoint(panel))
{
// for(int i : panel.getWaypoint(target).getOptions())
// {
// if(i == finalTarget)
// {
// target = finalTarget;
// }
// }
// ArrayList<int[]> options = new ArrayList<int[]>();
HashMap<Integer, int[]> options = new HashMap<Integer, int[]>();
for (Waypoint w : panel.getWaypoints())
{
options.put(w.getSelf(), w.getOptions());
}
for(Map.Entry<Integer, int[]> e : options.entrySet())
{
System.out.println(e.getKey());
System.out.println("=====================");
for(int i : e.getValue())
{
System.out.println(i);
}
System.out.println("----------");
}
}
}
public boolean checkIfOnWaypoint(Panel panel)
{
if (panel.getWaypoint(target).contains(position))
{
return true;
}
return false;
}
public void moveToTarget(DrawObject target, ArrayList<DrawObject> objects, ArrayList<Visitor> visitors, ArrayList<Path> paths)
{
Point2D tar = target.getPosition();
boolean hasPathBelow = false;
for (Path p : paths) {
if (p.containsPoint(position)) {
for (Path p : paths)
{
if (p.containsPoint(position))
{
hasPathBelow = true;
break;
}
Shape containsShape = p.containsPointShape(position);
if (containsShape != null) {
tar = new Point2D.Double(
containsShape.getBounds().getCenterX(), containsShape
.getBounds().getCenterY());
if (containsShape != null)
{
tar = new Point2D.Double(containsShape.getBounds().getCenterX(), containsShape.getBounds().getCenterY());
}
}
if (hasPathBelow) {
double newRot = Math.atan2(tar.getY() - position.getY(), tar.getX()
- position.getX());
if (hasPathBelow)
{
double newRot = Math.atan2(tar.getY() - position.getY(), tar.getX() - position.getX());
int difx = (int) (tar.getX() - position.getX());
int dify = (int) (tar.getY() - position.getY());
int distance = (int) Math.sqrt((difx * difx) + (dify * dify));
if (rotation > newRot && distance > 10) {
if (rotation > newRot && distance > 10)
{
rotation -= 0.15;
} else if (rotation < newRot && distance > 10) {
}
else if (rotation < newRot && distance > 10)
{
rotation += 0.15;
}
@@ -117,30 +224,39 @@ public class Visitor {
float directionX = (float) Math.cos(rotation);
float directionY = (float) Math.sin(rotation);
if (distance > 10) {
position = new Point2D.Double((position.getX() + directionX
* speed), (position.getY() + directionY * speed));
if (distance > 10)
{
position = new Point2D.Double((position.getX() + directionX * speed), (position.getY() + directionY * speed));
}
boolean possible = true;
for (DrawObject object : objects) {
if (hasCollisionDO(object)) {
for (DrawObject object : objects)
{
if (hasCollisionDO(object))
{
possible = false;
}
}
for (Visitor object : visitors) {
if (hasCollision(object) && object != this) {
for (Visitor object : visitors)
{
if (hasCollision(object) && object != this)
{
possible = false;
}
}
if (possible == false) {
if (possible == false)
{
position = oldPosition;
rotation += 0.2;
}
} else {
}
else
{
HashMap<Double, Shape> values = new HashMap<Double, Shape>();
for (Path p : paths) {
for (Shape s : p.getPath()) {
for (Path p : paths)
{
for (Shape s : p.getPath())
{
Rectangle b = s.getBounds();
double maxx = b.getMaxX();
double minx = b.getMinX();
@@ -154,14 +270,16 @@ public class Visitor {
points[3] = new Point2D.Double(maxx, maxy);
Double lastdistance = null;
for (Point2D.Double point : points) {
double distance = Point2D.distance(point.getX(),
point.getY(), position.getX(), position.getY());
if (lastdistance == null) {
for (Point2D.Double point : points)
{
double distance = Point2D.distance(point.getX(), point.getY(), position.getX(), position.getY());
if (lastdistance == null)
{
lastdistance = distance;
continue;
}
if (distance < lastdistance) {
if (distance < lastdistance)
{
lastdistance = distance;
}
}
@@ -171,31 +289,35 @@ public class Visitor {
}
Double lastdistance = null;
Shape lastShape = null;
for (Map.Entry<Double, Shape> e : values.entrySet()) {
for (Map.Entry<Double, Shape> e : values.entrySet())
{
double distance = e.getKey();
if (lastdistance == null) {
if (lastdistance == null)
{
lastdistance = distance;
continue;
}
if (distance < lastdistance) {
if (distance < lastdistance)
{
lastdistance = distance;
}
}
lastShape = values.get(lastdistance);
Point2D.Double target2 = new Point2D.Double(lastShape.getBounds()
.getCenterX(), lastShape.getBounds().getCenterY());
Point2D.Double target2 = new Point2D.Double(lastShape.getBounds().getCenterX(), lastShape.getBounds().getCenterY());
double newRot = Math.atan2(target2.getY() - position.getY(),
target2.getX() - position.getX());
double newRot = Math.atan2(target2.getY() - position.getY(), target2.getX() - position.getX());
int difx = (int) (target2.getX() - position.getX());
int dify = (int) (target2.getY() - position.getY());
int distance = (int) Math.sqrt((difx * difx) + (dify * dify));
if (rotation > newRot && distance > 10) {
if (rotation > newRot && distance > 10)
{
rotation -= 0.15;
} else if (rotation < newRot && distance > 10) {
}
else if (rotation < newRot && distance > 10)
{
rotation += 0.15;
}
@@ -205,23 +327,28 @@ public class Visitor {
float directionX = (float) Math.cos(rotation);
float directionY = (float) Math.sin(rotation);
if (distance > 10) {
position = new Point2D.Double((position.getX() + directionX
* speed), (position.getY() + directionY * speed));
if (distance > 10)
{
position = new Point2D.Double((position.getX() + directionX * speed), (position.getY() + directionY * speed));
}
boolean possible = true;
for (DrawObject object : objects) {
if (hasCollisionDO(object)) {
for (DrawObject object : objects)
{
if (hasCollisionDO(object))
{
possible = false;
}
}
for (Visitor object : visitors) {
if (hasCollision(object) && object != this) {
for (Visitor object : visitors)
{
if (hasCollision(object) && object != this)
{
possible = false;
}
}
if (possible == false) {
if (possible == false)
{
position = oldPosition;
rotation += 0.2;
}
@@ -237,9 +364,10 @@ public class Visitor {
int random = (int) Math.floor(Math.random() * 21);
if (random < 40) {
Point2D position = null;
DrawObject targetStage = null;
ArrayList<Event> posEvents = new ArrayList<Event>();
for (Event e : agenda.getEvents()) {
if (startTime >= e.getStart() && startTime < e.getStop() && date.DAY_OF_MONTH == e.getStartDate().DAY_OF_MONTH) {
if (startTime >= e.getStart() && startTime < e.getStop()) {
posEvents.add(e);
}
}
@@ -261,6 +389,7 @@ public class Visitor {
if (s.getStage().getName()
.equals(evs.getStage().getName())) {
position = s.getPosition();
targetStage = d;
}
}
}
@@ -269,12 +398,14 @@ public class Visitor {
int randomtime = (int) (40 + (Math.random() * (60 - 40)));
if (position != null) {
actions.add(new Action(position, startTime, randomtime));
Waypoint w = getClosedWaypoint(position);
actions.add(new Action(position, startTime, randomtime, targetStage, w));
}
startTime = startTime + randomtime;
} else if (random >= 40 && random < 45) {
Point2D position = null;
DrawObject targetStage = null;
ArrayList<DrawObject> foodPlaces = new ArrayList<DrawObject>();
for (DrawObject d : objects) {
if (d.getFileName().equals("food")) {
@@ -285,13 +416,16 @@ public class Visitor {
if (foodPlaces.size() != 0){
int r = (int) Math.floor(Math.random() * foodPlaces.size());
position = foodPlaces.get(r).getPosition();
actions.add(new Action(position, startTime, 35));
targetStage = foodPlaces.get(r);
Waypoint w = getClosedWaypoint(position);
actions.add(new Action(position, startTime, 35, targetStage, w));
startTime = startTime + 20;
}
startTime = startTime + 35;
} else if (random > 45) {
Point2D position = null;
DrawObject targetStage = null;
ArrayList<DrawObject> toilets = new ArrayList<DrawObject>();
for (DrawObject d : objects) {
if (d.getFileName().equals("wc")) {
@@ -301,7 +435,9 @@ public class Visitor {
if (toilets.size() != 0){
int r = (int) Math.floor(Math.random() * toilets.size());
position = toilets.get(r).getPosition();
actions.add(new Action(position, startTime, 35));
targetStage = toilets.get(r);
Waypoint w = getClosedWaypoint(position);
actions.add(new Action(position, startTime, 35, targetStage, w));
startTime = startTime + 20;
}
startTime = startTime + 35;
@@ -309,15 +445,19 @@ public class Visitor {
}
}
}
public int convertMinutesToHours(int startTime) {
int time = 0;
int hours = startTime / 60;
int minutes = startTime % 60;
time = (hours * 100) + minutes;
return time;
public Waypoint getClosedWaypoint(Point2D point){
Waypoint waypoint = null;
int distance = 10000;
for(Waypoint w : waypoints){
if (w.getPosition().distance(point) < distance){
waypoint = w;
distance = (int) w.getPosition().distance(point);
}
}
return waypoint;
}
public boolean hasCollisionDO(DrawObject object) {
Image image = Images.getImage(filename);
Image image2 = Images.getImage(object.getFileName());
@@ -373,5 +513,4 @@ public class Visitor {
}
return false;
}
}