GBDK tilemap冲突

时间:2018-08-06 00:14:17

标签: collision-detection collision game-development gbdk

我正在尝试使用带C的GBDK来使tilemap冲突正常工作,并且无法弄清为什么冲突很麻烦。我将col_map(用于碰撞的bg瓦片的地图数据)转换为2D数组,我尝试通过根据玩家的x和y坐标计算每个瓦片的列和行来索引,如下面的代码:

//this line here further down
//col_mapxy[playerxy[1] / 8][(playerxy[0] / 8) % 20]

您应该能够使用GBDK开发套件来编译代码。

#include <gb/gb.h>
#include <string.h>

#define Mwidth      20 // in 8x8 tiles
#define Mheight     18

unsigned char col_tile_dat[] =
{
  0xFF,0xFF,0xFF,0xFF,0xC3,0xFF,0xDB,0xE7,
  0xDB,0xE7,0xC3,0xFF,0xFF,0xFF,0xFF,0xFF,
  0x00,0x00,0x00,0x00,0x60,0x00,0x18,0x00,
  0x0C,0x00,0x70,0x00,0x10,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

unsigned char col_map[] =
{
  0x02,0x02,0x02,0x03,0x03,0x03,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x00,0x00,
  0x02,0x03,0x03,0x03,0x03,0x03,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x03,0x00,0x00,
  0x02,0x03,0x00,0x00,0x03,0x03,0x03,0x03,0x03,0x03,
  0x03,0x00,0x00,0x00,0x03,0x03,0x02,0x03,0x03,0x03,
  0x02,0x03,0x00,0x00,0x03,0x01,0x03,0x03,0x03,0x03,
  0x01,0x00,0x00,0x00,0x02,0x03,0x03,0x03,0x03,0x03,
  0x02,0x02,0x03,0x03,0x01,0x02,0x03,0x03,0x03,0x03,
  0x01,0x00,0x00,0x00,0x03,0x03,0x03,0x03,0x03,0x03,
  0x02,0x03,0x03,0x03,0x02,0x03,0x03,0x03,0x02,0x03,
  0x02,0x00,0x00,0x00,0x03,0x03,0x03,0x03,0x00,0x00,
  0x03,0x02,0x03,0x03,0x03,0x02,0x03,0x02,0x03,0x03,
  0x02,0x02,0x03,0x01,0x02,0x03,0x03,0x03,0x00,0x00,
  0x03,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x03,0x02,
  0x03,0x02,0x03,0x03,0x01,0x01,0x02,0x03,0x00,0x00,
  0x03,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x02,0x03,
  0x03,0x03,0x03,0x03,0x03,0x01,0x03,0x03,0x00,0x00,
  0x03,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x01,0x03,
  0x03,0x03,0x02,0x03,0x03,0x03,0x03,0x03,0x00,0x00,
  0x03,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x02,0x03,
  0x03,0x03,0x03,0x03,0x00,0x00,0x03,0x03,0x00,0x00,
  0x03,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x03,0x03,
  0x02,0x02,0x02,0x01,0x00,0x00,0x01,0x02,0x00,0x00,
  0x03,0x03,0x01,0x01,0x03,0x00,0x00,0x00,0x03,0x03,
  0x03,0x03,0x02,0x01,0x02,0x03,0x01,0x03,0x00,0x00,
  0x03,0x03,0x03,0x01,0x03,0x00,0x00,0x00,0x03,0x03,
  0x02,0x02,0x01,0x03,0x03,0x03,0x01,0x03,0x00,0x00,
  0x03,0x03,0x03,0x02,0x01,0x00,0x00,0x00,0x03,0x03,
  0x02,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x00,0x00,
  0x03,0x03,0x03,0x03,0x02,0x01,0x03,0x03,0x03,0x01,
  0x01,0x01,0x01,0x01,0x01,0x02,0x03,0x03,0x00,0x00,
  0x00,0x00,0x03,0x03,0x03,0x02,0x03,0x02,0x03,0x02,
  0x02,0x02,0x03,0x02,0x03,0x03,0x00,0x00,0x00,0x00,
  0x00,0x00,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x02,
  0x03,0x03,0x03,0x03,0x03,0x03,0x00,0x00,0x00,0x00
};

unsigned char player[] =
{
  0x3C,0x3C,0x42,0x7E,0x81,0xFF,0xA5,0xFF,
  0x81,0xFF,0xBD,0xFF,0x56,0x7E,0x3C,0x3C,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

UINT8 playerxy[2];
UINT8 col_mapxy[18][20];


void init() {
    memcpy(&col_mapxy, &col_map, sizeof(unsigned char) * 360);
    DISPLAY_ON;
    set_bkg_data(0, 2, col_tile_dat);
    set_bkg_tiles(0, 0, 20, 18, col_map);
    set_sprite_data(0, 2, player);
    set_sprite_tile(0, 0);
    playerxy[0] = 80;
    playerxy[1] = 72;
}

void updateSwitches() {
    HIDE_WIN;
    SHOW_BKG;
    SHOW_SPRITES;
}

void checkInput() { // here
// calculate the tiles in colmapxy based of player x, y cooridinates 
// I will add player[1] - 1 so that it checks for the next move. e.g up.
// when I get it working
    if ( (joypad() & J_UP) && (col_mapxy[playerxy[1] / 8][(playerxy[0] / 8) % 20] != 0x00)) {
                playerxy[1]--;  
    }

    if (joypad() & J_DOWN) playerxy[1]++;
    if (joypad() & J_LEFT) playerxy[0]--;
    if (joypad() & J_RIGHT) playerxy[0]++;

    move_sprite(0, playerxy[0], playerxy[1]);

}

void main() {
    init();
    while (1) {
        checkInput();
        updateSwitches();
        wait_vbl_done();
    }

}

0 个答案:

没有答案