added addPeoplePanel
This commit is contained in:
+223
-65
@@ -5,8 +5,10 @@ import java.awt.Image;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Shape;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.geom.Area;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.util.ArrayList;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -15,6 +17,7 @@ import Agenda.Event;
|
||||
import Objects.DrawObject;
|
||||
import Objects.Entrance;
|
||||
import Objects.Path;
|
||||
import Objects.Stage;
|
||||
|
||||
public class Visitor
|
||||
{
|
||||
@@ -27,21 +30,28 @@ public class Visitor
|
||||
ArrayList<Action> actions;
|
||||
Agenda agenda;
|
||||
ArrayList<DrawObject> objects;
|
||||
int target;
|
||||
|
||||
public Visitor(String filename, Point2D position, Agenda agenda, ArrayList<DrawObject> objects)
|
||||
ArrayList<Waypoint> waypoints;
|
||||
GregorianCalendar date;
|
||||
int target;
|
||||
int finalTarget;
|
||||
|
||||
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.speed = 1 + Math.random() * 4;
|
||||
this.waypoints = waypoints;
|
||||
this.date = date;
|
||||
scale = 1;
|
||||
actions = new ArrayList<Action>();
|
||||
this.agenda = agenda;
|
||||
this.objects = objects;
|
||||
fillActions();
|
||||
System.out.println(actions.size());
|
||||
target = 0;
|
||||
target = 1;
|
||||
finalTarget = 0;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2)
|
||||
@@ -63,29 +73,119 @@ public class Visitor
|
||||
|
||||
public DrawObject getEntrance()
|
||||
{
|
||||
for(DrawObject o : objects)
|
||||
for (DrawObject o : objects)
|
||||
{
|
||||
if(o instanceof Entrance)
|
||||
if (o instanceof Entrance)
|
||||
{
|
||||
return o;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void update(ArrayList<DrawObject> objects, int currentTime, ArrayList<Visitor> visitors, ArrayList<Path> paths)
|
||||
|
||||
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();
|
||||
this.finalTarget = a.getWaypoint().getSelf();
|
||||
}
|
||||
|
||||
}
|
||||
System.out.println(actions.size());
|
||||
|
||||
moveToTarget(target, objects, visitors, paths);
|
||||
// moveToTarget(target, objects, visitors, paths);
|
||||
move(target, panel);
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
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())
|
||||
{
|
||||
for (int i : e.getValue())
|
||||
{
|
||||
if (finalTarget != target)
|
||||
{
|
||||
if (finalTarget == i && e.getKey() == target)
|
||||
{
|
||||
target = finalTarget;
|
||||
break;
|
||||
}
|
||||
else if (finalTarget == i)
|
||||
{
|
||||
target = e.getKey();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -100,7 +200,7 @@ public class Visitor
|
||||
break;
|
||||
}
|
||||
Shape containsShape = p.containsPointShape(position);
|
||||
if(containsShape != null)
|
||||
if (containsShape != null)
|
||||
{
|
||||
tar = new Point2D.Double(containsShape.getBounds().getCenterX(), containsShape.getBounds().getCenterY());
|
||||
}
|
||||
@@ -137,14 +237,15 @@ public class Visitor
|
||||
boolean possible = true;
|
||||
for (DrawObject object : objects)
|
||||
{
|
||||
if (hitTest(object))
|
||||
if (hasCollisionDO(object))
|
||||
{
|
||||
possible = false;
|
||||
}
|
||||
}
|
||||
for (Visitor object : visitors)
|
||||
{
|
||||
if (hitTestVisitor(object) && object != this)
|
||||
|
||||
if (hasCollision(object) && object != this)
|
||||
{
|
||||
possible = false;
|
||||
}
|
||||
@@ -208,27 +309,23 @@ public class Visitor
|
||||
}
|
||||
}
|
||||
lastShape = values.get(lastdistance);
|
||||
|
||||
|
||||
Point2D.Double target2 = new Point2D.Double(lastShape.getBounds().getCenterX(), lastShape.getBounds().getCenterY());
|
||||
|
||||
|
||||
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));
|
||||
|
||||
System.out.println(rotation);
|
||||
System.out.println(newRot);
|
||||
System.out.println("--------------");
|
||||
// if (rotation > newRot && distance > 10)
|
||||
// {
|
||||
// rotation -= 0.15;
|
||||
// }
|
||||
// else if (rotation < newRot && distance > 10)
|
||||
// {
|
||||
// rotation += 0.15;
|
||||
// }
|
||||
rotation = newRot;
|
||||
if (rotation > newRot && distance > 10)
|
||||
{
|
||||
rotation -= 0.15;
|
||||
}
|
||||
else if (rotation < newRot && distance > 10)
|
||||
{
|
||||
rotation += 0.15;
|
||||
}
|
||||
|
||||
Point2D oldPosition = position;
|
||||
|
||||
@@ -244,14 +341,14 @@ public class Visitor
|
||||
boolean possible = true;
|
||||
for (DrawObject object : objects)
|
||||
{
|
||||
if (hitTest(object))
|
||||
if (hasCollisionDO(object))
|
||||
{
|
||||
possible = false;
|
||||
}
|
||||
}
|
||||
for (Visitor object : visitors)
|
||||
{
|
||||
if (hitTestVisitor(object) && object != this)
|
||||
if (hasCollision(object) && object != this)
|
||||
{
|
||||
possible = false;
|
||||
}
|
||||
@@ -264,6 +361,7 @@ public class Visitor
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
public void fillActions()
|
||||
{
|
||||
int startTime = 540;
|
||||
@@ -272,65 +370,91 @@ public class Visitor
|
||||
while (startTime < stopTime)
|
||||
{
|
||||
int random = (int) Math.floor(Math.random() * 51);
|
||||
if (random < 30)
|
||||
if (random < 40)
|
||||
{
|
||||
Point2D position = null;
|
||||
DrawObject targetStage = null;
|
||||
ArrayList<Event> posEvents = new ArrayList<Event>();
|
||||
for (Event e : agenda.getEvents())
|
||||
{
|
||||
if (convertMinutesToHours(startTime) > e.getStart() && convertMinutesToHours(startTime) < e.getStop())
|
||||
if (startTime >= e.getStart() && startTime < e.getStop())
|
||||
{
|
||||
for (DrawObject d : objects)
|
||||
posEvents.add(e);
|
||||
}
|
||||
}
|
||||
|
||||
if (posEvents.size() != 0)
|
||||
{
|
||||
int r = (int) Math.floor(Math.random() * posEvents.size());
|
||||
Event evs = posEvents.get(r);
|
||||
for (DrawObject d : objects)
|
||||
{
|
||||
if (d.getFileName().equals("stage"))
|
||||
{
|
||||
if (d.getFileName().equals(e.getStage().getName()))
|
||||
Stage s = (Stage) d;
|
||||
if (s.getStage().getName().equals(evs.getStage().getName()))
|
||||
{
|
||||
position = d.getPosition();
|
||||
position = s.getPosition();
|
||||
targetStage = d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int randomtime = (int) (40 + (Math.random() * (60 - 40)));
|
||||
if (position != null)
|
||||
{
|
||||
actions.add(new Action(position, startTime, randomtime, targetStage));
|
||||
Waypoint w = getClosestWaypoint(position);
|
||||
actions.add(new Action(position, startTime, randomtime, targetStage, w));
|
||||
}
|
||||
startTime = startTime + randomtime;
|
||||
|
||||
}
|
||||
else if (random >= 30 && random < 35)
|
||||
else if (random >= 40 && random < 45)
|
||||
{
|
||||
Point2D position = null;
|
||||
DrawObject targetDraw = null;
|
||||
DrawObject targetStage = null;
|
||||
ArrayList<DrawObject> foodPlaces = new ArrayList<DrawObject>();
|
||||
for (DrawObject d : objects)
|
||||
{
|
||||
if (d.getFileName().equals("entrance"))
|
||||
if (d.getFileName().equals("food"))
|
||||
{
|
||||
position = d.getPosition();
|
||||
foodPlaces.add(d);
|
||||
}
|
||||
}
|
||||
if (position != null)
|
||||
|
||||
if (foodPlaces.size() != 0)
|
||||
{
|
||||
actions.add(new Action(position, startTime, 35, targetDraw));
|
||||
startTime = startTime + 35;
|
||||
int r = (int) Math.floor(Math.random() * foodPlaces.size());
|
||||
position = foodPlaces.get(r).getPosition();
|
||||
targetStage = foodPlaces.get(r);
|
||||
Waypoint w = getClosestWaypoint(position);
|
||||
actions.add(new Action(position, startTime, 35, targetStage, w));
|
||||
startTime = startTime + 20;
|
||||
}
|
||||
startTime = startTime + 35;
|
||||
|
||||
}
|
||||
else if (random > 35)
|
||||
else if (random > 45)
|
||||
{
|
||||
Point2D position = null;
|
||||
DrawObject targetDrawobj = null;
|
||||
DrawObject targetStage = null;
|
||||
ArrayList<DrawObject> toilets = new ArrayList<DrawObject>();
|
||||
for (DrawObject d : objects)
|
||||
{
|
||||
if (d.getFileName().equals("wc"))
|
||||
{
|
||||
position = d.getPosition();
|
||||
toilets.add(d);
|
||||
}
|
||||
}
|
||||
if (position != null)
|
||||
if (toilets.size() != 0)
|
||||
{
|
||||
actions.add(new Action(position, startTime, 35, targetDrawobj));
|
||||
int r = (int) Math.floor(Math.random() * toilets.size());
|
||||
position = toilets.get(r).getPosition();
|
||||
targetStage = toilets.get(r);
|
||||
Waypoint w = getClosestWaypoint(position);
|
||||
actions.add(new Action(position, startTime, 35, targetStage, w));
|
||||
startTime = startTime + 20;
|
||||
}
|
||||
startTime = startTime + 35;
|
||||
|
||||
@@ -338,35 +462,69 @@ public class Visitor
|
||||
}
|
||||
}
|
||||
|
||||
public int convertMinutesToHours(int startTime)
|
||||
public Waypoint getClosestWaypoint(Point2D point)
|
||||
{
|
||||
int time = 0;
|
||||
int hours = startTime / 60;
|
||||
int minutes = startTime % 60;
|
||||
time = (hours * 100) + minutes;
|
||||
return time;
|
||||
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 int getEndX()
|
||||
public boolean hasCollisionDO(DrawObject object)
|
||||
{
|
||||
Image image = Images.getImage(filename);
|
||||
return (int) (position.getX() + image.getWidth(null));
|
||||
Image image2 = Images.getImage(object.getFileName());
|
||||
Rectangle recta = new Rectangle((int) position.getX(), (int) position.getY(), image.getWidth(null), image.getHeight(null));
|
||||
Rectangle rectb = new Rectangle((int) object.getPosition().getX(), (int) object.getPosition().getY(), image2.getWidth(null), image2.getHeight(null));
|
||||
|
||||
Area a = new Area(recta);
|
||||
Area b = new Area(rectb);
|
||||
|
||||
AffineTransform transA = new AffineTransform();
|
||||
transA.rotate(rotation, position.getX(), position.getY());
|
||||
|
||||
AffineTransform transB = new AffineTransform();
|
||||
transB.rotate(Math.toRadians(object.getRotation()), object.getPosition().getX(), object.getPosition().getY());
|
||||
|
||||
Area aa = a.createTransformedArea(transA);
|
||||
Area bb = b.createTransformedArea(transB);
|
||||
|
||||
if (bb.intersects(aa.getBounds2D()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getEndY()
|
||||
public boolean hasCollision(Visitor v)
|
||||
{
|
||||
Image image = Images.getImage(filename);
|
||||
return (int) (position.getY() + image.getHeight(null));
|
||||
}
|
||||
Rectangle a = new Rectangle((int) position.getX(), (int) position.getY(), image.getWidth(null), image.getHeight(null));
|
||||
Rectangle b = new Rectangle((int) v.position.getX(), (int) v.position.getY(), image.getWidth(null), image.getHeight(null));
|
||||
|
||||
public boolean hitTest(DrawObject to2)
|
||||
{
|
||||
return (getEndX() >= to2.getPosition().getX() && getEndY() >= to2.getPosition().getY() && to2.getEndX() >= position.getX() && to2.getEndY() >= position.getY());
|
||||
Area aa = new Area(a);
|
||||
Area bb = new Area(b);
|
||||
|
||||
}
|
||||
AffineTransform af = new AffineTransform();
|
||||
af.rotate(rotation, position.getX(), position.getY());
|
||||
|
||||
public boolean hitTestVisitor(Visitor v)
|
||||
{
|
||||
return (getEndX() >= v.position.getX() && getEndY() >= v.position.getY() && v.getEndX() >= position.getX() && v.getEndY() >= position.getY());
|
||||
AffineTransform bf = new AffineTransform();
|
||||
bf.rotate(v.rotation, v.position.getX(), v.position.getY());
|
||||
|
||||
Area ra = aa.createTransformedArea(af);
|
||||
Area rb = bb.createTransformedArea(bf);
|
||||
|
||||
if (ra.intersects(rb.getBounds2D()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user