0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

fixed collision interaction with player

This commit is contained in:
ineslelin 2024-01-28 16:49:08 +01:00
parent 314b58ac4a
commit c2cb8f9fc6
6 changed files with 38 additions and 19 deletions

1
extern/libpng vendored

@ -1 +0,0 @@
Subproject commit 71b9b5e16ef751d973a3935284382bc344ff9941

1
extern/zlib vendored

@ -1 +0,0 @@
Subproject commit 09155eaa2f9270dc4ed1fa13e2b4b2613e6e4851

View File

@ -3,6 +3,7 @@
#include <SDL.h> #include <SDL.h>
#include "Component.h" #include "Component.h"
#include "Vector2D.h"
class TransformComponent; class TransformComponent;
@ -15,10 +16,14 @@ public:
bool hasCollision; //added for removing collision of destroyed projectiles bool hasCollision; //added for removing collision of destroyed projectiles
float hitboxScale; //adds a seperate variable for the scale of the hitbox (not the sprite) so each sprite can have a different hitbox size if needed float hitboxScale; //adds a seperate variable for the scale of the hitbox (not the sprite) so each sprite can have a different hitbox size if needed
bool isProjectile = false;
ColliderComponent(const char* tag); ColliderComponent(const char* tag);
ColliderComponent(const char* tag, float hitboxScale); ColliderComponent(const char* tag, float hitboxScale);
void init() override; void init() override;
void update() override; void update() override;
void removeCollision(); void removeCollision();
void handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider);
}; };

View File

@ -31,8 +31,6 @@ public:
bool getWinner(); bool getWinner();
void handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider);
private: private:
int counter = 0; int counter = 0;
bool isRunning = false; bool isRunning = false;

View File

@ -25,6 +25,10 @@ void ColliderComponent::init()
entity->addComponent<TransformComponent>(); entity->addComponent<TransformComponent>();
} }
if (strcmp(this->tag, "projectile") == 0) {
this->isProjectile = true;
}
transform = &entity->getComponent<TransformComponent>(); transform = &entity->getComponent<TransformComponent>();
Game::colliders.push_back(this); Game::colliders.push_back(this);
} }
@ -43,3 +47,16 @@ void ColliderComponent::removeCollision()
{ {
this->hasCollision = false; this->hasCollision = false;
} }
void ColliderComponent::handleCollision(Vector2D& entityPos, SDL_Rect& entityCollider, SDL_Rect& componentCollider)
{
// collision to right of character
if (entityPos.x < componentCollider.x)
{
entityPos.x = componentCollider.x - entityCollider.w;
}
else // collision to left of character
{
entityPos.x = componentCollider.x + componentCollider.w;
}
}

View File

@ -158,11 +158,25 @@ void Game::update()
{ {
if (SDL_HasIntersection(&player.getComponent<ColliderComponent>().collider, &cc->collider) && strcmp(cc->tag, "player") && cc->hasCollision) if (SDL_HasIntersection(&player.getComponent<ColliderComponent>().collider, &cc->collider) && strcmp(cc->tag, "player") && cc->hasCollision)
{ {
handleCollision(player.getComponent<TransformComponent>().position, player.getComponent<ColliderComponent>().collider, cc->collider); if (!cc->isProjectile)
{
player.getComponent<ColliderComponent>().handleCollision(player.getComponent<TransformComponent>().position, player.getComponent<ColliderComponent>().collider, cc->collider);
}
else
{
player.getComponent<TransformComponent>().position = playerPos;
}
} }
if (SDL_HasIntersection(&enemy.getComponent<ColliderComponent>().collider, &cc->collider) && strcmp(cc->tag, "enemy") && cc->hasCollision) if (SDL_HasIntersection(&enemy.getComponent<ColliderComponent>().collider, &cc->collider) && strcmp(cc->tag, "enemy") && cc->hasCollision)
{ {
handleCollision(enemy.getComponent<TransformComponent>().position, enemy.getComponent<ColliderComponent>().collider, cc->collider); if (!cc->isProjectile)
{
enemy.getComponent<ColliderComponent>().handleCollision(enemy.getComponent<TransformComponent>().position, enemy.getComponent<ColliderComponent>().collider, cc->collider);
}
else
{
enemy.getComponent<TransformComponent>().position = enemyPos;
}
} }
} }
@ -262,16 +276,3 @@ bool Game::running() const
bool Game::getWinner() { bool Game::getWinner() {
return this->winner; return this->winner;
} }
void Game::handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider)
{
// collision to right of character
if (characterPos.x < componentCollider.x)
{
characterPos.x = componentCollider.x - characterCollider.w;
}
else // collision to left of character
{
characterPos.x = componentCollider.x + componentCollider.w;
}
}