📄 tower.java
字号:
package gameoflife;
import static java.lang.System.out;
import java.awt.Color;
/**************************************
* Acts as a tower factory.
**************************************/
public abstract class Tower
{
protected Point offset;
int sizeInCell;
protected Point[] points;
Xenomorph dest;
int demage;
int range;
int interval;
int leftSlice;
public Tower(int xof, int yof)
{
offset = new Point(xof,yof);
}
public Point[] getPoints()
{
return points;
}
public Point getOffset()
{
return offset;
}
public static Tower createMachineGun(int xOffset, int yOffset)
{
return new MachineGun(xOffset, yOffset);
}
public static Tower createQuickFirer(int xOffset, int yOffset)
{
return new QuickFirer(xOffset, yOffset);
}
public static Tower createSlowFirer(int xOffset, int yOffset)
{
return new SlowFirer(xOffset, yOffset);
}
public static Tower createAroundFirer(int xOffset, int yOffset)
{
return new AroundFirer(xOffset, yOffset);
}
protected double distance2(Xenomorph xeno) {
double x = xeno.x_center, y = xeno.y_center;
double thisx = this.offset.x + this.sizeInCell / 2;
double thisy = this.offset.y + this.sizeInCell / 2;
return Math.sqrt((x - thisx)*(x - thisx) + (y - thisy)*(y - thisy));
}
public Bullet searchAndFire(World world) {
//System.out.println("" + this.toString() + " fire at " + this.dest);
return null;
}
}
/**
*
*/
class MachineGun extends Tower {
public MachineGun(int xo, int yo)
{
super(xo,yo);
demage = 20;
range = 70;
sizeInCell = 6;
interval = 8;
points = new Point[] {
new Point(2,0), new Point(3,0),
new Point(2,1), new Point(3,1),
new Point(2,2), new Point(3,2),
new Point(0,3), new Point(2,3),new Point(3,3), new Point(5,3),
new Point(0,4), new Point(5,4),
new Point(1,5), new Point(2,5), new Point(3,5), new Point(4,5),
};
/** ..xx..
* ..xx..
..xx..
x.xx.x
x....x
.xxxx. */
for(Point p : points) {
p.color = Color.green;
}
}
public Bullet searchAndFire(World world) {
if(dest != null && distance2(dest) < range && dest.willBlood > 0) {
if(leftSlice == 0) {
Bullet b = new GunBullet(this.offset.x + this.sizeInCell / 2,
this.offset.y + this.sizeInCell / 2, dest, this.demage);
dest.onAttack( b );
leftSlice = interval;
super.searchAndFire(world);
//System.out.println( b );
return b;
} else {
leftSlice --;
}
} else {
dest = null;
for(Xenomorph xeno : world.xenos) {
if(xeno.willBlood > 0 && distance2(xeno) < range) {
this.dest = xeno;
}
}
}
return null;
}
}
/**
*
*/
class QuickFirer extends Tower
{
public QuickFirer(int xo, int yo)
{
super(xo,yo);
demage = 5;
range = 65;
sizeInCell = 6;
interval = 3;
points = new Point[] {
new Point(1,0), new Point(4,0),
new Point(1,1), new Point(4,1),
new Point(1,2), new Point(4,2),
new Point(0,3), new Point(1,3), new Point(2,3),new Point(3,3), new Point(4,3), new Point(5,3),
new Point(1,4), new Point(4,4),
new Point(0,5), new Point(1,5), new Point(2,5),new Point(3,5), new Point(4,5), new Point(5,5),
};
/** .x..x.
* .x..x.
.x..x.
xxxxxx
.x..x.
xxxxxx */
for(Point p : points) {
p.color = Color.green;
}
}
public Bullet searchAndFire(World world) {
if(dest != null && distance2(dest) < range && dest.willBlood > 0) {
if(leftSlice == 0) {
Bullet b = new QuickBullet(this.offset.x + this.sizeInCell / 2,
this.offset.y + this.sizeInCell / 2, dest, demage);
dest.onAttack(b);
leftSlice = interval;
super.searchAndFire(world);
return b;
} else {
leftSlice --;
}
} else {
dest = null;
for(Xenomorph xeno : world.xenos) {
if(xeno.willBlood > 0 && distance2(xeno) < range) {
this.dest = xeno;
}
}
}
return null;
}
}
/**
*
*/
class SlowFirer extends Tower
{
public SlowFirer(int xo, int yo)
{
super(xo,yo);
demage = 16;
range = 120;
sizeInCell = 6;
interval = 12;
points = new Point[] {
new Point(1,0), new Point(4,0),
new Point(1,1), new Point(4,1),
new Point(1,2), new Point(4,2),
new Point(2,3), new Point(3,3),
new Point(1,4), new Point(4,4),
new Point(0,5), new Point(1,5), new Point(2,5),new Point(3,5), new Point(4,5), new Point(5,5),
};
/** .x..x.
* .x..x.
.x..x.
..xx..
.x..x.
xxxxxx */
for(Point p : points) {
p.color = Color.green;
}
}
public Bullet searchAndFire(World world) {
if(dest != null && distance2(dest) < range && dest.willBlood > 0) {
if(leftSlice == 0) {
Bullet b = new SlowBullet(this.offset.x + this.sizeInCell / 2,
this.offset.y + this.sizeInCell / 2, dest, demage);
dest.onAttack(b);
leftSlice = interval;
super.searchAndFire(world);
return b;
} else {
leftSlice --;
}
} else {
dest = null;
for(Xenomorph xeno : world.xenos) {
if(xeno.willBlood > 0 && distance2(xeno) < range) {
this.dest = xeno;
}
}
}
return null;
}
}
/**
*
*/
class AroundFirer extends Tower
{
public AroundFirer(int xo, int yo)
{
super(xo,yo);
demage = 10;
range = 20;
sizeInCell = 6;
interval = 10;
points = new Point[] {
new Point(2,0), new Point(3,0),
new Point(1,1), new Point(4,1),
new Point(0,2), new Point(5,2),
new Point(0,3), new Point(5,3),
new Point(1,4), new Point(4,4),
new Point(2,5), new Point(3,5)
};
/** ..xx..
* .x..x.
x.xx.x
x.xx.x
.x..x.
..xx.. */
for(Point p : points) {
p.color = Color.green;
}
}
public Bullet searchAndFire(World world) {
if(leftSlice == 0) {
for(Xenomorph xeno : world.xenos) {
if(xeno.willBlood > 0 && distance2(xeno) < range) {
Bullet b = new AroundBullet(this.offset.x + this.sizeInCell / 2,
this.offset.y + this.sizeInCell / 2, xeno, demage);
xeno.onAttack(b);
/**
*
*/
world.bullets.add(b);
leftSlice = interval;
this.dest = xeno;
super.searchAndFire(world);
}
}
leftSlice = interval;
} else {
leftSlice --;
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -