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;
- }
- }
- //
- // 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);
- }
- }
- //
- // 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