Josh Davy / Mbed 2 deprecated Flip

Dependencies:   mbed el17jd

Committer:
joshdavy
Date:
Wed Apr 24 10:18:45 2019 +0000
Revision:
9:96969b1c6bde
Parent:
8:21b6d4dbce44
Child:
10:58cf89dd878c
Added win screen aswell as multiple levels.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joshdavy 3:b34685dbdb8d 1 #include "Player.h"
joshdavy 3:b34685dbdb8d 2
joshdavy 3:b34685dbdb8d 3
joshdavy 3:b34685dbdb8d 4
joshdavy 3:b34685dbdb8d 5 Player::Player()
joshdavy 3:b34685dbdb8d 6 {
joshdavy 8:21b6d4dbce44 7
joshdavy 3:b34685dbdb8d 8 }
joshdavy 3:b34685dbdb8d 9
joshdavy 3:b34685dbdb8d 10 Player::~Player()
joshdavy 3:b34685dbdb8d 11 {
joshdavy 3:b34685dbdb8d 12
joshdavy 3:b34685dbdb8d 13 }
joshdavy 3:b34685dbdb8d 14
joshdavy 8:21b6d4dbce44 15 void Player::init(int height,int width,Vector2D pos)
joshdavy 4:afbf3dd71403 16 {
joshdavy 8:21b6d4dbce44 17 _height = height;
joshdavy 8:21b6d4dbce44 18 _width = width;
joshdavy 8:21b6d4dbce44 19 _pos = pos;
joshdavy 9:96969b1c6bde 20 _initial_pos = pos;
joshdavy 8:21b6d4dbce44 21 _bitmap = (int *) player_bitmap_left; // Default
joshdavy 8:21b6d4dbce44 22 _orientation = 1;
joshdavy 8:21b6d4dbce44 23 _direction = -1;
joshdavy 4:afbf3dd71403 24 }
joshdavy 4:afbf3dd71403 25
joshdavy 9:96969b1c6bde 26 void Player::check_out_of_range()
joshdavy 9:96969b1c6bde 27 {
joshdavy 9:96969b1c6bde 28 if (_pos.x < -5 || _pos.x > 89 || _pos.y < -5 || _pos.y > 53) {
joshdavy 9:96969b1c6bde 29 _pos = _initial_pos;
joshdavy 9:96969b1c6bde 30 _orientation = 1;
joshdavy 9:96969b1c6bde 31 }
joshdavy 9:96969b1c6bde 32 }
joshdavy 9:96969b1c6bde 33
joshdavy 9:96969b1c6bde 34 bool Player::check_goal_reached(Vector2D goal)
joshdavy 9:96969b1c6bde 35 {
joshdavy 9:96969b1c6bde 36 bool goal_reached = false;
joshdavy 9:96969b1c6bde 37 int x_distance = abs(goal.x - _pos.x);
joshdavy 9:96969b1c6bde 38 int y_distance = abs(goal.y - _pos.y);
joshdavy 9:96969b1c6bde 39
joshdavy 9:96969b1c6bde 40 if (x_distance < 2 && y_distance < 6) {
joshdavy 9:96969b1c6bde 41 goal_reached = true;
joshdavy 9:96969b1c6bde 42 }
joshdavy 9:96969b1c6bde 43 return goal_reached;
joshdavy 9:96969b1c6bde 44 }
joshdavy 3:b34685dbdb8d 45
joshdavy 8:21b6d4dbce44 46 bool Player::can_move_down(Block blocks [],int number_of_blocks)
joshdavy 8:21b6d4dbce44 47 {
joshdavy 8:21b6d4dbce44 48 bool can_move_down = true;
joshdavy 8:21b6d4dbce44 49
joshdavy 8:21b6d4dbce44 50 for (int i = 0; i < number_of_blocks; i++) {
joshdavy 8:21b6d4dbce44 51 if (_pos.x + _width > blocks[i].first.x &&
joshdavy 8:21b6d4dbce44 52 _pos.x < blocks[i].second.x) {
joshdavy 8:21b6d4dbce44 53
joshdavy 8:21b6d4dbce44 54 if ( (_pos.y + _height) == blocks[i].first.y ) {
joshdavy 8:21b6d4dbce44 55 can_move_down = false;
joshdavy 8:21b6d4dbce44 56 }
joshdavy 8:21b6d4dbce44 57 }
joshdavy 8:21b6d4dbce44 58 }
joshdavy 8:21b6d4dbce44 59 return can_move_down;
joshdavy 8:21b6d4dbce44 60 }
joshdavy 8:21b6d4dbce44 61
joshdavy 8:21b6d4dbce44 62 bool Player::can_move_up(Block blocks [],int number_of_blocks)
joshdavy 8:21b6d4dbce44 63 {
joshdavy 8:21b6d4dbce44 64 bool can_move_up = true;
joshdavy 8:21b6d4dbce44 65 for (int i = 0; i < number_of_blocks; i++) {
joshdavy 8:21b6d4dbce44 66 if (_pos.x + _width > blocks[i].first.x &&
joshdavy 8:21b6d4dbce44 67 _pos.x < blocks[i].second.x) {
joshdavy 8:21b6d4dbce44 68
joshdavy 8:21b6d4dbce44 69 if ( (_pos.y) == blocks[i].second.y ) {
joshdavy 8:21b6d4dbce44 70 can_move_up = false;
joshdavy 8:21b6d4dbce44 71 }
joshdavy 8:21b6d4dbce44 72 }
joshdavy 8:21b6d4dbce44 73 }
joshdavy 8:21b6d4dbce44 74 return can_move_up;
joshdavy 8:21b6d4dbce44 75 }
joshdavy 8:21b6d4dbce44 76
joshdavy 8:21b6d4dbce44 77 bool Player::can_move_left(Block blocks [],int number_of_blocks)
joshdavy 8:21b6d4dbce44 78 {
joshdavy 8:21b6d4dbce44 79 bool can_move_left = true;
joshdavy 8:21b6d4dbce44 80 for (int i = 0; i < number_of_blocks; i++) {
joshdavy 8:21b6d4dbce44 81 if (_pos.y + _height > blocks[i].first.y &&
joshdavy 8:21b6d4dbce44 82 _pos.y < blocks[i].second.y) {
joshdavy 8:21b6d4dbce44 83
joshdavy 8:21b6d4dbce44 84 if ( (_pos.x) == blocks[i].second.x ) {
joshdavy 8:21b6d4dbce44 85 can_move_left = false;
joshdavy 8:21b6d4dbce44 86 }
joshdavy 8:21b6d4dbce44 87 }
joshdavy 8:21b6d4dbce44 88 }
joshdavy 8:21b6d4dbce44 89 return can_move_left;
joshdavy 8:21b6d4dbce44 90 }
joshdavy 8:21b6d4dbce44 91
joshdavy 8:21b6d4dbce44 92 bool Player::can_move_right(Block blocks [],int number_of_blocks)
joshdavy 8:21b6d4dbce44 93 {
joshdavy 8:21b6d4dbce44 94 bool can_move_right = true;
joshdavy 8:21b6d4dbce44 95
joshdavy 8:21b6d4dbce44 96 for (int i = 0; i < number_of_blocks; i++) {
joshdavy 8:21b6d4dbce44 97 if (_pos.y + _height > blocks[i].first.y &&
joshdavy 8:21b6d4dbce44 98 _pos.y < blocks[i].second.y) {
joshdavy 8:21b6d4dbce44 99
joshdavy 8:21b6d4dbce44 100 if ( (_pos.x + _width) == blocks[i].first.x ) {
joshdavy 8:21b6d4dbce44 101 can_move_right = false;
joshdavy 8:21b6d4dbce44 102 }
joshdavy 8:21b6d4dbce44 103 }
joshdavy 8:21b6d4dbce44 104 }
joshdavy 8:21b6d4dbce44 105 return can_move_right;
joshdavy 8:21b6d4dbce44 106 }
joshdavy 8:21b6d4dbce44 107
joshdavy 8:21b6d4dbce44 108 void Player::update(Gamepad &pad, Block blocks [],int number_of_blocks)
joshdavy 8:21b6d4dbce44 109 {
joshdavy 8:21b6d4dbce44 110 if (_direction == -1 && _orientation == 1) {
joshdavy 8:21b6d4dbce44 111 _bitmap = (int *) player_bitmap_left;
joshdavy 8:21b6d4dbce44 112 }
joshdavy 8:21b6d4dbce44 113 if (_direction == 1 && _orientation == 1) {
joshdavy 8:21b6d4dbce44 114 _bitmap = (int *) player_bitmap_right;
joshdavy 8:21b6d4dbce44 115 }
joshdavy 8:21b6d4dbce44 116 if (_direction == -1 && _orientation == -1) {
joshdavy 8:21b6d4dbce44 117 _bitmap = (int *) player_bitmap_left_flipped;
joshdavy 8:21b6d4dbce44 118 }
joshdavy 8:21b6d4dbce44 119 if (_direction == 1 && _orientation == -1) {
joshdavy 8:21b6d4dbce44 120 _bitmap = (int *) player_bitmap_right_flipped;
joshdavy 8:21b6d4dbce44 121 }
joshdavy 9:96969b1c6bde 122
joshdavy 9:96969b1c6bde 123 check_out_of_range();
joshdavy 8:21b6d4dbce44 124
joshdavy 8:21b6d4dbce44 125
joshdavy 8:21b6d4dbce44 126 if (pad.check_event(Gamepad::A_PRESSED) &&
joshdavy 9:96969b1c6bde 127 (!can_move_down(blocks,number_of_blocks) || !can_move_up(blocks,number_of_blocks))
joshdavy 9:96969b1c6bde 128 ) {
joshdavy 8:21b6d4dbce44 129
joshdavy 8:21b6d4dbce44 130 if (_orientation == 1) {
joshdavy 8:21b6d4dbce44 131 _orientation = -1;
joshdavy 8:21b6d4dbce44 132 } else {
joshdavy 8:21b6d4dbce44 133 _orientation = 1;
joshdavy 8:21b6d4dbce44 134 }
joshdavy 8:21b6d4dbce44 135 }
joshdavy 8:21b6d4dbce44 136
joshdavy 8:21b6d4dbce44 137 if (_orientation == 1 && can_move_down( blocks,
joshdavy 9:96969b1c6bde 138 number_of_blocks)) {
joshdavy 9:96969b1c6bde 139 _pos.y += GRAVITY;
joshdavy 8:21b6d4dbce44 140 }
joshdavy 9:96969b1c6bde 141 if (_orientation == -1 && can_move_up(blocks,
joshdavy 9:96969b1c6bde 142 number_of_blocks)) {
joshdavy 9:96969b1c6bde 143 _pos.y -= GRAVITY;
joshdavy 8:21b6d4dbce44 144 }
joshdavy 9:96969b1c6bde 145
joshdavy 8:21b6d4dbce44 146
joshdavy 8:21b6d4dbce44 147 if (pad.get_coord().x < -0.7f && can_move_left(blocks,number_of_blocks)) {
joshdavy 8:21b6d4dbce44 148 _pos.x -= 2;
joshdavy 8:21b6d4dbce44 149 _direction = -1;
joshdavy 8:21b6d4dbce44 150 }
joshdavy 8:21b6d4dbce44 151 if (pad.get_coord().x > 0.7f && can_move_right(blocks,number_of_blocks)) {
joshdavy 8:21b6d4dbce44 152 _pos.x += 2;
joshdavy 8:21b6d4dbce44 153 _direction = 1;
joshdavy 8:21b6d4dbce44 154 }
joshdavy 8:21b6d4dbce44 155
joshdavy 8:21b6d4dbce44 156
joshdavy 8:21b6d4dbce44 157
joshdavy 8:21b6d4dbce44 158
joshdavy 8:21b6d4dbce44 159 }
joshdavy 8:21b6d4dbce44 160
joshdavy 8:21b6d4dbce44 161
joshdavy 8:21b6d4dbce44 162