以一系列设定方向移动对象

时间:2018-05-03 15:18:33

标签: java processing

我不熟悉编码和项目的挣扎。我不太了解Java。

我的目标如下:由公司运营商(混凝土厂和卡车供应商)监控和优化建筑工地的混凝土输送服务。

我应该有一个混凝土工厂,两辆卡车和几个由于用户的点击而创建的站点。操作员可随时获得其公司卡车的视野,该卡车将混凝土输送到位于给定地理区域的工地。并且网站的数量在不断变化(=用户点击屏幕)。

变量

混凝土厂 - >坐标(xCoord,yCoord)+大小;

网站(arraylist) - >坐标(site.x,site.y或destination.x,在卡车类中的destination.y)+大小;

货车(x2) - >坐标(location.x,location.y);尺寸;起源;目的地。

货车的行动

转移到网站(1)

到达现场时等待约3秒

转到列表的下一个站点(2)(+等待3秒)

到达2个地点(等待3秒)后回到混凝土厂

转到下一个站点(3),然后(4),回到混凝土厂等...

(基本上它有2个站点并且返回,2个站点并且返回等等......并且它不能进入​​已经交付的站点。)

一旦卡车到达,网站的徽标也会发生变化。

卡车的选择:我有两辆卡车,需要选择哪辆货车:

如果它是免费的(尚未到达网站的途中);

如果已满(已完成0或1个站点,或未完成2个站点);

最近的一个(测量距离)。

与HTML互动

我想是否有可能以某种方式测量每辆卡车的距离并将其显示在HTML屏幕上(如果可能的话,用按键保存数据)。

所以我开始看看我是否可以添加一辆卡车(不知道它是否正确的部分开始,也许行动会更好)。我试着告诉它,如果siteNumber低于2,它将通过网站列表,但如果不是,则新目的地是具体工厂。但它不起作用。

所以这是我的剧本到目前为止(我已经用椭圆和矩形替换了混凝土,场地和卡车的图像,所以你可以运行它):

/*preload = "factory_12.png";*/
/*preload = "sign.png";*/
/*preload = "simple_truck.png";*/

Lorry lorry;

//PImage concretePlant;
//PFont aFont;
int xCoord;
int yCoord;
ArrayList<Site> sites;
int siteSize = 30;

void setup() // What is called once at the beginning
{
  size (500, 500);

  //concretePlant = loadImage("factory_12.png");
  //aFont = createFont("IndustrialRevolution-Regular", 12);
  //textFont(aFont);

  xCoord = int(width/2);
  yCoord = int(height/2);

  //Creating empty Array List where store sites objects
  sites = new ArrayList<Site>();

  //Adding first site
  sites.add(new Site(random(width), random(height), siteSize));

  //storing lorries
  lorry = new Lorry(xCoord, yCoord);
}

void draw() // Draw the background and concrete plant
{
  background (235, 247, 255); //light blue background, not in draw as it wouldn't allow the site image to stay
  //image(concretePlant, xCoord, yCoord, 60, 60);
  ellipse(xCoord, yCoord, 60, 60);
  //fill(1);
  //text("Concrete Plant", xCoord-20, yCoord+70);

  //Calling the sites
  for (int i = sites.size () - 1; i>=0; i--) {
    Site site = sites.get(i);
    site.displaySites();
  }

  //calling the lorry functions
  lorry.updateLorry();
}

void mousePressed() {
  sites.add(new Site(mouseX, mouseY, siteSize));
}

class Site

{
  float x,y;
  float size;
  //PImage picture;

  Site (float xin, float yin, float sin)
  {
    x = xin;
    y = yin;
    size = sin;
    //picture = loadImage("sign.png");
  }

  void displaySites()
  {
    //image(picture, x, y, 60, 60);
    rect(x, y, 60, 60);
  }
}

class Lorry
{
  PVector location1;
  PVector location2;
  PVector concretePlant;
  //PVector velocity;
  //PImage mixer;
  boolean changeDirection;
  int siteNumber = 0;
  Site destination;

  Lorry(float xCoord, float yCoord)
  {
    concretePlant = new PVector(xCoord, yCoord); //Initial start point
    location1 = new PVector(xCoord, yCoord); //Initial start point
    location2 = new PVector(xCoord, yCoord); //Initial start point
    //velocity = new PVector(2, 2);
    //mixer = loadImage("simple_truck.png");
    destination = sites.get(siteNumber);
    changeDirection = false;
  }

  void displayLorry()
  {
    //image(mixer, location1.x, location1.y, 30, 30);
    //image(mixer, location2.x, location2.y, 30, 30);
    ellipse(location1.x, location1.y, 30, 30);
    ellipse(location2.x, location2.y, 30, 30);
  }

  void Move()
  {
    float xdir1 = destination.x - location1.x;
    float ydir1 = destination.y - location1.y;
    PVector dir1 = new PVector (xdir1, ydir1);
    dir1.normalize();
    location1.add(dir1);
    print("1going");

    float xdir2 = destination.x - location2.x;
    float ydir2 = destination.y - location2.y;
    PVector dir2 = new PVector (xdir2, ydir2);
    dir2.normalize();
    location2.add(dir2);
    print("2going");
  }

  void checkProgress()
  {
    for (int siteNumber = 0; siteNumber < 2; siteNumber = siteNumber++);
    if (dist(destination.x, destination.y, location1.x, location1.y) < 1) {
      if (siteNumber <sites.size() -1) {
        siteNumber++; // siteNumber = siteNumber + 1;
        destination = sites.get(siteNumber);
        changeDirection = true;
        println("1reached final site");
      } else {
        destination.x = concretePlant.x;
        destination.y = concretePlant.y;
        println ("1back to home");
      }
      println("1progress checked ");
    }

    if (dist(destination.x, destination.y, location2.x, location2.y) < 1) {

      if (siteNumber <sites.size() -1) {
        siteNumber++; // siteNumber = siteNumber + 1;
        destination = sites.get(siteNumber);
        changeDirection = true;
        println("2reached final site");
      } else {
        destination.x = concretePlant.x;
        destination.y = concretePlant.y;
        println ("2back to home");
      }
      println("2progress checked ");
    }
  }

  void updateLorry()
  {
    displayLorry();
    Move();
    checkProgress();
  }
}

0 个答案:

没有答案