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

📄 xenomorph.java

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

import java.awt.Color;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.Random;
import static java.lang.System.out;

class Point
{
	public int x, y;
	
	Color   color;

	
	public Point(int x, int y)
	{
		this.x = x;
		this.y = y;
		color = Color.GRAY;
	}
	
	public Point(int x, int y, Color c)
	{
		this.x = x;
		this.y = y;
		color  = c;
	}	
}

/**************************************
 * Acts as a LifeForm factory. Can create three different life forms.
 **************************************/	
public abstract class Xenomorph
{
	protected Point[] points;
	
	double		speed;
	int			fullBlood;
	int			realBlood;
	int			willBlood;
	double		x_center, y_center;
	Stack<Point>	path = null;
	int			sizeInCell;
	List<Bullet>	attackedBy = new ArrayList<Bullet>();
	
	public static int ALIVE   = 4;
	public static int DEAD   = 6;
	public static int ZOMBIE = 5;
	int 	state;
	
	boolean		selected;

	public Xenomorph(int x_center, int y_center)
	{
		this.x_center = x_center;
		this.y_center = y_center;
		this.state    = ALIVE;
		this.selected = false;
	}

	public Point[] getPoints()
	{
		return points;
	}
	
	public synchronized void  onAttack(Bullet bt) {
		this.willBlood -= bt.demage;
		if(willBlood <= 0 && state == ALIVE) {
			state = DEAD;
		}
		this.attackedBy.add(bt);
	}
	public synchronized void  remove(Bullet bt) {
		attackedBy.remove(bt);
		if(attackedBy.size() == 0 && state == DEAD) {
			state = ZOMBIE;
		}
	}
	public boolean  isDead() {
		return  state == DEAD;
	}
	public boolean  isZombie() {
		return  state == ZOMBIE;
	}
	
	/**
	 * 随机移动
	 */
	public void  moveOneTimeSlice() {
		if(path != null && path.size() > 0) {
			Point dest = path.peek();
			double dist = distance(x_center, y_center, dest.x, dest.y);
			
			//out.println(this.toString());//////////////////////////////////
			
			/**  Has arrive one unit, then to the next  **/
			if(dist < speed) {
				path.pop();
				x_center = (0.5 + dest.x) * World.UNITSIZE;// + this.sizeInCell / 2;
				y_center = (0.5 + dest.y) * World.UNITSIZE;// + this.sizeInCell / 2;
				if( path.size() > 0 ) {
					dest = path.peek();
					double ndist = distance(x_center, y_center, dest.x, dest.y);
					x_center += (speed - dist)*((0.5 + dest.x) * World.UNITSIZE - x_center)/ndist;// + this.sizeInCell/2 - x_center);
					y_center += (speed - dist)*((0.5 + dest.y) * World.UNITSIZE - y_center)/ndist;
				}
			} else {
				x_center += speed / dist * ((0.5 + dest.x) * World.UNITSIZE - x_center);
				y_center += speed / dist * ((0.5 + dest.y) * World.UNITSIZE - y_center);
			}
			
		}
		else {
			Random r = new Random();
			path = new Stack<Point>();
			int x = r.nextInt();
			x = x < 0 ? -x : x;
			x %= 100;
			int y = r.nextInt();
			y = y < 0 ? -y : y;
			y %= 50;
			path.push(new Point(x, y));
		}
	}
	
	/**
	 * 走向出口
	 */
	public void  gotoExit(PCell cost[][], int usize) {
		int cux = (int)this.x_center / usize;
		int cuy = (int)this.y_center / usize;
		if(path != null && path.size() > 0) {
			moveOneTimeSlice();
		} else {
			path = new Stack<Point>();
			path.push( minAround(cost, cux, cuy) );
			moveOneTimeSlice();
		}
	}
	
	private Point minAround(PCell cost [][], int ux, int uy) {
		int w = cost.length;
		int h = cost[0].length;
		Point p = new Point(0, 0);
		int  min = Integer.MAX_VALUE;
		if(ux > 0 && cost[ux-1][uy].cost >= 0 && cost[ux-1][uy].cost < min) {
			min = cost[ux-1][uy].cost;
			p.x = ux - 1; p.y = uy;
		}
		if(ux < w-1 && cost[ux+1][uy].cost >= 0 && cost[ux+1][uy].cost < min) {
			min = cost[ux+1][uy].cost;
			p.x = ux + 1; p.y = uy;
		}
		if(uy > 0 && cost[ux][uy-1].cost >= 0 && cost[ux][uy-1].cost < min) {
			min = cost[ux][uy-1].cost;
			p.x = ux; p.y = uy-1;
		}
		if(uy < h-1 && cost[ux][uy+1].cost >= 0 && cost[ux][uy+1].cost < min) {
			min = cost[ux][uy+1].cost;
			p.x = ux; p.y = uy+1;
		}
		return p;
	}
	
	public static Xenomorph createRunner(int xOffset, int yOffset)
	{
		return new Runner(xOffset, yOffset);
	}

	public static Xenomorph createChestburster(int xOffset, int yOffset)
	{
		return new Chestburster(xOffset, yOffset);
	}

	public static Xenomorph createDrone(int xOffset, int yOffset)
	{
		return new Drone(xOffset, yOffset);
	}

