130 lines
2.8 KiB
C++
130 lines
2.8 KiB
C++
/*
|
|
* ColliderComponent.h
|
|
*
|
|
* Created on: Mar 8, 2020
|
|
* Author: ayoungblood
|
|
*/
|
|
|
|
#ifndef SRC_ECS_COLLIDERCOMPONENT_H_
|
|
#define SRC_ECS_COLLIDERCOMPONENT_H_
|
|
|
|
#include <string>
|
|
#include <SDL.h>
|
|
#include "Components.h"
|
|
#include "ECS.h"
|
|
#include "../assetmgr/TextureManager.h"
|
|
#include <iostream>
|
|
#include "../game/Vector2D.h"
|
|
|
|
class ColliderComponent : public Component
|
|
{
|
|
|
|
public:
|
|
|
|
SDL_Rect collider;
|
|
std::string tag;
|
|
|
|
SDL_Texture* tex;
|
|
SDL_Rect srcR, destR;
|
|
|
|
int offsetX = 0;
|
|
int offsetY = 0;
|
|
|
|
bool hidden = true;
|
|
|
|
Vector2D center;
|
|
|
|
TransformComponent* transform;
|
|
|
|
ColliderComponent()
|
|
{
|
|
center.Zero();
|
|
}
|
|
|
|
ColliderComponent(std::string t)
|
|
{
|
|
tag = t;
|
|
}
|
|
|
|
ColliderComponent(std::string t, int xpos, int ypos, int size, int scale, std::string texture)
|
|
{
|
|
tag = t;
|
|
collider.x = xpos;
|
|
collider.y = ypos;
|
|
collider.w = collider.h = size*scale;
|
|
if(texture != ""){
|
|
setTex(texture);
|
|
}
|
|
center.x = collider.x+collider.w/2;
|
|
center.y = collider.y+collider.h/2;
|
|
}
|
|
|
|
ColliderComponent(std::string t, int width, int height, bool hasOffset, int oX, int oY, std::string texture)
|
|
{
|
|
tag = t;
|
|
collider.w = width;
|
|
collider.h = height;
|
|
offsetX = oX;
|
|
offsetY = oY;
|
|
if(texture != ""){
|
|
setTex(texture);
|
|
}
|
|
center.x = collider.x+collider.w/2;
|
|
center.y = collider.y+collider.h/2;
|
|
}
|
|
|
|
void init() override
|
|
{
|
|
if (!entity->hasComponent<TransformComponent>())
|
|
{
|
|
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 };
|
|
}
|
|
|
|
void update() override
|
|
{
|
|
if(tag != "terrain")
|
|
{
|
|
collider.x = static_cast<int>(transform->position.x+offsetX);
|
|
collider.y = static_cast<int>(transform->position.y+offsetY);
|
|
}
|
|
destR.x = collider.x - Game::camera.x;
|
|
destR.y = collider.y - Game::camera.y;
|
|
center.x = collider.x+collider.w/2;
|
|
center.y = collider.y+collider.h/2;
|
|
// collider.x = transform->position.x;
|
|
// collider.y = transform->position.y;
|
|
}
|
|
|
|
void draw() override
|
|
{
|
|
// if(tag == "terrain")
|
|
// {
|
|
// TextureManager::Draw(tex, srcR, destR, SDL_FLIP_NONE);
|
|
// }
|
|
if(hidden == false)
|
|
{
|
|
// TextureManager::Draw(tex, srcR,destR,SDL_FLIP_NONE);
|
|
SDL_SetRenderDrawColor(Game::renderer,255,0,255,134);
|
|
SDL_RenderDrawRect(Game::renderer, &destR);
|
|
// SDL_RenderDrawPoint(Game::renderer, center.x, center.y);
|
|
}
|
|
}
|
|
|
|
void setTex(std::string id)
|
|
{
|
|
tex = Game::assets->GetTexture(id);
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
#endif /* SRC_ECS_COLLIDERCOMPONENT_H_ */
|