Advertisement
Cinestra

Project 3 StudentWorld.cpp Progress 2

Jun 2nd, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #include "StudentWorld.h"
  2. #include "GameConstants.h"
  3. #include <string>
  4. using namespace std;
  5.  
  6. #include "Actor.h"
  7. #include <iostream>
  8.  
  9. GameWorld* createStudentWorld(string assetPath)
  10. {
  11. return new StudentWorld(assetPath);
  12. }
  13.  
  14. // Students: Add code to this file, StudentWorld.h, Actor.h, and Actor.cpp
  15.  
  16. StudentWorld::StudentWorld(string assetPath)
  17. : GameWorld(assetPath)
  18. {
  19. m_board = new Board;
  20. m_peach = nullptr;
  21. m_yoshi = nullptr;
  22. }
  23.  
  24. // set<Player*>& is a reference to the set of pointers that point to a player object
  25. void StudentWorld::get_players_on_square(std::set<Player*>& players_on_square, int x, int y)
  26. {
  27. // Utilizing set because there could be multiple players colliding with a square at once
  28. // Want to process both players on the square
  29.  
  30. if (m_peach->getX() == x && m_peach->getY() == y)
  31. players_on_square.insert(m_peach);
  32. if (m_yoshi->getX() == x && m_yoshi->getY() == y)
  33. players_on_square.insert(m_yoshi);
  34. }
  35.  
  36. Actor* StudentWorld::get_random_square(int x, int y)
  37. {
  38. std::vector<Actor*> possible_squares;
  39.  
  40. for (int i = 0; i < m_actors.size(); i++)
  41. {
  42. if (m_actors[i]->is_square() && m_actors[i]->getX() != x && m_actors[i]->getY() != y)
  43. possible_squares.push_back(m_actors[i]);
  44. }
  45.  
  46. int random_choice = randInt(0, possible_squares.size() - 1);
  47. return possible_squares[random_choice];
  48. }
  49.  
  50. int StudentWorld::init()
  51. {
  52. string board_file = assetPath() + "board0" + to_string(getBoardNumber()) + ".txt";
  53. Board::LoadResult res = m_board->loadBoard(board_file);
  54.  
  55. // Iterate rows first then columns
  56. for (int x = 0; x < BOARD_WIDTH; x++)
  57. {
  58. for (int y = 0; y < BOARD_HEIGHT; y++)
  59. {
  60. Board::GridEntry grid_entry = m_board->getContentsOf(x, y);
  61. const int sprite_x = x * SPRITE_WIDTH;
  62. const int sprite_y = y * SPRITE_HEIGHT;
  63.  
  64. // Drill in using const when possible and private
  65. //
  66. // Define coin value, don't use the number 3
  67.  
  68. switch (grid_entry)
  69. {
  70. case Board::player:
  71. m_peach = new Player(PEACH, sprite_x, sprite_y, this);
  72. m_yoshi = new Player(YOSHI, sprite_x, sprite_y, this);
  73. m_actors.push_back(m_peach);
  74. m_actors.push_back(m_yoshi);
  75. m_actors.push_back(new Coin_Square(3, sprite_x, sprite_y, this));
  76. break;
  77. case Board::blue_coin_square:
  78. m_actors.push_back(new Coin_Square(3, sprite_x, sprite_y, this));
  79. break;
  80. case Board::boo:
  81. m_actors.push_back(new Boo(sprite_x, sprite_y, this));
  82. m_actors.push_back(new Coin_Square(3, sprite_x, sprite_y, this));
  83. break;
  84. case Board::bowser:
  85. m_actors.push_back(new Bowser(sprite_x, sprite_y, this));
  86. m_actors.push_back(new Coin_Square(3, sprite_x, sprite_y, this));
  87. break;
  88. default:
  89. break;
  90. }
  91.  
  92. }
  93. }
  94.  
  95. startCountdownTimer(99); // this placeholder causes timeout after 5 seconds
  96. return GWSTATUS_CONTINUE_GAME;
  97.  
  98. }
  99.  
  100. int StudentWorld::move()
  101. {
  102. // This code is here merely to allow the game to build, run, and terminate after you hit ESC.
  103. // Notice that the return value GWSTATUS_NOT_IMPLEMENTED will cause our framework to end the game.
  104.  
  105. setGameStatText("Game will end in a few seconds");
  106.  
  107. if (timeRemaining() <= 0)
  108. {
  109. return GWSTATUS_NOT_IMPLEMENTED;
  110. }
  111.  
  112. for (size_t i = 0; i < m_actors.size(); i++)
  113. {
  114. m_actors[i]->do_something();
  115. }
  116.  
  117. return GWSTATUS_CONTINUE_GAME;
  118. }
  119.  
  120. void StudentWorld::cleanUp()
  121. {
  122. for (size_t i = 0; i < m_actors.size(); i++)
  123. {
  124. delete m_actors[i];
  125. m_actors[i] = nullptr;
  126. }
  127.  
  128. delete m_board;
  129. m_board = nullptr;
  130. }
  131.  
  132. StudentWorld::~StudentWorld()
  133. {
  134. cleanUp();
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement