📄 world.java
字号:
for(int i = 0; i < tselect.n; i++) {
g2d.setColor(Color.BLACK);
g2d.drawRect(tselect.xoff + i*tselect.width/tselect.n, tselect.yoff, tselect.width/tselect.n, tselect.height);
for(Point p : tselect.menu[i].points) {
g2d.setColor(Color.ORANGE);
int x = tselect.xoff + i*tselect.width/tselect.n + p.x * cellSize;
int y = tselect.yoff + p.y * cellSize;
g2d.fillRect(5+x, 5+y, cellSize, cellSize);
}
}
}
/** Draw hight light of unit under mouse **/
if( tselect.planto ) {
//BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);// .TYPE_INT_RGB);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.15f));
if( tselect.outRange() ) {
tselect.catchupCursor();
}
for(int x = 0; x < 2; x++) {
for(int y = 0; y < 2; y++) {
int i = x+tselect.x_unit;
int j = y+tselect.y_unit;
if(units[i]
[j].state == OCCUPIED) {
g2d.setColor(Color.RED);
} else if(units[i]
[j].state == UNUSED) {
g2d.setColor(Color.GREEN);
}
g2d.fillRect(i * cellSize * UNITSIZE, j * cellSize * UNITSIZE, UNITSIZE * cellSize, UNITSIZE * cellSize);
}
}
}
g2d.setColor(ALIVECOLOR);
//g2d.drawRect(0, 0, 100, 100); // 画矩形的边框
//g2d.drawRoundRect(0, 0, 200, 150, 40, 40); // 画圆角矩形
/*
BufferedImage myi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g2d.drawImage( myi, 0, 0, null);
*/
}
/**************************************
* Draws the world to a the console.
**************************************/
public void consoleDraw() {
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
Cell cell = grid[x][y];
if(cell.color != Color.GRAY) System.out.print(" o ");
else System.out.print(" x ");
}
System.out.println();
}
System.out.println();
}
/**************************************************************************
*
**************************************************************************/
public void addXenos(Xenomorph[] ls) {
int i = 0;
for(Xenomorph lf : ls) {
//int xOffset = (int)lf.x_center - lf.sizeInCell / 2;
//int yOffset = (int)lf.y_center - lf.sizeInCell / 2;
//for(Point p : lf.getPoints()){
//setColor(p.x + xOffset, p.y + yOffset, p.color);
//}
this.xenos.add(lf);
}
}
/**************************************************************************
*
**************************************************************************/
public void addTower(Tower tw) {
int x = tw.offset.x;
int y = tw.offset.y;
x /= UNITSIZE;
y /= UNITSIZE;
/** 若已经占用,则不能添加 **/
if( ! unoccupied(x, y)) {
return ;
}
units[x][y].state = OCCUPIED;
units[x+1][y].state = OCCUPIED;
units[x][y+1].state = OCCUPIED;
units[x+1][y+1].state = OCCUPIED;
towers.add(tw);
}
private void removeTower(Tower t) {
int x = t.offset.x;
int y = t.offset.y;
x /= UNITSIZE;
y /= UNITSIZE;
units[x][y].state = UNUSED;
units[x+1][y].state = UNUSED;
units[x][y+1].state = UNUSED;
units[x+1][y+1].state = UNUSED;
towers.remove(t);
}
public void onMouseClicked(MouseEvent e) {
/** Middle button **/
if(e.getButton() == 2) {
/** click to chagne the state of plan to selecting tower **/
tselect.planto = ! tselect.planto;
}
}
/*****************************************************************
*
*
*****************************************************************/
public void onMousePressed(MouseEvent e) {
/** Search for Xeno, and display a high light, may be **/
//out.println("$$$$$$$$$$$" + e.getX() + ", " + e.getY());
int x = e.getX();
int y = e.getY();
/** Left button **/
if(e.getButton() == 1) {
/******** do selecting the xeno [ begin ] *******/
if(xenoSelected != null) {
xenoSelected.selected = false;
}
for(Xenomorph xeno : xenos) {
int xx = (int)(xeno.x_center - xeno.sizeInCell / 2) * cellSize;
int yx = (int)(xeno.y_center - xeno.sizeInCell / 2) * cellSize;
if(x > xx && x < xx + xeno.sizeInCell * cellSize
&& y > yx && y < yx + xeno.sizeInCell * cellSize) {
//if(Math.abs(xeno.x_center - x) < 5 && Math.abs(xeno.y_center - y) < 5) {
xenoSelected = xeno;
xeno.selected = true;
out.println("$$$$$$$$$$$" + e.getX() + ", " + e.getY());
break;
}
}
/******** do selecting the xeno [ end ] *******/
}
/** Right button **/
if(e.getButton() == 3) {
/********* do selecting the tower [ begin ] **********/
tselect.onRightButtonDown(x, y, 4*2*UNITSIZE*cellSize, 2*UNITSIZE*cellSize, 4);
/********* [ end ] **********/
//out.println("$$$$$$$$$$$" + e.getX() + ", " + e.getY());
//out.println("(" + tselect.xoff + ", " + tselect.yoff + ") (" + tselect.width + ", " + tselect.height + ") # " + tselect.n );
}
}
public void onMouseReleased(MouseEvent e) {
int x = e.getX();
int y = e.getY();
/** Rigth button **/
if(e.getButton() == 3) {
tselect.pressed = false;
/******** do select the tower [ begin ] *********/
if( tselect.planto ) {
int ith = tselect.onRightButtonUp(x, y);
int px = tselect.x_unit;
int py = tselect.y_unit;
Tower tw = null;
if(unoccupied(px, py)) {
px *= UNITSIZE;
py *= UNITSIZE;
switch(ith) {
case 0:
tw = Tower.createMachineGun(px, py);
this.addTower(tw);
break;
case 1:
tw = Tower.createQuickFirer(px, py);
this.addTower(tw);
break;
case 2:
tw = Tower.createSlowFirer(px, py);
this.addTower(tw);
break;
case 3:
tw = Tower.createAroundFirer(px, py);
this.addTower(tw);
break;
default:
break;
}
this.updateCostMap(exit);
if( blocked() ) {
out.println("onMouseRelease() - Blocked");
this.removeTower(tw);
}
}
}
}
}
/*****************************************************************
*
*
*****************************************************************/
public void onMouseMoved(MouseEvent e) {
//out.println("%%%%%%%%%%%% " + e.getX() + ", " + e.getY());
if( tselect.planto ) {
tselect.curx = e.getX();
tselect.cury = e.getY();
}
}
/*****************************************************************
*
*****************************************************************/
public void onKey(KeyEvent e) {
if(e.getKeyCode() == KeyCode.START) {
} else if (e.getKeyCode() == KeyCode.UP) {
} else if (e.getKeyCode() == KeyCode.DOWN) {
} else if (e.getKeyCode() == KeyCode.LEFT) {
} else if (e.getKeyCode() == KeyCode.RIGHT) {
}
}
public void onKeyPressed(KeyEvent e) {
out.println(e.getKeyChar() + " " + e.getKeyCode() + " "+ e.getKeyLocation());
}
public void onKeyReleased(KeyEvent e) {
}
/** (ux, uy) (ux+1, uy)
* (ux, uy+1) (ux+1, uy+1)
*/
private boolean unoccupied(int ux, int uy) {
if(units[ux][uy].state == UNUSED && units[ux+1][uy].state == UNUSED
&& units[ux][uy+1].state == UNUSED && units[ux+1][uy+1].state == UNUSED) {
return true;
}
return false;
}
/**
* from, to are using the units of cell, so we need to do transalte
*/
public void updateCostMap(Point p_in_unit) {
/*
int umap[][] = new int[units.length][units[0].length];
for(int x = 0; x < umap.length; x++) {
for(int y = 0; y < umap[0].length; y++) {
if(UNUSED == units[x][y].state) {
umap[x][y] = 0;
} else {
umap[x][y] = -1;
}
}
}
calCost(p_in_unit, umap);
*/
/** 转换成行优先的地图形式 **/
int umap[][] = new int[units[0].length][units.length];
for(int x = 0; x < umap[0].length; x++) {
for(int y = 0; y < umap.length; y++) {
if(UNUSED == units[x][y].state) {
umap[y][x] = 0;
} else {
umap[y][x] = -1;
}
}
}
calCost(p_in_unit, umap);
}
private void calCost(Point p_in_unit, int [][] map) {
MainShortestPath tool = new MainShortestPath();
PCell [][]pcost = tool.shortestPath( p_in_unit.x, p_in_unit.y, map);
/** 再把得到的行优先的结果pcost放入 列优先 的形式 **/
for(int x = 0; x < pcost.length; x++) {
for(int y = 0; y < pcost[0].length; y++) {
this.cost[y][x] = pcost[x][y];
}
}
/*
out.println();out.println();
for(int y = 0; y < height / UNITSIZE; y++) {
for(int x = 0; x < width / UNITSIZE; x++) {
out.printf("%3d", cost[x][y].cost);
}
out.println();
} */
}
private boolean blocked() {
for(int x = 0; x < cost.length; x++) {
for(int y = 0; y < cost[0].length; y++) {
if(this.cost[x][y].cost == Integer.MAX_VALUE) {
return true;
}
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -