Working on Collisions

This commit is contained in:
Alan Youngblood 2021-07-10 21:23:45 -04:00
parent de99b127b1
commit 10e5be7a65
6 changed files with 35 additions and 19 deletions

Binary file not shown.

View File

@ -70,19 +70,21 @@ public:
spriteType = sType; spriteType = sType;
if(sType == spriteAnimation) if(sType == spriteAnimation)
{ {
std::string bogusPath = "src/config/credits.json"; // std::string bogusPath = "src/config/credits.json";
std::ifstream fin(bogusPath); // std::ifstream fin(bogusPath);
std::ifstream fin(json);
if(fin.is_open()){ if(fin.is_open()){
std::cout<<"file is open"<<std::endl; // std::cout<<"file is open"<<std::endl;
} else { } else {
std::cout<<"file is NOT open"<<std::endl; std::cout<<"json file is NOT open"<<std::endl;
} }
if(fin.fail()){ if(fin.fail()){
std::cout<<"file open fail"<<std::endl; std::cout<<"json file open fail"<<std::endl;
} else{ } else{
std::cout<<"file open success"<<std::endl; // std::cout<<"json file open success"<<std::endl;
} }
std::ifstream jsonText(json); std::ifstream jsonText(json);
@ -111,7 +113,7 @@ public:
int toFrame = cJSON_GetObjectItem(animItem, "to")->valueint; int toFrame = cJSON_GetObjectItem(animItem, "to")->valueint;
Animation anim = Animation(fromFrame,toFrame,100); Animation anim = Animation(fromFrame,toFrame,100);
animations.emplace(name, anim); animations.emplace(name, anim);
// printf("Playing animation named: %s fromFrame:%d toFrame:%d \n",name,fromFrame,toFrame); // printf("Adding animation named: %s fromFrame:%d toFrame:%d \n",name,fromFrame,toFrame);
Play(name); Play(name);
} }

View File

@ -11,15 +11,14 @@
bool Collision::AABB(const SDL_Rect& recA, const SDL_Rect& recB) bool Collision::AABB(const SDL_Rect& recA, const SDL_Rect& recB)
{ {
if( if(
recA.x + recA.w >= recB.x && recA.x + recA.w > recB.x &&
recB.x + recB.w >= recA.x && recB.x + recB.w > recA.x &&
recA.y + recA.h >= recB.y && recA.y + recA.h > recB.y &&
recB.y + recB.h >= recA.y recB.y + recB.h > recA.y
) )
{ {
return true; return true;
} }
return false; return false;
} }
@ -28,6 +27,10 @@ bool Collision::AABB(const ColliderComponent& colA, const ColliderComponent& col
if(AABB(colA.collider, colB.collider)) if(AABB(colA.collider, colB.collider))
{ {
// std::cout << colA.tag << " hit: " << colB.tag << std::endl; // std::cout << colA.tag << " hit: " << colB.tag << std::endl;
// if(recA.x + recA.w >= recB.x) { printf("LeftCollision"); }
// if(recB.x + recB.w >= recA.x) { printf("RightCollision"); }
// if(recA.y + recA.h >= recB.y) { printf("TopCollision"); }
// if(recB.y + recB.h >= recA.y) { printf("BottomCollision"); }
return true; return true;
} }
else else

View File

@ -9,6 +9,7 @@
#define SRC_COLLISION_H_ #define SRC_COLLISION_H_
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
// #include "Vector2D.h"
class ColliderComponent; class ColliderComponent;
@ -17,6 +18,7 @@ class Collision
public: public:
static bool AABB(const SDL_Rect& recA, const SDL_Rect& recB); static bool AABB(const SDL_Rect& recA, const SDL_Rect& recB);
static bool AABB(const ColliderComponent& colA, const ColliderComponent& colB); static bool AABB(const ColliderComponent& colA, const ColliderComponent& colB);
// void showColType();
}; };

View File

@ -121,7 +121,8 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
map = new Map("terrain",globalScale,16); map = new Map("terrain",globalScale,16);
std::string myText = "Find lost puppies!\nThey need your help!"; // std::string myText = "Find lost puppies!\nThey need your help!";
std::string myText = "Press U to Start";
text = new UIText(myText, "font", 0, 0, 8, 12, globalScale); text = new UIText(myText, "font", 0, 0, 8, 12, globalScale);
text->ParseString(myText, 12, 22, globalScale, "text"); text->ParseString(myText, 12, 22, globalScale, "text");
@ -230,7 +231,9 @@ void Game::update()
SDL_Rect cCol = c->getComponent<ColliderComponent>().collider; SDL_Rect cCol = c->getComponent<ColliderComponent>().collider;
if(Collision::AABB(cCol, playerCol)) if(Collision::AABB(cCol, playerCol))
{ {
// printf("Collision detected!\nplayerIsGrounded:%d if 0: idle plays\n",playerIsGrounded); // printDebug("Collision Detected");
// printf("Collision Stats:\nPlayer Center\nx: %d\ny: %d\nObjectCollider Center\nx: %d\ny: %d\n",(playerCol.x+playerCol.w/2)/gScale,(playerCol.y+playerCol.h/2)/gScale,(cCol.x+cCol.w/2)/gScale,(cCol.y+cCol.h/2)/gScale);
// printDebug("");
if(!playerIsGrounded){ if(!playerIsGrounded){
player.getComponent<SpriteComponent>().Play("Idle"); player.getComponent<SpriteComponent>().Play("Idle");
} }
@ -324,10 +327,16 @@ void Game::clean()
printf("Game Cleaned\n"); printf("Game Cleaned\n");
} }
void Game::printDebug(char* debugInfo) std::string previousMessage = "";
void Game::printDebug(std::string debugInfo)
{ {
printf("%s",debugInfo); if (previousMessage != debugInfo)
printf("\n"); {
std::cout << debugInfo;
printf("\n");
}
previousMessage = debugInfo;
} }
void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue) void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue)

View File

@ -29,7 +29,7 @@ public:
void update(); void update();
void render(); void render();
void clean(); void clean();
void printDebug(char* debugInfo); void printDebug(std::string debugInfo);
static void drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue); static void drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue);
bool running() { return isRunning; } bool running() { return isRunning; }
// static void AddTile(int srcX, int srcY, int xpos, int ypos); // static void AddTile(int srcX, int srcY, int xpos, int ypos);