Follow waypoints added
This commit is contained in:
@@ -642,7 +642,7 @@ public class Panel extends JPanel implements ActionListener
|
|||||||
|
|
||||||
for (Visitor v : visitors)
|
for (Visitor v : visitors)
|
||||||
{
|
{
|
||||||
v.update(objects, currentTime, visitors, paths);
|
v.update(objects, currentTime, visitors, paths, this);
|
||||||
}
|
}
|
||||||
repaint();
|
repaint();
|
||||||
|
|
||||||
|
|||||||
+77
-13
@@ -42,8 +42,8 @@ public class Visitor
|
|||||||
this.objects = objects;
|
this.objects = objects;
|
||||||
fillActions();
|
fillActions();
|
||||||
System.out.println(actions.size());
|
System.out.println(actions.size());
|
||||||
target = 0;
|
target = 1;
|
||||||
finalTarget = 0;
|
finalTarget = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(Graphics2D g2)
|
public void draw(Graphics2D g2)
|
||||||
@@ -65,17 +65,17 @@ public class Visitor
|
|||||||
|
|
||||||
public DrawObject getEntrance()
|
public DrawObject getEntrance()
|
||||||
{
|
{
|
||||||
for(DrawObject o : objects)
|
for (DrawObject o : objects)
|
||||||
{
|
{
|
||||||
if(o instanceof Entrance)
|
if (o instanceof Entrance)
|
||||||
{
|
{
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
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();
|
DrawObject target = getEntrance();
|
||||||
for (Action a : actions)
|
for (Action a : actions)
|
||||||
@@ -87,14 +87,78 @@ public class Visitor
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
moveToTarget(target, objects, visitors, paths);
|
// moveToTarget(target, objects, visitors, paths);
|
||||||
|
move(target, panel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void move(DrawObject tar)
|
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 (hitTest(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
public void moveToTarget(DrawObject target, ArrayList<DrawObject> objects, ArrayList<Visitor> visitors, ArrayList<Path> paths)
|
||||||
{
|
{
|
||||||
Point2D tar = target.getPosition();
|
Point2D tar = target.getPosition();
|
||||||
@@ -107,7 +171,7 @@ public class Visitor
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Shape containsShape = p.containsPointShape(position);
|
Shape containsShape = p.containsPointShape(position);
|
||||||
if(containsShape != null)
|
if (containsShape != null)
|
||||||
{
|
{
|
||||||
tar = new Point2D.Double(containsShape.getBounds().getCenterX(), containsShape.getBounds().getCenterY());
|
tar = new Point2D.Double(containsShape.getBounds().getCenterX(), containsShape.getBounds().getCenterY());
|
||||||
}
|
}
|
||||||
@@ -215,9 +279,9 @@ public class Visitor
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
lastShape = values.get(lastdistance);
|
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 difx = (int) (target2.getX() - position.getX());
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import java.awt.BorderLayout;
|
|||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.FlowLayout;
|
import java.awt.FlowLayout;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.GridLayout;
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
@@ -22,7 +21,7 @@ public class WaypointPopup extends JFrame {
|
|||||||
private static final long serialVersionUID = 1;
|
private static final long serialVersionUID = 1;
|
||||||
private JLabel title;
|
private JLabel title;
|
||||||
|
|
||||||
public WaypointPopup(DrawObject w)
|
public WaypointPopup(DrawObject w, Panel panel)
|
||||||
{
|
{
|
||||||
super("Edit WayPoint");
|
super("Edit WayPoint");
|
||||||
Waypoint wp = (Waypoint) w;
|
Waypoint wp = (Waypoint) w;
|
||||||
@@ -91,13 +90,13 @@ public class WaypointPopup extends JFrame {
|
|||||||
public void actionPerformed(ActionEvent e)
|
public void actionPerformed(ActionEvent e)
|
||||||
{
|
{
|
||||||
wp.setSelf(Integer.valueOf(tf.getText()));
|
wp.setSelf(Integer.valueOf(tf.getText()));
|
||||||
|
panel.addWaypoint(wp);
|
||||||
dispose();
|
dispose();
|
||||||
String options[] = tx.getText().split("-");
|
String options[] = tx.getText().split("-");
|
||||||
int ioptions[] = new int[options.length];
|
int ioptions[] = new int[options.length];
|
||||||
for(int i = 0; i < options.length; i++)
|
for(int i = 0; i < options.length; i++)
|
||||||
{
|
{
|
||||||
ioptions[i] = Integer.valueOf(options[i]);
|
ioptions[i] = Integer.valueOf(options[i]);
|
||||||
System.out.println(ioptions[i]);
|
|
||||||
}
|
}
|
||||||
wp.setOptions(ioptions);
|
wp.setOptions(ioptions);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user