Player Library
Player.cpp@7:6987a8d682a3, 2017-04-29 (annotated)
- Committer:
- ll14c4p
- Date:
- Sat Apr 29 13:17:22 2017 +0000
- Revision:
- 7:6987a8d682a3
- Parent:
- 6:79a9c37a0b80
- Child:
- 8:4509ae53ca30
Playerx and y for projectile origin;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ll14c4p | 1:933b7aa73bbc | 1 | #include "Player.h" |
ll14c4p | 0:a88279bdf8c0 | 2 | |
ll14c4p | 0:a88279bdf8c0 | 3 | Player::Player() |
ll14c4p | 0:a88279bdf8c0 | 4 | { |
ll14c4p | 0:a88279bdf8c0 | 5 | |
ll14c4p | 0:a88279bdf8c0 | 6 | } |
ll14c4p | 0:a88279bdf8c0 | 7 | |
ll14c4p | 0:a88279bdf8c0 | 8 | Player::~Player() |
ll14c4p | 0:a88279bdf8c0 | 9 | { |
ll14c4p | 0:a88279bdf8c0 | 10 | |
ll14c4p | 0:a88279bdf8c0 | 11 | } |
ll14c4p | 0:a88279bdf8c0 | 12 | |
ll14c4p | 1:933b7aa73bbc | 13 | int sprite[3][3] = { |
ll14c4p | 1:933b7aa73bbc | 14 | {1,0,1}, |
ll14c4p | 4:6d5ea29f83b0 | 15 | {0,1,0}, |
ll14c4p | 4:6d5ea29f83b0 | 16 | {1,0,1}, |
ll14c4p | 1:933b7aa73bbc | 17 | }; |
ll14c4p | 1:933b7aa73bbc | 18 | |
ll14c4p | 6:79a9c37a0b80 | 19 | int m = 0; |
ll14c4p | 6:79a9c37a0b80 | 20 | |
ll14c4p | 6:79a9c37a0b80 | 21 | |
ll14c4p | 7:6987a8d682a3 | 22 | void Player::init() //Delete Int |
ll14c4p | 1:933b7aa73bbc | 23 | { |
ll14c4p | 6:79a9c37a0b80 | 24 | |
ll14c4p | 3:60a09496c839 | 25 | _speed = 0.3; // default speed |
ll14c4p | 1:933b7aa73bbc | 26 | |
ll14c4p | 1:933b7aa73bbc | 27 | } |
ll14c4p | 1:933b7aa73bbc | 28 | |
ll14c4p | 0:a88279bdf8c0 | 29 | |
ll14c4p | 0:a88279bdf8c0 | 30 | void Player::draw(N5110 &lcd) |
ll14c4p | 6:79a9c37a0b80 | 31 | { |
ll14c4p | 6:79a9c37a0b80 | 32 | if(m == 0){ |
ll14c4p | 6:79a9c37a0b80 | 33 | _x = WIDTH/2; // Middle of screen // Change these values to change starting position |
ll14c4p | 6:79a9c37a0b80 | 34 | _y = HEIGHT/2; // Near bottom of screen |
ll14c4p | 6:79a9c37a0b80 | 35 | m = m+1; |
ll14c4p | 6:79a9c37a0b80 | 36 | } |
ll14c4p | 6:79a9c37a0b80 | 37 | printf("SPRITE %d %d \n", _x, _y); |
ll14c4p | 1:933b7aa73bbc | 38 | lcd.drawSprite(_x,_y,3,3,(int *)sprite); |
ll14c4p | 7:6987a8d682a3 | 39 | playerx = _x; |
ll14c4p | 7:6987a8d682a3 | 40 | playery = _y; |
ll14c4p | 1:933b7aa73bbc | 41 | } |
ll14c4p | 1:933b7aa73bbc | 42 | |
ll14c4p | 1:933b7aa73bbc | 43 | |
ll14c4p | 1:933b7aa73bbc | 44 | |
ll14c4p | 4:6d5ea29f83b0 | 45 | Vector2D Player::get_pos() |
ll14c4p | 4:6d5ea29f83b0 | 46 | { |
ll14c4p | 5:cd9699b836c3 | 47 | Vector2D pos = {_x,_y}; |
ll14c4p | 5:cd9699b836c3 | 48 | return pos; |
ll14c4p | 4:6d5ea29f83b0 | 49 | } |
ll14c4p | 4:6d5ea29f83b0 | 50 | |
ll14c4p | 4:6d5ea29f83b0 | 51 | |
ll14c4p | 4:6d5ea29f83b0 | 52 | |
ll14c4p | 4:6d5ea29f83b0 | 53 | |
ll14c4p | 4:6d5ea29f83b0 | 54 | |
ll14c4p | 1:933b7aa73bbc | 55 | |
ll14c4p | 1:933b7aa73bbc | 56 | |
ll14c4p | 1:933b7aa73bbc | 57 | void Player::update(Direction d,float mag) |
ll14c4p | 1:933b7aa73bbc | 58 | { |
ll14c4p | 1:933b7aa73bbc | 59 | _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future |
ll14c4p | 1:933b7aa73bbc | 60 | |
ll14c4p | 1:933b7aa73bbc | 61 | |
ll14c4p | 1:933b7aa73bbc | 62 | // update y value depending on direction of movement |
ll14c4p | 1:933b7aa73bbc | 63 | // North is decrement as origin is at the top-left so decreasing moves up |
ll14c4p | 1:933b7aa73bbc | 64 | if (d == N) { |
ll14c4p | 1:933b7aa73bbc | 65 | _y-=_speed; |
ll14c4p | 1:933b7aa73bbc | 66 | } |
ll14c4p | 1:933b7aa73bbc | 67 | if (d == S) { |
ll14c4p | 1:933b7aa73bbc | 68 | _y+=_speed; |
ll14c4p | 1:933b7aa73bbc | 69 | } |
ll14c4p | 1:933b7aa73bbc | 70 | if (d == E) { |
ll14c4p | 1:933b7aa73bbc | 71 | _x+=_speed; |
ll14c4p | 1:933b7aa73bbc | 72 | } |
ll14c4p | 2:108a1fc56c4c | 73 | if (d == W) { |
ll14c4p | 1:933b7aa73bbc | 74 | _x-=_speed; |
ll14c4p | 1:933b7aa73bbc | 75 | } |
ll14c4p | 2:108a1fc56c4c | 76 | if (d == NE) { |
ll14c4p | 2:108a1fc56c4c | 77 | _y-=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 78 | _x+=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 79 | } |
ll14c4p | 2:108a1fc56c4c | 80 | if (d == NW) { |
ll14c4p | 2:108a1fc56c4c | 81 | _y-=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 82 | _x-=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 83 | } |
ll14c4p | 2:108a1fc56c4c | 84 | if (d == SE) { |
ll14c4p | 2:108a1fc56c4c | 85 | _y+=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 86 | _x+=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 87 | } |
ll14c4p | 2:108a1fc56c4c | 88 | if (d == SW) { |
ll14c4p | 2:108a1fc56c4c | 89 | _y+=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 90 | _x-=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 91 | } |
ll14c4p | 2:108a1fc56c4c | 92 | |
ll14c4p | 1:933b7aa73bbc | 93 | |
ll14c4p | 1:933b7aa73bbc | 94 | // check the y origin to ensure that the paddle doesn't go off screen |
ll14c4p | 3:60a09496c839 | 95 | if (_y <= 0) { |
ll14c4p | 3:60a09496c839 | 96 | _y = 0; |
ll14c4p | 1:933b7aa73bbc | 97 | } |
ll14c4p | 3:60a09496c839 | 98 | if (_x <= 0) { |
ll14c4p | 3:60a09496c839 | 99 | _x = 0; |
ll14c4p | 1:933b7aa73bbc | 100 | } |
ll14c4p | 1:933b7aa73bbc | 101 | if (_x > 81) { |
ll14c4p | 1:933b7aa73bbc | 102 | _x = 81; |
ll14c4p | 1:933b7aa73bbc | 103 | } |
ll14c4p | 1:933b7aa73bbc | 104 | if (_y > 45) { |
ll14c4p | 3:60a09496c839 | 105 | _y = 45; |
ll14c4p | 1:933b7aa73bbc | 106 | } |
ll14c4p | 1:933b7aa73bbc | 107 | |
ll14c4p | 0:a88279bdf8c0 | 108 | } |