Ok, ok. The title isn't that clear. So here's my problem : I'm doing a Tower Defense and as we might expect, I don't want to create a Tower over a Tower already there. But problems is, I think the .contains method for sprite doesn't work... Or I'm just a retard (the later might be true.) Here's my code :
Entity.cpp
#include "Entity.hpp"
using namespace Corno;
Entity::Entity(float _x, float _y)
{
position = Vector2(_x, _y);
velocity = Vector2();
acceleration = Vector2();
}
void Entity::LoadSprite(){
if (!texture.loadFromFile(filename))
return;
sprite.setTexture(texture);
sprite.setPosition(position.x, position.y);
}
void Entity::Move(){
}
void Entity::Draw(sf::RenderWindow &win){
win.draw(sprite);
}
sf::FloatRect Entity::BoundingBox(){
return sprite.getLocalBounds();
}
BasicTower.cpp
#include "BasicTower.hpp"
using namespace sf;
BasicTower::BasicTower(float _x, float _y) : Tower(_x, _y)
{
filename = "Textures/baseTower.png";
LoadSprite();
}
and finally Game.cpp
#include "Game.hpp"
using namespace std;
using namespace sf;
Game::Game(){
win.create(VideoMode(800, 600), "Tower Defense");
towers = vector<Tower*>();
}
int Game::Run(){
while (win.isOpen())
{
Update();
Draw();
}
return 0;
}
void Game::Update(){
Event event;
while (win.pollEvent(event))
{
if (event.type == Event::Closed)
win.close();
if (Mouse::isButtonPressed(Mouse::Left))
{
AddTower();
}
}
}
void Game::Draw(){
win.clear(Color::White);
for (int i = 0; i < towers.size(); i++)
towers[i]->Draw(win);
win.display();
}
void Game::AddTower(){
Vector2i mouse = Mouse::getPosition(win);
for (int i = 0; i < towers.size(); i++){
if (towers[i]->BoundingBox().contains(mouse.x, mouse.y))
return;
}
towers.push_back(new BasicTower(mouse.x, mouse.y));
}
I think the problem might be in the AddTower() function. By the way, the tower Class does nothing.
thanks