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

Author Topic: Chess game piece not moving  (Read 745 times)

0 Members and 1 Guest are viewing this topic.

GCGsauce

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Chess game piece not moving
« on: July 31, 2016, 03:41:02 pm »
#include "Tile.h"

Tile::Tile() : sf::RectangleShape(sf::Vector2f(105, 105)), m_isHighlighted(false)
{
    if (!m_cBuffer.loadFromFile("penclick.wav")) {
        std::cout << "Failed to load penclick.wav" << std::endl;
    }
}

bool Tile::isTileHighlighted() const {
    return m_isHighlighted;
}

void Tile::turnOffHighlight(){
    m_clickSound.setBuffer(m_cBuffer);
    m_clickSound.setVolume(15);
    m_clickSound.play();
    setPosition(sf::Vector2f(getPosition().x - 5, getPosition().y - 5));
    setSize(sf::Vector2f(getSize().x + 10, getSize().y + 10));
    setOutlineThickness(0);
    m_isHighlighted = false;
}

void Tile::highlightTile() {

    m_clickSound.setBuffer(m_cBuffer);
    m_clickSound.setVolume(15);
    m_clickSound.play();
    setPosition(sf::Vector2f(getPosition().x + 5, getPosition().y + 5));
    setSize(sf::Vector2f(95, 95)); //decrease size to be able to render border without clashes
    setOutlineThickness(5);
    setOutlineColor(sf::Color::Yellow);

    m_isHighlighted = true;
}

Tile::~Tile(){
}


#include "GamePieces.h"

float GamePieces::m_speed = 52.5;

GamePieces::GamePieces()
{
}

GamePieces::GamePieces(const std::string& type, const Color& c, const sf::Vector2f& position) : m_type(type), m_col(c), m_position(position)
{
    std::string filePath = ((c == Color::White) ? "w_" : "b_") + type + ".png";
    std::cout << filePath << std::endl;

    if (!m_gamePiece.loadFromFile(filePath)) {
        std::cout << "Failed" << std::endl;
    }
    else {
        m_gamePieceSprite.setTexture(m_gamePiece);
        m_gamePieceSprite.setOrigin(sf::Vector2f(m_gamePiece.getSize().x / 2, m_gamePiece.getSize().y / 2));
        m_gamePieceSprite.setPosition(position);
    }
}

sf::Sprite GamePieces::getPieceSprite() const
{
        return m_gamePieceSprite;
}

bool GamePieces::isWhite() const
{
    return (m_col==Color::White);
}

sf::Vector2f GamePieces::getPosition() const
{
    return m_position;
}

std::string GamePieces::getPieceType() const
{
    return m_type;
}

void GamePieces::movePiece(const Tile& tile, float dt)
{
    sf::Vector2f tileCoords = sf::Vector2f(tile.getPosition().x + 52.5, tile.getPosition().y + 52.5);
    std::cout << tileCoords.y << std::endl;
    while (m_gamePieceSprite.getPosition().y >= tileCoords.y) {
        m_gamePieceSprite.setPosition(sf::Vector2f(m_gamePieceSprite.getPosition().x, m_gamePieceSprite.getPosition().y - (m_speed*dt)));
        std::cout << m_gamePieceSprite.getPosition().y << std::endl;
    }
    m_position = sf::Vector2f(tileCoords.x, tileCoords.y);
}

void GamePieces::setPos(const sf::Vector2f& pos)
{
    m_position = pos;
}


    GamePieces::~GamePieces()
{
}


void Grid::update(Windows& wind, const sf::Event& event, const Team& w, const Team& b, float dt)
{
    if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {

        try {
            sf::Vector2f mousePosFloat = static_cast<sf::Vector2f>(sf::Mouse::getPosition(*wind.getWindows()));

            if ((mousePosFloat.x > (wind.getWindows()->getSize().x) || mousePosFloat.x < 0)) {
                throw 99;
            }
            if ((mousePosFloat.y > wind.getWindows()->getSize().y || mousePosFloat.y < 0)) {
                throw 99;
            }

            sf::Vector2f rowCol = windowCoordsToRowCol(mousePosFloat);
            Tile& tile = m_tileSet[(int)rowCol.x][(int)rowCol.y];
            //std::cout << tile.getPosition().x << ", " << tile.getPosition().y << std::endl;
            GamePieces* gamePiece = getGamePieceOnTile(rowCol.x, rowCol.y, w, b);

            if (tile.isTileHighlighted()) {
                tile.turnOffHighlight(); //moves square back in place and turns off outline

            }  
            else if (gamePiece != nullptr && gamePiece->isWhite() && !(isOneTileHighlighted(w))) { //only highlight tile if its white and no one in the team is
                                                                                                              //highlited
                tile.highlightTile();
            }
            else if (isOneTileHighlighted(w)&& (gamePiece == nullptr || !gamePiece->isWhite())) {

                if (gamePiece == nullptr) {//space clicked on is empty
                    getHighlightedGamePiece(w).movePiece(tile, dt);
                }
                else if (!gamePiece->isWhite()) {
                    //TODO move to space and eat opposing gamePiece
                }
            }
            else {
                if (!ebuffer.loadFromFile("error.wav")) {
                    std::cout << "Failed to load error.wav" << std::endl;
                }
                errorSound.setBuffer(ebuffer);
                errorSound.setVolume(8);
                errorSound.play();
            }

            //TODO find way of deleting the gamePiece pointer
        }
        catch (int x) {
            std::cout << "hello" << std::endl;
        }
    }

void Field::update(sf::Event& event)
{  
    float elapsedTime = m_elapsed.getElapsedTime().asSeconds();
    if (elapsedTime >= frameTime) {
        if (m_secondClock.getElapsedTime().asSeconds() >= 1.0f) {
            elapsedFrames = 0;
            m_secondClock.restart();
        }
        elapsedFrames++;
        m_elapsed.restart();
        while (m_windows.getWindows()->pollEvent(event)) {  // max 60 fps - cant go faster
            m_windows.Update(event);
            m_chessBoard.update(m_windows, event, m_wTeam, m_bTeam, elapsedTime);
        }
    }

So for now I'm just trying to select a game piece and then move it to an empty tile, and according to the debugger all of this executes fine as per the flow of the program. I event print out the location of the sprite as it makes its way to the new tile and it seems right. The "dt" time variable in this case is just the time between updates that exceeds 16 ms (so this is a fixed time step). I'm also wondering how I can make it so that it goes exactly to a specific point, while the printed location of the gamepiece sprite's y coord is close, i want it to be exact. My idea is for this gamepiece to move at a rate of 52.5 units per second, so a distance of around 2 tiles away or (105 units) in abt 2 seconds or ideally 120 frames. The counter in my while loop says it runs abt 230 times which is a bit odd as ive printed the frame rate in past...consistently at 59 fps.

Thanks for the help.


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10825
    • View Profile
    • development blog
    • Email
Re: Chess game piece not moving
« Reply #1 on: August 01, 2016, 01:49:54 pm »
And what's the question?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything