ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18jb

Dependencies:   mbed Gamepad2

Committer:
joebarhouch
Date:
Mon May 18 16:06:27 2020 +0000
Revision:
3:e4e1cbf750b6
Parent:
2:f22cb01c43bc
Child:
4:cf5088ace087
Character movement with game engine

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 3:e4e1cbf750b6 14 void Player::init(int x,int y)
joebarhouch 2:f22cb01c43bc 15 {
joebarhouch 3:e4e1cbf750b6 16 _playerX = x;
joebarhouch 3:e4e1cbf750b6 17 _playerY = y;
joebarhouch 2:f22cb01c43bc 18 }
joebarhouch 2:f22cb01c43bc 19
joebarhouch 3:e4e1cbf750b6 20
joebarhouch 3:e4e1cbf750b6 21
joebarhouch 3:e4e1cbf750b6 22
joebarhouch 3:e4e1cbf750b6 23 bool Player::jump(Gamepad &pad)
joebarhouch 2:f22cb01c43bc 24 {
joebarhouch 2:f22cb01c43bc 25 bool bttnA = pad.A_pressed();
joebarhouch 2:f22cb01c43bc 26 _jmp = false;
joebarhouch 2:f22cb01c43bc 27 // returns true when A is pressed for jump
joebarhouch 2:f22cb01c43bc 28 if(bttnA == true) {
joebarhouch 2:f22cb01c43bc 29 _jmp = true;
joebarhouch 3:e4e1cbf750b6 30 return _jmp;
joebarhouch 2:f22cb01c43bc 31 } else {
joebarhouch 3:e4e1cbf750b6 32 _jmp = false;
joebarhouch 3:e4e1cbf750b6 33 }
joebarhouch 2:f22cb01c43bc 34
joebarhouch 2:f22cb01c43bc 35 return _jmp;
joebarhouch 2:f22cb01c43bc 36 }
joebarhouch 2:f22cb01c43bc 37
joebarhouch 2:f22cb01c43bc 38
joebarhouch 2:f22cb01c43bc 39
joebarhouch 2:f22cb01c43bc 40 void Player::draw(N5110 &lcd)
joebarhouch 2:f22cb01c43bc 41 {
joebarhouch 3:e4e1cbf750b6 42 if(_dir == E) {
joebarhouch 3:e4e1cbf750b6 43 Bitmap bit_player(s_player, 8, 9);
joebarhouch 3:e4e1cbf750b6 44 bit_player.render(lcd, _playerX, _playerY);
joebarhouch 2:f22cb01c43bc 45 }
joebarhouch 2:f22cb01c43bc 46
joebarhouch 3:e4e1cbf750b6 47 if(_dir == W) {
joebarhouch 3:e4e1cbf750b6 48 Bitmap bit_player_inv(s_player_inv, 8, 9);
joebarhouch 3:e4e1cbf750b6 49 bit_player_inv.render(lcd, _playerX, _playerY);
joebarhouch 3:e4e1cbf750b6 50 } else {
joebarhouch 3:e4e1cbf750b6 51 Bitmap bit_player(s_player, 8, 9);
joebarhouch 3:e4e1cbf750b6 52 bit_player.render(lcd, _playerX, _playerY);
joebarhouch 2:f22cb01c43bc 53 }
joebarhouch 2:f22cb01c43bc 54 }
joebarhouch 3:e4e1cbf750b6 55
joebarhouch 3:e4e1cbf750b6 56 void Player::update(Direction d, float mag)
joebarhouch 3:e4e1cbf750b6 57 {
joebarhouch 3:e4e1cbf750b6 58 int _vx = int(mag*4.0f);
joebarhouch 3:e4e1cbf750b6 59 if(d == E) {
joebarhouch 3:e4e1cbf750b6 60 _playerX = _playerX + _vx;
joebarhouch 3:e4e1cbf750b6 61 _dir = E;
joebarhouch 3:e4e1cbf750b6 62 }
joebarhouch 3:e4e1cbf750b6 63
joebarhouch 3:e4e1cbf750b6 64 if(d == W) {
joebarhouch 3:e4e1cbf750b6 65 _playerX = _playerX - _vx;
joebarhouch 3:e4e1cbf750b6 66 _dir = W;
joebarhouch 3:e4e1cbf750b6 67 }
joebarhouch 3:e4e1cbf750b6 68 if (_playerX > WIDTH - 8) {
joebarhouch 3:e4e1cbf750b6 69 _playerX = WIDTH - 8;
joebarhouch 3:e4e1cbf750b6 70 }
joebarhouch 3:e4e1cbf750b6 71 if(_playerX < 0) {
joebarhouch 3:e4e1cbf750b6 72 _playerX = 0;
joebarhouch 3:e4e1cbf750b6 73 }
joebarhouch 3:e4e1cbf750b6 74 }
joebarhouch 3:e4e1cbf750b6 75
joebarhouch 3:e4e1cbf750b6 76 Vector2D Player::get_pos()
joebarhouch 3:e4e1cbf750b6 77 {
joebarhouch 3:e4e1cbf750b6 78 Vector2D pos = {_playerX, _playerY};
joebarhouch 3:e4e1cbf750b6 79 return pos;
joebarhouch 3:e4e1cbf750b6 80 }