📄 world.java
字号:
package gameoflife;
import static java.lang.System.out;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import com.axon7.jibu.CancelException;
import com.axon7.jibu.IForTask;
import com.axon7.jibu.Parallel;
enum CellAction { NONE, KILL, GIVE_LIFE }
enum CellState { DEAD, ALIVE }
interface KeyCode {
final int START = 10;// enter
final int LEFT = 37;
final int UP = 38;
final int RIGHT = 39;
final int DOWN = 40;
//////////////////////////////////////////// Small keyboard
final int PLACE = 5;//
final int UP_S = 5;//
final int DOWN_S = 5;//
final int LEFT_S = 5;//
final int RIGHT_S = 5;//
final int LEFTUP_S = 5;//
final int LEFTDOWN_S = 5;//
final int RIGHTUP_S = 5;//
final int RIGHTDOWN_S = 5;//
}
class Cell {
//private CellAction action = CellAction.NONE;
//private CellState state = CellState.DEAD;
Color color = Color.GRAY;
}
class Unit {
int state;
public Unit (int st) {
state = st;
}
}
public class World
{
private Cell[][] grid;
private Unit[][] units;// one unit is a UNITSIZE * UNITSIZE martrix of Cells
private PCell [][] cost;
private Point exit;// the destination of every xenomorph
private int width, height;
public static int cellSize;
public static final int UNITSIZE = 5;
private static final Color GRIDCOLOR = Color.WHITE;
private static final Color ALIVECOLOR = Color.ORANGE;
private static final Color DEADCOLOR = Color.GRAY;
/** For state of Unit **/
private static final int VISITED = 11;
private static final int UNVISITED = 10;
private static final int OCCUPIED = 9;
private static final int UNUSED = 8;
List<Xenomorph> xenos = new ArrayList();
List<Tower> towers = new ArrayList();
List<Bullet> bullets = new ArrayList();
List<Explode> explodes = new ArrayList();
Xenomorph xenoSelected;
TowerSelector tselect;
static interface GState {
final int STOP = 1;
final int RUNNING = 1;
}
private int gameState;
public World(int width, int height, int cellSize)
{
xenoSelected = null;
this.width = width;
this.height = height;
this.cellSize = cellSize;
gameState = GState.STOP;
tselect = new TowerSelector(UNITSIZE, cellSize, width, height);
grid = new Cell[width][height];
units = new Unit[width / UNITSIZE][height / UNITSIZE];
cost = new PCell[width / UNITSIZE][height / UNITSIZE];
exit = new Point(width / UNITSIZE - 1, height / UNITSIZE - 1);
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
grid[x][y] = new Cell();
}
}
for(int x = 0; x < width / UNITSIZE; x++) {
for(int y = 0; y < height / UNITSIZE; y++) {
units[x][y] = new Unit(UNUSED);
}
}
////////////////////////////////////////////////////////////////
// /*
this.addTower(Tower.createQuickFirer(150, 100));
this.addTower(Tower.createMachineGun(30, 20));
this.addTower(Tower.createSlowFirer(125, 150));
this.addTower(Tower.createAroundFirer(40, 50));
// */
updateCostMap( new Point(width/UNITSIZE-1, height/UNITSIZE-1) );
}
/*********************************************
* Sets the color of a single cell.
*********************************************/
public void setColor(int x, int y, Color c)
{
if((x >= 0 && x < width) && (y >= 0 && y < height))
grid[x][y].color = c;
}
/**************************************
* Transitions to the next iteration of life.
* Parallel version.
**************************************/
public void parallelOneTimeSlice() throws CancelException
{
/** Move each Xenomorph **/
for(Xenomorph xeno : this.xenos) {
//xeno.moveOneTimeSlice();
xeno.gotoExit(this.cost, UNITSIZE);
}
/** Tower fire, if needed **/
for(Tower t : this.towers) {
Bullet b = t.searchAndFire(this);
if(b != null) {
bullets.add(b);
}
}
/** Move each Bullets **/
List<Bullet> rmbs = new LinkedList();
for(Bullet b : bullets) {
if(b.state == b.REACHED) {
rmbs.add(b);
b.dest.realBlood -= b.demage;
explodes.add( b.createExplode((int)b.x_center, (int)b.y_center) );
} else {
b.moveOneTimeSlice();
}
}
bullets.removeAll(rmbs);
/** do the explode **/
List<Explode> rmexps = new LinkedList();
for(Explode e : explodes) {
e.passOneTimeSlice();
if(e.state == e.OVER) {
rmexps.add(e);
}
}
explodes.removeAll(rmexps);
/** Find the dead xenos, and remove them **/
List<Xenomorph> rms = new LinkedList();
for(Xenomorph x : xenos) {
if( x.isZombie() ) {
rms.add(x);//xenos.remove(x);
}
}
xenos.removeAll(rms);
/*
Parallel.forLoop(0, width, 10, new IForTask(){ public void loopBody(int x) { for(int y = 0; y < height; y++) { findNextState(x, y);} } });
Parallel.forLoop(0, width, 10, new IForTask() { public void loopBody(int x) { for(int y = 0; y < height; y++) { changeState(x, y); } } });
*/
}
/**************************************
* Draws the world to a BufferedImage instance.
* Parallel version.
**************************************/
public void parallelImageDraw(final BufferedImage img, final int cellSize) throws CancelException
{
Graphics2D g2d = img.createGraphics();
g2d.setColor(DEADCOLOR);
g2d.fillRect(0, 0, width * cellSize, height * cellSize);
Parallel.forLoop(0, width, 20, new IForTask()
{
public void loopBody(int x)
{
Graphics2D g2d = img.createGraphics();
for(int y = 0; y < height; y++) {
grid[x][y].color = DEADCOLOR;
//g2d.setColor(grid[x][y].color);
//g2d.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
//g2d.setColor(Color.BLUE);
//g2d.drawRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
});
/** Draw lines **/
for(int i = 0; i <= this.width / UNITSIZE; i++) {
g2d.setColor(Color.BLUE);
g2d.drawLine(i*UNITSIZE*cellSize, 0, i*UNITSIZE*cellSize, height*cellSize);
}
for(int j = 0; j <= this.height / UNITSIZE; j++) {
g2d.setColor(Color.BLUE);
g2d.drawLine(0, j*UNITSIZE*cellSize, width*cellSize, j*UNITSIZE*cellSize);
}
/** Draw Xenomorphs **/
for(Xenomorph xeno : xenos) {
for(Point p : xeno.points) {
g2d.setColor(p.color);
int x = (int)(xeno.x_center + p.x - xeno.sizeInCell / 2);
int y = (int)(xeno.y_center + p.y - xeno.sizeInCell / 2);
if(x >= 0 && x < width && y >= 0 && y < height) {
//grid[x][y].color = p.color;
g2d.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
int gw = xeno.realBlood * xeno.sizeInCell * cellSize / xeno.fullBlood;
int rw = (xeno.fullBlood - xeno.realBlood) * xeno.sizeInCell * cellSize / xeno.fullBlood;
/** Draw its blood **/
g2d.setColor(Color.GREEN);
g2d.fillRect(((int)xeno.x_center-xeno.sizeInCell/2) * cellSize,
((int)xeno.y_center-xeno.sizeInCell/2) * cellSize - 6,
gw,
4);
g2d.setColor(Color.RED);
g2d.fillRect(((int)xeno.x_center-xeno.sizeInCell/2) * cellSize + gw,
((int)xeno.y_center-xeno.sizeInCell/2) * cellSize - 6,
rw,
4);
if( xeno.selected ) {
g2d.setColor(Color.RED);
g2d.drawRect((int)(xeno.x_center-xeno.sizeInCell/2 - 1) * cellSize,
(int)(xeno.y_center-xeno.sizeInCell/2 - 3) * cellSize,
(xeno.sizeInCell+2) * cellSize, (xeno.sizeInCell+4) * cellSize);
}
}
/** Draw Towers **/
for(Tower tw : towers) {
for(Point p : tw.points) {
g2d.setColor(p.color);
int x = (int)(tw.offset.x + p.x);
int y = (int)(tw.offset.y + p.y);
//grid[x][y].color = p.color;
g2d.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
g2d.setColor(Color.RED);
g2d.drawRoundRect((tw.offset.x - tw.range + tw.sizeInCell/2) * cellSize,
(tw.offset.y - tw.range + tw.sizeInCell/2) * cellSize,
2 * tw.range * cellSize, 2 * tw.range * cellSize, 2*tw.range*cellSize, 2*tw.range*cellSize);
}
/** Draw Bullets **/
for(Bullet bt : bullets) {
for(Point p : bt.points) {
g2d.setColor(p.color);
int x = (int)(bt.x_center + p.x - bt.sizeInCell/2);
int y = (int)(bt.y_center + p.y - bt.sizeInCell/2);
if(x >= 0 && x < width && y >= 0 && y < height) {
//grid[x][y].color = p.color;
g2d.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
}
/** Draw Explodes **/
for(Explode ex : explodes) {
for(Point p : ex.pics[ex.life]) {
g2d.setColor( p.color );
int x = ex.x_center + p.x;
int y = ex.y_center + p.y;
if(x >= 0 && x < width && y >= 0 && y < height) {
g2d.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
ex.life ++;
}
/** Draw menu for selecting towers **/
if( tselect.pressed ) {
g2d.setColor(Color.WHITE);
g2d.fillRect(tselect.xoff, tselect.yoff, tselect.width, tselect.height);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -