Fixed up collision boxes allowing custom colliders

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 124 B

After

Width:  |  Height:  |  Size: 159 B

Binary file not shown.

View File

@ -17,6 +17,18 @@ SDL_Texture* TextureManager::LoadTexture(const char* texture)
void TextureManager::Draw(SDL_Texture* tex, SDL_Rect src, SDL_Rect dest, SDL_RendererFlip flip)
{
SDL_RenderCopyEx(Game::renderer, tex, &src, &dest, NULL, NULL, flip);
}
void TextureManager::DrawCollider(SDL_Rect colliderRect)
{
Vector2D pt1, pt2, pt3, pt4;
pt1 = Vector2D(colliderRect.x,colliderRect.y);
pt2 = Vector2D(colliderRect.w-2,colliderRect.y);
pt3 = Vector2D(colliderRect.x,colliderRect.h-2);
pt4 = Vector2D(colliderRect.w-2,colliderRect.h-2);
Game::drawLine(pt1,pt2,255,0,255);
Game::drawLine(pt1,pt3,255,255,0);
Game::drawLine(pt2,pt4,0,0,255);
Game::drawLine(pt3,pt4,0,255,0);
}

View File

@ -16,8 +16,7 @@ class TextureManager
public:
static SDL_Texture* LoadTexture(const char* fileName);
static void Draw(SDL_Texture* tex, SDL_Rect src, SDL_Rect dest, SDL_RendererFlip flip);
static void DrawCollider(SDL_Rect colliderRect);
};
#endif /* SRC_TEXTUREMANAGER_H_ */

View File

@ -24,6 +24,9 @@ public:
SDL_Texture* tex;
SDL_Rect srcR, destR;
int offsetX = 0;
int offsetY = 0;
TransformComponent* transform;
ColliderComponent(std::string t)
@ -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,9 +99,11 @@ 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;

View File

@ -137,7 +137,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
// player.addComponent<TransformComponent>(150*globalScale,80*globalScale,40,40,globalScale);
player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json");
player.addComponent<KeyboardController>();
player.addComponent<ColliderComponent>("player");
player.addComponent<ColliderComponent>("player",8*globalScale,8*globalScale, true, 7*globalScale,36*globalScale);
player.addGroup(groupPlayers);
puppy.addComponent<TransformComponent>(1024*globalScale,210*globalScale,36,30,globalScale);
@ -188,13 +188,13 @@ void Game::update()
if (Mix_PlayingMusic() == 0)
{
// std::cout << "Play Music Now" << std::endl;
Mix_PlayMusic(assets->GetMusicTrack("simonZ"), -1);
// Mix_PlayMusic(assets->GetMusicTrack("simonZ"), -1);
}
if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0) == 0)
{
Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0);
}
// if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0) == 0)
// {
// Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0);
// }
manager.refresh();
manager.update();
@ -210,8 +210,6 @@ void Game::update()
}
gravityOnPlayer = false;
playerIsGrounded = true;
}
}
@ -242,7 +240,6 @@ void Game::update()
camera.y = map->height-camera.h;
}
void Game::render()
{
SDL_RenderClear(renderer);
@ -288,6 +285,20 @@ void Game::render()
// destPt.x = 320.0*gScale;
// destPt.y = 240.0*gScale;
// drawLine(origPt,destPt,255,0,0);
// Vector2D pt1, pt2, pt3, pt4;
// pt1.x = 80.0*gScale;
// pt1.y = 80.0*gScale;
// pt2.x = 120.0*gScale;
// pt2.y = 80.0*gScale;
// pt3.x = 80.0*gScale;
// pt3.y = 120.0*gScale;
// pt4.x = 120.0*gScale;
// pt4.y = 120.0*gScale;
// Draw box with colors
// drawLine(pt1,pt2,0,0,255);
// drawLine(pt1,pt3,0,255,0);
// drawLine(pt2,pt4,255,255,0);
// drawLine(pt3,pt4,255,0,0);
SDL_RenderPresent(renderer);
}
@ -307,8 +318,8 @@ void Game::printDebug(char* debugInfo)
printf("\n");
}
// void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue)
// {
// SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
// SDL_RenderDrawLine(renderer, srcpt.x, srcpt.y, destpt.x, destpt.y);
// }
void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue)
{
SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
SDL_RenderDrawLine(renderer, srcpt.x, srcpt.y, destpt.x, destpt.y);
}

View File

@ -29,7 +29,7 @@ public:
void render();
void clean();
void printDebug(char* debugInfo);
// 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; }
// static void AddTile(int srcX, int srcY, int xpos, int ypos);
static SDL_Renderer *renderer;