タイル ベースのゲームでコリジョンを確認するにはどうすればよいですか? Android SDK のサンプルを見てきましたが、Android プログラミングは初めてなので理解できません。
これは、ヒーローが世界で彼を移動できるようにするためのマップとタイルのクラスです。
class Map{
int X = 0;
int Y = 0;
int tileSize = 30;
int [][]map= {
//0 = empty tile
//1 = wall
//2 = player
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,1,2,1,1,0,0,0,0,0,1},
{1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1},
{1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
ArrayList<Tile> tiles = new ArrayList<Tile>();;
public Map(){
loadArray();
}
public void loadArray(){
for(int xTiles = 0; xTiles< map.length; xTiles++ ){
for(int yTiles = 0; yTiles< map[xTiles].length; yTiles++){
if(map[xTiles][yTiles]==1){//Wall
tiles.add(new Tile(X, Y, tileSize, map[xTiles][yTiles]));
tiles.get(tiles.size()-1).setColor(Color.GRAY);
tiles.get(tiles.size()-1).walkable = false;
X+=tileSize;
}else if(map[xTiles][yTiles]==0){//Empty Tile
tiles.add(new Tile(X, Y, tileSize, map[xTiles] [yTiles]));
X+=tileSize;
}else if(map[xTiles][yTiles]==2){//Player
tiles.add(new Tile(X, Y, tileSize, map[xTiles][yTiles]));
X+=tileSize;
}
}
Y+=tileSize;
X=0;
}
}
public void drawMap(Canvas canvas){
for(int index = 0; index < tiles.size(); index++){
tiles.get(index).drawTile(canvas);
}
}
}
class Tile{
private int type;
int X;
int Y;
int size;
boolean walkable = true;
Paint color = new Paint();;
//rect for the bounds
Rect bounds = new Rect();
public Tile(){
type = 0;
X = 0;
Y = 0;
size = 0;
color.setColor(Color.TRANSPARENT);
bounds.set(X, Y, X+size, Y+size);
}
public Tile(int X, int Y , int tileSize, int type){
this.type = type;
this.X = X;
this.Y = Y;
size = tileSize;
color.setColor(Color.TRANSPARENT);
bounds.set(X, Y, X+size, Y+size);
}
public int getX() {
return X;
}
public int getY() {
return Y;
}
public void setX(int x) {
X = x;
}
public void setY(int y) {
Y = y;
}
public int getType() {
// TODO Auto-generated method stub
return type;
}
public Paint getColor() {
return color;
}
public void setColor(int color) {
this.color.setColor(color);
}
public void drawTile(Canvas canvas){
canvas.drawRect(bounds, color);
}
}
これは私のヒーロークラスです
class Hero{
private static final int SIZE = 30;
//Coordinates
int X = 60;
int Y = 60;
int sx=30, sy=30;
private Paint paint = new Paint();
public Rect Bounds = new Rect(X,Y,X+30,Y+30);
boolean solid = true;
//Map Things
Map map;
public Hero(Map map){
this.map = map;
paint.setColor(Color.RED);
paint.setStyle(Style.FILL_AND_STROKE);
location();
}
private void location() {
for(int index = 0; index<map.tiles.size(); index++){
if(map.tiles.get(index).getType() == 2){
this.X = map.tiles.get(index).getX();
this.Y = map.tiles.get(index).getY();
}
}
}
public void move(int KeyCode) {
switch(KeyCode){
case KeyEvent.KEYCODE_DPAD_DOWN:
{if(Collision()){Y+=sy;} break;}
case KeyEvent.KEYCODE_DPAD_UP:
{Y-=sy; break;}
case KeyEvent.KEYCODE_DPAD_LEFT:
{X-=sx; break;}
case KeyEvent.KEYCODE_DPAD_RIGHT:
{X+=sx; break;}
}
}
private boolean Collision() {
for(int index = 0; index< map.tiles.size(); index++){
if(Rect.intersects(map.tiles.get(index).bounds, this.Bounds)){
return map.tiles.get(index).walkable;}
}
return false;
}
public int getX() {
return X;
}
public int getY() {
return Y;
}
public void setX(int x) {
X = x;
}
public void setY(int y) {
Y = y;
}
public void position(int x, int y){
X = x;
Y = y;
}
public void update(){
Bounds.set(X, Y, (X+SIZE), (Y+SIZE));
}
public void Draw(Canvas canvas){
canvas.drawRect(Bounds, paint);
canvas.drawText("Player", Bounds.left, Bounds.top-5, paint);
}
}
衝突を確認するにはどうすればよいですか? すべてのタイルをループしてから、プレーヤーの新しい座標が衝突するかどうかを確認する必要がありますか?
これは正しい方法ではないかもしれませんが、うまくいきました。このように、右、左、上、下の各移動方向のメソッドを追加しました
private void moveLeft() {
for(int index = 0 ; index < map.tiles.size();index++){
if(yCoord-1 == map.tiles.get(index).yCoord && xCoord == map.tiles.get(index).xCoord){
if(map.tiles.get(index).walkable){
X-=SIZE;
//My coordinates are flipped for some reason my Xs are Ys and Ys are Xs
yCoord-=1;
break;
}
}
}
}