package tankwar;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Tank {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private int x,y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
TankWarClient twc = null;
public static final int XSPEED = 5;
public static final int YSPEED = 5;
public static final int WIDTH = 30;
public static final int HEIGHT = 30;
private boolean bL=false,bU=false,bR=false,bD=false;
private Direction dir;
public Direction getDir() {
return dir;
}
public void setDir(Direction dir) {
this.dir = dir;
}
public void setGood(boolean good) {
this.good = good;
}
private Direction ptDir = Direction.D;
public Direction getPtDir() {
return ptDir;
}
public void setPtDir(Direction ptDir) {
this.ptDir = ptDir;
}
private boolean good;
private boolean live = true;
private int step = r.nextInt(12) + 3;
private static Random r = new Random();
public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.good = good;
}
public Tank(int x, int y, boolean good, Direction dir, TankWarClient twc){
this(x, y, good);
this.dir = dir;
this.twc = twc;
}
public void draw(Graphics g){
if(!live) {
if(!good){
twc.tanks.remove(this);
}
return;
}
Color c = g.getColor();
if(good) g.setColor(Color.RED);
else g.setColor(Color.BLUE);
g.fillOval(x, y, WIDTH, HEIGHT);
g.drawString("id:"+id, x, y-10);
g.setColor(c);
switch(ptDir){
case L:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x, y+HEIGHT/2);
break;
case LU:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x, y);
break;
case U:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+WIDTH/2, y);
break;
case RU:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+WIDTH, y);
break;
case R:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+WIDTH, y+HEIGHT/2);
break;
case RD:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+WIDTH, y+HEIGHT);
break;
case D:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+WIDTH/2, y+HEIGHT);
break;
case LD:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x, y+HEIGHT);
break;
}
move();
}
void move(){
switch(dir){
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.dir != Direction.STOP){
this.ptDir = this.dir;
}
if(x < 0) x = 0;
if(y < 30) y = 30;
if(x + Tank.WIDTH > TankWarClient.GAME_WIDTH) x = TankWarClient.GAME_WIDTH - Tank.WIDTH;
if(y + Tank.HEIGHT > TankWarClient.GAME_HEIGHT) y = TankWarClient.GAME_HEIGHT - Tank.HEIGHT;
/* if(!good){
Direction[] dirs = Direction.values();
if(step == 0){
step = r.nextInt(12) + 3;
int rn = r.nextInt(dirs.length);
dir = dirs[rn];
}
step --;
if(r.nextInt(40)>38) this.fire();
}*/
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_UP:
bU = true;
break;
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_DOWN:
bD = true;
break;
}
locateDirection();
}
void locateDirection(){
Direction oldDir = this.dir;
if(bL && !bU && !bR && !bD) dir = Direction.L;
else if(bL && bU && !bR && !bD) dir = Direction.LU;
else if(!bL && bU && !bR && !bD) dir = Direction.U;
else if(!bL && bU && bR && !bD) dir = Direction.RU;
else if(!bL && !bU && bR && !bD) dir = Direction.R;
else if(!bL && !bU && bR && bD) dir = Direction.RD;
else if(!bL && !bU && !bR && bD) dir = Direction.D;
else if(bL && !bU && !bR && bD) dir = Direction.LD;
else if(!bL && !bU && !bR && !bD) dir = Direction.STOP;
if(dir != oldDir) {
TankMoveMsg msg = new TankMoveMsg(id, x, y, dir, ptDir);
twc.nc.send(msg);
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_CONTROL:
fire();
break;
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_UP:
bU = false;
break;
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_DOWN:
bD = false;
break;
}
locateDirection();
}
public Missile fire(){
if(!live) return null;
int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;
int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;
Missile m = new Missile(id, x, y, good,ptDir,this.twc);
twc.missiles.add(m);
MissileNewMsg msg = new MissileNewMsg(m);
twc.nc.send(msg);
return m;
}
public Rectangle getRec(){
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public void setLive(boolean live) {
this.live = live;
}
public boolean isLive() {
return live;
}
public boolean isGood() {
return good;
}
}