所以我试图让卡车只在符合某些标准(使用“ifs”)的情况下转向实体(网站)。
所以计划是我有2辆卡车(在数组中创建,以防我希望将来添加更多并且更容易在脚本中操作),并且它们最初放置在中心的混凝土工厂中。用户点击时会在屏幕上显示一个网站(每次点击都会添加其他网站)。
有三个标准可以选择在创建网站时移动到网站的卡车:
我已经创建了一系列循环来检查draw函数中的这些条件。首先说如果卡车可用,那么检查它是否有水泥单位。如果没有,则将目的地更改为concreteplant,如果是,则计算货车与场地之间的距离。
我有另一个循环,可以选择距离最小的货车(smallD)。
另一个循环要说用最小距离的卡车并将目的地指定为站点并转到下一个站点等等+设置交付为真,因为它变得不可用(因此它不会改变方向,因为它将进入点击新网站时的第一个网站。
我已经创建了一个最后一个循环来说明更新卡车,这意味着移动它,显示它并检查进度(卡车类中的功能)。
这是我的剧本:
/*preload = "factory_12.png";*/
/*preload = "sign.png";*/
/*preload = "simple_truck.png";*/
ArrayList<Lorry> lorries;
ArrayList<Site> sites;
PImage concretePlant;
PFont aFont;
int xCoord;
int yCoord;
int siteSize = 30;
int siteNumber = 0;
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
lorries = new ArrayList<Lorry>();
lorries.add(new Lorry(xCoord, yCoord));
lorries.add(new Lorry(xCoord, yCoord));
}
void draw() // Draw the background and concrete plant
{
background (235, 247, 255);
image(concretePlant, 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();
}
float[] distanceCheck = new float[lorries.size()];
for (int i = 0; i< lorries.size (); i++) {
if (lorries.get(i).deliv == false) {
println ("delivery check");
if (lorries.get(i).u > 0) {
PVector siteTemp = new PVector (sites.get(siteNumber).x, sites.get(siteNumber).y);
PVector lorryTemp = new PVector (lorries.get(i).location.x, lorries.get(i).location.y);
distanceCheck[i] = PVector.dist(siteTemp, lorryTemp);
println ("distanceChecked");
} else {
lorries.get(i).destination.x = xCoord;
lorries.get(i).destination.y = yCoord;
println("goingBack");
}
println("first for done");
}
}
int smallestD = -1;
for (int i = 0; i <lorries.size (); i++) {
if ((distanceCheck[i] > 0) && (distanceCheck[i] == min(distanceCheck))) {
smallestD = i;
}
println("smallest found");
}
if (smallestD >= 0) {
lorries.get(smallestD).destination.x = sites.get(siteNumber).x;
lorries.get(smallestD).destination.y = sites.get(siteNumber).y;
if (siteNumber < sites.size() -1) {
siteNumber++; // siteNumber = siteNumber + 1;
}
lorries.get(smallestD).deliv = true;
}
for (int i = 0; i < lorries.size (); i++) {
lorries.get(i).updateLorry();
}
}
void mousePressed() {
sites.add(new Site(mouseX, mouseY, siteSize));
}
class Lorry
{
PVector location;
PVector concretePlant;
PVector velocity;
float d; //distance
int u; //unit of cement
boolean r; //Reserved lorry
boolean deliv; //
PImage mixer;
boolean changeDirection;
PVector destination;
Lorry(float xCoord, float yCoord)
{
concretePlant = new PVector(xCoord, yCoord); //Initial start point
location = new PVector(xCoord, yCoord, 0); //Initial start point
velocity = new PVector(2, 2);
u = 2;
r = false;
deliv = false;
mixer = loadImage("simple_truck.png");
//destination = sites.get(siteNumber);
destination = concretePlant;
//PVector temp = new PVector (destination.x, destination.y);
changeDirection = false;
//d = PVector.dist(location, temp);
}
void displayLorry()
{
image(mixer, location.x, location.y, 30, 30);
}
void move()
{
float xdir = destination.x - location.x;
float ydir = destination.y - location.y;
PVector dir = new PVector (xdir, ydir);
dir.normalize();
location.add(dir);
print("going");
//deliv = true;
}
void checkProgress()
{
if (dist(destination.x, destination.y, location.x, location.y) < 1) {
deliv = false;
//u --;
// if (siteNumber <sites.size() -1) {
// siteNumber++; // siteNumber = siteNumber + 1;
// }
}
}
void updateLorry()
{
displayLorry();
move();
checkProgress();
}
}
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);
}
}
网站
卡车
混凝土厂
答案 0 :(得分:-2)
我做了一些修改:
Lorry
类上添加了一些实用工具,简化了draw
方法中的代码draw
方法中调整算法以选择正确的卡车我认为以下代码按预期工作。
您仍然需要为工厂建立一个站点,这样您就可以在需要重新加载时将其设置为卡车的目的地。然后当它到达工厂时,你将单位设置回2,然后它应该继续到下一个站点。祝你好运!
/*preload = "factory_12.png";*/
/*preload = "sign.png";*/
/*preload = "simple_truck.png";*/
ArrayList<Lorry> lorries;
ArrayList<Site> sites;
PImage concretePlant;
PFont aFont;
int xCoord;
int yCoord;
int siteSize = 30;
int siteNumber = 0; // keeps track of the index of the nest site to deliver to
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
lorries = new ArrayList<Lorry>();
lorries.add(new Lorry("Lorry 1", xCoord, yCoord));
lorries.add(new Lorry("Lorry 2", xCoord, yCoord));
}
void mousePressed() {
sites.add(new Site(mouseX, mouseY, siteSize));
}
// This returns the next site to deliver to
// Returns null if no more sites
Site getNextSite() {
Site site = null;
if(siteNumber < sites.size()) {
site = sites.get(siteNumber);
}
return site;
}
void draw() {
// Draw the background and concrete plant
background (235, 247, 255);
image(concretePlant, xCoord, yCoord, 60, 60);
fill(1);
text("Concrete Plant", xCoord-20, yCoord+70);
// Displaying the sites
for (int i = sites.size () - 1; i>=0; i--) {
sites.get(i).display();
}
// Ckeck if there is a next site to deliver to
Site nextSite = getNextSite();
if(nextSite!=null) {
// If yes, find out which lorry should go
Lorry lorryToGo = null;
// List all available lorries (not delivering or out of units)
ArrayList<Lorry> availableLorries = new ArrayList();
for(Lorry l : lorries) {
if(l.isAvailableForDelivery()) {
availableLorries.add(l);
}
}
// if any, find the closest
if(!availableLorries.isEmpty()) {
lorryToGo = availableLorries.get(0);
for(Lorry l : availableLorries) {
if(l.checkDistanceToSite(nextSite) < lorryToGo.checkDistanceToSite(nextSite)) {
lorryToGo = l;
}
}
}
// If a lorry has to go, set its new destination
// and increase the siteNumber
if(lorryToGo != null) {
println(lorryToGo.name + " to go");
lorryToGo.destination = nextSite;
lorryToGo.delivering = true;
siteNumber++;
}
}
// update all lorries
for(Lorry l : lorries) {
l.updateLorry();
}
}
class Lorry {
String name;
PVector location;
PVector concretePlant;
PVector velocity;
int units; //unit of cement
boolean reserved; //Reserved lorry
boolean delivering; //
PImage mixer;
boolean changeDirection;
Site destination;
Lorry(String name, float xCoord, float yCoord) {
this.name = name;
concretePlant = new PVector(xCoord, yCoord); //Initial start point
location = new PVector(xCoord, yCoord); //Initial start point
velocity = new PVector(2, 2);
units = 2;
reserved = false;
delivering = false;
mixer = loadImage("simple_truck.png");
destination = getNextSite();
changeDirection = false;
}
boolean isAvailableForDelivery() {
return !delivering && units > 0;
}
boolean needsToReload() {
return units <= 0;
}
void displayLorry() {
image(mixer, location.x, location.y, 30, 30);
}
float checkDistanceToSite(Site site) {
return PVector.dist(location, new PVector(site.x, site.y));
}
void move() {
float xdir = destination.x - location.x;
float ydir = destination.y - location.y;
PVector dir = new PVector (xdir, ydir);
dir.normalize();
location.add(dir);
}
void checkProgress() {
if (destination != null && checkDistanceToSite(destination) < 1) {
println(name + " reached destination");
delivering = false;
units--;
if(needsToReload()) {
println(name + " needs to reload");
destination = null;
// put some code here to send the truck back to the plant
//destination = plant
} else {
destination = getNextSite();
println("assigning new destination with index: "+siteNumber);
println("assigning new destination: "+destination);
}
}
}
void updateLorry() {
if(delivering)
move();
displayLorry();
checkProgress();
}
}
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 display() {
image(picture, x, y, 60, 60);
}
}