0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 09:03:42 +00:00

Merge pull request #18 from Nimac0/Collision

Sliding along collision boxes
This commit is contained in:
ines 2024-01-28 17:55:46 +01:00 committed by GitHub
commit 0a25cab6e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 59 additions and 4 deletions

View File

@ -3,6 +3,7 @@
#include <SDL.h>
#include "Component.h"
#include "Vector2D.h"
class TransformComponent;
@ -15,10 +16,14 @@ public:
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
bool isProjectile = false;
ColliderComponent(const char* tag);
ColliderComponent(const char* tag, float hitboxScale);
void init() override;
void update() override;
void removeCollision();
void handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider);
};

View File

@ -4,6 +4,8 @@
#include <SDL_image.h>
#include <vector>
#include "Vector2D.h"
class AssetManager;
class ColliderComponent;

View File

@ -25,6 +25,10 @@ void ColliderComponent::init()
entity->addComponent<TransformComponent>();
}
if (strcmp(this->tag, "projectile") == 0) {
this->isProjectile = true;
}
transform = &entity->getComponent<TransformComponent>();
Game::colliders.push_back(this);
}
@ -43,3 +47,16 @@ void ColliderComponent::removeCollision()
{
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

@ -260,11 +260,25 @@ void Game::update()
{
if (SDL_HasIntersection(&player.getComponent<ColliderComponent>().collider, &cc->collider) && strcmp(cc->tag, "player") && cc->hasCollision)
{
player.getComponent<TransformComponent>().position = playerPos;
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)
{
enemy.getComponent<TransformComponent>().position = enemyPos;
if (!cc->isProjectile)
{
enemy.getComponent<ColliderComponent>().handleCollision(enemy.getComponent<TransformComponent>().position, enemy.getComponent<ColliderComponent>().collider, cc->collider);
}
else
{
enemy.getComponent<TransformComponent>().position = enemyPos;
}
}
}

View File

@ -44,12 +44,29 @@ void TransformComponent::update()
// if(velocity.x != 0 && velocity.y != 0)
float multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1; //normalizes vector
Vector2D newPos(
position.x + velocity.x * speed * multiplier,
position.y + velocity.y * speed * multiplier
);
if (newPos.x < 0 || newPos.x + (this->width * this->scale) > SCREEN_SIZE_WIDTH || newPos.y < 0 || newPos.y + (this->height * this->scale) > SCREEN_SIZE_HEIGHT)
return;
if (newPos.x < 0)
{
newPos.x = 0;
}
else if (newPos.x + (this->width * this->scale) > SCREEN_SIZE_WIDTH)
{
newPos.x = SCREEN_SIZE_WIDTH - (this->width * this->scale);
}
if (newPos.y < 0)
{
newPos.y = 0;
}
else if (newPos.y + (this->height * this->scale) > SCREEN_SIZE_HEIGHT)
{
newPos.y = SCREEN_SIZE_HEIGHT - (this->height * this->scale);
}
position = newPos;
}