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.
Dependencies: mbed
Aim/Aim.cpp@13:cfdfe60a2327, 2020-05-18 (annotated)
- Committer:
- el18jgb
- Date:
- Mon May 18 15:46:04 2020 +0000
- Revision:
- 13:cfdfe60a2327
- Parent:
- Aim.cpp@9:2836fc3d9ede
- Child:
- 18:c600a6545e81
good bit of a clean up to create and test new functions withing classes with ease;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el18jgb | 4:6f898b000797 | 1 | #include "Aim.h" |
| el18jgb | 4:6f898b000797 | 2 | |
| el18jgb | 6:d560ecc21ae6 | 3 | const int boi [5][5] = |
| el18jgb | 4:6f898b000797 | 4 | { |
| el18jgb | 6:d560ecc21ae6 | 5 | {0,0,1,0,0}, |
| el18jgb | 6:d560ecc21ae6 | 6 | {0,0,1,0,0}, |
| el18jgb | 6:d560ecc21ae6 | 7 | {1,1,0,1,1}, |
| el18jgb | 6:d560ecc21ae6 | 8 | {0,0,1,0,0}, |
| el18jgb | 6:d560ecc21ae6 | 9 | {0,0,1,0,0}, |
| el18jgb | 4:6f898b000797 | 10 | }; |
| el18jgb | 4:6f898b000797 | 11 | |
| el18jgb | 6:d560ecc21ae6 | 12 | const int bang [5][5] = |
| el18jgb | 4:6f898b000797 | 13 | { |
| el18jgb | 6:d560ecc21ae6 | 14 | {1,0,0,0,1}, |
| el18jgb | 6:d560ecc21ae6 | 15 | {0,1,0,1,0}, |
| el18jgb | 6:d560ecc21ae6 | 16 | {0,0,1,0,0}, |
| el18jgb | 6:d560ecc21ae6 | 17 | {0,1,0,1,0}, |
| el18jgb | 6:d560ecc21ae6 | 18 | {1,0,0,0,1}, |
| el18jgb | 4:6f898b000797 | 19 | }; |
| el18jgb | 4:6f898b000797 | 20 | |
| el18jgb | 4:6f898b000797 | 21 | |
| el18jgb | 4:6f898b000797 | 22 | Aim::Aim() |
| el18jgb | 4:6f898b000797 | 23 | { |
| el18jgb | 4:6f898b000797 | 24 | |
| el18jgb | 4:6f898b000797 | 25 | } |
| el18jgb | 4:6f898b000797 | 26 | |
| el18jgb | 4:6f898b000797 | 27 | Aim::~Aim() |
| el18jgb | 4:6f898b000797 | 28 | { |
| el18jgb | 4:6f898b000797 | 29 | |
| el18jgb | 4:6f898b000797 | 30 | } |
| el18jgb | 4:6f898b000797 | 31 | |
| el18jgb | 4:6f898b000797 | 32 | |
| el18jgb | 4:6f898b000797 | 33 | |
| el18jgb | 4:6f898b000797 | 34 | |
| el18jgb | 4:6f898b000797 | 35 | void Aim::init() |
| el18jgb | 4:6f898b000797 | 36 | { |
| el18jgb | 4:6f898b000797 | 37 | _y = 5; // y value on screen is fixed |
| el18jgb | 4:6f898b000797 | 38 | _x = 42; // cant be past edge of screen |
| el18jgb | 6:d560ecc21ae6 | 39 | _height = 5; |
| el18jgb | 6:d560ecc21ae6 | 40 | _width = 5; |
| el18jgb | 6:d560ecc21ae6 | 41 | //_state = 0; |
| el18jgb | 4:6f898b000797 | 42 | |
| el18jgb | 4:6f898b000797 | 43 | } |
| el18jgb | 4:6f898b000797 | 44 | |
| el18jgb | 8:0b9a824c75fe | 45 | void Aim::draw(N5110 &lcd, int state) |
| el18jgb | 4:6f898b000797 | 46 | { |
| el18jgb | 4:6f898b000797 | 47 | // draw basket |
| el18jgb | 6:d560ecc21ae6 | 48 | bool _state = state; |
| el18jgb | 6:d560ecc21ae6 | 49 | |
| el18jgb | 5:c37f4ed2cad3 | 50 | if (_state == 0){ |
| el18jgb | 6:d560ecc21ae6 | 51 | lcd.drawSprite(_x,_y,5,5,(int*)boi); |
| el18jgb | 5:c37f4ed2cad3 | 52 | } |
| el18jgb | 5:c37f4ed2cad3 | 53 | if (_state == 1){ |
| el18jgb | 6:d560ecc21ae6 | 54 | lcd.drawSprite(_x,_y,5,5,(int*)bang); |
| el18jgb | 5:c37f4ed2cad3 | 55 | } |
| el18jgb | 4:6f898b000797 | 56 | } |
| el18jgb | 4:6f898b000797 | 57 | |
| el18jgb | 4:6f898b000797 | 58 | void Aim::update(Gamepad &pad) |
| el18jgb | 4:6f898b000797 | 59 | { |
| el18jgb | 4:6f898b000797 | 60 | Direction d = pad.get_direction(); |
| el18jgb | 4:6f898b000797 | 61 | mag = pad.get_mag(); |
| el18jgb | 4:6f898b000797 | 62 | |
| el18jgb | 9:2836fc3d9ede | 63 | _speed = int(mag*8.0f); // scale is arbitrary, could be changed in future |
| el18jgb | 4:6f898b000797 | 64 | |
| el18jgb | 4:6f898b000797 | 65 | // update x value depending on direction of movement |
| el18jgb | 4:6f898b000797 | 66 | // North is decrement as origin is at the top-left so decreasing moves up |
| el18jgb | 4:6f898b000797 | 67 | if (d == N) { |
| el18jgb | 4:6f898b000797 | 68 | _y-=_speed; |
| el18jgb | 4:6f898b000797 | 69 | } else if (d == NE) { |
| el18jgb | 4:6f898b000797 | 70 | _x+=_speed; |
| el18jgb | 4:6f898b000797 | 71 | _y-=_speed; |
| el18jgb | 4:6f898b000797 | 72 | } else if (d == E) { |
| el18jgb | 4:6f898b000797 | 73 | _x+=_speed; |
| el18jgb | 4:6f898b000797 | 74 | } else if (d == SE) { |
| el18jgb | 4:6f898b000797 | 75 | _x+=_speed; |
| el18jgb | 4:6f898b000797 | 76 | _y+=_speed; |
| el18jgb | 4:6f898b000797 | 77 | } else if (d == S) { |
| el18jgb | 4:6f898b000797 | 78 | _y+=_speed; |
| el18jgb | 4:6f898b000797 | 79 | } else if (d == SW) { |
| el18jgb | 4:6f898b000797 | 80 | _x-=_speed; |
| el18jgb | 4:6f898b000797 | 81 | _y+=_speed; |
| el18jgb | 4:6f898b000797 | 82 | } else if (d == W) { |
| el18jgb | 4:6f898b000797 | 83 | _x-=_speed; |
| el18jgb | 4:6f898b000797 | 84 | } else if (d == NW) { |
| el18jgb | 4:6f898b000797 | 85 | _x-=_speed; |
| el18jgb | 4:6f898b000797 | 86 | _y-=_speed; |
| el18jgb | 4:6f898b000797 | 87 | } |
| el18jgb | 4:6f898b000797 | 88 | |
| el18jgb | 4:6f898b000797 | 89 | // check the x origin to ensure that the paddle doesn't go off screen |
| el18jgb | 4:6f898b000797 | 90 | if (_x < 1) { |
| el18jgb | 4:6f898b000797 | 91 | _x = 1; |
| el18jgb | 4:6f898b000797 | 92 | } |
| el18jgb | 4:6f898b000797 | 93 | if (_x > 84 - _width - 1) { |
| el18jgb | 4:6f898b000797 | 94 | _x = 84 - _width - 1; |
| el18jgb | 4:6f898b000797 | 95 | } |
| el18jgb | 4:6f898b000797 | 96 | // check the y origin to ensure that the sprite doesn't go off screen |
| el18jgb | 4:6f898b000797 | 97 | if (_y < 1) { |
| el18jgb | 4:6f898b000797 | 98 | _y = 1; |
| el18jgb | 4:6f898b000797 | 99 | } |
| el18jgb | 4:6f898b000797 | 100 | if (_y > 48 - _height - 1) { |
| el18jgb | 4:6f898b000797 | 101 | _y = 48 - _height - 1; |
| el18jgb | 4:6f898b000797 | 102 | } |
| el18jgb | 4:6f898b000797 | 103 | } |
| el18jgb | 4:6f898b000797 | 104 | |
| el18jgb | 5:c37f4ed2cad3 | 105 | Vector2D Aim::get_pos() { |
| el18jgb | 6:d560ecc21ae6 | 106 | Vector2D p = {(_x+2),(_y+2)}; |
| el18jgb | 5:c37f4ed2cad3 | 107 | return p; |
| el18jgb | 5:c37f4ed2cad3 | 108 | } |
| el18jgb | 4:6f898b000797 | 109 | |
| el18jgb | 4:6f898b000797 | 110 |