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
+7 -1
View File
@@ -14,7 +14,11 @@ import java.io.Serializable;
import javax.swing.ImageIcon;
public class DrawObject implements Serializable{
public abstract class DrawObject implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
Point2D position;
double rotation;
double scale;
@@ -41,6 +45,8 @@ public class DrawObject implements Serializable{
this.position = position;
}
public abstract String getName();
public void draw(Graphics2D g)
{
AffineTransform tx = getTransform();
+2 -3
View File
@@ -10,8 +10,7 @@ public class Images{
private static Map<String, Image> images;
public Images() {
public Images() {
images = new HashMap<String, Image>();
images.put("entrance", loadImage("images/entrance.png"));
@@ -20,7 +19,7 @@ public class Images{
images.put("stage", loadImage("images/stage4.png"));
images.put("wall", loadImage("images/wall.png"));
images.put("stage1", loadImage("images/stage3.png"));
images.put("visitor", loadImage("images/visitor.png"));
}
public static Image loadImage(String path) {
-1
View File
@@ -5,7 +5,6 @@ import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
public class MenuBar extends JMenuBar {
+4 -4
View File
@@ -105,7 +105,7 @@ public class Panel extends JPanel implements ActionListener {
}
public void addVisitors(){
visitors.add(new Visitor("images/coin.png", new Point(100,300), agenda, objects));
visitors.add(new Visitor("visitor", new Point(100,300), agenda, objects));
}
public int getPanelInfoLength() {
@@ -181,14 +181,14 @@ public class Panel extends JPanel implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
tick++;
if ( tick >= 10){
if ( tick >= 10 && visitors.size() > 0){
tick = 0;
currentTime++;
//System.out.println(currentTime);
System.out.println(currentTime);
}
for (Visitor v: visitors){
v.update(objects, currentTime);
v.update(objects, currentTime, visitors);
}
repaint();
-2
View File
@@ -11,8 +11,6 @@ import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import Agenda.Agenda;
public class SaveLoad {
+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());
}
}
-4
View File
@@ -24,10 +24,6 @@ public class MouseMotion extends MouseMotionAdapter {
panel.getDragObject().setPosition(new Point2D.Double(
(panel.getDragObject().getPosition().getX() ) - ((panel.getLastClickPosition().getX() - clickPoint.getX()) ) / panel.getDragObject().getScale(),
(panel.getDragObject().getPosition().getY()) - (panel.getLastClickPosition().getY() - clickPoint.getY()) / panel.getDragObject().getScale() ));
System.out.println(panel.getDragObject().getPosition().getX());
System.out.println(panel.getLastClickPosition().getX());
System.out.println(clickPoint.getX());
System.out.println("----------'");
break;
case "upperLeft":
if(clickPoint.getX() < panel.getDragObject().getPosition().getX() && clickPoint.getY() < panel.getDragObject().getPosition().getY()) {
-1
View File
@@ -10,7 +10,6 @@ public class Path extends DrawObject {
super("path", position);
}
@Override
public String getName() {
return "Path";
}
+6
View File
@@ -10,4 +10,10 @@ public class Podium extends DrawObject {
super("stage", position);
}
@Override
public String getName() {
return "stage";
}
}