Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: Player/Player.cpp
- Revision:
- 10:58cf89dd878c
- Parent:
- 9:96969b1c6bde
- Child:
- 11:db27d3838514
--- a/Player/Player.cpp Wed Apr 24 10:18:45 2019 +0000
+++ b/Player/Player.cpp Mon May 06 10:11:42 2019 +0000
@@ -12,15 +12,15 @@
}
-void Player::init(int height,int width,Vector2D pos)
+void Player::init(Vector2D pos)
{
- _height = height;
- _width = width;
- _pos = pos;
- _initial_pos = pos;
+ _height = PLAYER_HEIGHT;
+ _width = PLAYER_WIDTH;
+ _pos = pos;
+ _initial_pos = pos;
_bitmap = (int *) player_bitmap_left; // Default
- _orientation = 1;
- _direction = -1;
+ _orientation = 1; // Upright
+ _direction = -1; // Left
}
void Player::check_out_of_range()
@@ -31,12 +31,12 @@
}
}
-bool Player::check_goal_reached(Vector2D goal)
+bool Player::check_goal_reached(Vector2D goal)
{
bool goal_reached = false;
int x_distance = abs(goal.x - _pos.x);
int y_distance = abs(goal.y - _pos.y);
-
+
if (x_distance < 2 && y_distance < 6) {
goal_reached = true;
}
@@ -105,56 +105,77 @@
return can_move_right;
}
-void Player::update(Gamepad &pad, Block blocks [],int number_of_blocks)
+void Player::update_sprite(int orientation, int direction)
{
- if (_direction == -1 && _orientation == 1) {
+ // If poiting left and upright
+ if (direction == -1 && orientation == 1) {
_bitmap = (int *) player_bitmap_left;
}
- if (_direction == 1 && _orientation == 1) {
+ // If poiting right and upright
+ if (direction == 1 && orientation == 1) {
_bitmap = (int *) player_bitmap_right;
}
- if (_direction == -1 && _orientation == -1) {
+ // If poiting left and upside down
+ if (direction == -1 && orientation == -1) {
_bitmap = (int *) player_bitmap_left_flipped;
}
- if (_direction == 1 && _orientation == -1) {
+ // If poiting right and upside down
+ if (direction == 1 && orientation == -1) {
_bitmap = (int *) player_bitmap_right_flipped;
}
-
- check_out_of_range();
+}
+void Player::process_inputs(Gamepad &pad,Block blocks [], int number_of_blocks)
+{
+ // If A pressed and touching a block vertically
if (pad.check_event(Gamepad::A_PRESSED) &&
- (!can_move_down(blocks,number_of_blocks) || !can_move_up(blocks,number_of_blocks))
+ (!can_move_down(blocks,number_of_blocks) ||
+ !can_move_up(blocks,number_of_blocks))
) {
-
+ // then flip the orientation
if (_orientation == 1) {
_orientation = -1;
} else {
_orientation = 1;
}
}
+ // If joystick is held left and not touching a block on the left
+ if (pad.get_coord().x < -0.7f && can_move_left(blocks,number_of_blocks)) {
+ // then move left and set the direction facing to -1 (left)
+ _pos.x -= SPEED;
+ _direction = -1;
+ }
+ // If joystick is held right and not touching a block on the right
+ if (pad.get_coord().x > 0.7f && can_move_right(blocks,number_of_blocks)) {
+ // then move right and set the direction facing to 1 (right)
+ _pos.x += SPEED;
+ _direction = 1;
+ }
+}
+void Player::gravity(Block blocks [], int number_of_blocks)
+{
+ // If upright and can move down gravity increases the position
if (_orientation == 1 && can_move_down( blocks,
number_of_blocks)) {
_pos.y += GRAVITY;
}
+ // If upside down and can move up gravity decreases the position
if (_orientation == -1 && can_move_up(blocks,
number_of_blocks)) {
_pos.y -= GRAVITY;
}
-
- if (pad.get_coord().x < -0.7f && can_move_left(blocks,number_of_blocks)) {
- _pos.x -= 2;
- _direction = -1;
- }
- if (pad.get_coord().x > 0.7f && can_move_right(blocks,number_of_blocks)) {
- _pos.x += 2;
- _direction = 1;
- }
-
-
-
+}
+void Player::update(Gamepad &pad, Block blocks [],int number_of_blocks)
+{
+ process_inputs(pad,blocks,number_of_blocks);
+ gravity(blocks,number_of_blocks);
+ check_out_of_range();
+ update_sprite(_orientation,_direction);
+
+
}