Przeglądaj źródła

Added AccessPoint support, watch out, still buggy

Supermaniac101 10 lat temu
rodzic
commit
ad1b9d2cdd

BIN
res/simulator/ingang.png


BIN
res/visitor.png


+ 85 - 1
src/gui/simulator/AccessPoint.java

@@ -1,5 +1,89 @@
 package gui.simulator;
 
-public class AccessPoint {
+import gui.frames.MainFrame;
 
+public class AccessPoint extends Draw{
+	
+	private static String imageLocation = "/simulator/ingang.png";
+	private static String facilityName = "Ingang";
+	private Terrain terrain;
+
+    public AccessPoint(Terrain terrain, int x, int y) {
+        super(imageLocation, x, y, 1, 0);
+        this.terrain = terrain;
+    }
+
+    public AccessPoint(Terrain terrain, int x, int y, double scale, double distance) {
+        super(imageLocation, x, y, scale, distance);
+        this.terrain = terrain;
+    }
+    
+    public String getFacilityName(){
+    	return facilityName;
+    }
+    
+    @Override
+    public void setX(double x){
+    	if(x>0 && x<terrain.getWidth()/6){
+    		this.setRotation(90);
+    		this.x = -50; 
+    	} else if(x>=terrain.getWidth()/6 && x<5*terrain.getWidth()/6){
+    		this.x = x;
+    		this.setRotation(180);
+    	} else if(x>=terrain.getWidth()/6 && x<5*terrain.getWidth()/6 && this.getHeight()>0 && this.getHeight()<terrain.getLength()/4){
+    		this.x = x;
+    		this.setRotation(180);
+    	} else if(x>=terrain.getWidth()/6 && x<5*terrain.getWidth()/6 && this.getHeight()>3*(terrain.getLength()/4) && this.getHeight()<terrain.getLength()){
+    		this.x = x;
+    		this.setRotation(180);
+    	} else if(x>=5*(terrain.getWidth()/6) && x<=terrain.getWidth()){
+    		this.setRotation(90);
+    		this.x = terrain.getWidth()-200;
+    	}
+    	else if(x<0){
+    		this.setRotation(90);
+    		this.x = -50;
+    	}
+    	else if(x>terrain.getWidth()){
+    		this.setRotation(90);
+    		this.x = terrain.getWidth()-200;
+    	}
+    	else {
+    		this.x = x;
+    		this.setRotation(180);
+    	}
+    }
+
+    @Override
+    public void setY(double y){
+    	if(y>0 && y<terrain.getLength()/6){
+    		this.setRotation(90);
+    		this.y = 100;     
+    	} else if(y>0 && y<terrain.getLength()/6&& this.getX()>terrain.getWidth()/6 && this.getX()<5*terrain.getWidth()/6){
+    		this.y = 100; 
+        		this.setRotation(180);	
+    	} else if(y>=terrain.getLength()/6 && y<5*terrain.getLength()/6){
+    		this.y = y;
+    		this.setRotation(90);
+    	}
+    	else if(y>=5*(terrain.getLength()/6) && y<=terrain.getLength()){
+    		this.setRotation(90);
+    		this.y = terrain.getLength()-200;
+    	}
+    	else if(y>=5*(terrain.getLength()/6) && y<=terrain.getLength() && this.getX()>terrain.getWidth()/6 && this.getX()<5*terrain.getWidth()/6){
+    		this.setRotation(180);
+    		this.y = terrain.getLength()-200;
+    	}
+    	else if(y<0){
+    		this.setRotation(180);
+    		this.y = 100;
+    	}
+    	else if(y>terrain.getLength()){
+    		this.setRotation(180);
+    		this.y = terrain.getLength()-200;
+    	}
+    	else {
+    		this.y = y;
+    	}
+    }
 }

+ 4 - 0
src/gui/simulator/Draw.java

@@ -46,6 +46,10 @@ public abstract class Draw {
         Shape shape = new Rectangle2D.Double(0, 0, image.getWidth(null), image.getHeight(null));
         return this.getAffineTransform().createTransformedShape(shape).contains(point);
     }
+    
+    public Image getImage(){
+    	return image;
+    }
 
     //getters and setters down here
     public double getX() {

+ 16 - 6
src/gui/simulator/Terrain.java

@@ -85,12 +85,12 @@ public class Terrain extends JPanel {
 		{
 			if(e.getY() < 300){
 				dragObject = new Stage((int)clickPoint.getX(),(int)clickPoint.getY(), 0.5, 10);
-			}else if(e.getY() > 300 && e.getY() < 600){
+			}else if(e.getY() > 300 && e.getY() < 550){
 				dragObject = new RestRoom((int)clickPoint.getX(),(int)clickPoint.getY(), 0.5, 10);
-			}else if(e.getY() > 600 && e.getY() < 900){
-				//dragObject = new custom facility
+			}else if(e.getY() > 550 && e.getY() < 800){
+				dragObject = new AccessPoint(this, (int)clickPoint.getX(),(int)clickPoint.getY(), 0.5, 10);
 			}
-			else if(e.getY() > 900 && e.getY() < 1200){
+			else if(e.getY() > 800 && e.getY() < 1100){
 				//dragObject = new custom facility
 			}
 			entities.add(dragObject);
@@ -202,20 +202,23 @@ public class Terrain extends JPanel {
         g2.setColor(Color.BLACK);
         g2.setStroke(new BasicStroke(5));
         g2.drawLine(199, 0, 199, getHeight());
-        	//draw objects
+        //draw objects
         Draw object1 = new Stage(0,50);
         object1.draw(g2);
         g2.drawString("stage", 80, 275);
         Draw object2 = new RestRoom(0,300, 0.5, 10);
         object2.draw(g2);
         g2.drawString("toilettage", 70, 525);
+        Draw object3 = new AccessPoint(this, 0,550, 0.5, 10);
+        object3.draw(g2);
+        g2.drawString("ingang", 70, 675);
 		
 		//draw simulation field
 		g2.setClip(new Rectangle2D.Double(200, 0, getWidth()-200, getHeight()));
 		AffineTransform oldTransform = g2.getTransform();
 		g2.translate(200,0);
 		g2.setTransform(getCamera());
-        TexturePaint p = new TexturePaint(background, new Rectangle2D.Double(0, 0, 500, 500));
+        TexturePaint p = new TexturePaint(background, new Rectangle2D.Double(0, 0, 200, 200));
 		g2.setPaint(p);
 		g2.fill(new Rectangle2D.Double(0,0,width,length));
         
@@ -278,4 +281,11 @@ public class Terrain extends JPanel {
 		tx.scale(cameraScale, cameraScale);
 		return tx;
 	}
+    
+    public int getLength(){
+    	return length;
+    }
+    public int getWidth(){
+    	return width;
+    }
 }

+ 86 - 0
src/gui/simulator/Visitor.java

@@ -1,5 +1,91 @@
 package gui.simulator;
 
+import java.awt.Graphics2D;
+import java.awt.Image;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Point2D;
+import java.util.ArrayList;
+
+import javax.swing.ImageIcon;
+
+
 public class Visitor {
+	Point2D positie;
+	double rotation;
+	double speed;
+	Point2D target;
+	
+	Image image = new ImageIcon("visitor.png").getImage();
+	
+
 
+	public Visitor(Point2D positie) {
+		this.positie = positie;
+		this.rotation = 0;
+		this.speed = 1;
+		this.target = new Point2D.Double(0, 0);
+	}
+
+	void update(ArrayList<Visitor> visitors)
+	{
+		
+		Point2D difference = new Point2D.Double(
+				target.getX() - positie.getX(),
+				target.getY() - positie.getY()
+				);
+		
+		double newRotation = Math.atan2(difference.getY(), difference.getX());
+		double rotDifference = rotation - newRotation;
+		while(rotDifference > Math.PI)
+			rotDifference -= 2 * Math.PI;
+		while(rotDifference < -Math.PI)
+			rotDifference += 2 * Math.PI;
+		
+		if(Math.abs(rotDifference) < 0.1)
+			rotation = newRotation;
+		else if(rotDifference < 0)
+			rotation += 0.1;
+		else if(rotDifference > 0)
+			rotation -= 0.1;
+		
+		
+		
+		Point2D oldPositie = positie;
+		
+		positie = new Point2D.Double(
+				positie.getX() + speed * Math.cos(rotation),
+				positie.getY() + speed * Math.sin(rotation)
+				);
+		
+
+		if(hasCollision(visitors))
+		{
+			positie = oldPositie;
+			rotation += 0.2;
+		}
+		
+		
+	}
+	
+	void paint(Graphics2D g)
+	{
+		AffineTransform tx = new AffineTransform();
+		tx.translate(positie.getX()-32, positie.getY()-32);
+		tx.rotate(rotation, 32, 32);
+		
+		g.drawImage(image, tx ,null);
+		
+	}
+
+	public boolean hasCollision(ArrayList<Visitor> visitors) {
+		for(Visitor b : visitors)
+		{
+			if(b == this)
+				continue;
+			if(b.positie.distance(positie) < 64)
+				return true;
+		}
+		return false;
+	}
 }
+