Motion field display on tile map SFML

89 views Asked by At

I've tried to write a code that will draw a motion field by drawing green rectangles on a tile map everywhere where hero can go (hero can't move diagonally). It's very hard to me I have read: https://en.wikipedia.org/wiki/Taxicab_geometry but I still have problems.

This is a class:

class Range : public sf::Drawable, public sf::Transformable
    {
    public:
        sf::Texture tileset;
        void load(sf::Vector2u tileSize, int* floorC, int hero, unsigned int width, unsigned int height, int speed);
    
    private:
    
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
            states.transform *= getTransform();
    
            states.texture = &tileset;
    
            target.draw(m_vertices, states);
        }
    
        sf::VertexArray m_vertices;
    };

This a function:

void Range::load(sf::Vector2u tileSize, int* floorC, int hero, unsigned int width, unsigned int height, int speed)
    {
    int wid, hei;
    std::vector <int> tab;
    for (unsigned int i = 0; i < width; i++)
    {
        for (unsigned int j = 0; j < height; j++)//This means that the cell on left is i-1, on right i+1, up j-1, down j+1
        {
            tab.push_back(floorC[i + j * width] + 1);//every number will be higher because 0 means clear space and the class will create a tile map in clear space
            if (floorC[i + j * width] == hero)
            {
                floorC[i + j * width] += 30;
                wid = i;//I'm copying floorC which contains information about where are walls
                hei = j;//I'm saving the numbers of a cell which contains a hero position
            }
        }
    }


    for (unsigned int m = 0; m < speed; m++)//This loop should create left straight line
    {
        if (m == 0)//If m = 0 then 1 * k = 0 too. 
        {
            if (tab[(wid - 1) + hei * width] == 1 && wid - 1 > 0)
            {
                tab[(wid - 1) + hei * width] -= 1;//Now it lower a number create clear space if number in a cell is 1
            }
            else
            {
                m = speed;
            }

            for (unsigned int k = 0; k < speed - m; k++)//this should create a line up from current point on the left line
            {
                if (k == 0)
                {
                    if (tab[(wid - 1) + (hei - 1) * width] == 1 && hei - 1 > 0)
                    {
                        tab[(wid - 1) + (hei - 1) * width] -= 1;
                    }
                    else
                    {
                        k = speed;//it prevent getting a negative cell number
                    }
                }
                else
                {
                    if (tab[(wid - 1) + (hei - (1 * k)) * width] == 1 && hei - (1 * k) > 0)
                    {
                        tab[(wid - 1) + (hei - (1 * k)) * width] -= 1;
                    }
                    else
                    {
                        k = speed;
                    }
                }
            }
            for (unsigned int l = 0; l < speed - m; l++)//This loop creates a line down
            {
                if (l == 0)
                {
                    if (tab[(wid - 1) + (hei + 1) * width] == 1 && hei + 1 < height)
                    {
                        tab[(wid - 1) + (hei + 1) * width] -= 1;
                    }
                    else
                    {
                        l = speed;
                    }
                }
                else
                {
                    if (tab[(wid - 1) + (hei + (1 * l)) * width] == 1 && hei + (1 * l) < height)
                    {
                        tab[(wid - 1) + (hei + (1 * l)) * width] -= 1;
                    }
                    else
                    {
                        l = speed;
                    }
                }
            }
        }
        else//if m>0 everything is ok
        {
            if (tab[(wid - (1 * m)) + hei * width] == 1 && wid - (1 * m) > 0)
            {
                tab[(wid - (1 * m)) + hei * width] -= 1;
            }
            else
            {
                m = speed;
            }
            //These loops are the same:
            for (unsigned int k = 0; k < speed - m; k++)
            {
                if (k == 0)
                {
                    if (tab[(wid - (1 * m)) + (hei - 1) * width] == 1 && hei - 1 > 0)
                    {
                        tab[(wid - (1 * m)) + (hei - 1) * width] -= 1;
                    }
                    else
                    {
                        k = speed;
                    }
                }
                else
                {
                    if (tab[(wid - (1 * m)) + (hei - (1 * k)) * width] == 1 && hei - (1 * k) > 0)
                    {
                        tab[(wid - (1 * m)) + (hei - (1 * k)) * width] -= 1;
                    }
                    else
                    {
                        k = speed;
                    }
                }
            }
            for (unsigned int l = 0; l < speed - m; l++)
            {
                if (l == 0)
                {
                    if (tab[(wid - (1 * m)) + (hei + 1) * width] == 1 && hei + 1 < height)
                    {
                        tab[(wid - (1 * m)) + (hei + 1) * width] -= 1;
                    }
                    else
                    {
                        l = speed;
                    }
                }
                else
                {
                    if (tab[(wid - (1 * m)) + (hei + (1 * l)) * width] == 1 && hei + (1 * l) < height)
                    {
                        tab[(wid - (1 * m)) + (hei + (1 * l)) * width] -= 1;
                    }
                    else
                    {
                        l = speed;
                    }
                }
            }
        }
    }
    //second loop does the same thing but on right and that two loops at the end creates lines up and down
    //sorry if my English wasn't perfect it's a foreign language for me

    
    
    
        for (unsigned int t = 0; t < speed; t++)
        {
            if (t == 0)
            {
                if (tab[(wid + 1) + hei * width] == 1 && wid + 1 < width)
                {
                    tab[(wid + 1) + hei * width] -= 1;
                }
                else
                {
                    t = speed;
                }
    
                for (unsigned int o = 0; o < speed - t; o++)
                {
                    if (o == 0)
                    {
                        if (tab[(wid + 1) + (hei - 1) * width] == 1 && hei - 1 > 0)
                        {
                            tab[(wid + 1) + (hei - 1) * width] -= 1;
                        }
                        else
                        {
                            o = speed;
                        }
                    }
                    else
                    {
                        if (tab[(wid + 1) + (hei - (1 * o)) * width] == 1 && hei - (1 * o) > 0)
                        {
                            tab[(wid + 1) + (hei - (1 * o)) * width] -= 1;
                        }
                        else
                        {
                            o = speed;
                        }
                    }
                }
                for (unsigned int p = 0; p < speed - t; p++)
                {
                    if (p == 0)
                    {
                        if (tab[(wid + 1) + (hei + 1) * width] == 1 && hei + 1 < height)
                        {
                            tab[(wid + 1) + (hei + 1) * width] -= 1;
                        }
                        else
                        {
                            p = speed;
                        }
                    }
                    else
                    {
                        if (tab[(wid + 1) + (hei + (1 * p)) * width] == 1 && hei + (1 * p) < height)
                        {
                            tab[(wid + 1) + (hei + (1 * p)) * width] -= 1;
                        }
                        else
                        {
                            p = speed;
                        }
                    }
                }
            }
            else
            {
                if (tab[(wid + (1 * t)) + hei * width] == 1 && wid + (1 * t) < width)
                {
                    tab[(wid + (1 * t)) + hei * width] -= 1;
                }
                else
                {
                    t = speed;
                }
    
                for (unsigned int o = 0; o < speed - t; o++)
                {
                    if (o == 0)
                    {
                        if (tab[(wid + (1 * t)) + (hei - 1) * width] == 1 && hei - 1 > 0)
                        {
                            tab[(wid + (1 * t)) + (hei - 1) * width] -= 1;
                        }
                        else
                        {
                            o = speed;
                        }
                    }
                    else
                    {
                        if (tab[(wid + (1 * t)) + (hei - (1 * o)) * width] == 1 && hei - (1 * o) > 0)
                        {
                            tab[(wid + (1 * t)) + (hei - (1 * o)) * width] -= 1;
                        }
                        else
                        {
                            o = speed;
                        }
                    }
                }
                for (unsigned int p = 0; p < speed - t; p++)
                {
                    if (p == 0)
                    {
                        if (tab[(wid + (1 * t)) + (hei + 1) * width] == 1 && hei + 1 < height)
                        {
                            tab[(wid + (1 * t)) + (hei + 1) * width] -= 1;
                        }
                        else
                        {
                            p = speed;
                        }
                    }
                    else
                    {
                        if (tab[(wid + (1 * t)) + (hei + (1 * p)) * width] == 1 && hei + (1 * p) < height)
                        {
                            tab[(wid + (1 * t)) + (hei + (1 * p)) * width] -= 1;
                        }
                        else
                        {
                            p = speed;
                        }
                    }
                }
            }
        }
    
    
        for (unsigned int r = 0; r < speed; r++)
        {
            if (r == 0)
            {
                if (tab[wid + (hei - 1) * width] == 1 && hei - 1 > 0)
                {
                    tab[wid + (hei - 1) * width] -= 1;
                }
                else
                {
                    r = speed;
                }
            }
            else
            {
                if (tab[wid + (hei - (1 * r)) * width] == 1 && hei - (1 * r) > 0)
                {
                    tab[wid + (hei - (1 * r)) * width] -= 1;
                }
                else
                {
                    r = speed;
                }
            }
        }
        for (unsigned int s = 0; s < speed; s++)
        {
            if (s == 0)
            {
                if (tab[wid + (hei + 1) * width] == 1 && hei + 1 < height)
                {
                    tab[wid + (hei + 1) * width] -= 1;
                }
                else
                {
                    s = speed;
                }
            }
            else
            {
                if (tab[wid + (hei + (1 * s)) * width] == 1 && hei + (1 * s) < height)
                {
                    tab[wid + (hei + (1 * s)) * width] -= 1;
                }
                else
                {
                    s = speed;
                }
            }
        }
    
    
    
    
        m_vertices.setPrimitiveType(sf::Quads);
        m_vertices.resize(width * height * 4);
    
        int counter = 0;
    
        for (unsigned int g = 0; g < width; g++)
        {
            for (unsigned int h = 0; h < height; h++)
            {
                int tileNumber = tab[g + h * width];
    
                int tu = tileNumber % (tileset.getSize().x / tileSize.x);
                int tv = tileNumber / (tileset.getSize().x / tileSize.x);
    
                sf::Vertex* quad = &m_vertices[counter * 4];
    
                quad[0].position = sf::Vector2f((g * tileSize.x), (h * tileSize.y));
                quad[1].position = sf::Vector2f(((g + 1) * tileSize.x), (h * tileSize.y));
                quad[2].position = sf::Vector2f(((g + 1) * tileSize.x), ((h + 1) * tileSize.y));
                quad[3].position = sf::Vector2f((g * tileSize.x), ((h + 1) * tileSize.y));
    
                if (tileNumber == 0)
                {
                    counter++;
                    quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                    quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                    quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                    quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
                }
            }
        }
        m_vertices.resize(counter * 4);
    }

And this is a code in in main:

    int floorCreatures[] =
{
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 0, 0, 0, 5, 0, 2, 2, 2,
    2, 2, 0, 0, 0, 0, 0, 0, 0, 2,
    2, 2, 0, 0, 0, 0, 0, 0, 0, 2,
};

Range move;
move.tileset.loadFromFile("moveTile.png");
move.load(sf::Vector2u(64, 57), floorCreatures, 5, 10, 8, 2);

If I run the code only one rectangle will appear. What I did wrong?

0

There are 0 answers