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.
Level/Level.cpp@13:32d580b3935c, 2019-05-06 (annotated)
- Committer:
- joshdavy
- Date:
- Mon May 06 15:07:28 2019 +0000
- Revision:
- 13:32d580b3935c
- Parent:
- 11:db27d3838514
- Child:
- 16:13044188dcc3
test;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| joshdavy | 6:2ca1516ec1e2 | 1 | #include "Level.h" |
| joshdavy | 11:db27d3838514 | 2 | |
| joshdavy | 11:db27d3838514 | 3 | |
| joshdavy | 11:db27d3838514 | 4 | |
| joshdavy | 11:db27d3838514 | 5 | /** |
| joshdavy | 11:db27d3838514 | 6 | * @brief Constructor (no paramateters) |
| joshdavy | 11:db27d3838514 | 7 | */ |
| joshdavy | 6:2ca1516ec1e2 | 8 | Level::Level() {} |
| joshdavy | 6:2ca1516ec1e2 | 9 | |
| joshdavy | 11:db27d3838514 | 10 | /** |
| joshdavy | 11:db27d3838514 | 11 | * @brief Deconstructor |
| joshdavy | 11:db27d3838514 | 12 | */ |
| joshdavy | 6:2ca1516ec1e2 | 13 | Level::~Level() {} |
| joshdavy | 6:2ca1516ec1e2 | 14 | |
| joshdavy | 11:db27d3838514 | 15 | |
| joshdavy | 11:db27d3838514 | 16 | /** |
| joshdavy | 11:db27d3838514 | 17 | * @brief Initaliser. Loads the level it is passed. See LevelDefinitions for |
| joshdavy | 11:db27d3838514 | 18 | * the format of the structs. |
| joshdavy | 11:db27d3838514 | 19 | * @param Block blocks [] @details The array of blocks. |
| joshdavy | 11:db27d3838514 | 20 | * @param int number_of_blocks @details The number of blocks |
| joshdavy | 11:db27d3838514 | 21 | * @param Vector2D goal @details Location of the goal |
| joshdavy | 11:db27d3838514 | 22 | * @param MovingBlockDefiniton moving_blocks [] @details Array of moving block |
| joshdavy | 11:db27d3838514 | 23 | * definitions. |
| joshdavy | 11:db27d3838514 | 24 | * @param int number_of_moving_blocks @details Number of moving blocks. |
| joshdavy | 11:db27d3838514 | 25 | */ |
| joshdavy | 9:96969b1c6bde | 26 | void Level::init(Block blocks [], |
| joshdavy | 8:21b6d4dbce44 | 27 | int number_of_blocks, |
| joshdavy | 11:db27d3838514 | 28 | Vector2D goal, |
| joshdavy | 11:db27d3838514 | 29 | MovingBlockDefinition moving_blocks[], |
| joshdavy | 11:db27d3838514 | 30 | int number_of_moving_blocks) |
| joshdavy | 6:2ca1516ec1e2 | 31 | { |
| joshdavy | 11:db27d3838514 | 32 | // Store in private members |
| joshdavy | 8:21b6d4dbce44 | 33 | _number_of_blocks = number_of_blocks; |
| joshdavy | 11:db27d3838514 | 34 | _goal = goal; |
| joshdavy | 8:21b6d4dbce44 | 35 | for (int i = 0; i<_number_of_blocks; i++) { |
| joshdavy | 10:58cf89dd878c | 36 | _blocks[i] = blocks[i]; |
| joshdavy | 6:2ca1516ec1e2 | 37 | } |
| joshdavy | 8:21b6d4dbce44 | 38 | |
| joshdavy | 11:db27d3838514 | 39 | // Each MovingBlockDefintion is given to the MovingBlock object array. |
| joshdavy | 11:db27d3838514 | 40 | // MovingBlock Struct is very similar to its defintion but stores the |
| joshdavy | 11:db27d3838514 | 41 | // current position and initial position of the block. |
| joshdavy | 9:96969b1c6bde | 42 | _number_of_moving_blocks = 0; |
| joshdavy | 11:db27d3838514 | 43 | for (int j = 0; j < number_of_moving_blocks; j++) { |
| joshdavy | 11:db27d3838514 | 44 | MovingBlockDefinition x = moving_blocks[j]; |
| joshdavy | 11:db27d3838514 | 45 | declare_moving_block(x.index,x.extending,x.distance); |
| joshdavy | 11:db27d3838514 | 46 | } |
| joshdavy | 9:96969b1c6bde | 47 | } |
| joshdavy | 9:96969b1c6bde | 48 | |
| joshdavy | 11:db27d3838514 | 49 | /** |
| joshdavy | 11:db27d3838514 | 50 | * @brief Returns a pointer to the blocks array |
| joshdavy | 11:db27d3838514 | 51 | * @returns The blocks array |
| joshdavy | 11:db27d3838514 | 52 | */ |
| joshdavy | 9:96969b1c6bde | 53 | Block * Level::get_blocks() |
| joshdavy | 9:96969b1c6bde | 54 | { |
| joshdavy | 9:96969b1c6bde | 55 | return _blocks; |
| joshdavy | 9:96969b1c6bde | 56 | } |
| joshdavy | 9:96969b1c6bde | 57 | |
| joshdavy | 11:db27d3838514 | 58 | /** |
| joshdavy | 11:db27d3838514 | 59 | * @brief Returns number of blocks |
| joshdavy | 11:db27d3838514 | 60 | * @returns number of blocks |
| joshdavy | 11:db27d3838514 | 61 | */ |
| joshdavy | 9:96969b1c6bde | 62 | int Level::get_number_of_blocks() |
| joshdavy | 9:96969b1c6bde | 63 | { |
| joshdavy | 9:96969b1c6bde | 64 | return _number_of_blocks; |
| joshdavy | 9:96969b1c6bde | 65 | } |
| joshdavy | 9:96969b1c6bde | 66 | |
| joshdavy | 11:db27d3838514 | 67 | /** |
| joshdavy | 11:db27d3838514 | 68 | * @brief Returns goal location |
| joshdavy | 11:db27d3838514 | 69 | * @returns Vector2D of goal location |
| joshdavy | 11:db27d3838514 | 70 | */ |
| joshdavy | 9:96969b1c6bde | 71 | Vector2D Level::get_goal() |
| joshdavy | 9:96969b1c6bde | 72 | { |
| joshdavy | 9:96969b1c6bde | 73 | return _goal; |
| joshdavy | 9:96969b1c6bde | 74 | } |
| joshdavy | 9:96969b1c6bde | 75 | |
| joshdavy | 11:db27d3838514 | 76 | |
| joshdavy | 11:db27d3838514 | 77 | /** |
| joshdavy | 11:db27d3838514 | 78 | * @brief Updates the location of the moving blocks. Should be called every |
| joshdavy | 11:db27d3838514 | 79 | * frame. |
| joshdavy | 11:db27d3838514 | 80 | */ |
| joshdavy | 9:96969b1c6bde | 81 | void Level::update_moving_blocks() |
| joshdavy | 9:96969b1c6bde | 82 | { |
| joshdavy | 11:db27d3838514 | 83 | // For every block |
| joshdavy | 9:96969b1c6bde | 84 | for (int i = 0; i < _number_of_moving_blocks; i++) { |
| joshdavy | 11:db27d3838514 | 85 | |
| joshdavy | 11:db27d3838514 | 86 | // If in the extending state |
| joshdavy | 11:db27d3838514 | 87 | if (_moving_blocks[i].extending) { |
| joshdavy | 11:db27d3838514 | 88 | // Move the block to the right |
| joshdavy | 11:db27d3838514 | 89 | _blocks[_moving_blocks[i].index].first.x += 1; |
| joshdavy | 11:db27d3838514 | 90 | _blocks[_moving_blocks[i].index].second.x += 1; |
| joshdavy | 11:db27d3838514 | 91 | // Else its in the retracting state |
| joshdavy | 9:96969b1c6bde | 92 | } else { |
| joshdavy | 11:db27d3838514 | 93 | // So move the block to the left. |
| joshdavy | 11:db27d3838514 | 94 | _blocks[_moving_blocks[i].index].first.x -= 1; |
| joshdavy | 11:db27d3838514 | 95 | _blocks[_moving_blocks[i].index].second.x -= 1; |
| joshdavy | 11:db27d3838514 | 96 | } |
| joshdavy | 11:db27d3838514 | 97 | |
| joshdavy | 11:db27d3838514 | 98 | // If the blocks location is past the distance it should extend |
| joshdavy | 11:db27d3838514 | 99 | if (_blocks[_moving_blocks[i].index].first.x > |
| joshdavy | 11:db27d3838514 | 100 | _moving_blocks[i].initial_pos + _moving_blocks[i].distance) { |
| joshdavy | 11:db27d3838514 | 101 | // then change to the retracting state |
| joshdavy | 11:db27d3838514 | 102 | _moving_blocks[i].extending = false; |
| joshdavy | 11:db27d3838514 | 103 | } |
| joshdavy | 11:db27d3838514 | 104 | // If the block has retracted passed its initial position |
| joshdavy | 11:db27d3838514 | 105 | if (_blocks[_moving_blocks[i].index].first.x < |
| joshdavy | 11:db27d3838514 | 106 | _moving_blocks[i].initial_pos) { |
| joshdavy | 11:db27d3838514 | 107 | // then change to the extending state. |
| joshdavy | 11:db27d3838514 | 108 | _moving_blocks[i].extending = true; |
| joshdavy | 9:96969b1c6bde | 109 | |
| joshdavy | 9:96969b1c6bde | 110 | } |
| joshdavy | 9:96969b1c6bde | 111 | |
| joshdavy | 8:21b6d4dbce44 | 112 | } |
| joshdavy | 8:21b6d4dbce44 | 113 | |
| joshdavy | 8:21b6d4dbce44 | 114 | |
| joshdavy | 8:21b6d4dbce44 | 115 | |
| joshdavy | 8:21b6d4dbce44 | 116 | } |
| joshdavy | 8:21b6d4dbce44 | 117 | |
| joshdavy | 11:db27d3838514 | 118 | /** |
| joshdavy | 11:db27d3838514 | 119 | * @brief Declares a moving block |
| joshdavy | 11:db27d3838514 | 120 | * @param int index @details The index in the block array of the block in which |
| joshdavy | 11:db27d3838514 | 121 | * you want to move. |
| joshdavy | 11:db27d3838514 | 122 | * @param bool extending @details If true the block begins in the extending phase |
| joshdavy | 11:db27d3838514 | 123 | * if false then it begins in the retracting phase. |
| joshdavy | 11:db27d3838514 | 124 | * @param int distance @details The distance to extend. |
| joshdavy | 11:db27d3838514 | 125 | */ |
| joshdavy | 9:96969b1c6bde | 126 | void Level::declare_moving_block(int index,bool extending,int distance) |
| joshdavy | 9:96969b1c6bde | 127 | { |
| joshdavy | 9:96969b1c6bde | 128 | MovingBlock new_moving_block; |
| joshdavy | 9:96969b1c6bde | 129 | new_moving_block.index = index; |
| joshdavy | 9:96969b1c6bde | 130 | new_moving_block.distance = distance; |
| joshdavy | 9:96969b1c6bde | 131 | new_moving_block.extending = extending; |
| joshdavy | 11:db27d3838514 | 132 | // The top left coord is used for tracking the location of block |
| joshdavy | 9:96969b1c6bde | 133 | new_moving_block.initial_pos = _blocks[index].first.x; |
| joshdavy | 9:96969b1c6bde | 134 | |
| joshdavy | 11:db27d3838514 | 135 | // If beginning in the retracting state then move the block to its most |
| joshdavy | 11:db27d3838514 | 136 | // extended state. |
| joshdavy | 9:96969b1c6bde | 137 | if(!extending) { |
| joshdavy | 9:96969b1c6bde | 138 | _blocks[index].first.x += distance; |
| joshdavy | 9:96969b1c6bde | 139 | _blocks[index].second.x += distance; |
| joshdavy | 9:96969b1c6bde | 140 | } |
| joshdavy | 9:96969b1c6bde | 141 | |
| joshdavy | 11:db27d3838514 | 142 | // Add to the array and increment the number of declared moving blocks. |
| joshdavy | 9:96969b1c6bde | 143 | _moving_blocks[_number_of_moving_blocks] = new_moving_block; |
| joshdavy | 9:96969b1c6bde | 144 | _number_of_moving_blocks += 1; |
| joshdavy | 9:96969b1c6bde | 145 | |
| joshdavy | 9:96969b1c6bde | 146 | |
| joshdavy | 9:96969b1c6bde | 147 | } |
| joshdavy | 11:db27d3838514 | 148 | /** |
| joshdavy | 11:db27d3838514 | 149 | * @brief Renders the frame on the lcd. |
| joshdavy | 11:db27d3838514 | 150 | * @param lcd @details The N5110 lcd object. |
| joshdavy | 11:db27d3838514 | 151 | */ |
| joshdavy | 6:2ca1516ec1e2 | 152 | void Level::render(N5110 &lcd) |
| joshdavy | 6:2ca1516ec1e2 | 153 | { |
| joshdavy | 11:db27d3838514 | 154 | // For every block |
| joshdavy | 8:21b6d4dbce44 | 155 | for (int i = 0; i<_number_of_blocks; i++) { |
| joshdavy | 11:db27d3838514 | 156 | // Draw a rectangle |
| joshdavy | 8:21b6d4dbce44 | 157 | lcd.drawRect(_blocks[i].first.x,_blocks[i].first.y, |
| joshdavy | 11:db27d3838514 | 158 | _blocks[i].second.x - _blocks[i].first.x, // X distance |
| joshdavy | 11:db27d3838514 | 159 | _blocks[i].second.y - _blocks[i].first.y, // Y distance |
| joshdavy | 8:21b6d4dbce44 | 160 | FILL_BLACK); |
| joshdavy | 6:2ca1516ec1e2 | 161 | } |
| joshdavy | 11:db27d3838514 | 162 | // Draw the goal |
| joshdavy | 8:21b6d4dbce44 | 163 | lcd.drawSprite(_goal.x,_goal.y,11,6,(int *) goalMap); |
| joshdavy | 8:21b6d4dbce44 | 164 | |
| joshdavy | 6:2ca1516ec1e2 | 165 | } |