ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18jb

Dependencies:   mbed Gamepad2

Committer:
joebarhouch
Date:
Wed May 27 00:49:39 2020 +0000
Revision:
9:9830d3a78572
Parent:
8:d19b30a6cd69
Child:
11:b3024ab59fa5

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joebarhouch 2:f22cb01c43bc 1 #include "Player.h"
joebarhouch 3:e4e1cbf750b6 2 #include "Sprites.h"
joebarhouch 2:f22cb01c43bc 3
joebarhouch 2:f22cb01c43bc 4 Player::Player()
joebarhouch 2:f22cb01c43bc 5 {
joebarhouch 2:f22cb01c43bc 6
joebarhouch 2:f22cb01c43bc 7 }
joebarhouch 2:f22cb01c43bc 8
joebarhouch 2:f22cb01c43bc 9 Player::~Player()
joebarhouch 2:f22cb01c43bc 10 {
joebarhouch 2:f22cb01c43bc 11
joebarhouch 2:f22cb01c43bc 12 }
joebarhouch 2:f22cb01c43bc 13
joebarhouch 5:928c2eee4109 14 //initialize
joebarhouch 3:e4e1cbf750b6 15 void Player::init(int x,int y)
joebarhouch 2:f22cb01c43bc 16 {
joebarhouch 3:e4e1cbf750b6 17 _playerX = x;
joebarhouch 3:e4e1cbf750b6 18 _playerY = y;
joebarhouch 2:f22cb01c43bc 19 }
joebarhouch 2:f22cb01c43bc 20
joebarhouch 3:e4e1cbf750b6 21
joebarhouch 3:e4e1cbf750b6 22
joebarhouch 2:f22cb01c43bc 23
joebarhouch 2:f22cb01c43bc 24
joebarhouch 5:928c2eee4109 25 //Draw the sprites depending on the orientation
joebarhouch 2:f22cb01c43bc 26 void Player::draw(N5110 &lcd)
joebarhouch 2:f22cb01c43bc 27 {
joebarhouch 3:e4e1cbf750b6 28 if(_dir == E) {
joebarhouch 3:e4e1cbf750b6 29 Bitmap bit_player(s_player, 8, 9);
joebarhouch 9:9830d3a78572 30 Bitmap bit_player2(s_player2, 8, 9);
joebarhouch 3:e4e1cbf750b6 31 bit_player.render(lcd, _playerX, _playerY);
joebarhouch 2:f22cb01c43bc 32 }
joebarhouch 2:f22cb01c43bc 33
joebarhouch 3:e4e1cbf750b6 34 if(_dir == W) {
joebarhouch 3:e4e1cbf750b6 35 Bitmap bit_player_inv(s_player_inv, 8, 9);
joebarhouch 3:e4e1cbf750b6 36 bit_player_inv.render(lcd, _playerX, _playerY);
joebarhouch 3:e4e1cbf750b6 37 } else {
joebarhouch 3:e4e1cbf750b6 38 Bitmap bit_player(s_player, 8, 9);
joebarhouch 3:e4e1cbf750b6 39 bit_player.render(lcd, _playerX, _playerY);
joebarhouch 2:f22cb01c43bc 40 }
joebarhouch 2:f22cb01c43bc 41 }
joebarhouch 3:e4e1cbf750b6 42
joebarhouch 5:928c2eee4109 43 //Update player movement and set screen restrictions
joebarhouch 7:530ca713d2b2 44 void Player::update(Direction d, float mag, int Ypos, bool fall, bool jump)
joebarhouch 3:e4e1cbf750b6 45 {
joebarhouch 8:d19b30a6cd69 46 _vx = int(mag*4.0f);
joebarhouch 7:530ca713d2b2 47 //GO RIGHT
joebarhouch 3:e4e1cbf750b6 48 if(d == E) {
joebarhouch 3:e4e1cbf750b6 49 _playerX = _playerX + _vx;
joebarhouch 3:e4e1cbf750b6 50 _dir = E;
joebarhouch 3:e4e1cbf750b6 51 }
joebarhouch 7:530ca713d2b2 52 //GO LEFT
joebarhouch 3:e4e1cbf750b6 53 if(d == W) {
joebarhouch 3:e4e1cbf750b6 54 _playerX = _playerX - _vx;
joebarhouch 3:e4e1cbf750b6 55 _dir = W;
joebarhouch 3:e4e1cbf750b6 56 }
joebarhouch 8:d19b30a6cd69 57
joebarhouch 8:d19b30a6cd69 58 switch(jump) {
joebarhouch 8:d19b30a6cd69 59 case 0:
joebarhouch 8:d19b30a6cd69 60 //FREE FALL
joebarhouch 8:d19b30a6cd69 61 if( fall == true) {
joebarhouch 8:d19b30a6cd69 62 _playerY = _playerY + 3;
joebarhouch 8:d19b30a6cd69 63 }
joebarhouch 8:d19b30a6cd69 64
joebarhouch 8:d19b30a6cd69 65 //FLOOR COLLISION
joebarhouch 8:d19b30a6cd69 66 if ( fall == false) {
joebarhouch 8:d19b30a6cd69 67 _playerY = Ypos;
joebarhouch 7:530ca713d2b2 68 }
joebarhouch 8:d19b30a6cd69 69 break;
joebarhouch 8:d19b30a6cd69 70
joebarhouch 8:d19b30a6cd69 71 case 1:
joebarhouch 8:d19b30a6cd69 72 //JUMP UNTIL TWICE PLAYER HEIGHT
joebarhouch 8:d19b30a6cd69 73 _playerY -=4;
joebarhouch 8:d19b30a6cd69 74
joebarhouch 8:d19b30a6cd69 75 break;
joebarhouch 8:d19b30a6cd69 76
joebarhouch 8:d19b30a6cd69 77 }
joebarhouch 8:d19b30a6cd69 78
joebarhouch 8:d19b30a6cd69 79 //SCREEN RESTRICTIONS
joebarhouch 4:cf5088ace087 80 if (_playerX > WIDTH - 9) {
joebarhouch 4:cf5088ace087 81 _playerX = WIDTH - 9;
joebarhouch 3:e4e1cbf750b6 82 }
joebarhouch 3:e4e1cbf750b6 83 if(_playerX < 0) {
joebarhouch 3:e4e1cbf750b6 84 _playerX = 0;
joebarhouch 3:e4e1cbf750b6 85 }
joebarhouch 8:d19b30a6cd69 86
joebarhouch 8:d19b30a6cd69 87
joebarhouch 8:d19b30a6cd69 88
joebarhouch 8:d19b30a6cd69 89 //debug
joebarhouch 8:d19b30a6cd69 90 //printf("x: %i, y: %i \n", _playerX, _playerY);
joebarhouch 3:e4e1cbf750b6 91 }
joebarhouch 3:e4e1cbf750b6 92
joebarhouch 5:928c2eee4109 93 //get the position of the player in a 2D vector
joebarhouch 3:e4e1cbf750b6 94 Vector2D Player::get_pos()
joebarhouch 3:e4e1cbf750b6 95 {
joebarhouch 3:e4e1cbf750b6 96 Vector2D pos = {_playerX, _playerY};
joebarhouch 3:e4e1cbf750b6 97 return pos;
joebarhouch 8:d19b30a6cd69 98 }
joebarhouch 8:d19b30a6cd69 99