📄 gamelayer.java
字号:
package com.ipanel.games.tank;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
public class GameLayer {
/**
* 0,empty; 1,砖墙; 2,树林; 3,钢墙; 4,水域。
*/
public static final int CELL_EMPTY = 0;
public static final int CELL_BRICK = 1;
public static final int CELL_GREEN = 2;
public static final int CELL_IRON = 3;
public static final int CELL_WATER = 4;
/**
* dataViewLayer是View的数据存储; 程序用View作为Presentation;
* 程序不会用dataViewLayer来检测collidesWith(),但是程序行为引起的相关Data以至于View的变化需要被体现出来。
*/
private int[][] dataViewLayer = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 3, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 3, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 4, 0, 2, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 4, 0, 2, 0, 0, 0, 0, 0 },
{ 3, 0, 1, 1, 0, 4, 0, 2, 0, 1, 1, 0, 3 },
{ 0, 0, 1, 1, 0, 4, 0, 2, 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 } };
/**
* dataTankLayer是针对Tank的TiledLayer的数据存储,它构成的TiledLayer将用来检测Tank的collidesWith();
* 另外,程序的行为引起的相关Data的变化需要和dataTankLayer同步。
*
* 相对于dataViewLayer,这个数据存储将其中的"2"(即"树林")置换成为"0"。
*/
private int[][] dataTankLayer = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 1,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1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 } };
/**
* dataBulletLayer是针对Bullet的TiledLayer的数据存储,它构成的TiledLayer将用来检测Bullet的collidesWith();
* 另外,程序的行为引起的相关Data的变化需要和dataBulletLayer同步。
* 相对于dataViewLayer,这个数据存储将其中的"2"(即"树林")和"4"(即"水域")置换成为"0"。
*
* 对于Bullets之间以及和Tank的collidesWith()不由这个int[][]存储,而由它们(Sprite)之间处理。
*/
private int[][] dataBulletLayer = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 3, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 3, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 3, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 3 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 } };
/**
* 以下三个常数设置可供调整: 存储TiledLayer的int[][]的维,相应的图片的pixel数值。
*
* 宽: 46*13=598; 高: 16*32=512; 宽: 9*5*13=9*65=585; 高: 4*4*32=4*128=512;
*/
private static final int ARRAY_COLUMN = 13;
private static final int ARRAY_ROW = 32;
private static final int IMAGE_WIDTH = 46;
private static final int IMAGE_HEIGHT = 16;
public static final int AREA_UP_LEFT_X = 0;
public static final int AREA_UP_LEFT_Y = 0;
public static final int AREA_WIDTH = IMAGE_WIDTH * ARRAY_COLUMN;
public static final int AREA_HEIGHT = IMAGE_HEIGHT * ARRAY_ROW;
private static final int PLAYER_REMAIN_X_BASE = AREA_WIDTH + 5;
public static final int SPRITE_DIRCTION_UP = 1;
public static final int SPRITE_DIRCTION_DOWN = 2;
public static final int SPRITE_DIRCTION_LEFT = 3;
public static final int SPRITE_DIRCTION_RIGHT = 4;
/**
* SIZE/STEP的定义应该和Sprite的像素width/height有关: 需要保证不会出现"吃包子馅"的错误。
*
* 实际运行中,要求定义的值为(所有collision范畴的最小图片像素size)/2 - 1。
*/
public static final int SPRITE_COLLISION_MIN_SIZE = 3;
private Image imageBoard = null;
/**
* 以下三个TiledLayer分别面向View、Tank、Bullet。
*/
private TiledLayer viewLayer = null;
private TiledLayer tankLayer = null;
private TiledLayer bulletLayer = null;
/**
* 存储游戏中的Objects。
*/
private Vector vPlayerBullets = new Vector();
private Vector vGameBullets = new Vector();
private Vector vPlayerTanks = new Vector();
private Vector vGameTanks = new Vector();
/**
* 存储要求DrawBoom的boomDrawer。
*/
private Vector vDrawer = new Vector();
/**
* 为了使Boom图片在flush时候(持续)可见,需要在多次flush的时候paint;这里定义flush的次数。
*/
public static final int BOOM_DRAW_TIMES = 3;
private IPTankGameCanvas canvas = null;
public GameLayer() {
super();
}
public GameLayer(IPTankGameCanvas canvas) {
this();
this.canvas = canvas;
init();
}
private void init() {
try {
imageBoard = Image.createImage("/res/Board.png");
} catch (IOException e) {
paintException("Exception when createImage in GameLayer()!");
}
viewLayer = new TiledLayer(ARRAY_COLUMN, ARRAY_ROW, imageBoard,
IMAGE_WIDTH, IMAGE_HEIGHT);
tankLayer = new TiledLayer(ARRAY_COLUMN, ARRAY_ROW, imageBoard,
IMAGE_WIDTH, IMAGE_HEIGHT);
bulletLayer = new TiledLayer(ARRAY_COLUMN, ARRAY_ROW, imageBoard,
IMAGE_WIDTH, IMAGE_HEIGHT);
initLayer(viewLayer, dataViewLayer);
initLayer(tankLayer, dataTankLayer);
initLayer(bulletLayer, dataBulletLayer);
}
/**
* 将int[][]的数据存储设置为TiledLayer的初始化;这以后,三个TiledLayer就不同了。
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -