Projekty z kursu Systemy operacyjne 2 na PWr
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Brick.cpp 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "Brick.h"
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <unistd.h>
  5. Platform *Brick::platform = 0;
  6. Brick::Brick(int xPosition, int descentRate)
  7. {
  8. //ctor
  9. this->xPosition = xPosition;
  10. this->yPosition = -1;
  11. this->descentRate = descentRate;
  12. this->falling = false;
  13. randomColor();
  14. }
  15. Brick::~Brick()
  16. {
  17. //dtor
  18. }
  19. void Brick::setPlatform(Platform *newPlatform)
  20. {
  21. platform = newPlatform;
  22. }
  23. int Brick::getxPosition()
  24. {
  25. return xPosition;
  26. }
  27. int Brick::getyPosition()
  28. {
  29. return yPosition;
  30. }
  31. int Brick::getColor()
  32. {
  33. return color;
  34. }
  35. bool Brick::isFalling()
  36. {
  37. return falling;
  38. }
  39. void Brick::fall()
  40. {
  41. if(!initialized)
  42. {
  43. std::cout << "Scene size not initialized!" << std::endl;
  44. return;
  45. }
  46. if(platform == 0)
  47. {
  48. std::cout << "Platform not set!" << std::endl;
  49. return;
  50. }
  51. falling = true;
  52. while(running & falling & yPosition < yMax - 2)
  53. {
  54. // Freeze game
  55. if(freezed)
  56. {
  57. std::unique_lock<std::mutex> freezeLock(freezeMutex);
  58. while(freezed)
  59. {
  60. freezeCondition.wait(freezeLock);
  61. }
  62. }
  63. // If game terminated, we shouldn't do all this stuff
  64. if(running)
  65. {
  66. yPosition++;
  67. if(yPosition == yMax - 2 && platform->getPosition() <= xPosition && platform->getEnd() >= xPosition)
  68. {
  69. falling = false;
  70. if(platform->getColor() == color)
  71. {
  72. points += 5;
  73. }
  74. else
  75. {
  76. if(points != 0)
  77. {
  78. points--;
  79. }
  80. freeze();
  81. }
  82. break;
  83. }
  84. usleep(250000 - 10000 * descentRate);
  85. }
  86. }
  87. if(running)
  88. {
  89. // Reset
  90. yPosition = -1;
  91. falling = false;
  92. randomColor();
  93. }
  94. }
  95. std::thread Brick::fallThread()
  96. {
  97. return std::thread(&Brick::fall, this);
  98. }
  99. void Brick::randomColor()
  100. {
  101. color = rand() % 6 + 1;
  102. }