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 :D
By 'do nothing' I mean it has no methods, it's just here for Polymorphism so I can add new Tower type in the towers vector
Edit :
#include "Tower.hpp"
Tower::Tower(float _x, float _y) : Entity(_x, _y){
}
I'm sorry but looking at just part of your code is just guesswork. You should (preferably) reduce it down by taking out parts that aren't a part of the problem - a bit at a time - and post a smaller, complete version that still has the same problem, or you could attach the entire code and someone might have a look through it.
Also, I'm curious. Why does this have to be a vector of pointers?
towers = vector<Tower*>();
So this is the part that causes problems :
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));
}
The rest of the code I posted was so you could understand the 'behind the screens' but fairly, it's the if statement that doesn't hold.
(And I honestly don't quite know... I just thought, if I were to share the vector accross classes and functions I might want pointers... But it's kind of a no-brain act)
You have to use:
sprite.getGlobalBounds();
instead of:
sprite.getLocalBounds();
You have to use:
sprite.getGlobalBounds();
instead of:
sprite.getLocalBounds();
OMG! I can't believe I missed that :-[
I think it's because I've been staring at lots of getLocalBounds() in my own stuff >:(
Problem is... I did debug my stuff, and the number were perfectly fine! contains() should have returned true
It can't have returned the correct values since you were using the wrong boundaries. Looks like you need to debug your debugger!