mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-13 14:53:41 +00:00
Merge pull request #18 from Nimac0/Collision
Sliding along collision boxes
This commit is contained in:
commit
0a25cab6e8
@ -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);
|
||||||
};
|
};
|
||||||
@ -4,6 +4,8 @@
|
|||||||
#include <SDL_image.h>
|
#include <SDL_image.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "Vector2D.h"
|
||||||
|
|
||||||
class AssetManager;
|
class AssetManager;
|
||||||
class ColliderComponent;
|
class ColliderComponent;
|
||||||
|
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/Game.cpp
14
src/Game.cpp
@ -259,14 +259,28 @@ void Game::update()
|
|||||||
for (auto cc : colliders)
|
for (auto cc : colliders)
|
||||||
{
|
{
|
||||||
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)
|
||||||
|
{
|
||||||
|
if (!cc->isProjectile)
|
||||||
|
{
|
||||||
|
player.getComponent<ColliderComponent>().handleCollision(player.getComponent<TransformComponent>().position, player.getComponent<ColliderComponent>().collider, cc->collider);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
player.getComponent<TransformComponent>().position = playerPos;
|
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)
|
||||||
|
{
|
||||||
|
if (!cc->isProjectile)
|
||||||
|
{
|
||||||
|
enemy.getComponent<ColliderComponent>().handleCollision(enemy.getComponent<TransformComponent>().position, enemy.getComponent<ColliderComponent>().collider, cc->collider);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
enemy.getComponent<TransformComponent>().position = enemyPos;
|
enemy.getComponent<TransformComponent>().position = enemyPos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//checking if projectiles hit player1 or player2
|
//checking if projectiles hit player1 or player2
|
||||||
for (auto& p : projectiles) {
|
for (auto& p : projectiles) {
|
||||||
|
|||||||
@ -44,12 +44,29 @@ void TransformComponent::update()
|
|||||||
// if(velocity.x != 0 && velocity.y != 0)
|
// if(velocity.x != 0 && velocity.y != 0)
|
||||||
|
|
||||||
float multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1; //normalizes vector
|
float multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1; //normalizes vector
|
||||||
|
|
||||||
Vector2D newPos(
|
Vector2D newPos(
|
||||||
position.x + velocity.x * speed * multiplier,
|
position.x + velocity.x * speed * multiplier,
|
||||||
position.y + velocity.y * 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;
|
position = newPos;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user