📄 gamemap.java
字号:
/*
* Created on 2004-10-24
* Author: Xuefeng, Copyright (C) 2004, Xuefeng.
*/
package com.javaeedev.j2megame.tank;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/**
* Hold a map.
*
* @author Xuefeng
*/
public class GameMap extends TiledLayer {
public static final int TILE_WIDTH = 8;
public static final int MAP_WIDTH = 22;
public static final int MAP_HEIGHT = 22;
// a tile take 1 and a tank will take 4:
public static final int TILE_TANK = 6;
public static final int TILE_EMPTY = 0;
public static final int TILE_TREE = 1;
public static final int TILE_ICE = 2;
public static final int TILE_BRICK = 3;
public static final int TILE_ROCK = 4;
public static final int TILE_RIVER = 5;
// map data:
private int[][] map;
/**
* Load the map resource. <code>col</code> and <code>row</code> specify the visible region's tiles.
*
* @param map The int[][] array.
*/
public GameMap(int[][] map, Image image) {
super(MAP_WIDTH, MAP_HEIGHT, image, TILE_WIDTH, TILE_WIDTH);
this.map = map;
System.out.println("Map info: width = " + map[0].length + ", height = " + map.length);
for(int x=0; x<MAP_WIDTH; x++) {
for(int y=0; y<MAP_HEIGHT; y++) {
setCell(x, y, map[y][x]);
}
}
}
private void setEmpty(int x, int y) {
map[y][x] = TILE_EMPTY;
setCell(x, y, TILE_EMPTY);
}
public int getTile(int x, int y) {
return map[x][y];
}
public boolean canMoveLeft(int curr_x, int curr_y, int obj_width, int obj_height) {
if(curr_x>0)
return canSet(curr_x-1, curr_y, obj_width, obj_height);
return false;
}
public boolean canMoveRight(int curr_x, int curr_y, int obj_width, int obj_height) {
if(curr_x<MAP_WIDTH-obj_width) {
return canSet(curr_x+1, curr_y, obj_width, obj_height);
}
return false;
}
public boolean canMoveUp(int curr_x, int curr_y, int obj_width, int obj_height) {
if(curr_y>0) {
return canSet(curr_x, curr_y-1, obj_width, obj_height);
}
return false;
}
public boolean canMoveDown(int curr_x, int curr_y, int obj_width, int obj_height) {
if(curr_y<MAP_HEIGHT-obj_height) {
return canSet(curr_x, curr_y+1, obj_width, obj_height);
}
return false;
}
/**
* If a object is set to (x, y), with its size (obj_width, obj_height),
* can it be set to map?
*
* @param x
* @param y
* @param obj_width
* @param obj_height
* @return
*/
private boolean canSet(int x, int y, int obj_width, int obj_height) {
for(int i=0; i<obj_width; i++) {
for(int j=0; j<obj_height; j++) {
if( map[y+j][x+i]>TILE_ICE )
return false;
}
}
return true;
}
/**
* Move object to the map(x, y).
*
* @param layer Layer object.
* @param x Position x in map.
* @param y Position y in map.
*/
public void setPosition(Layer layer, int x, int y) {
layer.setPosition(x*TILE_WIDTH, y*TILE_WIDTH);
}
/**
* Check if a bullet can pass:
* @param x
* @param y
* @return
*/
public boolean canPass(int direction, int x, int y) {
int nx = x / TILE_WIDTH;
int ny = y / TILE_WIDTH;
System.out.println("canPass? x=" + x + ", y=" + y);
boolean hit = false;
switch(direction) {
case Canvas.UP:
case Canvas.DOWN:
if(map[ny][nx]==TILE_EMPTY && map[ny][nx-1]==TILE_EMPTY)
return true;
if(map[ny][nx]==TILE_BRICK) {
setEmpty(nx, ny);
hit = true;
}
if(map[ny][nx-1]==TILE_BRICK) {
setEmpty(nx-1, ny);
hit = true;
}
if(hit)
return false;
break;
case Canvas.LEFT:
case Canvas.RIGHT:
if(map[ny][nx]==TILE_EMPTY && map[ny-1][nx]==TILE_EMPTY)
return true;
if(map[ny][nx]==TILE_BRICK) {
setEmpty(nx, ny);
hit = true;
}
if(map[ny-1][nx]==TILE_BRICK) {
setEmpty(nx, ny-1);
hit = true;
}
if(hit)
return false;
break;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -