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

📄 gmmovcomponent.java

📁 坦克游戏
💻 JAVA
字号:
/*
 * Created on 2005-1-25
 *
 * Tank_game
 */
package components;

//import java.awt.Point;
import java.util.Vector;

import shape.*;

/**
 * @author AnSen
 * 
 * object that could move
 */
public abstract class GmMovComponent extends GmAutomatic {
	protected int width = 30;

	protected int height = 30;

	protected double dSpeed = 1;

	protected int iUpRate = 1;

	protected double dAccRate = 0.1;

	protected int iSpeed = 1;

	protected int iMaxSpeed = 5;

	protected boolean isMoving = false;

	protected double dDirect = 0;

	protected double radius = 15;

	/**
	 * @param life
	 * @param ptX
	 * @param ptY
	 * @param width
	 * @param height
	 * @param direct
	 * @param container
	 */
	public GmMovComponent(long life, double ptX, double ptY, int width,
			int height, double direct, Vector container) {
		super(life, ptX, ptY, container);
		this.width = width;
		this.height = height;
		this.dDirect = direct;
		radius = Math.sqrt(height * height / 4 + width * width / 4);
	}

	/**
	 * @return Returns the iMaxSpeed.
	 */
	public int getIMaxSpeed() {
		return iMaxSpeed;
	}

	/**
	 * @param maxSpeed
	 *            The iMaxSpeed to set.
	 */
	public void setIMaxSpeed(int maxSpeed) {
		iMaxSpeed = maxSpeed;
	}

	/**
	 * @return Returns the dAccRate.
	 */
	public double getDAccRate() {
		return dAccRate;
	}

	/**
	 * @param accRate
	 *            The dAccRate to set.
	 */
	public void setDAccRate(double accRate) {
		dAccRate = accRate;
	}

	/**
	 * @return Returns the initSpeed.
	 */
	public int getInitSpeed() {
		return iSpeed;
	}

	/**
	 * @param initSpeed
	 *            The initSpeed to set.
	 */
	public void setInitSpeed(int initSpeed) {
		this.iSpeed = initSpeed;
	}

	/**
	 * @return Returns the dSpeed.
	 */
	public double getDSpeed() {
		return dSpeed;
	}

	public void updateOject() {
		super.updateOject();
		if (isMoving) {
			this.move();
		} else {
			this.initSpeed();
		}
	}

	public void move() {
		double dTrgX = this.ptX + Math.cos(dDirect) * (dSpeed + radius);
		double dTrgY = this.ptY + Math.sin(dDirect) * (dSpeed + radius);
		if (this.isMovable(dTrgX, dTrgY)) {
			this.ptX += Math.cos(dDirect) * dSpeed;
			this.ptY += Math.sin(dDirect) * dSpeed;
			upSpeed();

			//listener proforme
			for (int i = 0; i < this.vGmListeners.size(); i++) {
				Object obj = vGmListeners.get(i);
				if (obj instanceof IGmMovListener) {
					((IGmMovListener) obj).moveProformed(dDirect, dSpeed, ptX,
							ptY);
				}
			}

			moveFrame(this.ishpframe);
			return;
		}
		initSpeed();
	}

	protected void moveFrame(IShape shp) {
		shp.move(dDirect, dSpeed);
	}

	protected boolean isMovable(double dTrgX, double dTrgY) {
		IShape frame;
		for (int i = 0; i < vContainer.size(); i++) {
			Object obj = vContainer.get(i);
			if (obj == this) {
				continue;
			} else if (obj instanceof GmComponent) {
				frame = ((GmComponent) obj).getFrame();
				if (frame != null) {
					if (frame.isAcross(ptX,ptY,dTrgX,dTrgY)) {
						hitWallProformed();
						return false;
					}
					continue;
				}
			}
		}
		return true;
	}

	protected void hitWallProformed(){
		for(int i=0;i<this.vGmListeners.size();i++){
			Object obj=vGmListeners.get(i);
			if(obj instanceof IGmMovListener){
				IGmMovListener gmls=(IGmMovListener)obj;
				gmls.hitWallProformed(ptX,ptY);
			}			
		}
	}
	/**
	 * @return Returns the dDirect.
	 */
	public double getDirect() {
		return dDirect;
	}

	/**
	 * @param inputDirect
	 *            The dInputDirect to set.
	 */
	public void setDirect(double inputDirect) {
		dDirect = inputDirect;
	}

	/**
	 * @return Returns the isMoving.
	 */
	public boolean isMoving() {
		return isMoving;
	}

	/**
	 * @param isMoving
	 *            The isMoving to set.
	 */
	public void setMoving(boolean isMoving) {
		if (this.isMoving == true && isMoving == false) {
			//listener proforme
			for (int i = 0; i < this.vGmListeners.size(); i++) {
				Object obj = vGmListeners.get(i);
				if (obj instanceof IGmMovListener) {
					((IGmMovListener) obj).stopMoveProformed();
				}
			}
		}
		this.isMoving = isMoving;
	}

	/**
	 * @return Returns the height.
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * @return Returns the width.
	 */
	public int getWidth() {
		return width;
	}

	public void addGmListener(IGmMovListener gls) {
		this.vGmListeners.addElement(gls);
	}

	public void upSpeed() {
		/*
		 * if(dSpeed <iMaxSpeed){ dSpeed = dAccRate * (iUpRate++); }
		 */
	}

	public void initSpeed() {
		dSpeed = iSpeed;
		iUpRate = 1;
	}
}

⌨️ 快捷键说明

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