Jump to content
  • Advertisement
Sign in to follow this  
Giancarlo Gatti

Chess Game Pieces Not Moving

This topic is 868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

  • #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.

 
Edited by GCGsauce

Share this post


Link to post
Share on other sites
Advertisement

It's a little hard to follow without the indentation but from what I can tell your movePiece function only ever gets called when  the mouse is pressed. Has this ever worked or is it the first time you are trying to do it? 

 

Also what does this do? getGamePieceOnTile There's not really enough here to figure out what is going on but I would advise rewriting this part. How do you see interaction occurring? First user selects a piece to move, then they select a tile to move to, then it animate moving there? If this is the case you might be better off going with a state based system which will be much easier for you to deal with than all those nasty if else things.

 

A few states you might want:

WaitingForPlayerToSelectPiece

WaitingForPlayerToSelectDestination

MovingPiece

 

You'll start in the WaitingForPlayerToSelectPiece (this can be just an enum, state = WaitingForPlayerToSelectPiece), you can either do this as an event or poll for mouse down as you are now in your update function.

switch(state)
{
   case WaitingForPlayerToSelectPiece:
      if(mouse down)
      {
         // do some checks, e.g. right color, can be moved and so on
         // if the checks fail then just break
         // Now you have a valid piece so store it and switch states
         piece = ...
         state = WaitingForPlayerToSelectDestination
      }
      break;
   case WaitingForPlayerToSelectDestination:
      // user might decide to cancel and select a different piece, in which case:
      if(cancel)
      {
         piece = null;
         state = WaitingForPlayerToSelectPiece
         break;
      }
      if(mouse down)
      {
         // check destination is valid, if it's not then just break
         // if you have a valid destination then:
         destination = ...
         state = MovingPiece
      }
      break;
   case MovingPiece:
      // check the piece is in position
      if(InPosition(piece, destination))
      {
         // swap players
         piece = null;
         destination = null;
         state = WaitingForPlayerToSelectPiece;
         break;
      }
      // otherwise you have to do your movement animation
      piece->move(destination, dt);
      break;
   default:
      break;
}

It would be good if you can do this more as an event based thing (OnMouseDown etc) but at the heart of it will still be polling. Usually 'click' is dealt with on mouse up rather than down (try it on something like a button on your browser, mouse down on it but don't mouse up, move the mouse off then release and nothing happens). With more of an event based approach you could do things like user drags and drops pieces (they could still animate after they have made their choice).

Edited by Nanoha

Share this post


Link to post
Share on other sites

Thanks for the input. The GamePieceOnTile method returns the game piece that is on the selected tile. 

 

http://gamedev.stackexchange.com/questions/126444/game-chess-sprite-not-moving-across-board

 

here you can see better format of the same code, im not sure how to make it appear similar on here (first time poster). 

 

What i have right now is select white tile, tile gets highlighted then you can unclick or move. i kinda just want to see if the current design to work and then revamp

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!