📄 truck.java
字号:
import java.util.*;
import java.lang.*;
import java.awt.*;
public class truck extends vehicle{
final static int LIspeed=4;//the speed is assigned for straight road;
final static int CIspeed=2;//the speed is assigned for curve road;
public truck(int xPosition, int yPosition,int angle1){
super(xPosition,yPosition,angle1);
safeSecond=0;
}
public void draw(Graphics g){
g.setColor(Color.green);
g.drawOval(x-5,y-5,10,10);
g.fillOval(x-5,y-5,10,10);
}
//check if a vehicle can enter road from a station
public boolean canEnter(){
return !(mateVehi.angle>240 && mateVehi.angle<300 && mateVehi.stayStation==0);
}
//check if A truck can hit another truck
public boolean canHit(){
if (angle==0 && mateVehi.angle==0){
return (y-mateVehi.y<=15 && y-mateVehi.y>0);
}
else if (angle==180 && mateVehi.angle==180){
return (mateVehi.y-y<=15 && mateVehi.y-y>0);
}
else if (angle>=0 && angle<=180 && mateVehi.angle>=0 && mateVehi.angle<=180){
if (mateVehi.x>x){return false;}
else{
int xDif=Math.abs(x-mateVehi.x);
int yDif=Math.abs(y-mateVehi.y);
return (xDif*xDif+yDif*yDif<225);
}
}
else{
if (mateVehi.x<x){return false;}
else{
int xDif=Math.abs(x-mateVehi.x);
int yDif=Math.abs(y-mateVehi.y);
return (xDif*xDif+yDif*yDif<225);
}
}
}
//check if a vehicle is close enough to one intersection
public boolean closeToCross(leverage oneleve){
// if (angle==0 && Math.abs(x-oneleve.x)<20){
if (angle==0 && Math.abs(x-oneleve.x)<20){
return (y-(oneleve.y-15)<=30 && y-(oneleve.y-15)>20);
}
// else if (angle==180 && Math.abs(x-oneleve.x)<20){
else if (angle==180 && Math.abs(x-oneleve.x)<20){
return ((oneleve.y+15)-y<=30 && (oneleve.y+15)-y>20);
}
else {return false;}
}
public void move(){
//make choice to stay Station
if (angle<270 && angle>=270-CIspeed){
if (canStay()){
stayStation();
if (mateVehi.stayStation!=0){
setPosition(centerX,centerY+225);
}
else{
setPosition(centerX,centerY+210);
}
angle=270;
return; //stay in station
}
}
if (stayStation!=0){
if (stayStation==1){
//when canEnter() is false, the truck keep its stayStation=1
if (canEnter()){
setPosition(centerX,centerY+250);
stayStation=0;
}
}
else{stayStation--;}
return;
}
//A truck may start walking in the curve part of the road;
if (angle==0 && (y-LIspeed)<(centerY-150)){
angle=0;
setPosition(centerX+100,centerY-150);
}
if (angle==180 && (y+LIspeed)>(centerY+150)){
angle=180;
setPosition(centerX-100,centerY+150);
}
//A truck may start walking in the straight part of the road;
if (angle<360 && angle+CIspeed>=360){
angle=0;
setPosition(centerX+100,centerY+150);
return;
}
if (angle<180 && angle+CIspeed>=180){
angle=180;
setPosition(centerX-100,centerY-150);
return;
}
//walk in straight line or curve circle;
if (angle==0 && !(y==centerY-150)){
setPosition(x,y-LIspeed);
}
else if (angle==180 && !(y==centerY+150)){
setPosition(x,y+LIspeed);
}
else if (angle>=0 && angle<180){
//y0=centerY-150, x0=centerX is the origin point for up half circle;
//x=x0+r*cos(angle), y=y0-r*sin(angle) to truck the trace of circle
angle=(angle+CIspeed)%360;
double c=(double) ((double) angle)*Math.PI/180;
setPosition((centerX)+((int) Math.floor(100*Math.cos(c))),(centerY-150)-((int) Math.floor(100*Math.sin(c))));
}
else {
//y0=centerY+150, x0=centerX is the origin point for down half circle;
//x=x0+r*cos(angle), y=y0-r*sin(angle) to truck the trace of circle
angle=(angle+CIspeed)%360;
double c=(double) ((double) angle)*Math.PI/180;
setPosition((centerX)+((int) Math.floor(100*Math.cos(c))),(centerY+150)-((int) Math.floor(100*Math.sin(c))));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -