Java 如何使对象随条件移动
因此,我试图让卡车只在符合某些标准(使用“ifs”)的情况下才驶向实体(场地) 因此,我的计划是,我有两辆卡车(在一个数组中创建,以防将来我想添加更多,并在脚本中更容易操作),它们最初放置在中心的一个混凝土工厂。用户单击时会在屏幕上显示一个站点(每次单击时都会添加其他站点) 创建站点时,有三个标准可用于选择要移动到站点的卡车:Java 如何使对象随条件移动,java,Java,因此,我试图让卡车只在符合某些标准(使用“ifs”)的情况下才驶向实体(场地) 因此,我的计划是,我有两辆卡车(在一个数组中创建,以防将来我想添加更多,并在脚本中更容易操作),它们最初放置在中心的一个混凝土工厂。用户单击时会在屏幕上显示一个站点(每次单击时都会添加其他站点) 创建站点时,有三个标准可用于选择要移动到站点的卡车: 如果卡车可用(未交付,交付=交付) 如果卡车有足够的水泥(每辆卡车启动时有2个单位(u),每次到达现场时,会松开1个单位。当达到0时,需要返回混凝土厂“填充”) 最近的
- 如果卡车可用(未交付,交付=交付)李>
- 如果卡车有足够的水泥(每辆卡车启动时有2个单位(u),每次到达现场时,会松开1个单位。当达到0时,需要返回混凝土厂“填充”)
- 最近的卡车(通过检查距离)李>
/*preload=“factory_12.png”*/
/*preload=“sign.png”*/
/*preload=“simple\u truck.png”*/
ArrayList卡车;
阵列列表站点;
PImage混凝土厂;
普丰阿丰;
int xCoord;
国际协调;
int siteSize=30;
int siteNumber=0;
void setup()//开始时调用一次的内容
{
大小(500500);
concretePlant=loadImage(“factory_12.png”);
aFont=createFont(“工业革命常规”,12);
文本字体(aFont);
xCoord=int(宽度/2);
yCoord=int(高度/2);
//创建存储站点对象的空数组列表
sites=新的ArrayList();
//添加第一个站点
添加(新站点(随机(宽度)、随机(高度)、站点大小);
//储存卡车
卡车=新阵列列表();
卡车。添加(新卡车(xCoord,yCoord));
卡车。添加(新卡车(xCoord,yCoord));
}
void draw()//绘制背景和混凝土植物
{
背景(235、247、255);
图像(混凝土厂、xCoord、yCoord、60、60);
填充(1);
文本(“混凝土厂”,xCoord-20,yCoord+70);
//调用站点
对于(int i=sites.size()-1;i>=0;i--){
Site=sites.get(i);
site.displaySites();
}
浮动[]距离检查=新浮动[lorries.size()];
对于(int i=0;i0){
PVector siteTemp=新的PVector(sites.get(siteNumber).x,sites.get(siteNumber).y);
PVector-lorryTemp=新的PVector(lorries.get(i).location.x,lorries.get(i).location.y);
距离检查[i]=PVector.dist(siteTemp,lorryTemp);
println(“距离检查”);
}否则{
卡车.get(i).destination.x=xCoord;
卡车.get(i).destination.y=yCoord;
println(“返回”);
}
println(“先完成后完成”);
}
}
int smallestD=-1;
对于(inti=0;i0)和&(距离检查[i]==min(距离检查))){
smallestD=i;
}
println(“最小发现”);
}
如果(smallestD>=0){
卡车.get(smallestD).destination.x=站点.get(站点号).x;
货车.get(smallestD).destination.y=sites.get(siteNumber).y;
if(siteNumber //如果(siteNumber我做了一些修改:
- 首先,它不是编译
- 然后我在
Lorry
类中添加了一些实用程序,简化了draw
方法中的代码
- 最后,我调整了
draw
方法中的算法,以选择正确的卡车
我认为下面的代码按预期工作
你仍然需要为工厂建立一个场地,这样你就可以在卡车需要重新装载时将其设置为目的地。然后,当卡车到达工厂时,你可以将其设置为目的地
/*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);
}
}