//Base class
enum class type{
Jammer,
Door,
Box,
}
class BaseClass
{
void render();
void update();
int getId();
type m_type;
};
//This class is inherited by doors, enemy sphere...
class Jammable : public BaseClass
{
void jam(int l_id);
void unJam();
bool isJammed();
protected:
bool m_isJammed;
};
//This class is inherited by jammer, box...
class Movable : public BaseClass
{
void setPicekdUpStatus(bool l_status);
bool isPickedUp();
};
class Jammer : public Movable
{
void setTarget(std::unique_ptr<qw::BaseObject>* l_target)
{
m_target = dynamic_cast<qw::Jammable*>(l_target->get());
if(m_target != nullptr)
{
m_target->jam(this->getId());
m_targetId = m_target->getId();
m_hasTarget = true;
update();
}
}
void Jammer::releaseTarget(int l_targetId)
{
if(m_target != nullptr)
{
m_target->unJam();
m_hasTarget = false;
m_target = nullptr;
m_targetId = -1;
}
}
private:
Jammable* m_target;
int m_targetId = -1;
bool m_hasTarget;
};
enum class doorStatus{
Open,
Closed,
};
class Door : public Jammable
{
void update()
{
if(m_status == doorStatus::Closed)
m_sprite.setTextureRect(0,0,128,64);
else
m_sprite.setTextureRect(0,64,128,64);
}
private:
doorStatus m_status;
};
std::vector<std::unique_ptr<qw::BaseObject>> m_objectContainer;
m_objectContainer.emplace_back(new Jammer(&m_assetManager->GetTexture("jammer.png"), 1, sf::Vector2f(-100, -200)));
m_objectContainer.emplace_back(new Jammer(&m_assetManager->GetTexture("jammer.png"), 2, sf::Vector2f(100, 2000)));
m_objectContainer.emplace_back(new ForceFieldDoor(&m_assetManager->GetTexture("door-horizontal.png"), 3, sf::Vector2f(-100, 0)));
m_objectContainer.emplace_back(new ForceFieldDoor(&m_assetManager->GetTexture("door-vertical.png"), 4, sf::Vector2f(100,0)));
for(int i=0; i<m_objectContainer.size(), ++i)
{
if(m_objectContainer[itr]->getType() == qw::ObjectType::Jammer)
{
Jammer* childPtr = dynamic_cast<Jammer*>(m_objectContainer[itr].get());
if(childPtr != nullptr)
{
bool m_foundTarget = false;
for(unsigned int itr2 = 0; itr2 < m_objectContainer.size(); ++itr2)
{
//If ray collides with some jammable object. Here i had to add isJammable() to BaseObject so i don't need to cast it
if(Collision::BoundingBoxTest(*childPtr->getRay(), *m_objectContainer[itr2]->getSprite()) && m_objectContainer[itr2]->isJammable())
{
m_foundTarget = true;
if(childPtr->getTargetId() != m_objectContainer[itr2]->getId())
{
childPtr->releaseTarget(childPtr->getTargetId());
childPtr->setTarget(&m_objectContainer[itr2]);
}
}
}
if(!m_foundTarget)
{
childPtr->releaseTarget(-1);
}
}
}
}