This commit is contained in:
unknown
2015-04-07 12:36:14 +02:00
parent d8efbc8616
commit a2110d3e58
9 changed files with 81 additions and 60 deletions
+62 -44
View File
@@ -16,79 +16,90 @@ public class Visitor {
Point2D position;
double rotation;
double scale;
double speed;
String filename;
ArrayList<Action> actions;
Agenda agenda;
ArrayList<DrawObject> objects;
public Visitor(String filename, Point2D position, Agenda agenda, ArrayList<DrawObject> objects) {
public Visitor(String filename, Point2D position, Agenda agenda,
ArrayList<DrawObject> objects) {
this.position = position;
this.filename = filename;
scale = 1;
rotation = 0;
this.rotation = 0;
this.speed = 1 + Math.random()*4;
actions = new ArrayList<Action>();
this.agenda = agenda;
this.objects = objects;
fillActions();
for (Action a : actions){
System.out.println(a.getStartTime());
}
}
public void draw(Graphics2D g2) {
Image image = new ImageIcon(filename).getImage();
g2.drawImage(image, getTransform(), null);
AffineTransform tx = getTransform();
Image image = Images.getImage(filename);
g2.drawImage(image, tx, null);
}
private AffineTransform getTransform() {
AffineTransform tx = new AffineTransform();
tx.scale(scale, scale);
tx.translate(position.getX(), position.getY());
Image image = new ImageIcon(filename).getImage();
tx.rotate(Math.toRadians(rotation), image.getWidth(null) / 2,
image.getHeight(null) / 2);
Image image = Images.getImage(filename);
tx.rotate(rotation, image.getWidth(null) / 2, image.getHeight(null) / 2);
return tx;
}
public void update(ArrayList<DrawObject> objects, int currentTime) {
Point2D target = new Point(-100,-100);
for (Action a : actions){
if (currentTime >= a.getStartTime() && currentTime < a.getStoptime()){
public void update(ArrayList<DrawObject> objects, int currentTime,
ArrayList<Visitor> visitors) {
Point2D target = new Point(-100, -100);
for (Action a : actions) {
if (currentTime >= a.getStartTime()
&& currentTime < a.getStoptime()) {
target = a.getPosition();
}
}
moveToTarget(target, objects);
moveToTarget(target, objects, visitors);
}
public void moveToTarget(Point2D target, ArrayList<DrawObject> objects){
int x = 0;
int y = 0;
public void moveToTarget(Point2D target, ArrayList<DrawObject> objects, ArrayList<Visitor> visitors) {
double newRot = Math.atan2(target.getY()-position.getY(), target.getX()- position.getX());
if (target.getX() > position.getX()){
x = 2;
} else if (target.getX() < position.getX()){
x = - 2;
int difx = (int)(target.getX() - position.getX());
int dify = (int)(target.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;
}
if (target.getY() > position.getY()){
y = 2;
} else if (target.getY() < position.getY()){
y = - 2;
}
Point2D oldPosition = position;
position = new Point2D.Double((int) (position.getX() + x),
(int) (position.getY() + y));
//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 (hitTest(object)) {
possible = false;
}
}
for (Visitor object : visitors) {
if (hitTestVisitor(object) && object != this) {
possible = false;
}
}
if (possible == false) {
position = oldPosition;
rotation += 0.2;
}
}
@@ -98,7 +109,6 @@ public class Visitor {
while (startTime < stopTime) {
int random = (int) Math.floor(Math.random() * 51);
System.out.println(random);
if (random < 30) {
Point2D position = null;
for (Event e : agenda.getEvents()) {
@@ -112,7 +122,7 @@ public class Visitor {
}
}
int randomtime = (int) (40 + (Math.random() * (60 - 40)));
if ( position != null ){
if (position != null) {
actions.add(new Action(position, startTime, randomtime));
}
startTime = startTime + randomtime;
@@ -124,10 +134,10 @@ public class Visitor {
position = d.getPosition();
}
}
if ( position != null){
if (position != null) {
actions.add(new Action(position, startTime, 35));
startTime = startTime + 35;
}
}
startTime = startTime + 35;
} else if (random > 35) {
@@ -137,7 +147,7 @@ public class Visitor {
position = d.getPosition();
}
}
if ( position != null){
if (position != null) {
actions.add(new Action(position, startTime, 35));
}
startTime = startTime + 35;
@@ -155,12 +165,12 @@ public class Visitor {
}
public int getEndX() {
Image image = new ImageIcon(filename).getImage();
Image image = Images.getImage(filename);
return (int) (position.getX() + image.getWidth(null));
}
public int getEndY() {
Image image = new ImageIcon(filename).getImage();
Image image = Images.getImage(filename);
return (int) (position.getY() + image.getHeight(null));
}
@@ -168,5 +178,13 @@ public class Visitor {
return (getEndX() >= to2.getX() && getEndY() >= to2.getY()
&& to2.getEndX() >= position.getX() && to2.getEndY() >= position
.getY());
}
public boolean hitTestVisitor(Visitor v) {
return (getEndX() >= v.position.getX()
&& getEndY() >= v.position.getY()
&& v.getEndX() >= position.getX() && v.getEndY() >= position
.getY());
}
}