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

Pages: [1]
1




This shows the targetdistance > distancetotravel when it goes towards the boundary, as you can see it bugs out at one point, and I can't think why because it works fine if its not against the boundary.

2
http://pastebin.com/JM1ckbKt

The problem is that clicking on a new target position or dragging the mouse while the spaceship is in motion will cause the ship to follow the cursor, but I am trying to do 1 click, spaceship flies there and cannot redirect before it reaches the target. I tried to achieve this by not letting it change the target position with the flag (!m_moving) while flying and I tested this by debugging it and it seems the target is not changing which is great which is why im confused as to why it lets me do this. I've disabled repeated key presses for the windows.

The second problem is the boundaries, sometimes it works correctly but often it won't, the spaceship will fly through at times. I've printed out targetposition vs the distancetotravel, and it showed it working for a while and then eventually (the targetposition starts increasing when it should be gradually decreasing before it satisfies the condition that targetposition

3
General / 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.


4
good point. fixed, thanks. program still crashes after a while, but it did before.

5
class Tile {

public:
        Tile();
        ~Tile();
        sf::RectangleShape getRect() const;
        sf::Vector2f getPos() const;

        void highlightTile();
        bool isTileHighlighted() const;
        void turnOffHighlight();

        void setPos(const sf::Vector2f&);
        void setColor(const sf::Color&);

private:
        sf::RectangleShape m_tile;
        bool m_isHighlighted;
};


#include "Tile.h"

Tile::Tile() : m_tile(sf::Vector2f(0, 0)), m_isHighlighted(false) {
}

void Tile::setPos(const sf::Vector2f& pos){
        m_tile.setPosition(pos);
}

sf::RectangleShape Tile::getRect() const {
        return m_tile;
}

sf::Vector2f Tile::getPos() const
{
        return m_tile.getPosition();
}

bool Tile::isTileHighlighted() const {
        return (m_tile.getOutlineColor() == sf::Color::Yellow);
}

void Tile::turnOffHighlight(){
        m_tile.setOutlineThickness(0);
}

void Tile::setColor(const sf::Color& col){
        m_tile.setFillColor(col);
}

void Tile::highlightTile() {
        m_tile.setOutlineThickness(5);
        m_tile.setOutlineColor(sf::Color::Yellow);
}

Tile::~Tile(){
}


Grid::Grid(float squareDim)
{
        Tile tilePiece;
        sf::Vector2f position(0, 0);
        int counter = 0; //counter for whether the column is even or odd
        int counter1 = 0; //counter for whether we are on an even or odd row
        for (int row = 0; row < 8; row++) {
                for (int column = 0; column < 8; column++) {

                        if (counter1 % 2 == 0 && counter % 2 == 0 || counter1 % 2 != 0 && counter % 2 != 0) {
                                tilePiece.setColor(sf::Color::Red);
                        }
                        else {
                                tilePiece.setColor(sf::Color::White);
                        }

                        tilePiece.setPos(position);
                        m_tileSet[row][column] = tilePiece; //correct coordinates
                        m_gridMap[row][column] = sf::Vector2f(tilePiece.getPos().x + squareDim / 2, tilePiece.getPos().y + squareDim / 2);
                        position.x += squareDim;
                        counter++;
                }
                position.y += squareDim;
                position.x = 0;
                counter = 0;
                counter1++;
        }
}



void Grid::drawBoard(Windows& wind)
{
        for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                        wind.Draw(m_tileSet[i][j].getRect());
                }
        }
}

 
Then I put the grid in a Game class where i have a bunch of draw calls, and place my drawcall into that. I dont understand at all why my tiles won't render since before I basically had a rectangle instead in my Grid's constructor, but wanted the better design so opted for a tile class. really confused since this is essentially the same thing and its not rendering at all.

6
#pragma once
#include "Windows.h"
#include "Grid.h"
#include "Team.h"
#include "GamePieces.h"

class Field
{
public:
        Field(const std::string&, int);
        void reset();
        void update();
        Windows* getWindow();
        void render();
        void drawTeamStart();
        ~Field();
private:
        GamePieces m_knight;
        Windows m_windows;
        float m_squareDim;
        Grid m_chessGrid;
        void constructChessBoard();
        Team m_wTeam;
        Team m_bTeam;
};



#include "Field.h"

Field::Field(const std::string& l_title, int size) : m_windows(l_title, sf::Vector2u(size, size)),
m_squareDim(size / 8), m_chessGrid(m_squareDim), m_wTeam(TeamColor::WHITE, m_chessGrid),
m_bTeam(TeamColor::BLACK, m_chessGrid)
{
        reset();
}

void Field::reset()
{      
        drawTeamStart();
}

void Field::update()
{
        m_windows.Update();
}

Windows* Field::getWindow()
{
        return &m_windows;
}

void Field::render()
{
        m_windows.BeginDraw();
        constructChessBoard();
        drawTeamStart();
        m_windows.EndDraw();
}

void Field::drawTeamStart() //draws both the black and white team in their starting positions
{
        for (int i = 0; i < 16; i++) {
                m_windows.Draw(m_wTeam.m_chessPieces[i].getPieceSprite());
                m_windows.Draw(m_bTeam.m_chessPieces[i].getPieceSprite());
        }
}

Field::~Field()
{
}

void Field::constructChessBoard()
{
        //construct chess grid

        sf::RectangleShape square(sf::Vector2f(m_squareDim,m_squareDim));
        sf::Vector2f position(0, 0);
        int counter = 0; //counter for whether the column is even or odd
        int counter1 = 0; //counter for whether we are on an even or odd row
        for (int row = 0; row < 8; row++) {
                for (int column = 0; column < 8; column++) {

                        if (counter1 % 2 == 0 && counter % 2 == 0 || counter1 % 2 != 0 && counter % 2 != 0) {
                                square.setFillColor(sf::Color::Red);
                        }
                        else {
                                square.setFillColor(sf::Color::White);
                        }

                        square.setPosition(position);
                        m_windows.Draw(square);

                        position.x += m_squareDim;
                        counter++;
                }
                position.y += m_squareDim;
                position.x = 0;
                counter = 0;
                counter1++;
        }
}



struct Grid {

        sf::Vector2f m_gridMap[8][8];
       
        Grid(){}

        Grid(float squareDim) {
                sf::Vector2f coord;
                coord.x += squareDim/2;
                coord.y += squareDim/2;
                for (int i = 0; i < 8; i++) {
                        coord.y += squareDim;
                        for (int j = 0; j < 8; j++) {
                                coord.x += squareDim;
                                m_gridMap[i][j] = coord;
                        }
                }
        }

};


#pragma once

#include "SFML\Graphics.hpp"
#include "Grid.h"

enum PieceType { //place gamePieces into Grid not field, cus grid is already in field
        W_BISHOP,
        W_PAWN,
        W_KING,
        W_QUEEN,
        W_ROOK,
        W_KNIGHT,
        B_BISHOP,
        B_PAWN,
        B_KING,
        B_QUEEN,
        B_ROOK,
        B_KNIGHT
};

class GamePieces
{
public:
        GamePieces(){}
        GamePieces(const PieceType& type, const sf::Vector2f& position) : m_type(type),m_position(position)
        {
                switch (type) {
                        case W_BISHOP:
                               
                                m_gamePiece.loadFromFile("w_bishop.png");
                                if (!m_gamePiece.loadFromFile("w_bishop.png")) {
                                        std::cout << "proof" << std::endl;
                                }
                                break;
                        case W_PAWN:
                                m_gamePiece.loadFromFile("w_pawn.png");
                                break;
                        case W_KING:
                                m_gamePiece.loadFromFile("w_king.png");
                                break;
                        case W_QUEEN:
                                m_gamePiece.loadFromFile("w_queen.png");
                                break;
                        case W_ROOK:
                                m_gamePiece.loadFromFile("w_rook.png");
                                break;
                        case W_KNIGHT:
                                m_gamePiece.loadFromFile("w_knight.png");
                                break;
                        case B_BISHOP:
                                m_gamePiece.loadFromFile("b_bishop.png");
                                break;
                        case B_PAWN:
                                m_gamePiece.loadFromFile("b_pawn.png");
                                break;
                        case B_KING:
                                m_gamePiece.loadFromFile("b_king.png");
                                break;
                        case B_QUEEN:
                                m_gamePiece.loadFromFile("b_queen.png");
                                break;
                        case B_ROOK:
                                m_gamePiece.loadFromFile("b_rook.png");
                                break;
                        default:
                                m_gamePiece.loadFromFile("b_knight.png");
                }
                m_size = m_gamePiece.getSize();
                m_gamePieceSprite.setTexture(m_gamePiece);
                m_gamePieceSprite.setOrigin(sf::Vector2f(m_size.x/2, m_size.y/2));
                m_gamePieceSprite.setPosition(position);
        }

