Advertisement
Cinestra

Project 3 StudentWorld.cpp Part 1 (Finished)

May 29th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 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. }
  22.  
  23. int StudentWorld::init()
  24. {
  25. string board_file = assetPath() + "board0" + to_string(getBoardNumber()) + ".txt";
  26. Board::LoadResult res = m_board->loadBoard(board_file);
  27.  
  28. // Iterate rows first then columns
  29. for (int x = 0; x < BOARD_WIDTH; x++)
  30. {
  31. for (int y = 0; y < BOARD_HEIGHT; y++)
  32. {
  33. Board::GridEntry grid_entry = m_board->getContentsOf(x, y);
  34. const int sprite_x = x * SPRITE_WIDTH;
  35. const int sprite_y = y * SPRITE_HEIGHT;
  36.  
  37. // Drill in using const when possible and private
  38. //
  39. // Define coin value, don't use the number 3
  40.  
  41. switch (grid_entry)
  42. {
  43. case Board::player:
  44. m_peach = new Player(PEACH, sprite_x, sprite_y, this);
  45. m_actors.push_back(m_peach);
  46. m_actors.push_back(new Coin_Square(3, sprite_x, sprite_y, this));
  47. break;
  48. case Board::blue_coin_square:
  49. m_actors.push_back(new Coin_Square(3, sprite_x, sprite_y, this));
  50. break;
  51. default:
  52. break;
  53. }
  54.  
  55. }
  56. }
  57.  
  58. startCountdownTimer(99); // this placeholder causes timeout after 5 seconds
  59. return GWSTATUS_CONTINUE_GAME;
  60.  
  61. }
  62.  
  63. int StudentWorld::move()
  64. {
  65. // This code is here merely to allow the game to build, run, and terminate after you hit ESC.
  66. // Notice that the return value GWSTATUS_NOT_IMPLEMENTED will cause our framework to end the game.
  67.  
  68. setGameStatText("Game will end in a few seconds");
  69.  
  70. if (timeRemaining() <= 0)
  71. {
  72. return GWSTATUS_NOT_IMPLEMENTED;
  73. }
  74.  
  75. for (size_t i = 0; i < m_actors.size(); i++)
  76. {
  77. m_actors[i]->do_something();
  78. }
  79.  
  80. return GWSTATUS_CONTINUE_GAME;
  81. }
  82.  
  83. void StudentWorld::cleanUp()
  84. {
  85. for (size_t i = 0; i < m_actors.size(); i++)
  86. {
  87. delete m_actors[i];
  88. m_actors[i] = nullptr;
  89. }
  90.  
  91. delete m_board;
  92. m_board = nullptr;
  93. }
  94.  
  95. StudentWorld::~StudentWorld()
  96. {
  97. cleanUp();
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement