package cn.tedu.submarine;
import javax.swing.*;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import java.util.Arrays;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/**
* @author dh
* @version 1.0
*/
public class World extends JPanel {
//设置常量
public static final int WIDTH=641;
public static final int HEIGHT=479;
private static final int RUNNING=0;//运行
private static final int GAME_OVER=1;
private int state=RUNNING;
/*双倍火力*/
private static final int ONE_FIRE=0;
private static final int TWO_FIRE=1;
private int doubleFire=ONE_FIRE;
/**/
private Battleship battleship=new Battleship();//战舰
private SeaObject[] submarines = {};//潜艇
private Mine[] mines={};//水雷
private Bomb[] bombs={};//炸弹
private int score=0;
/*生成潜艇对象*/
/**
* @param
* @return
*/
public SeaObject nextSubmarine(){
Random random = new Random();//随机数
int type = random.nextInt(20);//0-19
if (type<10){
return new ObserveSubmarine();
}else if (type<15){
return new TorpedoSubmarine();
}else {
return new MineSubmarine();
}
}
/*潜艇*/
private int subEnterIndex = 0;
public void submarineEnterAction(){
subEnterIndex++;
if (subEnterIndex%40==0){
SeaObject seaObject = nextSubmarine();
//扩容数组
submarines=Arrays.copyOf(submarines,submarines.length+1);
//赋值
submarines[submarines.length-1]=seaObject;
}
}
/*水雷*/
private int mineEnterIndex = 0;
/* private int mineSpeed=100;*/
public void mineEnterAction(){
mineEnterIndex++;
if (mineEnterIndex%100==0){
for (int i=0;i<submarines.length;i++){
if (submarines[i] instanceof MineSubmarine){
MineSubmarine mineSubmarine= (MineSubmarine) submarines[i];
Mine mine = mineSubmarine.shootMine();
mines=Arrays.copyOf(mines,mines.length+1);
mines[mines.length-1]=mine;
}
}
}
}
/*海洋对象移动*/
public void moveAction(){
for(int i=0;i<submarines.length;i++){
submarines[i].move();
}
for (int i=0;i<mines.length;i++){
mines[i].move();
}
for (int i=0;i<bombs.length;i++){
bombs[i].move();
}
}
/*删除越界*/
public void outOfBoundsAction(){//10毫秒
/*潜艇*/
for (int i=0;i<submarines.length;i++){
if (submarines[i].isOutOfBounds() || submarines[i].isDead()){//越界或者死了
submarines[i]=submarines[submarines.length-1];//最后一个元素赋值给i这个位置
submarines=Arrays.copyOf(submarines,submarines.length-1);
}
}
/*炸弹*/
for (int i = 0; i <bombs.length ; i++) {
if (bombs[i].isOutOfBounds() || bombs[i].isDead()){
bombs[i]=bombs[bombs.length-1];
bombs=Arrays.copyOf(bombs,bombs.length-1);
}
}
/*水雷*/
for (int i = 0; i <mines.length ; i++) {
if (mines[i].isOutOfBounds() || mines[i].isDead()){
mines[i]=mines[mines.length-1];
mines=Arrays.copyOf(mines,mines.length-1);
}
}
}
/*碰撞--->炸弹炸潜艇*/
public void hitBombAction(){
for (int i = 0; i <submarines.length ; i++) {
SeaObject s=submarines[i];
for (int j = 0; j <bombs.length ; j++) {
Bomb b=bombs[j];
if (s.isLive() && b.isLive() && s.isHit(b)){
s.goDead();//炸弹死
b.goDead();//潜艇死
/*if (submarines[i] instanceof MineSubmarine){
battleship.addLife(1);
System.out.println("战舰生命"+battleship.getLife());
}else if(submarines[i] instanceof TorpedoSubmarine){
battleship.addScore(40);
System.out.println("得分"+battleship.getScore());
}else if(submarines[i] instanceof ObserveSubmarine){
battleship.addScore(10);
System.out.println("得分"+battleship.getScore());
}*/
if (s instanceof EnemyScore){//战舰得分
EnemyScore es = (EnemyScore) s;
score+=es.getScore();
//System.out.println("得分"+score);
/*每400分加一条命*/
if (score%400==0){
battleship.addLife(1);
}
}
/*打掉鱼雷艇加一条命*/
/*if (s instanceof EnemyLife){//战舰得命
EnemyLife el = (EnemyLife) s;
int num=el.getLife();
battleship.addLife(num);
System.out.println("生命"+battleship.getLife());
}*/
/*双倍火力*/
if (s instanceof TorpedoSubmarine){
doubleFire=TWO_FIRE;
}
}
}
}
}
/*碰撞--->鱼雷打战舰*/
public void hitMineAction(){
for (int i = 0; i <mines.length ; i++) {
if (mines[i].isLive() && battleship.isLive() && mines[i].isHit(battleship)){
mines[i].goDead();
battleship.subtractLife(1);
doubleFire=ONE_FIRE;
}
}
}
/*水雷炸弹撞*/
public void hitMineBombAction(){
for (int i = 0; i <bombs.length ; i++) {
//b=bombs[i];
for (int j = 0; j <mines.length ; j++) {
//m=mines[j];
if (bombs[i].isLive() && mines[j].isLive() && bombs[i].isHit(mines[j])){
bombs[i].goDead();
mines[j].goDead();
}
}
}
}
/*游戏结束*/
public void checkGameOverAction(){
if (battleship.getLife()<=0){
state=GAME_OVER;
}
}
/*启动程序的执行 --- 定时器*/
public void action(){
/*监听器*/
KeyAdapter k = new KeyAdapter() {
/*重写KeyReleased,按键抬起事件*/
/* @Override
public void keyReleased(KeyEvent e) {//当按键抬起时自动执行
if (e.getKeyCode()==KeyEvent.VK_SPACE) {//按空格就执行
//炸弹发射
Bomb bomb = battleship.shootBomb();
bombs=Arrays.copyOf(bombs,bombs.length+1);
bombs[bombs.length-1]=bomb;
}else if (e.getKeyCode()==KeyEvent.VK_RIGHT){
battleship.move();
}else if (e.getKeyCode()==KeyEvent.VK_LEFT){
battleship.moveL();
}
}*/
public void keyPressed(KeyEvent e) {//按下自动执行
if (e.getKeyCode()==KeyEvent.VK_SPACE) {//按空格就执行
if (doubleFire==0){
//炸弹发射
Bomb bomb = battleship.shootBomb();
bombs=Arrays.copyOf(bombs,bombs.length+1);
bombs[bombs.length-1]=bomb;
}else if (doubleFir
评论0