Fixed up collision boxes allowing custom colliders

This commit is contained in:
2021-02-15 00:30:26 -05:00
parent 19f160b6ea
commit 78629be4d8
8 changed files with 69 additions and 41 deletions

View File

@ -23,6 +23,9 @@ public:
SDL_Texture* tex;
SDL_Rect srcR, destR;
int offsetX = 0;
int offsetY = 0;
TransformComponent* transform;
@ -42,11 +45,13 @@ public:
collider.w = collider.h = size*scale;
}
ColliderComponent(std::string t, int width, int height)
ColliderComponent(std::string t, int width, int height, bool hasOffset, int oX, int oY)
{
tag = t;
collider.w = width;
collider.h = height;
offsetX = oX;
offsetY = oY;
}
void init() override
@ -56,18 +61,21 @@ public:
entity->addComponent<TransformComponent>();
}
transform = &entity->getComponent<TransformComponent>();
collider.x = collider.x + offsetX;
collider.y = collider.y + offsetX;
tex = TextureManager::LoadTexture("assets/ColTex.png");
srcR = { 0, 0, 16, 16};
destR = { collider.x, collider.y, collider.w, collider.h };
// if(tag == "player"){
// destR = { 18, 28, 24, 24 };
// transform->height = 24;
// transform->width = 24;
// std::cout << "player collider init() ran" << std::endl;
// std::cout << "destR.w: " << destR.w << std::endl;
// std::cout << "destR.h: " << destR.h << std::endl;
// }
// if(tag == "player"){
// destR = { 18, 28, 24, 24 };
// collider.w = 16;
// collider.h = 16;
// transform->height = 24;
// transform->width = 24;
// std::cout << "player collider init() ran" << std::endl;
// std::cout << "destR.w: " << destR.w << std::endl;
// std::cout << "destR.h: " << destR.h << std::endl;
// }
// Game::colliders.push_back(this);
}
@ -75,20 +83,15 @@ public:
{
if(tag != "terrain")
{
collider.x = static_cast<int>(transform->position.x);
collider.y = static_cast<int>(transform->position.y);
collider.w = transform->width * transform->scale;
collider.h = transform->height * transform->scale;
collider.x = static_cast<int>(transform->position.x+offsetX);
collider.y = static_cast<int>(transform->position.y+offsetY);
// collider.w = transform->width * transform->scale;
// collider.h = transform->height * transform->scale;
// collider.w = 12 * transform->scale;
// collider.h = 12 * transform->scale;
//
// std::cout << "collider.w: " << collider.w << std::endl;
// std::cout << "collider.h: " << collider.h << std::endl;
// std::cout << "tag: " << tag << std::endl;
}
destR.x = collider.x - Game::camera.x;
destR.y = collider.y - Game::camera.y;
// std::cout << "tag: " << tag << std::endl;
}
void draw() override
@ -96,8 +99,10 @@ public:
if(tag == "terrain")
{
TextureManager::Draw(tex, srcR, destR, SDL_FLIP_NONE);
}
// TextureManager::DrawCollider(destR);
}
}
};

View File

@ -98,10 +98,11 @@ public:
}
break;
case SDLK_k:
if (Game::debugCollisionBoxes)
{ Game::debugCollisionBoxes = false; }
SDL_Delay(100);
if (!Game::debugCollisionBoxes)
{ Game::debugCollisionBoxes = true; }
else
{Game::debugCollisionBoxes = true; }
{Game::debugCollisionBoxes = false; }
break;
case SDLK_j:
transform->velocity.y = 0;