	public static Xenomorph createPraetorian(int xOffset, int yOffset)
	{
		return new Praetorian(xOffset, yOffset);
	}
	
	
	public double  distance(double p_x, double p_y, int unit_x, int unit_y) {
		double ux = (unit_x + 0.5) * World.UNITSIZE;
		double uy = (unit_y + 0.5) * World.UNITSIZE;
		//int ux = unit_x * World.UNITSIZE + this.sizeInCell / 2;
		//int uy = unit_y * World.UNITSIZE + this.sizeInCell / 2;
		return Math.sqrt( (ux - p_x)*(ux - p_x)
						+  (uy - p_y)*(uy - p_y) );
	}
	
	public String toString() {
		int x = -1;
		int y = -1;
		if(path.size() > 0) {
			x = (int)(path.peek().x + 0.5 )* World.UNITSIZE;
			y = (int)(path.peek().y + 0.5 )* World.UNITSIZE;
		}
		return "@(" + (int)this.x_center + ", " + (int)this.y_center + ")#{"
			+   this.speed + ", " + this.willBlood + ", B" + attackedBy.size() + "} ~~> (" + x +", " + y+")";
	}
}

/**
 * Chestburster(破胸者)
破胸者是异形胎儿生长的最后阶段,它们具有其宿主的基因特征.一旦长成,它们会选择离开
宿主,通过破开其胸腔而出从而杀死宿主. 破胸者在成长为成年异形的期间只有锋利的主颌
骨来咬伤对手,不过它们应尽量避免战斗.因为没有脚,它们只能在地上爬行.它们要在成长为
成年异形需要肉类食物和蜕一次皮. 
 */
class Chestburster extends Xenomorph
{
	public Chestburster(int x, int y)
	{
		super(x,y);

		fullBlood = willBlood = realBlood = 300;
		speed = 0.75;
		sizeInCell = 6;
		
		points = new Point[] {
		new Point(1,0), new Point(2,0), new Point(3,0), new Point(4,0),
		new Point(0,1), new Point(1,1),
		new Point(0,2),
		new Point(0,3),
		new Point(0,4), new Point(1,4),
		new Point(1,5), new Point(2,5), new Point(3,5), new Point(4,5),
		};
		/** .xxxx.
		 *  xx...x
		    x.....
		    x.....
		    xx...x
		    .xxxx.   */
		for(Point p : points) {
			p.color = Color.CYAN;
		}
	}
}

/**
 * Drone(雄蜂) 
雄蜂是以人类为宿主的异形,它们是优秀的武士,其速度和战斗力均衡.它们头脑灵活,擅长
在通风管活动.它们通常群体活动,虽然它们单独活动时也很致命.
 */
class Drone extends Xenomorph
{
	public Drone(int x, int y)
	{
		super(x,y);		
		
		fullBlood = willBlood = realBlood = 450;
		speed = 0.5;
		sizeInCell = 6;
		
		points = new Point[] {
				new Point(0,0), new Point(1,0), new Point(2,0), new Point(3,0),
				new Point(0,1), new Point(4,1),
				new Point(0,2), new Point(5,2),
				new Point(0,3), new Point(5,3),
				new Point(0,4), new Point(4,4),
				new Point(0,5), new Point(1,5), new Point(2,5), new Point(3,5)
				};
		/** xxxx..
		 *  x...x.
		    x....x
		    x....x
		    x...x.
		    xxxx..   */
		for(Point p : points) {
			p.color = Color.DARK_GRAY;
		}
	}
}

/**
 * Runner(信使) 
信使在巢穴里担任侦察兵的角色,体形略小,更适合在狭窄空间活动.它们是以其他非人类的
哺乳动物为宿主的. 信使拥有其他异形所没有的速度,它们有光滑坚硬的头部,没有脊椎,以
快速扑向对手为主要攻击方式. 

 */
class Runner extends Xenomorph
{
	public Runner(int x, int y)
	{
		super(x,y);
		
		fullBlood = realBlood = willBlood = 500;
		speed = 1;
		sizeInCell = 6;
		
		points = new Point[] {
				new Point(0,0), new Point(1,0), new Point(2,0), new Point(3,0),
				new Point(0,1), new Point(4,1),
				new Point(0,2), new Point(4,2),
				new Point(0,3), new Point(1,3), new Point(2,3), new Point(3,3),
				new Point(0,4), new Point(4,4),
				new Point(0,5), new Point(5,5)
				};
		/** xxxx..
		 *  x...x.
		    x...x.
		    xxxx..
		    x...x.
		    x....x   */
		for(Point p : points) {
			p.color = Color.RED;
		}
	}
}

/**
 * Praetorian(禁卫队) 
禁卫队体形较大,却不容易发现.它们通常潜伏在巢穴周围,是异形女皇的忠诚守卫.禁卫异
形的“身材”仅比王后小一号,, 禁卫队虽然笨重,但拥有像装甲金属板的坚硬皮肤,能抵御一般的武器进攻.而且肉搏能力在5只普通异形之上,其敏捷性和跳跃力一般,它们不能像其它异形一样攀爬墙壁和进行突袭. 
 */
class Praetorian extends Xenomorph
{
	public Praetorian(int x, int y)
	{
		super(x,y);
		
		fullBlood = realBlood = willBlood = 600;
		speed = 1;
		sizeInCell = 6;
		
		points = new Point[] {
				new Point(0,0), new Point(1,0), new Point(2,0), new Point(3,0),
				new Point(0,1), new Point(4,1),
				new Point(0,2), new Point(4,2),
				new Point(0,3), new Point(1,3), new Point(2,3), new Point(3,3),
				new Point(0,4),
				new Point(0,5)
				};
		/** xxxx..
		 *  x...x.
		    x...x.
		    xxxx..
		    x.....
		    x.....   */
		for(Point p : points) {
			p.color = Color.PINK;
		}
	}
}

⌨️ 快捷键说明

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