Fixed up collision boxes allowing custom colliders
This commit is contained in:
parent
19f160b6ea
commit
78629be4d8
Binary file not shown.
Before Width: | Height: | Size: 124 B After Width: | Height: | Size: 159 B |
Binary file not shown.
@ -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)
|
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);
|
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);
|
||||||
|
}
|
||||||
|
@ -16,8 +16,7 @@ class TextureManager
|
|||||||
public:
|
public:
|
||||||
static SDL_Texture* LoadTexture(const char* fileName);
|
static SDL_Texture* LoadTexture(const char* fileName);
|
||||||
static void Draw(SDL_Texture* tex, SDL_Rect src, SDL_Rect dest, SDL_RendererFlip flip);
|
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_ */
|
#endif /* SRC_TEXTUREMANAGER_H_ */
|
||||||
|
@ -23,6 +23,9 @@ public:
|
|||||||
|
|
||||||
SDL_Texture* tex;
|
SDL_Texture* tex;
|
||||||
SDL_Rect srcR, destR;
|
SDL_Rect srcR, destR;
|
||||||
|
|
||||||
|
int offsetX = 0;
|
||||||
|
int offsetY = 0;
|
||||||
|
|
||||||
TransformComponent* transform;
|
TransformComponent* transform;
|
||||||
|
|
||||||
@ -42,11 +45,13 @@ public:
|
|||||||
collider.w = collider.h = size*scale;
|
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;
|
tag = t;
|
||||||
collider.w = width;
|
collider.w = width;
|
||||||
collider.h = height;
|
collider.h = height;
|
||||||
|
offsetX = oX;
|
||||||
|
offsetY = oY;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init() override
|
void init() override
|
||||||
@ -56,18 +61,21 @@ public:
|
|||||||
entity->addComponent<TransformComponent>();
|
entity->addComponent<TransformComponent>();
|
||||||
}
|
}
|
||||||
transform = &entity->getComponent<TransformComponent>();
|
transform = &entity->getComponent<TransformComponent>();
|
||||||
|
collider.x = collider.x + offsetX;
|
||||||
|
collider.y = collider.y + offsetX;
|
||||||
tex = TextureManager::LoadTexture("assets/ColTex.png");
|
tex = TextureManager::LoadTexture("assets/ColTex.png");
|
||||||
srcR = { 0, 0, 16, 16};
|
srcR = { 0, 0, 16, 16};
|
||||||
destR = { collider.x, collider.y, collider.w, collider.h };
|
destR = { collider.x, collider.y, collider.w, collider.h };
|
||||||
// if(tag == "player"){
|
// if(tag == "player"){
|
||||||
// destR = { 18, 28, 24, 24 };
|
// destR = { 18, 28, 24, 24 };
|
||||||
// transform->height = 24;
|
// collider.w = 16;
|
||||||
// transform->width = 24;
|
// collider.h = 16;
|
||||||
// std::cout << "player collider init() ran" << std::endl;
|
// transform->height = 24;
|
||||||
// std::cout << "destR.w: " << destR.w << std::endl;
|
// transform->width = 24;
|
||||||
// std::cout << "destR.h: " << destR.h << std::endl;
|
// 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);
|
// Game::colliders.push_back(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,20 +83,15 @@ public:
|
|||||||
{
|
{
|
||||||
if(tag != "terrain")
|
if(tag != "terrain")
|
||||||
{
|
{
|
||||||
collider.x = static_cast<int>(transform->position.x);
|
collider.x = static_cast<int>(transform->position.x+offsetX);
|
||||||
collider.y = static_cast<int>(transform->position.y);
|
collider.y = static_cast<int>(transform->position.y+offsetY);
|
||||||
collider.w = transform->width * transform->scale;
|
// collider.w = transform->width * transform->scale;
|
||||||
collider.h = transform->height * transform->scale;
|
// collider.h = transform->height * transform->scale;
|
||||||
// collider.w = 12 * transform->scale;
|
// collider.w = 12 * transform->scale;
|
||||||
// collider.h = 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.x = collider.x - Game::camera.x;
|
||||||
destR.y = collider.y - Game::camera.y;
|
destR.y = collider.y - Game::camera.y;
|
||||||
// std::cout << "tag: " << tag << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw() override
|
void draw() override
|
||||||
@ -96,8 +99,10 @@ public:
|
|||||||
if(tag == "terrain")
|
if(tag == "terrain")
|
||||||
{
|
{
|
||||||
TextureManager::Draw(tex, srcR, destR, SDL_FLIP_NONE);
|
TextureManager::Draw(tex, srcR, destR, SDL_FLIP_NONE);
|
||||||
}
|
// TextureManager::DrawCollider(destR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -98,10 +98,11 @@ public:
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SDLK_k:
|
case SDLK_k:
|
||||||
if (Game::debugCollisionBoxes)
|
SDL_Delay(100);
|
||||||
{ Game::debugCollisionBoxes = false; }
|
if (!Game::debugCollisionBoxes)
|
||||||
|
{ Game::debugCollisionBoxes = true; }
|
||||||
else
|
else
|
||||||
{Game::debugCollisionBoxes = true; }
|
{Game::debugCollisionBoxes = false; }
|
||||||
break;
|
break;
|
||||||
case SDLK_j:
|
case SDLK_j:
|
||||||
transform->velocity.y = 0;
|
transform->velocity.y = 0;
|
||||||
|
@ -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<TransformComponent>(150*globalScale,80*globalScale,40,40,globalScale);
|
||||||
player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json");
|
player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json");
|
||||||
player.addComponent<KeyboardController>();
|
player.addComponent<KeyboardController>();
|
||||||
player.addComponent<ColliderComponent>("player");
|
player.addComponent<ColliderComponent>("player",8*globalScale,8*globalScale, true, 7*globalScale,36*globalScale);
|
||||||
player.addGroup(groupPlayers);
|
player.addGroup(groupPlayers);
|
||||||
|
|
||||||
puppy.addComponent<TransformComponent>(1024*globalScale,210*globalScale,36,30,globalScale);
|
puppy.addComponent<TransformComponent>(1024*globalScale,210*globalScale,36,30,globalScale);
|
||||||
@ -188,13 +188,13 @@ void Game::update()
|
|||||||
if (Mix_PlayingMusic() == 0)
|
if (Mix_PlayingMusic() == 0)
|
||||||
{
|
{
|
||||||
// std::cout << "Play Music Now" << std::endl;
|
// 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)
|
// if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0) == 0)
|
||||||
{
|
// {
|
||||||
Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0);
|
// Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0);
|
||||||
}
|
// }
|
||||||
|
|
||||||
manager.refresh();
|
manager.refresh();
|
||||||
manager.update();
|
manager.update();
|
||||||
@ -210,8 +210,6 @@ void Game::update()
|
|||||||
}
|
}
|
||||||
gravityOnPlayer = false;
|
gravityOnPlayer = false;
|
||||||
playerIsGrounded = true;
|
playerIsGrounded = true;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -242,7 +240,6 @@ void Game::update()
|
|||||||
camera.y = map->height-camera.h;
|
camera.y = map->height-camera.h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Game::render()
|
void Game::render()
|
||||||
{
|
{
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
@ -288,6 +285,20 @@ void Game::render()
|
|||||||
// destPt.x = 320.0*gScale;
|
// destPt.x = 320.0*gScale;
|
||||||
// destPt.y = 240.0*gScale;
|
// destPt.y = 240.0*gScale;
|
||||||
// drawLine(origPt,destPt,255,0,0);
|
// 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);
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -307,8 +318,8 @@ void Game::printDebug(char* debugInfo)
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue)
|
void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue)
|
||||||
// {
|
{
|
||||||
// SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
|
SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
|
||||||
// SDL_RenderDrawLine(renderer, srcpt.x, srcpt.y, destpt.x, destpt.y);
|
SDL_RenderDrawLine(renderer, srcpt.x, srcpt.y, destpt.x, destpt.y);
|
||||||
// }
|
}
|
||||||
|
@ -29,7 +29,7 @@ public:
|
|||||||
void render();
|
void render();
|
||||||
void clean();
|
void clean();
|
||||||
void printDebug(char* debugInfo);
|
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; }
|
bool running() { return isRunning; }
|
||||||
// static void AddTile(int srcX, int srcY, int xpos, int ypos);
|
// static void AddTile(int srcX, int srcY, int xpos, int ypos);
|
||||||
static SDL_Renderer *renderer;
|
static SDL_Renderer *renderer;
|
||||||
|
Loading…
Reference in New Issue
Block a user