package com.tank;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;
/**
* 坦克类
* @author Administrator
*
*/
public class Tank {
/**
* 初始化坦克信息
* @param x
* @param y
* @param live
* @param dir
*/
public Tank(int x,int y,boolean live,Direction dir,boolean good,MainPanel mp){
this.x = x;
this.y = y;
this.OldX = x;
this.OldY = y;
this.live = live;
this.dir = dir;
this.good = good;
this.mp = mp;
}
//坦克横坐标移动位置
private static final int XSPEED = 5;
//坦克纵坐标移动的位置
private static final int YSPEED = 5;
//坦克的x坐标
private int x;
//坦克的y坐标
private int y;
//坦克是否还活着
private boolean live;
//坦克移动的方向
private Direction dir;
//坦克的方向
private Direction ptdir = Direction.U;
//判断是玩家坦克还是电脑坦克
private boolean good;
//控制坦克的方向
private boolean L = false,U = false,R = false,D = false;
//产生随机方向
private Random rand = new Random();
//产生一个随机数
private int step = rand.nextInt(15)+3;
//移动之前的x,y坐标
private int OldX,OldY;
//实例化一个血块
private Blood bb = new Blood();
//坦克的生命
private int life = 100;
//判断是否暂停
public boolean pause = true;
MainPanel mp;
/**
* 获取坦克的x坐标
* @return
*/
public int getX() {
return x;
}
/**
* 设置坦克的x坐标
* @param x
*/
public void setX(int x) {
this.x = x;
}
/**
* 获取坦克的y坐标
* @return
*/
public int getY() {
return y;
}
/**
* 设置坦克的y坐标
* @param y
*/
public void setY(int y) {
this.y = y;
}
/**
* 获取坦克是否还活着
* @return
*/
public boolean isLive() {
return live;
}
/**
* 设置坦克是否还活着
* @param live
*/
public void setLive(boolean live) {
this.live = live;
}
/**
* 获取坦克的方向
* @return
*/
public Direction getDir() {
return dir;
}
/**
* 设置坦克的方向
* @param dir
*/
public void setDir(Direction dir) {
this.dir = dir;
}
/**
* 获取是玩家坦克还是电脑坦克
* @return
*/
public boolean isGood() {
return good;
}
/**
* 设置是玩家坦克还是电脑坦克
* @param good
*/
public void setGood(boolean good) {
this.good = good;
}
/**
* 获取坦克的生命
* @return
*/
public int getLife() {
return life;
}
/**
* 设置坦克的生命
* @param life
*/
public void setLife(int life) {
this.life = life;
}
/**
* 根据方向的不同画出不同方向的坦克
*/
public void drawTank(Graphics g){
if(!isLive())return;
if(this.isGood())bb.drawBlood(g);
switch(this.ptdir){
case L:
g.drawImage(ImageInfo.mapTank.get("L"),this.getX(),this.getY(),null);
break;
case LU:
g.drawImage(ImageInfo.mapTank.get("LU"),this.getX(),this.getY(),null);
break;
case U:
g.drawImage(ImageInfo.mapTank.get("U"),this.getX(),this.getY(),null);
break;
case RU:
g.drawImage(ImageInfo.mapTank.get("RU"),this.getX(),this.getY(),null);
break;
case R:
g.drawImage(ImageInfo.mapTank.get("R"),this.getX(),this.getY(),null);
break;
case RD:
g.drawImage(ImageInfo.mapTank.get("RD"),this.getX(),this.getY(),null);
break;
case D:
g.drawImage(ImageInfo.mapTank.get("D"),this.getX(),this.getY(),null);
break;
case LD:
g.drawImage(ImageInfo.mapTank.get("LD"),this.getX(),this.getY(),null);
break;
}
moveTank();
}
/**
* 根据按键的方向移动坦克
*/
public void moveTank(){
if(!pause)return;
OldX = x;
OldY = y;
if(!isLive()){
mp.listForTank.remove(this);
return;
}
switch(this.getDir()){
case L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case U:
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case D:
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}
if(this.getDir() != Direction.STOP){
this.ptdir = this.getDir();
}
if(x <= 0) x = 0;
if(y <= 20) y = 20;
if(x >= MainPanel.GAME_WIDTH - 50) x = MainPanel.GAME_WIDTH - 50;
if(y >= MainPanel.GAME_HEIGHT - 50) y = MainPanel.GAME_HEIGHT - 50;
//判断是电脑的话,就随机方向移动
if(!good){
//产生一个方向的数组
Direction[] dire = Direction.values();
if(step == 0){
step = rand.nextInt(15) + 3;
int random = rand.nextInt(dire.length);
dir = dire[random];
}
step--;
if(rand.nextInt(30) > 20)this.fire();
}
}
/**
* 键盘按下事件
*/
public void keyPressed(KeyEvent kv) {
int keyCode = kv.getKeyCode();
switch(keyCode){
case KeyEvent.VK_UP:
U = true;
break;
case KeyEvent.VK_DOWN:
D = true;
break;
case KeyEvent.VK_LEFT:
L = true;
break;
case KeyEvent.VK_RIGHT:
R = true;
break;
}
locationTank();
}
/**
* 键盘弹起事件
*/
public void keyReleased(KeyEvent kv) {
int keyCode = kv.getKeyCode();
switch(keyCode){
case KeyEvent.VK_UP:
U = false;
break;
case KeyEvent.VK_DOWN:
D = false;
break;
case KeyEvent.VK_LEFT:
L = false;
break;
case KeyEvent.VK_RIGHT:
R = false;
break;
case KeyEvent.VK_J:
fire();
break;
case KeyEvent.VK_A:
superFire();
break;
case KeyEvent.VK_F2:
if(!this.isLive()){
this.setLife(100);
this.setLive(true);
}
break;
}
locationTank();
}
/**
* 定位坦克的方向
*/
public void locationTank(){
if(U && !D && !L && !R) this.setDir(Direction.U);
else if(!U && D && !L && !R) this.setDir(Direction.D);
else if(!U && !D && L && !R) this.setDir(Direction.L);
else if(!U && !D && !L && R) this.setDir(Direction.R);
else if(U && !D && L && !R) this.setDir(Direction.LU);
else if(U && !D && !L && R) this.setDir(Direction.RU);
else if(!U && D && !L && R) this.setDir(Direction.RD);
else if(!U && D && L && !R) this.setDir(Direction.LD);
else if(!U && !D && !L && !R) this.setDir(Direction.STOP);
}
/**
* 坦克开火
*/
public void fire(){
if(!isLive())return;
Missible m = new Missible(this.getX()+18,this.getY()+20,true,this.good,this.ptdir,mp);
mp.listMissible.add(m);
}
/**
* 重写坦克开火
*/
public void fire(Direction dir){
if(!isLive())return;
Missible m = new Missible(this.getX()+18,this.getY()+20,true,this.good,dir,mp);
mp.listMissible.add(m);
}
/**
* 超级开火
*/
public void superFire(){
Direction[] dires = Direction.values();
for(int i=0;i<dires.length;i++){
fire(dires[i]);
}
}
/**
* 判断是否碰撞
* @return
*/
public Rectangle getRect(){
return new Rectangle(this.getX(),this.getY(),ImageInfo.mapTank.get("L").getWidth(null),ImageInfo.mapTank.get("L").getHeight(null));
}
/**
* 坦克碰撞的时候停止
* @param tank
*/
public void hitTank(Tank tank){
if(this.isLive() && this.getRect().intersects(tank.getRect())){
this.stay();
tank.stay();
}
}
/**
* 碰撞时返回为碰撞之前的坐标
*/
public void stay(){
x = OldX;
y = OldY;
}
/**
* 内部类,控制玩家坦克的血量
* @author Administrator
*
*/
private class Blood{
/**
* 画处血条
* @param g
*/
public void drawBlood(Graphics g){
Color c = g.getColor();
g.setColor(Color.RED);
g.drawRect(x, y-10, ImageInfo.mapTank.get("L").getWidth(null), 10);
int w = ImageInfo.mapTank.get("L").getWidth(null) * life/100;
g.fillRect(x, y-10, w, 10);
g.setColor(c);
}
}
}