Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - DatCorno

Pages: [1]
1
Graphics / Draw a rotating 'head'
« on: February 22, 2014, 11:03:35 pm »
Hey there! I'm currently working on a Tower Defense type of game and it came to my mind that it would look way cooler if the 'head' of the towers (or the canon or whatever you want to call it) could rotate around and aim at the target. So I create two images : the tower's body and the canon. But here comes the problem : how do I put it in the middle of my sprite? I tried a lot of things : setting the origin to the center of the sprite, calculate the position of the head with the x and y of the body + de width/height but none of that worked...

I should mention that the image representing the head is too small, so I use sprite.scale to grow it, and I think it affects the result of what I'm trying to do.

I attached the cpp to the post. Here's the headers :

Tower.hpp :
#ifndef TOWER_HPP
#define TOWER_HPP

#include "Entity.hpp"
#include <functional>
#include <map>

class Tower;

namespace std {
        template <typename T>
        using add_pointer_ti = typename add_pointer<T>::type;
}

using TowerCreator = std::add_pointer_ti<Tower*()>;

class Tower : public Entity{
public:
        virtual ~Tower(){}

        void Draw(sf::RenderWindow& win);

        void setPosition(float, float);

        static Tower* create(sf::Keyboard::Key id);
        static void registerToFactory(sf::Keyboard::Key id, TowerCreator func);
        //TO ADD : Shoot and ApplyEffect (virtual)

protected :
        void LoadSprite();

        Tower(float, float);

        std::string headFileName;
        sf::Texture headTexture;
        sf::Sprite headSprite;
        int cost;
        int upgradeCost;
        float range;

private :
        static std::map<sf::Keyboard::Key, TowerCreator>& factories();

};

#endif

BasicTower.cpp :
#include "BasicTower.hpp"

using namespace sf;

class BasicTower::Factory{
public :
        Factory(){
                Tower::registerToFactory(Keyboard::Q, create);
        }

        static Tower* create();
};

BasicTower::Factory* BasicTower::factory = new BasicTower::Factory();

Tower* BasicTower::Factory::create(){
        return new BasicTower();
}

BasicTower::BasicTower() : Tower(0.0f, 0.0f) {
        filename = "Textures/baseTower.png";
        headFileName = "Textures/baseTowerHead.png";
        LoadSprite();
}

BasicTower::BasicTower(float _x, float _y) : Tower(_x, _y)
{
        headFileName = "Textures/baseTowerHead.png";
        filename = "Textures/baseTower.png";
        LoadSprite();
}

Game.hpp :
#ifndef GAME_HPP
#define GAME_HPP

#include "SFML\Graphics.hpp"
#include <vector>
#include <memory>
#include "Tower.hpp"
#include "BasicTower.hpp"

struct Game {

        Game();
        ~Game();
        int Run();

private:
        void Update();
        void Draw();
        void AddTower();
       
        std::unique_ptr<Entity> selectedEntity;
        std::unique_ptr<Tower> selectedTowerType;
        sf::RenderWindow win;
        std::vector<std::unique_ptr<Tower>> towers;
};

#endif

2
Graphics / Re: Check if draw over something
« on: February 17, 2014, 04:28:29 am »
I just figured it out when you were posting Alejandro! But thank you both for your help :D

3
Graphics / Re: Check if draw over something
« on: February 17, 2014, 04:18:47 am »
Problem is... I did debug my stuff, and the number were perfectly fine! contains() should have returned true

4
Graphics / Re: Check if draw over something
« on: February 17, 2014, 04:09:27 am »
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)

5
Graphics / Re: Check if draw over something
« on: February 17, 2014, 03:43:32 am »
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){

}


6
Graphics / Re: Check if draw over something
« on: February 17, 2014, 03:12:21 am »
That's what I meant sorry! The boundingBox method from Entity returns the Rect and I use it to check if the mouse is inside. But it doesn't work and keeps drwaing a tower over an other tower.

7
Graphics / Check if draw over something
« on: February 17, 2014, 02:58:21 am »
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

Pages: [1]