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
CrossHairs/CrossHairs.cpp@1:3916f272663e, 2019-04-21 (annotated)
- Committer:
- adat80
- Date:
- Sun Apr 21 19:02:21 2019 +0000
- Revision:
- 1:3916f272663e
- Child:
- 5:8bd09c675f28
Program with working spawning of enemies and killing of enemies;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adat80 | 1:3916f272663e | 1 | #include "CrossHairs.h" |
adat80 | 1:3916f272663e | 2 | |
adat80 | 1:3916f272663e | 3 | |
adat80 | 1:3916f272663e | 4 | int crossHairsSprite[7][7] = { |
adat80 | 1:3916f272663e | 5 | { 0,0,1,1,1,0,0 }, |
adat80 | 1:3916f272663e | 6 | { 0,1,0,0,0,1,0 }, |
adat80 | 1:3916f272663e | 7 | { 1,0,0,1,0,0,1 }, |
adat80 | 1:3916f272663e | 8 | { 1,0,1,1,1,0,1 }, |
adat80 | 1:3916f272663e | 9 | { 1,0,0,1,0,0,1 }, |
adat80 | 1:3916f272663e | 10 | { 0,1,0,0,0,1,0 }, |
adat80 | 1:3916f272663e | 11 | { 0,0,1,1,1,0,0 }, |
adat80 | 1:3916f272663e | 12 | }; |
adat80 | 1:3916f272663e | 13 | |
adat80 | 1:3916f272663e | 14 | CrossHairs::CrossHairs() |
adat80 | 1:3916f272663e | 15 | { |
adat80 | 1:3916f272663e | 16 | |
adat80 | 1:3916f272663e | 17 | } |
adat80 | 1:3916f272663e | 18 | |
adat80 | 1:3916f272663e | 19 | CrossHairs::~CrossHairs() |
adat80 | 1:3916f272663e | 20 | { |
adat80 | 1:3916f272663e | 21 | |
adat80 | 1:3916f272663e | 22 | } |
adat80 | 1:3916f272663e | 23 | |
adat80 | 1:3916f272663e | 24 | void CrossHairs::init(int speed) |
adat80 | 1:3916f272663e | 25 | { |
adat80 | 1:3916f272663e | 26 | |
adat80 | 1:3916f272663e | 27 | _x = WIDTH/2 - (7/2); |
adat80 | 1:3916f272663e | 28 | _y = HEIGHT/2 - (7/2); |
adat80 | 1:3916f272663e | 29 | _speed = speed; |
adat80 | 1:3916f272663e | 30 | |
adat80 | 1:3916f272663e | 31 | |
adat80 | 1:3916f272663e | 32 | |
adat80 | 1:3916f272663e | 33 | } |
adat80 | 1:3916f272663e | 34 | |
adat80 | 1:3916f272663e | 35 | void CrossHairs::draw(N5110 &lcd) |
adat80 | 1:3916f272663e | 36 | { |
adat80 | 1:3916f272663e | 37 | // x origin, y origin, rows, cols, sprite |
adat80 | 1:3916f272663e | 38 | lcd.drawSprite(_x,_y,7,7,(int *)crossHairsSprite); |
adat80 | 1:3916f272663e | 39 | |
adat80 | 1:3916f272663e | 40 | } |
adat80 | 1:3916f272663e | 41 | |
adat80 | 1:3916f272663e | 42 | void CrossHairs::update(float angle,float mag, int fps) |
adat80 | 1:3916f272663e | 43 | { |
adat80 | 1:3916f272663e | 44 | //Correcting JoyStick to match screen angles |
adat80 | 1:3916f272663e | 45 | angle = angle - 90.0f; |
adat80 | 1:3916f272663e | 46 | |
adat80 | 1:3916f272663e | 47 | //Conversion To Radians |
adat80 | 1:3916f272663e | 48 | double angleRads = (angle/360.0f)*2.0f*3.14f; |
adat80 | 1:3916f272663e | 49 | |
adat80 | 1:3916f272663e | 50 | |
adat80 | 1:3916f272663e | 51 | |
adat80 | 1:3916f272663e | 52 | _x+= _speed*mag*cos(angleRads); |
adat80 | 1:3916f272663e | 53 | _y += _speed*mag*sin(angleRads); |
adat80 | 1:3916f272663e | 54 | |
adat80 | 1:3916f272663e | 55 | //check if cross hairs are outside screen |
adat80 | 1:3916f272663e | 56 | int halfCrossHairs = 3; |
adat80 | 1:3916f272663e | 57 | if (_x < -halfCrossHairs) { |
adat80 | 1:3916f272663e | 58 | _x = -halfCrossHairs; |
adat80 | 1:3916f272663e | 59 | } else if (_x > 84-halfCrossHairs) { |
adat80 | 1:3916f272663e | 60 | _x = 84-halfCrossHairs; |
adat80 | 1:3916f272663e | 61 | } |
adat80 | 1:3916f272663e | 62 | if (_y < -halfCrossHairs) { |
adat80 | 1:3916f272663e | 63 | _y = -halfCrossHairs; |
adat80 | 1:3916f272663e | 64 | } else if (_y > 48-halfCrossHairs) { |
adat80 | 1:3916f272663e | 65 | _y = 48-halfCrossHairs; |
adat80 | 1:3916f272663e | 66 | } |
adat80 | 1:3916f272663e | 67 | |
adat80 | 1:3916f272663e | 68 | |
adat80 | 1:3916f272663e | 69 | //_x = WIDTH/2 + 10.0*mag*cos(angleRads); |
adat80 | 1:3916f272663e | 70 | //_y = HEIGHT/2 + 10.0*mag*sin(angleRads); |
adat80 | 1:3916f272663e | 71 | } |
adat80 | 1:3916f272663e | 72 | |
adat80 | 1:3916f272663e | 73 | |
adat80 | 1:3916f272663e | 74 | |
adat80 | 1:3916f272663e | 75 | Vector2D CrossHairs::get_pos() |
adat80 | 1:3916f272663e | 76 | { |
adat80 | 1:3916f272663e | 77 | Vector2D p = {_x,_y}; |
adat80 | 1:3916f272663e | 78 | return p; |
adat80 | 1:3916f272663e | 79 | } |
adat80 | 1:3916f272663e | 80 | |
adat80 | 1:3916f272663e | 81 | void CrossHairs::set_pos(Vector2D p) |
adat80 | 1:3916f272663e | 82 | { |
adat80 | 1:3916f272663e | 83 | _x = p.x; |
adat80 | 1:3916f272663e | 84 | _y = p.y; |
adat80 | 1:3916f272663e | 85 | } |