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:
- 8:21b6d4dbce44
- Parent:
- 7:68e06dda79f7
- Child:
- 9:96969b1c6bde
--- a/Player/Player.cpp Wed Apr 17 12:39:46 2019 +0000
+++ b/Player/Player.cpp Fri Apr 19 17:54:09 2019 +0000
@@ -4,7 +4,7 @@
Player::Player()
{
- _orientation = 1;
+
}
Player::~Player()
@@ -12,44 +12,129 @@
}
-void Player::update(Gamepad &pad, Block blocks [],int number_of_blocks)
+void Player::init(int height,int width,Vector2D pos)
{
-// if orientation = 1 then
-// for block
-// if in x range of the block
-// if y - height is equal to block y
-// then dont move down
-// else move down
-
-
- if (pad.check_event(Gamepad::L_PRESSED)) {
- _pos.x -= 2;
- }
- if (pad.check_event(Gamepad::R_PRESSED)) {
- _pos.x += 2;
- }
- if (pad.check_event(Gamepad::A_PRESSED)) {
- flip();
- }
- bool can_move_down = false;
- for (int i = 0; i < number_of_blocks;i++) {
- if (_pos.x + _width > blocks[i].first.x &&
- _pos.x - _width < blocks[i].second.x) {
-
- if ( (_pos.y + _height) == blocks[i].first.y ) {
- can_move_down = false;
- }
- }
- }
-
- if (can_move_down) {
- _pos.y += GRAVITY;
- }
-
-
-
+ _height = height;
+ _width = width;
+ _pos = pos;
+ _bitmap = (int *) player_bitmap_left; // Default
+ _orientation = 1;
+ _direction = -1;
}
+bool Player::can_move_down(Block blocks [],int number_of_blocks)
+{
+ bool can_move_down = true;
+
+ for (int i = 0; i < number_of_blocks; i++) {
+ if (_pos.x + _width > blocks[i].first.x &&
+ _pos.x < blocks[i].second.x) {
+
+ if ( (_pos.y + _height) == blocks[i].first.y ) {
+ can_move_down = false;
+ }
+ }
+ }
+ return can_move_down;
+}
+
+bool Player::can_move_up(Block blocks [],int number_of_blocks)
+{
+ bool can_move_up = true;
+ for (int i = 0; i < number_of_blocks; i++) {
+ if (_pos.x + _width > blocks[i].first.x &&
+ _pos.x < blocks[i].second.x) {
+
+ if ( (_pos.y) == blocks[i].second.y ) {
+ can_move_up = false;
+ }
+ }
+ }
+ return can_move_up;
+}
+
+bool Player::can_move_left(Block blocks [],int number_of_blocks)
+{
+ bool can_move_left = true;
+ for (int i = 0; i < number_of_blocks; i++) {
+ if (_pos.y + _height > blocks[i].first.y &&
+ _pos.y < blocks[i].second.y) {
+
+ if ( (_pos.x) == blocks[i].second.x ) {
+ can_move_left = false;
+ }
+ }
+ }
+ return can_move_left;
+}
+
+bool Player::can_move_right(Block blocks [],int number_of_blocks)
+{
+ bool can_move_right = true;
+
+ for (int i = 0; i < number_of_blocks; i++) {
+ if (_pos.y + _height > blocks[i].first.y &&
+ _pos.y < blocks[i].second.y) {
+
+ if ( (_pos.x + _width) == blocks[i].first.x ) {
+ can_move_right = false;
+ }
+ }
+ }
+ return can_move_right;
+}
+
+void Player::update(Gamepad &pad, Block blocks [],int number_of_blocks)
+{
+ if (_direction == -1 && _orientation == 1) {
+ _bitmap = (int *) player_bitmap_left;
+ }
+ if (_direction == 1 && _orientation == 1) {
+ _bitmap = (int *) player_bitmap_right;
+ }
+ if (_direction == -1 && _orientation == -1) {
+ _bitmap = (int *) player_bitmap_left_flipped;
+ }
+ if (_direction == 1 && _orientation == -1) {
+ _bitmap = (int *) player_bitmap_right_flipped;
+ }
+
+
+ if (pad.check_event(Gamepad::A_PRESSED) &&
+ (!can_move_down(blocks,number_of_blocks) || !can_move_up(blocks,number_of_blocks))
+ ) {
+
+ if (_orientation == 1) {
+ _orientation = -1;
+ } else {
+ _orientation = 1;
+ }
+ }
+
+ if (_orientation == 1 && can_move_down( blocks,
+ number_of_blocks)) {
+ _pos.y += GRAVITY;
+ }
+ if (_orientation == -1 && can_move_up(blocks,
+ number_of_blocks)) {
+ _pos.y -= GRAVITY;
+ }
-
\ No newline at end of file
+
+ 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;
+ }
+
+
+
+
+}
+
+
+