work on obstacle collision

This commit is contained in:
t. boddy 2022-08-16 21:36:49 -04:00
parent d788b3931d
commit 8646b6c1a0
5 changed files with 37 additions and 20 deletions

View File

@ -19,8 +19,8 @@ static void drawBg(){
// update // update
#define BG_SPEED FIX16(1) #define BG_SPEED FIX16(0.5)
#define BG_SPEED_NORM FIX16(0.707) #define BG_SPEED_NORM FIX16(0.354)
#define BG_SIZE 64 #define BG_SIZE 64
@ -28,7 +28,6 @@ static void drawBg(){
s16 bgPosX[GAME_H_T]; s16 bgPosX[GAME_H_T];
fix16 bgPosXF[GAME_H_T]; fix16 bgPosXF[GAME_H_T];
Vect2D_f16 bgPos;
static void scrollBg(){ static void scrollBg(){
for(u8 y = 0; y < GAME_H_T; y++){ for(u8 y = 0; y < GAME_H_T; y++){

View File

@ -9,7 +9,7 @@
struct obstacle { struct obstacle {
bool active, top; bool active, top;
Vect2D_f16 pos; Vect2D_f16 pos, size;
s16 startX; s16 startX;
}; };
struct obstacle obstacles[OBSTACLE_COUNT]; struct obstacle obstacles[OBSTACLE_COUNT];
@ -31,10 +31,13 @@ static void drawFg(){
// obstacle // obstacle
#define OBS_CEIL_Y CEIL_Y + 4 #define OBS_CEIL_Y 7
#define OBS_FLOOR_Y FLOOR_Y - 4 #define OBS_FLOOR_Y 20
#define OBS_X 32 #define OBS_X 32
#define OBS_TOP_Y FIX16(24)
#define OBS_BOTTOM_Y FIX16(160)
s16 fgPos; s16 fgPos;
static void spawnObstacle(bool top){ static void spawnObstacle(bool top){
@ -44,6 +47,9 @@ static void spawnObstacle(bool top){
obstacles[i].active = TRUE; obstacles[i].active = TRUE;
obstacles[i].top = top; obstacles[i].top = top;
obstacles[i].pos.x = FIX16(256); obstacles[i].pos.x = FIX16(256);
obstacles[i].pos.y = top ? OBS_TOP_Y : OBS_BOTTOM_Y;
obstacles[i].size.x = FIX16(64);
obstacles[i].size.y = FIX16(32);
obstacles[i].startX = OBS_X; obstacles[i].startX = OBS_X;
if(top){ if(top){
VDP_drawImageEx(BG_B, &rock1lt, TILE_ATTR_FULL(PAL2, 1, 0, 0, FG_I + 32), OBS_X, OBS_CEIL_Y - 2, 0, DMA); VDP_drawImageEx(BG_B, &rock1lt, TILE_ATTR_FULL(PAL2, 1, 0, 0, FG_I + 32), OBS_X, OBS_CEIL_Y - 2, 0, DMA);
@ -64,20 +70,19 @@ static void spawnObstacle(bool top){
static void killObstacle(s16 i){ static void killObstacle(s16 i){
obstacles[i].active = FALSE; obstacles[i].active = FALSE;
if(obstacles[i].top){ VDP_clearTileMapRect(BG_B, obstacles[i].startX, obstacles[i].top ? OBS_CEIL_Y : OBS_FLOOR_Y, 8, 4);
VDP_clearTileMapRect(BG_B, obstacles[i].startX, OBS_CEIL_Y, 8, 8); for(u8 x = 0; x < 2; x++)
for(u8 i = 0; i < 2; i++) VDP_drawImageEx(BG_B, &rock1t, TILE_ATTR_FULL(PAL2, 1, 0, 0, FG_I),obstacles[i].startX + 4 * i, CEIL_Y, 0, DMA); VDP_drawImageEx(BG_B, obstacles[i].top ? &rock1t : &rock1, TILE_ATTR_FULL(PAL2, 1, 0, 0, FG_I + (obstacles[i].top ? 0 : 16)),
} else { obstacles[i].startX + 4 * x, obstacles[i].top ? CEIL_Y : FLOOR_Y, 0, DMA);
VDP_clearTileMapRect(BG_B, obstacles[i].startX, OBS_FLOOR_Y, 8, 8); }
for(u8 i = 0; i < 2; i++) VDP_drawImageEx(BG_B, &rock1, TILE_ATTR_FULL(PAL2, 1, 0, 0, FG_I + 16), obstacles[i].startX + 4 * i, FLOOR_Y, 0, DMA);
} static void collideObstacle(s16 i){
collideObstacleWithPlayer(i);
} }
static void updateObstacle(s16 i){ static void updateObstacle(s16 i){
obstacles[i].pos.x = fix16Sub(obstacles[i].pos.x, FG_SPEED); obstacles[i].pos.x = fix16Sub(obstacles[i].pos.x, FG_SPEED);
if(obstacles[i].pos.x <= OBSTACLE_LIMIT_X){ obstacles[i].pos.x <= OBSTACLE_LIMIT_X ? killObstacle(i) : collideObstacle(i);
killObstacle(i);
}
} }

View File

@ -36,4 +36,6 @@ struct bulletSpawner {
bool bools[COUNT_INT]; bool bools[COUNT_INT];
s16 ints[COUNT_INT]; s16 ints[COUNT_INT];
fix16 fixes[COUNT_INT]; fix16 fixes[COUNT_INT];
}; };
void collideObstacleWithPlayer(s16);

View File

@ -27,9 +27,9 @@ static void loadInternals(){
static void loadGame(){ static void loadGame(){
loadBg(); loadBg();
loadChrome();
loadFg(); loadFg();
loadPlayer(); loadPlayer();
loadChrome();
} }
static void updateGame(){ static void updateGame(){

View File

@ -27,6 +27,8 @@ static void spawnPlayer(){
TILE_ATTR(PAL1, 0, 0, 0)); TILE_ATTR(PAL1, 0, 0, 0));
} }
#define PLAYER_SPEED FIX16(2.5)
#define PLAYER_SPEED_NORM FIX16(1.768)
// collision // collision
@ -45,10 +47,19 @@ static void checkPlayerBounds(){
else if(player.pos.y > PLAYER_LIMIT_H_HORI) player.pos.y = PLAYER_LIMIT_H_HORI; else if(player.pos.y > PLAYER_LIMIT_H_HORI) player.pos.y = PLAYER_LIMIT_H_HORI;
} }
void collideObstacleWithPlayer(s16 i){
if(obstacles[i].pos.x < fix16Add(player.pos.x, PLAYER_OFF) &&
fix16Add(obstacles[i].pos.x, obstacles[i].size.x) > fix16Sub(player.pos.x, PLAYER_OFF) &&
obstacles[i].pos.y < fix16Add(player.pos.y, PLAYER_OFF) &&
fix16Add(obstacles[i].pos.y, obstacles[i].size.y) > fix16Sub(player.pos.y, PLAYER_OFF)){
if(obstacles[i].pos.x > player.pos.x) player.pos.x = fix16Sub(fix16Sub(obstacles[i].pos.x, FG_SPEED), PLAYER_OFF);
else if(obstacles[i].pos.y > player.pos.y) player.pos.y = fix16Sub(obstacles[i].pos.y, PLAYER_OFF);
checkPlayerBounds();
}
}
// movement // movement
#define PLAYER_SPEED FIX16(2.5)
#define PLAYER_SPEED_NORM FIX16(1.768)
s16 playerFrame; s16 playerFrame;