📄 storage.java
字号:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.io.*;
import java.util.*;
/**
* 该类描述了游戏中的仓库,提供了移动箱子的功能。
*/
public class Storage {
public static final int CELL_WIDTH = 16; //仓库单元格的宽
public static final int CELL_HEIGHT = 16; //仓库单元格的高
public static final int WALL_BRICK = 1;
public static final int CHANNELS_BRICK = 2;
public static final int STORE_BRICK = 3;
private LayerManager manager; //图层管理器
private TiledLayer background; //背景
private TiledLayer wall; //仓库墙壁,用于碰撞检测
private Vector boxPool; //备用箱子集合
private Sprite[] boxes; //仓库中的箱子
private int columns = 5; //仓库宽
private int rows = 6; //仓库高
private int[] boxesInitX; //仓库中箱子的初始位置
private int[] boxesInitY;
private int workerInitX = -1; //搬运工初始位置
private int workerInitY = -1;
//构造方法,创建一个maxCols宽,maxRows高的仓库
public Storage(int maxCols, int maxRows, LayerManager manager) {
this.manager = manager;
try {
//创建背景图层和围墙碰撞检测图层
Image img = Image.createImage("/img/storage16.png");
background = new TiledLayer(maxCols, maxRows, img, CELL_WIDTH, CELL_HEIGHT);
wall = new TiledLayer(maxCols, maxRows,img, CELL_WIDTH, CELL_HEIGHT);
//创建箱子集合,已备后续使用
Image boxImg = Image.createImage("/img/box16.png");
Sprite box = new Sprite(boxImg, CELL_WIDTH, CELL_HEIGHT);
boxPool = new Vector(10, 5);
boxPool.addElement(box);
for(int i=1; i<10; i++) {
boxPool.addElement(new Sprite(box));
}
}
catch(IOException ie) {
System.out.println("error: " + ie.toString());
}
boxes = new Sprite[0]; //初始状态仓库中没有要移动的箱子
boxesInitX = new int[0];
boxesInitY = new int[0];
}
//返回仓库的墙壁
public TiledLayer getWall() {
return wall;
}
//获取仓库中的箱子
public Sprite[] getBoxes() {
return boxes;
}
//使用plat地图数据更新仓库
public void update(Plat plat) {
clear(); //清除以前的数据
dispose(plat); //部署新的初始数据
}
//清除仓库的数据
private void clear() {
background.fillCells(0, 0, columns, rows, 0);
wall.fillCells(0, 0, columns, rows, 0);
manager.remove(background);
for(int i=0; i<boxes.length; i++) {
manager.remove(boxes[i]);
}
}
//根据plat地图数据设置仓库的状态
private void dispose(Plat plat) {
//向当前的仓库摆放箱子
dispenseBox(plat.getBoxesTotal());
//获取仓库中箱子的初始位置
boxesInitX = new int[plat.getBoxesTotal()];
boxesInitY = new int[plat.getBoxesTotal()];
plat.getBoxesInitLocation(boxesInitX, boxesInitY);
//获取搬运工初始位置
int[] wp = new int[2];
plat.getWorkerInitLocation(wp);
workerInitX = wp[0]*CELL_WIDTH;
workerInitY = wp[1]*CELL_HEIGHT;
//设置仓库尺寸
columns = plat.getColumns();
rows = plat.getRows();
//设置仓库背景数据
int len = columns*rows;
int x = 0;
int y = 0;
char ch = Plat.NIL;
for(int i=0; i<len; i++) {
x = i%columns;
y = i/columns;
ch = plat.getValue(x, y);
switch(ch) {
case Plat.WALL:
background.setCell(x, y, WALL_BRICK);
wall.setCell(x, y, WALL_BRICK);
break;
case Plat.CHANNELS:
background.setCell(x, y, CHANNELS_BRICK);
break;
case Plat.STORE:
background.setCell(x, y, STORE_BRICK);
break;
default:
background.setCell(x, y, 0);
wall.setCell(x, y, 0);
}
}
manager.append(background);
//设置箱子的初始状态
setBoxesInit();
}
//分发num个箱子到manager中
private void dispenseBox(int num) {
if(num > boxPool.size()) {
Sprite box = (Sprite)boxPool.elementAt(0);
for(int i=num-boxPool.size(); i>0; i--) {
boxPool.addElement(new Sprite(box));
}
}
boxes = new Sprite[num];
for(int i=0; i<num; i++) {
boxes[i] = (Sprite)boxPool.elementAt(i);
manager.append(boxes[i]);
}
}
//设置仓库中箱子为初始状态
public void setBoxesInit() {
int cow = 0;
int col = 0;
for(int i=0; i<boxes.length; i++) {
boxes[i].setPosition(boxesInitX[i]*CELL_WIDTH, boxesInitY[i]*CELL_HEIGHT);
if(background.getCell(boxesInitX[i], boxesInitY[i]) == STORE_BRICK) {
boxes[i].setFrame(1);
}
else {
boxes[i].setFrame(0);
}
}
}
//移动box,水平距离为:hd;垂直距离为:vd,返回移动是否成功
public boolean moveBox(Sprite box, int hd, int vd) {
boolean can = true;
box.move(hd, vd);
//与墙壁进行碰撞检测
if(box.collidesWith(wall, false)) {
can = false;
}
//没有与墙壁相碰,与其他的箱子进行碰撞检测
int i=0;
while(can && i<boxes.length) {
if(box == boxes[i]) {
i++;
continue;
}
if(box.collidesWith(boxes[i], false)) {
can = false;
}
i++;
}
if(!can) {//与墙壁或其他的箱子相碰,不能移动
box.move(-hd, -vd);
}
else {//能移动,更新箱子状态
int col = box.getX()/CELL_WIDTH;
int row = box.getY()/CELL_HEIGHT;
if(background.getCell(col, row) == STORE_BRICK) {
box.setFrame(1);
}
else {
box.setFrame(0);
}
}
return can;
}
//获取搬运工的水平初始位置
public int getWorkerInitX() {
return workerInitX;
}
//获取搬运工的垂直初始位置
public int getWorkerInitY() {
return workerInitY;
}
//获取仓库在屏幕上显示的宽度
public int getViewWidth() {
return columns*CELL_WIDTH;
}
//获取仓库在屏幕上显示的高度
public int getViewHeight() {
return rows*CELL_HEIGHT;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -