📄 world.java
字号:
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
public class World {
private World() {
gameScreen = GameScreen.getInstance();
menu = GameMenu.getInstance();
frMapData = loadMapData("/frMap.bin", frMapData);
secMapData = loadMapData("/secMap.bin", secMapData);
thMapData = loadMapData("/thMap.bin", thMapData);
// mapFileName = "/frMap.bin";
// break;
// case 2:
// bgImage = Image.createImage("/b2.png");
// mapFileName = "/secMap.bin";
// break;
// case 3:
// bgImage = Image.createImage("/b3.png");
// mapFileName = "/thMap.bin";
loadEnemyDatae();
System.gc();
}
public static World getInstance() {
return world;
}
public byte[] getWeaponType() {
return weaponType;
}
public void setWeaponeType(byte[] weaponType) {
this.weaponType = weaponType;
}
public void loadGameDate(int loadCount) {
gameScreen.updateProgress();
switch (loadCount) {
case 20:
//System.out.println("20% = " + Runtime.getRuntime().freeMemory());
// if(gameLevel == 3){
// menu.releaseAllMenuObject(); // 清除所有菜单对象
// }
// if (taskCompelete != null) {
// taskCompelete = null;
// }
// if (bgImage != null) {
// bgImage = null;
// }
//第一关把所有的菜单对象清除
if (gameLevel == 1) {
menu.releaseAllMenuObject(); // 清除所有菜单对象
GameConstant.MAX_ENEMY_AIRCRAFT = 3;
GameConstant.MAX_ENEMY_HELICOPTER = 6;
GameConstant.MAX_ENEMY_HIDING = 4;
GameConstant.MAX_ENEMY_BOSSBULLET = 40;
}
// 当是第2关的时候家载敌舰
if(gameLevel == 2){
shipActor = new Ship[GameConstant.MAX_ENEMY_SHIP];
GameConstant.MAX_ENEMY_AIRCRAFT = 3;
GameConstant.MAX_ENEMY_HELICOPTER = 12;
GameConstant.MAX_ENEMY_HIDING = 4;
GameConstant.MAX_ENEMY_BOSSBULLET = 24;
}
if (gameLevel == 3) {
GameConstant.MAX_ENEMY_AIRCRAFT = 8;
GameConstant.MAX_ENEMY_HELICOPTER = 12;
GameConstant.MAX_ENEMY_HIDING = 5;
}
// 创建敌人对象
scoutPlaneActor = new ScoutPlane[GameConstant.MAX_ENEMY_SCOUTPLANE];
hidingPlaneActor = new HidingPlane[GameConstant.MAX_ENEMY_HIDING];
helicopterActor = new Helicopter[GameConstant.MAX_ENEMY_HELICOPTER];
plAircraftActor = new Aircraft[GameConstant.MAX_ENEMY_AIRCRAFT];
largePlaneActor = new LargePlane[GameConstant.MAX_ENEMY_LARGEPLANE];
// 只在第1关加载主角对象和主角武器对象
if (gameLevel == 1) {
battlePlane = BattlePlaneActor.getInstance();// 创建主角对象
planeBullet = new Bullet[GameConstant.MAX_PLANE_BULLET_MAIN];
for (byte weaponIdx = 0; weaponIdx < weaponType.length; weaponIdx++) {
switch (weaponType[weaponIdx]) {
case 1: // 大型导弹
planeBigMissle = new Bullet[GameConstant.MAX_PLANE_BULLET_BIGMISSILE];
break;
case 2: // 机枪
planeMachineGun = new Bullet[GameConstant.MAX_PLANE_BULLET_MACHINEGUN];
break;
case 3: // 直射火箭
planeRocket = new Bullet[GameConstant.MAX_PLANE_BULLET_ROCKET];
break;
case 5: // 呼叫僚机
planeWing = new Bullet[GameConstant.MAX_PLANE_BULLET_WING];
break;
case 6: // 大型炸弹
planeBomb = new Bullet[GameConstant.MAX_PLANE_BULLET_BOMB];
break;
}
}
// 特殊效果
bombEffect = new Bullet[GameConstant.MAX_EFFECT_BOMB];
somkeEffect = new Bullet[GameConstant.MAX_EFFECT_SOMKE];
for (int bombEffectIdx = 0; bombEffectIdx < GameConstant.MAX_EFFECT_BOMB; bombEffectIdx++) {
bombEffect[bombEffectIdx] = new Bullet();
}
for (int somkeEffectIdx = 0; somkeEffectIdx < GameConstant.MAX_EFFECT_SOMKE; somkeEffectIdx++) {
somkeEffect[somkeEffectIdx] = new Bullet();
}
}
// 创建敌人子弹对象
helicopterBullet = new Bullet[GameConstant.MAX_ENEMY_HELICOPTERBULLET];
largePlaneBullet = new Bullet[GameConstant.MAX_ENEMY_LARGEPLANEBULLET];
// if(gameLevel != 3)
// bossBullet = new Bullet[GameConstant.MAX_ENEMY_BOSSBULLET];
missileBullet = new Bullet[GameConstant.MAX_BULLET_MISSILEBULLET];
for (int j = 0; j < GameConstant.MAX_BULLET_MISSILEBULLET; j++) {
missileBullet[j] = new Bullet();
missileBullet[j].setDamage((byte) 20);
missileBullet[j].init("/rocket.png");
}
missileBulletPool = new ObjectPool(missileBullet);
for(int j = 0;j<GameConstant.MAX_ENEMY_HELICOPTER;j++){
helicopterActor[j] = new Helicopter();
helicopterActor[j].initData();
}
for(int sIdx=0;sIdx<GameConstant.MAX_ENEMY_SCOUTPLANE;sIdx++){
scoutPlaneActor[sIdx] = new ScoutPlane();
scoutPlaneActor[sIdx].initData();
}
for (int hIdx = 0; hIdx < GameConstant.MAX_ENEMY_HIDING; hIdx++) {
hidingPlaneActor[hIdx] = new HidingPlane();
hidingPlaneActor[hIdx].initData();
//plAircraftActor[i] = new Aircraft();
}
for (int plIdx = 0; plIdx < GameConstant.MAX_ENEMY_AIRCRAFT; plIdx++) {
plAircraftActor[plIdx] = new Aircraft();
plAircraftActor[plIdx].initData();
}
for (int larIdx = 0; larIdx < GameConstant.MAX_ENEMY_LARGEPLANE; larIdx++) {
largePlaneActor[larIdx] = new LargePlane();
largePlaneActor[larIdx].initData();
}
if (gameLevel == 2) {
for (int shiIdx = 0; shiIdx < GameConstant.MAX_ENEMY_SHIP; shiIdx++) {
shipActor[shiIdx] = new Ship();
shipActor[shiIdx].initData();
}
}
for (int eBullet1Idx = 0; eBullet1Idx < GameConstant.MAX_ENEMY_HELICOPTERBULLET; eBullet1Idx++) {
helicopterBullet[eBullet1Idx] = new Bullet();
}
for (int eBullet2Idx = 0; eBullet2Idx < GameConstant.MAX_ENEMY_LARGEPLANEBULLET; eBullet2Idx++) {
largePlaneBullet[eBullet2Idx] = new Bullet();
}
transport = new Transport();
supplyBullet = new Bullet[2];
for (int sIdx = 0; sIdx < 2; sIdx++) {
supplyBullet[sIdx] = new Bullet();
}
System.gc();
break;
case 40:
//System.out.println("40% = " + Runtime.getRuntime().freeMemory());
// 初始化主角特殊武器数据
if (gameLevel == 1) {
for (int bulletIdex = 0; bulletIdex < GameConstant.MAX_PLANE_BULLET_MAIN; bulletIdex++) {
planeBullet[bulletIdex] = new Bullet();
planeBullet[bulletIdex].init("/playerbullet1.png");
planeBullet[bulletIdex].setIWidth(5 << 8);
planeBullet[bulletIdex].setIHeight(10 << 8);
planeBullet[bulletIdex].setDamage((byte) 5);
}
for (byte weaponIdx = 0; weaponIdx < weaponType.length; weaponIdx++) {
switch (weaponType[weaponIdx]) {
case 1: // 大型导弹
for (int bulletIdex = 0; bulletIdex < GameConstant.MAX_PLANE_BULLET_BIGMISSILE; bulletIdex++) {
planeBigMissle[bulletIdex] = new Bullet();
planeBigMissle[bulletIdex].init("/missile.png");
planeBigMissle[bulletIdex].setIWidth(11 << 8);
planeBigMissle[bulletIdex].setIHeight(29 << 8);
}
break;
case 2: // 机枪
for (int missleIdx = 0; missleIdx < GameConstant.MAX_PLANE_BULLET_MACHINEGUN; missleIdx++) {
planeMachineGun[missleIdx] = new Bullet();
planeMachineGun[missleIdx].init("/machineGun.png",
5, 5, 3, 0);
planeMachineGun[missleIdx].setIWidth(5 << 8);
planeMachineGun[missleIdx].setIHeight(5 << 8);
planeMachineGun[missleIdx].setDamage((byte) 5);
}
break;
case 3: // 直射火箭
for (int roketIdx = 0; roketIdx < GameConstant.MAX_PLANE_BULLET_ROCKET; roketIdx++) {
planeRocket[roketIdx] = new Bullet();
planeRocket[roketIdx].init("/rocket.png");
planeRocket[roketIdx].setIWidth(6 << 8);
planeRocket[roketIdx].setIHeight(17 << 8);
planeRocket[roketIdx].setDamage((byte) 20);
}
break;
case 5: // 呼叫僚机
for (int widIdx = 0; widIdx < GameConstant.MAX_PLANE_BULLET_WING; widIdx++) {
planeWing[widIdx] = new Bullet();
planeWing[widIdx].init("/wingPlane.png");
}
break;
case 6: // 大型炸弹
for (int bombIdx = 0; bombIdx < GameConstant.MAX_PLANE_BULLET_BOMB; bombIdx++) {
planeBomb[bombIdx] = new Bullet();
planeBomb[bombIdx].setNotUsed(true);
planeBomb[bombIdx].setIWidth(90 << 8);
planeBomb[bombIdx].setIHeight(90 << 8);
planeBomb[bombIdx].setDamage((byte) 50);
}
break;
}
}
}
// 第3关不加载
//if(gameLevel != 3){
// 加载BOSS子弹
// for (int bossBulletIdx = 0; bossBulletIdx < GameConstant.MAX_ENEMY_BOSSBULLET; bossBulletIdx++) {
// bossBullet[bossBulletIdx] = new Bullet();
//
// bossBullet[bossBulletIdx].init("/enemybullet4.png", 13, 13, 4,
// 0);
// bossBullet[bossBulletIdx].setIWidth(11 << 8);
// bossBullet[bossBulletIdx].setIHeight(11 << 8);
// bossBullet[bossBulletIdx].setDamage((byte) 35);
// }
//}
// // 根据关数初始化BOSS对象
// switch (gameLevel) {
// case 1:
// frBoss = new FrBoss();
// frBoss.initData();
// break;
// case 2:
// secBoss = new SecBoss();
// secBoss.initData();
// break;
// case 3:
// thBoss = new ThBoss();
// thBoss.initData();
// break;
// }
System.gc();
break;
case 60:
//System.out.println("60% = " + Runtime.getRuntime().freeMemory());
for (int eBullet1Idx = 0; eBullet1Idx < GameConstant.MAX_ENEMY_HELICOPTERBULLET; eBullet1Idx++) {
helicopterBullet[eBullet1Idx].init("/enemybullet1.png",5,5,3,0);
helicopterBullet[eBullet1Idx].setIWidth(5 << 8);
helicopterBullet[eBullet1Idx].setIHeight(5 << 8);
helicopterBullet[eBullet1Idx].setDamage((byte)20);
}
//System.out.println("helicopterBullet!!!!!!!!");
for (int eBullet2Idx = 0; eBullet2Idx < GameConstant.MAX_ENEMY_LARGEPLANEBULLET; eBullet2Idx++) {
largePlaneBullet[eBullet2Idx].init("/enemybullet3.png", 10, 10,
4, 0);
largePlaneBullet[eBullet2Idx].setIWidth(10 << 8);
largePlaneBullet[eBullet2Idx].setIHeight(10 << 8);
largePlaneBullet[eBullet2Idx].setDamage((byte) 35);
}
//System.out.println("largePlaneBullet!!!!!!!!");
if (gameLevel == 1) {
for (int bombEffectIdx = 0; bombEffectIdx < GameConstant.MAX_EFFECT_BOMB; bombEffectIdx++) {
bombEffect[bombEffectIdx].initSpiritDate("/bomb.bin",
"/bomb.png", "/bombcom.bin");
}
//System.out.println("bombEffect!!!!!!!!");
for (int somkeEffectIdx = 0; somkeEffectIdx < GameConstant.MAX_EFFECT_SOMKE; somkeEffectIdx++) {
somkeEffect[somkeEffectIdx].initSpiritDate("/fume.bin",
"/fumeEffect.png", "/fumeCom.bin");
}
//System.out.println("fumeEffect!!!!!!!!");
}
transport.initData();
for (int sIdx = 0; sIdx < 2; sIdx++) {
supplyBullet[sIdx].initSpiritDate("/supplyBullet.bin",
"/supplyBullet.png", "/supplyBulletCom.bin");
supplyBullet[sIdx].setIWidth(8 << 8);
supplyBullet[sIdx].setIHeight(13 << 8);
supplyBullet[sIdx].setNotUsed(true);
}
System.gc();
break;
case 80:
//System.out.println("80% = " + Runtime.getRuntime().freeMemory());
// 初始化主角飞机信息
if (gameLevel == 1) {
battlePlane.loadData(gameScreen.getPlaneType());
planeBulletPool = new ObjectPool(planeBullet); // 初始化普通子弹对象池
for (byte weaponIdx = 0; weaponIdx < weaponType.length; weaponIdx++) {
switch (weaponType[weaponIdx]) {
case 1: // 大型导弹
planeBigMisslePool = new ObjectPool(planeBigMissle);
break;
case 2: // 机枪
planeMachineGunPool = new ObjectPool(planeMachineGun);
break;
case 3: // 直射火箭
planeRocketPool = new ObjectPool(planeRocket);
break;
case 5: // 呼叫僚机
planeWingPool = new ObjectPool(planeWing);
break;
case 6: // 大型炸弹
planeBombPool = new ObjectPool(planeBomb);
break;
}
}
battlePlane.initWeaponCommand(weaponType);
//bulletEffectPool = new ObjectPool(bulletEffect);
bombEffectPool = new ObjectPool(bombEffect);
somkeEffectPool = new ObjectPool(somkeEffect);
}
scoutPlaneActorPool = new ObjectPool(scoutPlaneActor);
helicopterActorPool = new ObjectPool(helicopterActor);
hidingPlaneActorPool = new ObjectPool(hidingPlaneActor);
plAircraftActorPool = new ObjectPool(plAircraftActor);
largePlaneActorPool = new ObjectPool(largePlaneActor);
if (gameLevel == 2) {
shipActorPool = new ObjectPool(shipActor);
}
helicopterBulletPool = new ObjectPool(helicopterBullet);
largePlaneBulletPool = new ObjectPool(largePlaneBullet);
//thunderBulletpool = new ObjectPool(thunderBullet);
//if (gameLevel != 3)
//bossBulletPool = new ObjectPool(bossBullet);
System.gc();
break;
case 100:
//System.out.println("100%" + Runtime.getRuntime().freeMemory());
try {
loadBackGround(gameLevel); //加载地图信息
//loadMapData(mapFileName); // 读取地图信息
} catch (IOException e) {
e.printStackTrace();
}
battlePlane.init();
backMoveSpeed = 4;
GameConstant.MUSIC_GAME_STATE = GameConstant.MUSIC_GAME_RUN;
//gameScreen.playSound(GameConstant.MUSIC_GAME_RUN);
gameScreen.playSound(GameConstant.MUSIC_GAME_STATE);
System.gc();
GameScreen.gameState = GameConstant.STATE_GAME_RUN; // 改变游戏状态
break;
}
}
// private void loadbulletEffect() {
// bulletEffect = new Bullet[GameConstant.MAX_EFFECT_BULLET];
// for (int bulletEffectIdx = 0; bulletEffectIdx < GameConstant.MAX_EFFECT_BULLET; bulletEffectIdx++) {
// bulletEffect[bulletEffectIdx] = new Bullet();
//
// bulletEffect[bulletEffectIdx].initSpiritDate("/bulletEffect.bin",
// "/fumeEffect.png", "/bulletEffectCom.bin");
// }
//
// bulletEffectPool = new ObjectPool(bulletEffect);
// }
// private void loadBossBullet(int stIdx,int edIdx) {
// // 第3关不需要加载BOSS子弹
// if (gameLevel != 3) {
// for (int bossBulletIdx = stIdx; bossBulletIdx < edIdx; bossBulletIdx++) {
// bossBullet[bossBulletIdx] = new Bullet();
//
// bossBullet[bossBulletIdx].init("/enemybullet4.png", 13, 13, 4,
// 0);
// bossBullet[bossBulletIdx].setIWidth(13 << 8);
// bossBullet[bossBulletIdx].setIHeight(13 << 8);
// bossBullet[bossBulletIdx].setDamage((byte) 35);
// }
// }
// }
// private void initBoss() {
// // 根据关数初始化BOSS对象
// switch (gameLevel) {
// case 1:
// //frBoss = new FrBoss();
// frBoss.initData();
// break;
// case 2:
// //secBoss = new SecBoss();
// secBoss.initData();
// break;
// case 3:
// //thBoss = new ThBoss();
// thBoss.initData();
// break;
// }
// }
public void loadBackGround(int gameLevel) throws IOException {
switch (gameLevel) {
case 1:
bgImage = Image.createImage("/b1.png");
break;
case 2:
bgImage = Image.createImage("/b2.png");
break;
case 3:
bgImage = Image.createImage("/b3.png");
break;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -