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 - Matan

Pages: [1]
1
General / How to keep the player on the grid, with smooth movement?
« on: June 10, 2021, 09:39:50 am »
Hi!
I want to remake the Xonix game.
I want my player (the yellow creature, Pikachu) to be able to move only "from cell to cell", so it can't stay in between.
The following situation is unwanted, because the player stands in the middle of a cell(s).
The green area is where I stepped.



How can I make the player move smoothly only within the cells?
(it already scaled to the size of the cell)

Controller Class:
while (window.isOpen())
        {

                if (auto event = sf::Event{}; window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        }
                }
               
                m_board.handleCollisions();
                deltaTime = clock.restart().asSeconds();
                window.clear(sf::Color::White);
                m_board.updateObjects(deltaTime);
               

                drawObjects(window, m_windowType);
                window.display();


        }


 


Player class:

void Player::update(float deltaTime)
{
        sf::Vector2f movement(0.0f, 0.0f);

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                movement.x -= m_speed * deltaTime;
                m_row = 0;
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                movement.x += m_speed * deltaTime;
                m_row = 1;
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                movement.y += m_speed * deltaTime;
                m_row = 2;
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                movement.y -= m_speed * deltaTime;
                m_row = 3;
        }
        else // stay in place
        {
                m_row = 2;
        }


        if (movement.x > 0.0f) // we moved right
                m_dir = Direction::Right;
        else if (movement.x < 0.0f)
                m_dir = Direction::Left;
        else if (movement.y > 0.0f) // we moved down
                m_dir = Direction::Down;
        else if (movement.y < 0.0f)
                m_dir = Direction::Up;
        else
                m_dir = Direction::Stay;

        Object::update(m_row, deltaTime, m_dir);
       
        m_sprite.move(movement);
}
 

Object class:
{
        m_animation.update(row, deltaTime, dir);
        m_sprite.setTextureRect(m_animation.uvRect);
}
 

Animation class:

void Animation::update(int row, float deltaTime, Direction dir)
{
        m_currentImage.y = row;
        m_totalTime += deltaTime;

        if (m_totalTime >= m_switchTime)
        {
                m_totalTime -= m_switchTime;
                m_currentImage.x++;

                if (m_currentImage.x >= m_imageCount.x)
                        m_currentImage.x = 0;
        }

        uvRect.left = m_currentImage.x * uvRect.width;
        uvRect.top = m_currentImage.y * uvRect.height;

}

 

Collision handler:  (it is not complete yet  :) )
void playerTile(Object& player,
                  Object& tile)
{
   Player& p = static_cast<Player&>(player);
    Tile& t = static_cast<Tile&>(tile);
   

    switch (t.getOccupationStatus())
    {
      case Occupation::Free:
            t.setOccupied(Occupation::Potential);
            break;

      case Occupation::Potential:
        // p.incPlayerHPBy(-1);
         
            break;

      case Occupation::Occupied:
          //p.closedArea(); // ----- to be added :)

    case Occupation::TrappedArea:
        break;
   
    }
}




Thank you very much! :)

2
General / Re: make part of the spritesheet transparent?
« on: June 09, 2021, 07:44:11 pm »
you don't set it every frame, you just set it once.

#include <SFML/Graphics.hpp>
int main(){
    sf::RenderWindow window(sf::VideoMode(200, 150), "SFML window");

    sf::Texture texture;
    if (!texture.loadFromFile("tileset.png"))
        return 1;
    sf::Sprite sprite(texture);
    sprite.setColor(sf::Color(255, 255, 255, 128));

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color::White);
        window.draw(sprite);
        window.display();
    }
    return 0;
}
 






BUT if you need to use SOME PARTS of the same sprite as half transparent, and other parts as opaque, you have these options (ranked from best to worst IMO):
  • use an image that is already colored half-transparent in the parts it should be
  • use two different sprites, one being for transparent parts; two sprites can share the same texture, and you then set the color of one as half transparent, as in the code above.
  • change the transparency every time you draw a rect with transparency different from the last one. you don't need to change transparency every frame; if in this loop you are moving from a rect half transparent to another half transparent, just dont touch the colors.

Thank you very much!
Yes, the last case is what I had:  some parts of the same sprite as half transparent.
I followed your first tip, and changed it original sprite.

3
General / Re: make part of the spritesheet transparent?
« on: June 09, 2021, 09:06:26 am »
How can I change it once so it will stay for the whole run?
What do you mean "it" exactly?
only part of my sprite.
the black part of the "tiles" picture (attached)
when i display, i want the grid to be less dominant (attached, too)

are you trying to replace a 'default' color with transparent (like old RPG Maker versions)?
can't you simply use PNGs that have transparent backgrounds by default?

i'm not trying to make the background trasnparent - i want the color to be half transparent
(something like mySprite.setFillColor(sf::Color(255, 255, 255, 128)) ; )
however, i don't know how to do so, and i don't want to set the color on each loop everytime over and over :)

4
General / make part of the spritesheet transparent?
« on: June 08, 2021, 02:52:16 pm »
Hello!
I'm trying to make only part of my spritesheet transparent.
I'm using this spritesheet for animation, however I don't want to change the sf::Color everytime I'm changing the rect.
How can I change it once so it will stay for the whole run?
Thanks!

Pages: [1]
anything