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.
Map/Map.cpp@6:39bda45efeed, 2019-04-08 (annotated)
- Committer:
- kocemax
- Date:
- Mon Apr 08 09:14:33 2019 +0000
- Revision:
- 6:39bda45efeed
- Parent:
- 5:12c179da4788
- Child:
- 7:cd3cafda3dd4
Finally had more time during the holiday, reedited lots of the code to make it simpler, more readable, more robust, and better structured.Also added collision detection with the bricks, but no deflection yet.(Took the algorithm from an online forum).
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| kocemax | 5:12c179da4788 | 1 | #include "Map.h" | 
| kocemax | 6:39bda45efeed | 2 | #include "Ball.h" | 
| kocemax | 5:12c179da4788 | 3 | |
| kocemax | 5:12c179da4788 | 4 | |
| kocemax | 5:12c179da4788 | 5 | // Constructor | 
| kocemax | 5:12c179da4788 | 6 | Map::Map() | 
| kocemax | 5:12c179da4788 | 7 | { | 
| kocemax | 6:39bda45efeed | 8 | |
| kocemax | 5:12c179da4788 | 9 | } | 
| kocemax | 5:12c179da4788 | 10 | |
| kocemax | 5:12c179da4788 | 11 | // Destructor | 
| kocemax | 5:12c179da4788 | 12 | Map::~Map() | 
| kocemax | 5:12c179da4788 | 13 | { | 
| kocemax | 5:12c179da4788 | 14 | |
| kocemax | 5:12c179da4788 | 15 | } | 
| kocemax | 5:12c179da4788 | 16 | |
| kocemax | 6:39bda45efeed | 17 | void Map::initBricks() | 
| kocemax | 5:12c179da4788 | 18 | { | 
| kocemax | 6:39bda45efeed | 19 | int w = 5; | 
| kocemax | 6:39bda45efeed | 20 | int h = 1; | 
| kocemax | 6:39bda45efeed | 21 | int gap = 1; | 
| kocemax | 6:39bda45efeed | 22 | |
| kocemax | 6:39bda45efeed | 23 | int y = 0; | 
| kocemax | 6:39bda45efeed | 24 | |
| kocemax | 6:39bda45efeed | 25 | for (int j = 0; j < 15; ++j) { | 
| kocemax | 6:39bda45efeed | 26 | int x = 0; | 
| kocemax | 6:39bda45efeed | 27 | for (int i = 0; i < 14; ++i) { | 
| kocemax | 6:39bda45efeed | 28 | Brick b; | 
| kocemax | 6:39bda45efeed | 29 | b.x = x; | 
| kocemax | 6:39bda45efeed | 30 | b.y = y; | 
| kocemax | 6:39bda45efeed | 31 | b.w = w; | 
| kocemax | 6:39bda45efeed | 32 | b.h = h; | 
| kocemax | 6:39bda45efeed | 33 | bricks.push_back(b); | 
| kocemax | 6:39bda45efeed | 34 | |
| kocemax | 6:39bda45efeed | 35 | x += w + gap; | 
| kocemax | 6:39bda45efeed | 36 | } | 
| kocemax | 6:39bda45efeed | 37 | y += h + gap; | 
| kocemax | 6:39bda45efeed | 38 | } | 
| kocemax | 6:39bda45efeed | 39 | } | 
| kocemax | 6:39bda45efeed | 40 | |
| kocemax | 6:39bda45efeed | 41 | void Map::drawMap(N5110 &lcd) | 
| kocemax | 6:39bda45efeed | 42 | { | 
| kocemax | 6:39bda45efeed | 43 | vector<Brick>::size_type end = bricks.size(); | 
| kocemax | 6:39bda45efeed | 44 | for (int i = 0; i < end; i++) | 
| kocemax | 5:12c179da4788 | 45 | { | 
| kocemax | 6:39bda45efeed | 46 | const Brick& b = bricks[i]; | 
| kocemax | 6:39bda45efeed | 47 | lcd.drawRect(b.x,b.y,b.w,b.h,FILL_BLACK); | 
| kocemax | 6:39bda45efeed | 48 | } | 
| kocemax | 6:39bda45efeed | 49 | } | 
| kocemax | 6:39bda45efeed | 50 | |
| kocemax | 6:39bda45efeed | 51 | //Taken from a forum, took me a bit to understand/main collision detection | 
| kocemax | 6:39bda45efeed | 52 | bool isIntersect( | 
| kocemax | 6:39bda45efeed | 53 | int Ax, int Ay, int Aw, int Ah, | 
| kocemax | 6:39bda45efeed | 54 | float Bx, float By, float Bw, float Bh) | 
| kocemax | 6:39bda45efeed | 55 | { | 
| kocemax | 6:39bda45efeed | 56 | return | 
| kocemax | 6:39bda45efeed | 57 | Bx + Bw > Ax && | 
| kocemax | 6:39bda45efeed | 58 | By + Bh > Ay && | 
| kocemax | 6:39bda45efeed | 59 | Ax + Aw > Bx && | 
| kocemax | 6:39bda45efeed | 60 | Ay + Ah > By; | 
| kocemax | 6:39bda45efeed | 61 | } | 
| kocemax | 6:39bda45efeed | 62 | |
| kocemax | 6:39bda45efeed | 63 | void Map::checkCollision(GameObject &obj) | 
| kocemax | 6:39bda45efeed | 64 | { | 
| kocemax | 6:39bda45efeed | 65 | vector<Brick>::size_type end = bricks.size(); | 
| kocemax | 6:39bda45efeed | 66 | for (int i = 0; i < end; i++) | 
| kocemax | 6:39bda45efeed | 67 | { | 
| kocemax | 6:39bda45efeed | 68 | const Brick& b = bricks[i]; | 
| kocemax | 6:39bda45efeed | 69 | if (isIntersect(b.x, b.y, b.w, b.h, | 
| kocemax | 6:39bda45efeed | 70 | obj.getballPos().x, obj.getballPos().y, obj.getW(), obj.getH())) { | 
| kocemax | 6:39bda45efeed | 71 | bricks.erase(bricks.begin() + i); | 
| kocemax | 6:39bda45efeed | 72 | break; | 
| kocemax | 5:12c179da4788 | 73 | } | 
| kocemax | 5:12c179da4788 | 74 | } | 
| kocemax | 5:12c179da4788 | 75 | } | 
| kocemax | 5:12c179da4788 | 76 | |
| kocemax | 6:39bda45efeed | 77 | void Map::destroyMap(N5110 &lcd, Gamepad &pad, Ball &ball) | 
| kocemax | 5:12c179da4788 | 78 | { | 
| kocemax | 6:39bda45efeed | 79 | /* | 
| kocemax | 6:39bda45efeed | 80 | Vector2D posBall = ball.get_ballPos(pad); | 
| kocemax | 5:12c179da4788 | 81 | vector<Centerpoints>::size_type end = point.size(); | 
| kocemax | 5:12c179da4788 | 82 | for (int i = 0; i < end; i++) | 
| kocemax | 5:12c179da4788 | 83 | { | 
| kocemax | 5:12c179da4788 | 84 | for (int j = 0; j < 5; j++) | 
| kocemax | 5:12c179da4788 | 85 | { | 
| kocemax | 6:39bda45efeed | 86 | if (posBall.x == point[i].x+j) | 
| kocemax | 6:39bda45efeed | 87 | { | 
| kocemax | 6:39bda45efeed | 88 | point.at(i).x = 70; | 
| kocemax | 6:39bda45efeed | 89 | point.at(i).y = 40; | 
| kocemax | 6:39bda45efeed | 90 | printf("collided\n"); | 
| kocemax | 6:39bda45efeed | 91 | } | 
| kocemax | 5:12c179da4788 | 92 | } | 
| kocemax | 5:12c179da4788 | 93 | } | 
| kocemax | 6:39bda45efeed | 94 | */ | 
| kocemax | 5:12c179da4788 | 95 | } |