From de6d60a1ce9bc3223bc80f9cac7d03a16c63d8b2 Mon Sep 17 00:00:00 2001 From: Stephen Enders Date: Mon, 18 Jan 2021 16:19:40 -0500 Subject: Update vectors of pointers to vectors of objects Having a vector you can access the object stored and modify it if you ensure you get the reference i.e: T& o = vec[0]; --- helper.cpp | 50 ++++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) (limited to 'helper.cpp') diff --git a/helper.cpp b/helper.cpp index 07c917d..f1bb417 100644 --- a/helper.cpp +++ b/helper.cpp @@ -144,19 +144,18 @@ createPlayer(sf::Texture& texture) { std::shared_ptr player = std::make_shared(); player->score = 0; - player->pieces = - std::make_shared>>(); + player->pieces = std::make_shared>(); for (int i = 0; i < NUM_PIECES; i++) { - player->pieces->push_back(createPiece(i + 1, texture)); + player->pieces->push_back(*createPiece(i + 1, texture)); } return player; } -std::shared_ptr>> +std::shared_ptr> createAllDice(sf::Texture& die0Texture, sf::Texture& die1Texture) { - auto dice = std::make_shared>>(); + auto dice = std::make_shared>(); // create dice, even 0 odds 1 // there are 8 dice results int total @@ -173,18 +172,12 @@ createAllDice(sf::Texture& die0Texture, sf::Texture& die1Texture) if (i % 2 == 0) { sf::Sprite s; s.setTexture(die0Texture); - auto die = std::make_shared(); - die->value = 0; - die->show = true; - die->sprite = s; + struct dice_t die = { 0, true, s }; dice->push_back(die); } else { sf::Sprite s; s.setTexture(die1Texture); - auto die = std::make_shared(); - die->value = 1; - die->show = false; - die->sprite = s; + struct dice_t die = { 1, false, s }; dice->push_back(die); } } @@ -192,19 +185,13 @@ createAllDice(sf::Texture& die0Texture, sf::Texture& die1Texture) return dice; } -std::shared_ptr>> +std::shared_ptr> createRollSprites(sf::Texture& t1, sf::Texture& t2) { - auto sprites = std::make_shared>>(); - - auto s1 = std::make_shared(); - s1->setTexture(t1); - - auto s2 = std::make_shared(); - s2->setTexture(t2); + auto sprites = std::make_shared>(); - sprites->push_back(s1); - sprites->push_back(s2); + sprites->push_back(sf::Sprite(t1)); + sprites->push_back(sf::Sprite(t2)); return sprites; } @@ -226,11 +213,10 @@ clickedPiece(sf::Vector2i mousePosition, struct piece_t* piece) } bool -canMovePiece( - struct piece_t* piece, - int roll, - std::shared_ptr>> myPieces, - std::shared_ptr>> enemyPieces) +canMovePiece(struct piece_t* piece, + int roll, + std::shared_ptr> myPieces, + std::shared_ptr> enemyPieces) { int next = piece->position + roll; @@ -240,17 +226,17 @@ canMovePiece( } // colliding with another piece - for (std::shared_ptr p : (*myPieces)) { + for (struct piece_t& p : (*myPieces)) { // cannot move onto your own piece - if (p->id != piece->id && p->position == next) { + if (p.id != piece->id && p.position == next) { return false; } } // can't attack in safe square - for (std::shared_ptr p : (*enemyPieces)) { + for (struct piece_t& p : (*enemyPieces)) { // cannot move onto a protected enemy piece - if (next == SAFE_SPACE && p->position == SAFE_SPACE) { + if (next == SAFE_SPACE && p.position == SAFE_SPACE) { return false; } } -- cgit v1.2.3-54-g00ecf