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

📄 towerselector.java

📁 一个Java的塔防小游戏
💻 JAVA
字号:
package gameoflife;

public class TowerSelector {
	
	/***
	 * Set to be the left up coordinate
	 */
	int xoff, yoff;
	int width, height;
	int n;
	
	/**  the real time position of cursor  **/
	int curx, cury;
	
	/**  the position of current hight light area under cursor, in UNIT  **/
	int x_unit,  y_unit;
	
	boolean  planto  = false;
	boolean  pressed = false;
	
	int USIZE, CELLSIZE;
	int w_in_unit, h_in_unit;
	
	Tower  menu[] = new Tower[]{
			/**  the offset is in unit of unit in menu  **/
			Tower.createMachineGun(0, 0),
			Tower.createQuickFirer(1, 0),
			Tower.createSlowFirer(2, 0),
			Tower.createAroundFirer(3, 0)
	}; 
	
	public TowerSelector(int usize, int cellSize, int w, int h) {
		USIZE = usize; 
		CELLSIZE = cellSize;
		w_in_unit = w / USIZE;
		h_in_unit = h / USIZE;
	}
	
	public void  onRightButtonDown(int xd, int yd, int w, int h, int n) {
		this.xoff = xd - w / 2;
		this.yoff = yd - h;
		this.width  = w;
		this.height = h;
		this.n      = n;
		if(w <= 0) {
			throw new RuntimeException("Invalid width");
		}
		if(h <= 0) {
			throw new RuntimeException("Invalid height");
		}
		if(n <= 0 || n > w / 2) {
			throw new RuntimeException("Invalid n");
		}
		pressed = true;
	}
	
	public int  onRightButtonUp(int xu, int yu) {
		pressed = false;
		if(xu >= xoff && xu <= xoff + width && yu >= yoff && yu <= yoff + height) {
			int diff = xu - xoff;
			return (n*diff) / width;
		}
		return -1;
	}
	
	public boolean  outRange() {
		int  cx = this.curx;
		int  cy = this.cury;
		if(cx < this.x_unit * USIZE * CELLSIZE || cx > (this.x_unit + 2) * USIZE * CELLSIZE) {
			return true;
		}
		if(cy < this.y_unit * USIZE * CELLSIZE || cy > (this.y_unit + 2) * USIZE * CELLSIZE) {
			return true;
		}
		return false;
	}
	
	public void  catchupCursor() {
		while( curx < (x_unit + 0.5) * CELLSIZE * USIZE ) {
			x_unit --;
		}
		if(x_unit < 0) {
			x_unit = 0;
		}
		while( curx > (x_unit+1.5) * CELLSIZE * USIZE ) {
			x_unit ++;
		}
		if(x_unit >= w_in_unit - 1) {
			x_unit = w_in_unit - 2;
		}
		while( cury < (y_unit+0.5) * CELLSIZE * USIZE ) {
			y_unit --;
		}
		if(y_unit < 0) {
			y_unit = 0;
		}
		while( cury > (y_unit+1.5) * CELLSIZE * USIZE ) {
			y_unit ++;
		}
		if(y_unit >= h_in_unit - 1) {
			y_unit = h_in_unit - 2;
		}
	}
}

⌨️ 快捷键说明

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