        ~GamePieces(){}

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

private:
        sf::Texture m_gamePiece;
        sf::Sprite m_gamePieceSprite;
        sf::Vector2f m_position;
        PieceType m_type;
        sf::Vector2u m_size;
};


#pragma once
#include "SFML\Graphics.hpp"
#include "GamePieces.h"

enum TeamColor {
    BLACK,
    WHITE
};

struct Team {
        //16 pieces in regular chess
        //map that assigns specific game pieces coordinates

        GamePieces m_chessPieces[16];
        TeamColor color;

        Team() {}

        Team(const TeamColor& color, const Grid& grid) { //reference to constant grid variable
                sf::Vector2f coord;

        switch (color) {
                case WHITE:
                {
                        m_chessPieces[0] = GamePieces(PieceType::W_ROOK, grid.m_gridMap[0][0]);
                        m_chessPieces[1] = GamePieces(PieceType::W_KNIGHT, grid.m_gridMap[0][1]);
                        m_chessPieces[2] = GamePieces(PieceType::W_BISHOP, grid.m_gridMap[0][2]);
                        m_chessPieces[3] = GamePieces(PieceType::W_KING, grid.m_gridMap[0][3]);
                        m_chessPieces[4] = GamePieces(PieceType::W_QUEEN, grid.m_gridMap[0][4]);
                        m_chessPieces[5] = GamePieces(PieceType::W_BISHOP, grid.m_gridMap[0][5]);
                        m_chessPieces[6] = GamePieces(PieceType::W_KNIGHT, grid.m_gridMap[0][6]);
                        m_chessPieces[7] = GamePieces(PieceType::W_ROOK, grid.m_gridMap[0][7]);
                        int counter = 8;
                        for (int j = 0; j < 8; j++) {
                                //arranging pawns left to right on
                                m_chessPieces[counter] = GamePieces(PieceType::W_PAWN, grid.m_gridMap[1][j]);
                                counter++;
                        }
                }
                break;

                default:
                {
                        for (int j = 0; j < 8; j++) {
                                //arranging pawns left to right on
                                m_chessPieces[j] = GamePieces(PieceType::B_PAWN, grid.m_gridMap[7][j]);
                        }

                        m_chessPieces[8] = GamePieces(PieceType::B_ROOK, grid.m_gridMap[8][0]);
                        m_chessPieces[9] = GamePieces(PieceType::B_KNIGHT, grid.m_gridMap[8][1]);
                        m_chessPieces[10] = GamePieces(PieceType::B_BISHOP, grid.m_gridMap[8][2]);
                        m_chessPieces[11] = GamePieces(PieceType::B_KING, grid.m_gridMap[8][3]);
                        m_chessPieces[12] = GamePieces(PieceType::B_QUEEN, grid.m_gridMap[8][4]);
                        m_chessPieces[13] = GamePieces(PieceType::B_BISHOP, grid.m_gridMap[8][5]);
                        m_chessPieces[14] = GamePieces(PieceType::B_KNIGHT, grid.m_gridMap[8][6]);
                        m_chessPieces[15] = GamePieces(PieceType::B_ROOK, grid.m_gridMap[8][7]);
                }
                }
        }
};


So  the game sprite loads correctly when i just place it into the header but when i try to draw it to the screen it doesnt work. I ommitted the window class i made cus its not as relevant, but let me know if you want me t post it.

Pages: [1]
anything