Advertisement
Cinestra

Project 3 Actor.cpp Part 1 (Finished)

May 29th, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #include "Actor.h"
  2. #include "StudentWorld.h"
  3.  
  4. // Students: Add code to this file, Actor.h, StudentWorld.h, and StudentWorld.cpp
  5.  
  6. #include <iostream>
  7. #include <string>
  8. using namespace std;
  9.  
  10. //
  11. // CHARACTER SECTION
  12. //
  13.  
  14. bool Character::can_move_in_direction(int dir) const
  15. {
  16. int potential_x, potential_y;
  17.  
  18. // getPositionInThisDirection() is used to calculate the position that is a particular distance
  19. // at a particular angle from the object's current position.
  20. //
  21. // void getPositionInThisDirection(int angle, int distance, int& newX, int& newY) const
  22. getPositionInThisDirection(dir, SPRITE_WIDTH, potential_x, potential_y);
  23.  
  24. // Now that we have the potential x and y from moving in that direction, check if it's empty or not
  25. return !(get_world()->is_empty_square(potential_x, potential_y));
  26. }
  27.  
  28. int Character::get_random_direction() const
  29. {
  30. int random_direction = randInt(0, 3) * 90;
  31. while (!can_move_in_direction(random_direction))
  32. random_direction = randInt(0, 3) * 90;
  33. return random_direction;
  34. }
  35.  
  36. void Character::update_sprite_direction()
  37. {
  38. if (m_walk_direction == left)
  39. setDirection(left);
  40. else
  41. setDirection(right);
  42. }
  43.  
  44. void Character::do_corner_turn()
  45. {
  46. // Going left or right prefers going up when possible
  47. if (m_walk_direction == left || m_walk_direction == right)
  48. {
  49. if (can_move_in_direction(up))
  50. m_walk_direction = up;
  51. else
  52. m_walk_direction = down;
  53. }
  54.  
  55. // Going up or down prefers going right when possible
  56. else if (m_walk_direction == up || m_walk_direction == down)
  57. {
  58. if (can_move_in_direction(right))
  59. m_walk_direction = right;
  60. else
  61. m_walk_direction = left;
  62. }
  63. }
  64.  
  65. //
  66. // PLAYER SECTION
  67. //
  68.  
  69. void Player::do_something()
  70. {
  71. // Two different states to account for
  72.  
  73. if (get_state() == WAITING_TO_ROLL)
  74. {
  75. const int action = get_world()->getAction(m_player_number);
  76.  
  77. if (action == ACTION_ROLL)
  78. {
  79. // Rolling the dice
  80.  
  81. const int dice_roll = randInt(1, 10);
  82. set_ticks(dice_roll * 8);
  83. set_state(WALKING);
  84.  
  85. // Comment the next line out when actually running program
  86. cout << "Rolled a " << dice_roll << endl;
  87. }
  88.  
  89. return;
  90. }
  91.  
  92. if (get_state() == WALKING)
  93. {
  94. // Check to make sure it can actually walk forward
  95. // getX() % SPRITE_WIDTH == 0 && getY() % SPRITE_HEIGHT == 0 means its arrived at a square
  96. if (getX() % SPRITE_WIDTH == 0 && getY() % SPRITE_HEIGHT == 0)
  97. {
  98. // If it cannot move forward, then do a corner turn
  99. if (!can_move_in_direction(get_walk_direction()))
  100. {
  101. do_corner_turn();
  102. update_sprite_direction();
  103. }
  104. }
  105.  
  106. // After the check, simply make it move forward and decrease the ticks
  107. moveAtAngle(get_walk_direction(), 2);
  108. set_ticks(get_ticks() - 1);
  109.  
  110. // Set back to walking state after exhausting all ticks
  111. if (get_ticks() == 0)
  112. set_state(WAITING_TO_ROLL);
  113. }
  114. }
  115.  
  116. //
  117. // SQUARE SECTION
  118. //
  119.  
  120. void Square::do_something()
  121. {
  122.  
  123. // Check if it is still alive/active (since Coin Squares can be destroyed by Bowsers).
  124. // If it is not active, the Coin Square must do nothingand immediately return.
  125.  
  126. if (is_active() == false)
  127. return;
  128. }
  129.  
  130. //
  131. // COIN SQUARE SECTION
  132. //
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement