predictCollisions working in debug mode

This commit is contained in:
Alan Youngblood 2022-05-14 17:52:05 -04:00
parent 36adcdeac7
commit 4aa7fe1692
3 changed files with 88 additions and 15 deletions

Binary file not shown.

View File

@ -1,7 +1,7 @@
{
"GameName":"Beagle Rescue",
"WindowName":"Beagle Rescue",
"WindowSize":{"w":320,"h":200},
"WindowSize":{"w":320,"h":240},
"WindowFullScreen": 0,
"GlobalScale": 4
"GlobalScale": 1
}

View File

@ -55,6 +55,10 @@ auto& uiCamXInfo(manager.addEntity());
auto& uiCamYInfo(manager.addEntity());
auto& uiPlayerXInfo(manager.addEntity());
auto& uiPlayerYInfo(manager.addEntity());
auto& uiBoundary1Info(manager.addEntity());
auto& uiBoundary2Info(manager.addEntity());
auto& uiBoundary3Info(manager.addEntity());
auto& uiTextInstructions(manager.addEntity());
auto& gameScene(manager.addEntity());
@ -181,10 +185,10 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
uiInfo.addComponent<TransformComponent>(camera.w/gScale-94,10,72*gScale,96*gScale,gScale);
// uiInfo.addComponent<UITextComponent>("font", "CollisionHori: Vert: Jump: P.y : P.dy: YVec: ", 8, 12, 1);
uiInfo.addComponent<UITextComponent>("font", "Player PTiX: PTiY: P.x: P.y : Null: Null: ", 8, 12, gScale);
uiInfo.addComponent<UITextComponent>("font", "Player PTiX: PTiY: P.x: P.y : Bnd1: Bnd2: Bnd3:", 8, 12, gScale);
uiInfo.addGroup(groupUI_Layer3);
uiCamXInfo.addComponent<TransformComponent>(camera.w/gScale-48,24,40*gScale,12*gScale,gScale);
uiCamXInfo.addComponent<TransformComponent>(camera.w/gScale-48,23,40*gScale,12*gScale,gScale);
uiCamXInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiCamXInfo.addGroup(groupUI_Layer3);
@ -192,14 +196,27 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
uiCamYInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiCamYInfo.addGroup(groupUI_Layer3);
uiPlayerXInfo.addComponent<TransformComponent>(camera.w/gScale-48,48,40*gScale,12*gScale,gScale);
uiPlayerXInfo.addComponent<TransformComponent>(camera.w/gScale-49,49,40*gScale,12*gScale,gScale);
uiPlayerXInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiPlayerXInfo.addGroup(groupUI_Layer3);
uiPlayerYInfo.addComponent<TransformComponent>(camera.w/gScale-48,60,40*gScale,12*gScale,gScale);
uiPlayerYInfo.addComponent<TransformComponent>(camera.w/gScale-48,62,40*gScale,12*gScale,gScale);
uiPlayerYInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiPlayerYInfo.addGroup(groupUI_Layer3);
uiBoundary1Info.addComponent<TransformComponent>(camera.w/gScale-48,75,40*gScale,12*gScale,gScale);
uiBoundary1Info.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiBoundary1Info.addGroup(groupUI_Layer3);
uiBoundary2Info.addComponent<TransformComponent>(camera.w/gScale-48,88,40*gScale,12*gScale,gScale);
uiBoundary2Info.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiBoundary2Info.addGroup(groupUI_Layer3);
uiBoundary3Info.addComponent<TransformComponent>(camera.w/gScale-48,101,40*gScale,12*gScale,gScale);
uiBoundary3Info.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiBoundary3Info.addGroup(groupUI_Layer3);
// uiJumpInfo.addComponent<TransformComponent>(camera.w-48,48,40,12,gScale);
// uiJumpInfo.addComponent<UITextComponent>("font", "false", 8, 12, 1);
// uiJumpInfo.addGroup(groupUI_Layer3);
@ -209,7 +226,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
debugBoxRect.x = camera.w-(100*gScale);
debugBoxRect.y = 4*gScale;
debugBoxRect.w = 98*gScale;
debugBoxRect.h = 100*gScale;
debugBoxRect.h = 112*gScale;
debugBox = new UINineSlice("textBox");
debugBox->MakeSlices("textBox",32,32,14,16,14,16,debugBoxRect,1,Game::groupUI_Layer2);
@ -345,6 +362,10 @@ void Game::update()
uiPlayerXInfo.getComponent<UITextComponent>().updateString(std::to_string(playerX));
uiPlayerYInfo.getComponent<UITextComponent>().updateString(std::to_string(playerY));
int * foundBoundaries = predictCollisions();
uiBoundary1Info.getComponent<UITextComponent>().updateString(std::to_string((int)foundBoundaries[0]));
uiBoundary2Info.getComponent<UITextComponent>().updateString(std::to_string((int)foundBoundaries[1]));
uiBoundary3Info.getComponent<UITextComponent>().updateString(std::to_string((int)foundBoundaries[2]));
playerPosition.x = playerX;
playerPosition.y = playerY;
@ -454,14 +475,17 @@ void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blu
SDL_RenderDrawLine(renderer, srcpt.x, srcpt.y, destpt.x, destpt.y);
}
int * predictCollisions(){
int * Game::predictCollisions(){
static int boundaries[3];
// ===== LEFT =====
if (player.getComponent<TransformComponent>().velocity.x<0){
// ====== For Each Row ====
int i = 0;
for (int r=Game::pTileY-1;r<=Game::pTileY+1;r++){
// ====== For Each Tile (Column) =====
if(r<0){r=0;}
for (int c=Game::pTileX;c>Game::pTileX-Game::camera.w/gameScene.getComponent<TileMapComponent>().tileWidth;c--){
if(c<0){c=0;}
if(gameScene.getComponent<TileMapComponent>().colliders[r][c]>0){
boundaries[i] = c*gameScene.getComponent<TileMapComponent>().tileWidth+gameScene.getComponent<TileMapComponent>().tileWidth;
i++;
@ -470,8 +494,57 @@ int * predictCollisions(){
}
}
}
// ====== TODO: setup player moving in positive X, and Y axes =====
// ====== RIGHT ====
if (player.getComponent<TransformComponent>().velocity.x>0){
// ====== For Each Row ====
int i = 0;
for (int r=Game::pTileY-1;r<=Game::pTileY+1;r++){
if(r<0){r=0;}
// ====== For Each Tile (Column) =====
for (int c=Game::pTileX;c<Game::pTileX+Game::camera.w/gameScene.getComponent<TileMapComponent>().tileWidth;c++){
if(c<0){c=0;}
if(gameScene.getComponent<TileMapComponent>().colliders[r][c]>0){
boundaries[i] = c*gameScene.getComponent<TileMapComponent>().tileWidth;
i++;
break;
}
}
}
}
// ===== UP ====
if (player.getComponent<TransformComponent>().velocity.y<0){
// ====== For Each Column ====
int i = 0;
for (int c=Game::pTileX-1;c<=Game::pTileX+1;c++){
if(c<0){c=0;}
// ====== For Each Tile (Row) =====
for (int r=Game::pTileY;r>Game::pTileY-Game::camera.h/gameScene.getComponent<TileMapComponent>().tileWidth;r--){
if(r<0){r=0;}
if(gameScene.getComponent<TileMapComponent>().colliders[r][c]>0){
boundaries[i] = r*gameScene.getComponent<TileMapComponent>().tileWidth+gameScene.getComponent<TileMapComponent>().tileWidth;
i++;
break;
}
}
}
}
// ===== DOWN ====
if (player.getComponent<TransformComponent>().velocity.y>0){
// ====== For Each Column ====
int i = 0;
for (int c=Game::pTileX-1;c<=Game::pTileX+1;c++){
if(c<0){c=0;}
// ====== For Each Tile (Row) =====
for (int r=Game::pTileY;r<Game::pTileY+Game::camera.h/gameScene.getComponent<TileMapComponent>().tileWidth;r++){
if(r<0){r=0;}
if(gameScene.getComponent<TileMapComponent>().colliders[r][c]>0){
boundaries[i] = r*gameScene.getComponent<TileMapComponent>().tileWidth+gameScene.getComponent<TileMapComponent>().tileWidth;
i++;
break;
}
}
}
}
return boundaries;
}