⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 moveto.java

📁 java的机器人大战
💻 JAVA
字号:
package samples.advancedbody;
import AIplatform.event.*;
import AIplatform.robot.*;
import java.awt.geom.Point2D;
/**
 *-----------------------------------------------------------------
 * @author:iiley (iiley@hotmail.com)
 * http://www.robochina.org
 * 一个向一系列指定位置移动的机器人
 */
public class MoveTo extends Robot
{
	private double scanedTime;
	private double absBearing;
	private double enemyX;
	private double enemyY;
	private AdvancedBody body;
	//一系列的运动目标点坐标
	private static Point2D.Double[] pos={
		new Point2D.Double(100d,100d),
		new Point2D.Double(400d,400d),
		new Point2D.Double(400d,100d),
		new Point2D.Double(100d,400d),
	};
	private static int posIndex=0;
    public void onBegin(BeginEvent beginevent)
    {   //初始化
		scanedTime = -10;
		//设置车身为一个AdvancedBody
		setBody(body=new AdvancedBody(this));
    }
	public void work(){
		doRadar();
		doMove();
		doGun();
	}

	private void doRadar(){
		//雷达锁定
		if(getTime()-scanedTime>4){ //如果距上次扫描到敌人超过了4个单位时间,视为丢失了敌人,转动一个很大的角度搜寻敌人
			getRadar().turn(Math.PI);
		}else{
			//雷达转向敌人,以实现锁定
	    	double radarTurn=Util.standardAngle(absBearing-getRadar().getHeading());
			getRadar().turn(radarTurn);
		}
	}
	private void doMove(){
		if(body.getDistance()<10d){
			//如果里目标点距离小于10,视为到达目的地,开始向下一个目标点移动
    		body.moveTo(pos[posIndex].x,pos[posIndex].y);
			posIndex++;
			if(posIndex>=pos.length){
				posIndex=0;
			}
		}
		body.work();
	}
	private void doGun(){
		double gunTurn=Util.standardAngle(absBearing-getGun().getHeading());
		getGun().turn(gunTurn);
		getGun().fire(1d);
	}
    // -------------------- function for event handle ---------------
   	public void onScannedRobot( ScannedRobotEvent e ) {
		//记录下扫描到敌人的时间
		scanedTime = getTime();
		//记录下敌人相对自己的方向
		absBearing=Math.atan2(e.getY()-getBody().getY(),e.getX()-getBody().getX());
		enemyX=e.getX();
		enemyY=e.getY();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -