Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Actor.h"
- #include "StudentWorld.h"
- // Students: Add code to this file, Actor.h, StudentWorld.h, and StudentWorld.cpp
- #include <iostream>
- #include <string>
- using namespace std;
- //
- // CHARACTER SECTION
- //
- bool Character::can_move_in_direction(int dir) const
- {
- int potential_x, potential_y;
- // getPositionInThisDirection() is used to calculate the position that is a particular distance
- // at a particular angle from the object's current position.
- //
- // void getPositionInThisDirection(int angle, int distance, int& newX, int& newY) const
- getPositionInThisDirection(dir, SPRITE_WIDTH, potential_x, potential_y);
- // Now that we have the potential x and y from moving in that direction, check if it's empty or not
- return !(get_world()->is_empty_square(potential_x, potential_y));
- }
- int Character::get_random_direction() const
- {
- int random_direction = randInt(0, 3) * 90;
- while (!can_move_in_direction(random_direction))
- random_direction = randInt(0, 3) * 90;
- return random_direction;
- }
- void Character::update_sprite_direction()
- {
- if (m_walk_direction == left)
- setDirection(left);
- else
- setDirection(right);
- }
- void Character::do_corner_turn()
- {
- // Going left or right prefers going up when possible
- if (m_walk_direction == left || m_walk_direction == right)
- {
- if (can_move_in_direction(up))
- m_walk_direction = up;
- else
- m_walk_direction = down;
- }
- // Going up or down prefers going right when possible
- else if (m_walk_direction == up || m_walk_direction == down)
- {
- if (can_move_in_direction(right))
- m_walk_direction = right;
- else
- m_walk_direction = left;
- }
- }
- bool Character::is_at_fork()
- {
- int number_of_possible_paths = 0;
- for (int i = 0; i < 360; i += 90)
- {
- if (can_move_in_direction(i))
- number_of_possible_paths++;
- }
- // There will always be one path given because it is the path it came from
- // Two paths present means its just moving forward in a given path
- // Three paths mean that there is actually a fork
- return (number_of_possible_paths > 2);
- }
- //
- // PLAYER SECTION
- //
- void Player::do_something()
- {
- // Two different states to account for
- if (get_state() == WAITING_TO_ROLL)
- {
- const int action = get_world()->getAction(m_player_number);
- if (action == ACTION_ROLL)
- {
- // Rolling the dice
- const int dice_roll = randInt(1, 10);
- set_ticks(dice_roll * 8);
- set_state(WALKING);
- // Comment the next line out when actually running program
- cout << "Rolled a " << dice_roll << endl;
- }
- return;
- }
- if (get_state() == WALKING)
- {
- // Check to make sure it can actually walk forward
- // getX() % SPRITE_WIDTH == 0 && getY() % SPRITE_HEIGHT == 0 means its arrived at a square
- if (getX() % SPRITE_WIDTH == 0 && getY() % SPRITE_HEIGHT == 0)
- {
- // If it cannot move forward, then do a corner turn
- if (!can_move_in_direction(get_walk_direction()))
- {
- do_corner_turn();
- update_sprite_direction();
- }
- }
- // After the check, simply make it move forward and decrease the ticks
- moveAtAngle(get_walk_direction(), 2);
- set_ticks(get_ticks() - 1);
- // Set back to walking state after exhausting all ticks
- if (get_ticks() == 0)
- set_state(WAITING_TO_ROLL);
- }
- }
- //
- // BADDIE SECTION
- //
- void Baddie :: do_something()
- {
- if (get_state() == PAUSED)
- {
- // players_on_square is DIFFERENT from m_players_on_square
- set<Player*> players_on_square;
- get_world()->get_players_on_square(players_on_square, getX(), getY());
- std::set<Player*>::iterator it;
- for (it = players_on_square.begin(); it != players_on_square.end(); it++)
- {
- // See if each object in players_on_square is present in m_players_on_square
- // If there is a new object in players_on_square, then we will add it to m_players_on_square
- // Then do the baddie behavior
- if (m_players_on_square->find(*it) == m_players_on_square->end())
- {
- m_players_on_square->insert(*it);
- do_baddie_behavior(*it);
- }
- }
- // Although we've handeled how to add new players to m_players_on_square
- // We also need to handle removing players on square if they're not there anymore
- // If they were there in the previous step, then they should still have a pointer to the player
- update_players_on_square(players_on_square);
- m_pause_counter--;
- if (m_pause_counter == 0)
- {
- // Roll the dice
- int squares_to_move = randInt(1, m_max_possible_squares_to_move);
- set_ticks(squares_to_move * 8);
- // Choose random direction to move
- int random_direction = get_random_direction();
- set_walk_direction(random_direction);
- update_sprite_direction();
- set_state(WALKING);
- }
- }
- if (get_state() == WALKING)
- {
- // If it has arrived at a square
- if (getX() % SPRITE_WIDTH == 0 && getY() % SPRITE_HEIGHT == 0)
- {
- if (is_at_fork())
- {
- int random_direction = get_random_direction();
- set_walk_direction(random_direction);
- }
- else if (!can_move_in_direction(get_walk_direction()))
- do_corner_turn();
- update_sprite_direction();
- }
- moveAtAngle(get_walk_direction(), 2);
- set_ticks(get_ticks() - 1);
- if (get_ticks() == 0)
- {
- set_state(PAUSED);
- m_pause_counter = 180;
- do_special_pause_behavior();
- }
- }
- }
- void Baddie::update_players_on_square(set<Player*>& updated)
- {
- // Remove all pointers present in m_players_on_square that isn't in updated
- auto itr = m_players_on_square->begin();
- while (itr != m_players_on_square->end())
- {
- // Iterate through m_players_on_square and check if each object pointed to is present in updated
- // If an object is in m_players_on_square but not on updated, then we will erase that pointer in m_players_on_square
- if (updated.find(*itr) == updated.end())
- // Erase actually shrinks the set and so we don't need to iterate just yet
- itr = m_players_on_square->erase(itr);
- else
- itr++;
- }
- }
- void Baddie::do_impacted_behavior()
- {
- // Blank for now
- }
- Baddie::~Baddie()
- {
- // Don't have to delete items that m_players_on_square is pointing to because it will be destructed manually by Student World
- m_players_on_square->clear();
- delete m_players_on_square;
- }
- //
- // BOO SECTION
- //
- void Boo::do_baddie_behavior(Player* player)
- {
- if (player->get_state() == WAITING_TO_ROLL)
- {
- Player* other_player = get_world()->get_other_player(player);
- if (randInt(1, 2) == 1)
- {
- // 50% chance to swap coins with other player
- int old_coins = player->get_coins();
- player->set_coins(other_player->get_coins());
- other_player->set_coins(old_coins);
- }
- else
- {
- // 50% chance to swap stars with other player
- int old_stars = player->get_stars();
- player->set_stars(other_player->get_stars());
- other_player->set_stars(old_stars);
- }
- get_world()->playSound(SOUND_BOO_ACTIVATE);
- }
- }
- //
- // BOWSER SECTION
- //
- void Bowser::do_baddie_behavior(Player* player)
- {
- if (player->get_state() == WAITING_TO_ROLL)
- {
- if (randInt(1, 2) == 1)
- {
- player->set_stars(0);
- player->set_coins(0);
- get_world()->playSound(SOUND_BOWSER_ACTIVATE);
- }
- }
- }
- void Bowser::do_special_pause_behavior()
- {
- // leave blank for now
- }
- //
- // SQUARE SECTION
- //
- void Square::do_something()
- {
- // Check if it is still alive/active (since Coin Squares can be destroyed by Bowsers).
- // If it is not active, the Coin Square must do nothingand immediately return.
- if (is_active() == false)
- return;
- }
- //
- // COIN SQUARE SECTION
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement