ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18jb

Dependencies:   mbed Gamepad2

Committer:
joebarhouch
Date:
Tue May 26 22:15:42 2020 +0000
Revision:
8:d19b30a6cd69
Parent:
7:530ca713d2b2
Child:
9:9830d3a78572
Implemented enemies with AI that walk around with physics

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 3:e4e1cbf750b6 30 bit_player.render(lcd, _playerX, _playerY);
joebarhouch 2:f22cb01c43bc 31 }
joebarhouch 2:f22cb01c43bc 32
joebarhouch 3:e4e1cbf750b6 33 if(_dir == W) {
joebarhouch 3:e4e1cbf750b6 34 Bitmap bit_player_inv(s_player_inv, 8, 9);
joebarhouch 3:e4e1cbf750b6 35 bit_player_inv.render(lcd, _playerX, _playerY);
joebarhouch 3:e4e1cbf750b6 36 } else {
joebarhouch 3:e4e1cbf750b6 37 Bitmap bit_player(s_player, 8, 9);
joebarhouch 3:e4e1cbf750b6 38 bit_player.render(lcd, _playerX, _playerY);
joebarhouch 2:f22cb01c43bc 39 }
joebarhouch 2:f22cb01c43bc 40 }
joebarhouch 3:e4e1cbf750b6 41
joebarhouch 5:928c2eee4109 42 //Update player movement and set screen restrictions
joebarhouch 7:530ca713d2b2 43 void Player::update(Direction d, float mag, int Ypos, bool fall, bool jump)
joebarhouch 3:e4e1cbf750b6 44 {
joebarhouch 8:d19b30a6cd69 45 _vx = int(mag*4.0f);
joebarhouch 7:530ca713d2b2 46 //GO RIGHT
joebarhouch 3:e4e1cbf750b6 47 if(d == E) {
joebarhouch 3:e4e1cbf750b6 48 _playerX = _playerX + _vx;
joebarhouch 3:e4e1cbf750b6 49 _dir = E;
joebarhouch 3:e4e1cbf750b6 50 }
joebarhouch 7:530ca713d2b2 51 //GO LEFT
joebarhouch 3:e4e1cbf750b6 52 if(d == W) {
joebarhouch 3:e4e1cbf750b6 53 _playerX = _playerX - _vx;
joebarhouch 3:e4e1cbf750b6 54 _dir = W;
joebarhouch 3:e4e1cbf750b6 55 }
joebarhouch 8:d19b30a6cd69 56
joebarhouch 8:d19b30a6cd69 57 switch(jump) {
joebarhouch 8:d19b30a6cd69 58 case 0:
joebarhouch 8:d19b30a6cd69 59 //FREE FALL
joebarhouch 8:d19b30a6cd69 60 if( fall == true) {
joebarhouch 8:d19b30a6cd69 61 _playerY = _playerY + 3;
joebarhouch 8:d19b30a6cd69 62 }
joebarhouch 8:d19b30a6cd69 63
joebarhouch 8:d19b30a6cd69 64 //FLOOR COLLISION
joebarhouch 8:d19b30a6cd69 65 if ( fall == false) {
joebarhouch 8:d19b30a6cd69 66 _playerY = Ypos;
joebarhouch 7:530ca713d2b2 67 }
joebarhouch 8:d19b30a6cd69 68 break;
joebarhouch 8:d19b30a6cd69 69
joebarhouch 8:d19b30a6cd69 70 case 1:
joebarhouch 8:d19b30a6cd69 71 //JUMP UNTIL TWICE PLAYER HEIGHT
joebarhouch 8:d19b30a6cd69 72 _playerY -=4;
joebarhouch 8:d19b30a6cd69 73
joebarhouch 8:d19b30a6cd69 74 break;
joebarhouch 8:d19b30a6cd69 75
joebarhouch 8:d19b30a6cd69 76 }
joebarhouch 8:d19b30a6cd69 77
joebarhouch 8:d19b30a6cd69 78 //SCREEN RESTRICTIONS
joebarhouch 4:cf5088ace087 79 if (_playerX > WIDTH - 9) {
joebarhouch 4:cf5088ace087 80 _playerX = WIDTH - 9;
joebarhouch 3:e4e1cbf750b6 81 }
joebarhouch 3:e4e1cbf750b6 82 if(_playerX < 0) {
joebarhouch 3:e4e1cbf750b6 83 _playerX = 0;
joebarhouch 3:e4e1cbf750b6 84 }
joebarhouch 8:d19b30a6cd69 85
joebarhouch 8:d19b30a6cd69 86
joebarhouch 8:d19b30a6cd69 87
joebarhouch 8:d19b30a6cd69 88 //debug
joebarhouch 8:d19b30a6cd69 89 //printf("x: %i, y: %i \n", _playerX, _playerY);
joebarhouch 3:e4e1cbf750b6 90 }
joebarhouch 3:e4e1cbf750b6 91
joebarhouch 5:928c2eee4109 92 //get the position of the player in a 2D vector
joebarhouch 3:e4e1cbf750b6 93 Vector2D Player::get_pos()
joebarhouch 3:e4e1cbf750b6 94 {
joebarhouch 3:e4e1cbf750b6 95 Vector2D pos = {_playerX, _playerY};
joebarhouch 3:e4e1cbf750b6 96 return pos;
joebarhouch 8:d19b30a6cd69 97 }
joebarhouch 8:d19b30a6cd69 98