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@15:90b6821bcf64, 2020-04-28 (annotated)
- Committer:
- evanso
- Date:
- Tue Apr 28 19:13:12 2020 +0000
- Revision:
- 15:90b6821bcf64
- Parent:
- 14:7419c680656f
- Child:
- 18:11068b98e261
Added DEBUG compile macros to check calculate_map_map was producing correct output
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| evanso | 6:12e8433382b3 | 1 | #include "Map.h" |
| evanso | 11:ab578a151f67 | 2 | |
| evanso | 9:1ddb8dc93e48 | 3 | #define MAP_TERRAIN_Y_POSITION 42 |
| evanso | 11:ab578a151f67 | 4 | #define MAP_TERRAIN_X_POSITION -84 |
| evanso | 11:ab578a151f67 | 5 | #define MAP_TERRAIN_X_LENGTH 0 |
| evanso | 11:ab578a151f67 | 6 | |
| evanso | 6:12e8433382b3 | 7 | Map::Map() { |
| evanso | 6:12e8433382b3 | 8 | |
| evanso | 6:12e8433382b3 | 9 | } |
| evanso | 6:12e8433382b3 | 10 | |
| evanso | 6:12e8433382b3 | 11 | Map::~Map() { |
| evanso | 6:12e8433382b3 | 12 | |
| evanso | 6:12e8433382b3 | 13 | } |
| evanso | 6:12e8433382b3 | 14 | |
| evanso | 13:12276eed13ac | 15 | void Map::init(Gamepad &pad) { |
| evanso | 11:ab578a151f67 | 16 | map_length_ = MAP_TERRAIN_X_LENGTH; |
| evanso | 11:ab578a151f67 | 17 | position_x_map_ = MAP_TERRAIN_X_POSITION; |
| evanso | 9:1ddb8dc93e48 | 18 | position_y_map_ = MAP_TERRAIN_Y_POSITION; |
| evanso | 11:ab578a151f67 | 19 | |
| evanso | 11:ab578a151f67 | 20 | // Initialises random arrays to make random map |
| evanso | 14:7419c680656f | 21 | fill_random_arrays(pad); |
| evanso | 6:12e8433382b3 | 22 | } |
| evanso | 7:0af4ced868f5 | 23 | |
| evanso | 14:7419c680656f | 24 | void Map::fill_random_arrays(Gamepad &pad){ |
| evanso | 13:12276eed13ac | 25 | srand(pad.read_adc()*64000); |
| evanso | 9:1ddb8dc93e48 | 26 | for(int i = 0; i < 11; i++){ |
| evanso | 9:1ddb8dc93e48 | 27 | rand_heights_[i]= rand() % 8 + 5; |
| evanso | 11:ab578a151f67 | 28 | rand_lengths_[i]= rand() % 16 + 15; |
| evanso | 7:0af4ced868f5 | 29 | } |
| evanso | 15:90b6821bcf64 | 30 | |
| evanso | 15:90b6821bcf64 | 31 | //To to check random numbers generated are actaully random |
| evanso | 15:90b6821bcf64 | 32 | #ifdef RANDOM_NUMBER_DEBUG |
| evanso | 15:90b6821bcf64 | 33 | for (int i = 0; i < 11; i++){ |
| evanso | 15:90b6821bcf64 | 34 | printf("map height random array = %d\n", rand_heights_[i]); |
| evanso | 15:90b6821bcf64 | 35 | } |
| evanso | 15:90b6821bcf64 | 36 | #endif |
| evanso | 7:0af4ced868f5 | 37 | } |
| evanso | 7:0af4ced868f5 | 38 | |
| evanso | 9:1ddb8dc93e48 | 39 | void Map::draw_triangle(N5110 &lcd,int triangle_height){ |
| evanso | 7:0af4ced868f5 | 40 | // draws triangle by drawing two lines with one line having negative gadient |
| evanso | 9:1ddb8dc93e48 | 41 | lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + triangle_height, position_y_map_ - triangle_height,1); |
| evanso | 9:1ddb8dc93e48 | 42 | lcd.drawLine(position_x_map_ + triangle_height, position_y_map_ - triangle_height,position_x_map_ + 2*triangle_height,position_y_map_,1); |
| evanso | 11:ab578a151f67 | 43 | |
| evanso | 11:ab578a151f67 | 44 | // changes the position of the map next draw line starts at the end of the drawn triangle |
| evanso | 9:1ddb8dc93e48 | 45 | position_x_map_ = position_x_map_ + 2*triangle_height,position_y_map_; |
| evanso | 7:0af4ced868f5 | 46 | } |
| evanso | 7:0af4ced868f5 | 47 | |
| evanso | 7:0af4ced868f5 | 48 | void Map::draw_line(N5110 &lcd,int line_length){ |
| evanso | 7:0af4ced868f5 | 49 | lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + line_length, position_y_map_,1); |
| evanso | 11:ab578a151f67 | 50 | |
| evanso | 11:ab578a151f67 | 51 | // changes the position of the map next draw triangle starts at the end of the drawn line |
| evanso | 9:1ddb8dc93e48 | 52 | position_x_map_ += line_length; |
| evanso | 7:0af4ced868f5 | 53 | } |
| evanso | 7:0af4ced868f5 | 54 | |
| evanso | 7:0af4ced868f5 | 55 | void Map::draw_map(N5110 &lcd, int move_map){ |
| evanso | 8:dd1037c5435b | 56 | //usb.printf("position_x_map_ = %d\n", position_x_map_); |
| evanso | 8:dd1037c5435b | 57 | //usb.printf("move map = %d\n", move_map); |
| evanso | 9:1ddb8dc93e48 | 58 | |
| evanso | 9:1ddb8dc93e48 | 59 | reset_position_x_map_to_origonal_ = position_x_map_; |
| evanso | 9:1ddb8dc93e48 | 60 | int map_length_ = 0; |
| evanso | 9:1ddb8dc93e48 | 61 | |
| evanso | 9:1ddb8dc93e48 | 62 | //prints main part of map |
| evanso | 9:1ddb8dc93e48 | 63 | for(int i = 0; i < 11; i++){ |
| evanso | 9:1ddb8dc93e48 | 64 | draw_triangle(lcd,rand_heights_[i]); |
| evanso | 9:1ddb8dc93e48 | 65 | draw_line(lcd,rand_lengths_[i]); |
| evanso | 9:1ddb8dc93e48 | 66 | final_random_element_used_ = i; |
| evanso | 11:ab578a151f67 | 67 | |
| evanso | 11:ab578a151f67 | 68 | // calculates the length of the random map produced |
| evanso | 9:1ddb8dc93e48 | 69 | map_length_ += rand_lengths_[i] + 2*rand_heights_[i]; |
| evanso | 11:ab578a151f67 | 70 | |
| evanso | 11:ab578a151f67 | 71 | // stops random maps lengths being to large only want it about 3 screen widths |
| evanso | 11:ab578a151f67 | 72 | if (map_length_ >252){ |
| evanso | 9:1ddb8dc93e48 | 73 | break; |
| evanso | 9:1ddb8dc93e48 | 74 | } |
| evanso | 9:1ddb8dc93e48 | 75 | } |
| evanso | 9:1ddb8dc93e48 | 76 | |
| evanso | 9:1ddb8dc93e48 | 77 | //checks is map need duplicating on forward and backwards loop and fills gap |
| evanso | 11:ab578a151f67 | 78 | check_duplicates_map_forward(lcd); |
| evanso | 11:ab578a151f67 | 79 | check_duplicates_map_backwards(lcd); |
| evanso | 9:1ddb8dc93e48 | 80 | |
| evanso | 11:ab578a151f67 | 81 | // Resets postion of map and moves it |
| evanso | 9:1ddb8dc93e48 | 82 | position_x_map_ = reset_position_x_map_to_origonal_ + move_map; |
| evanso | 9:1ddb8dc93e48 | 83 | |
| evanso | 11:ab578a151f67 | 84 | // Moves map to different persition so make it look like its looping |
| evanso | 9:1ddb8dc93e48 | 85 | if(position_x_map_+ map_length_ < 0){ |
| evanso | 9:1ddb8dc93e48 | 86 | position_x_map_ = 0; |
| evanso | 11:ab578a151f67 | 87 | } else if(position_x_map_ > 84){ |
| evanso | 9:1ddb8dc93e48 | 88 | position_x_map_ = 84 - map_length_; |
| evanso | 9:1ddb8dc93e48 | 89 | } |
| evanso | 9:1ddb8dc93e48 | 90 | } |
| evanso | 9:1ddb8dc93e48 | 91 | |
| evanso | 13:12276eed13ac | 92 | void Map::check_duplicates_map_forward(N5110 &lcd){ |
| evanso | 9:1ddb8dc93e48 | 93 | // Prints 1st part of map to fill gap wear map isn't present just befor its about to loop round it's self |
| evanso | 9:1ddb8dc93e48 | 94 | if(reset_position_x_map_to_origonal_ + map_length_ <84 ){ |
| evanso | 9:1ddb8dc93e48 | 95 | for(int i = 0; i < 4; i++){ |
| evanso | 9:1ddb8dc93e48 | 96 | draw_triangle(lcd,rand_heights_[i]); |
| evanso | 9:1ddb8dc93e48 | 97 | draw_line(lcd,rand_lengths_[i]); |
| evanso | 9:1ddb8dc93e48 | 98 | } |
| evanso | 9:1ddb8dc93e48 | 99 | } |
| evanso | 9:1ddb8dc93e48 | 100 | |
| evanso | 9:1ddb8dc93e48 | 101 | } |
| evanso | 9:1ddb8dc93e48 | 102 | |
| evanso | 11:ab578a151f67 | 103 | void Map::check_duplicates_map_backwards(N5110 &lcd){ |
| evanso | 9:1ddb8dc93e48 | 104 | // Prints last part of map to fill gap wear map isn't present just befor its about to loop round it's self |
| evanso | 9:1ddb8dc93e48 | 105 | if(reset_position_x_map_to_origonal_ > 0 ){ |
| evanso | 9:1ddb8dc93e48 | 106 | int print_reverse_position = 0; |
| evanso | 11:ab578a151f67 | 107 | |
| evanso | 11:ab578a151f67 | 108 | // prints the last 4 parts of map to fill gap |
| evanso | 9:1ddb8dc93e48 | 109 | for(int i = final_random_element_used_ ; i > final_random_element_used_ - 4; i--){ |
| evanso | 9:1ddb8dc93e48 | 110 | position_x_map_ = reset_position_x_map_to_origonal_ - rand_lengths_[i] - print_reverse_position; |
| evanso | 9:1ddb8dc93e48 | 111 | draw_line(lcd,rand_lengths_[i]); |
| evanso | 9:1ddb8dc93e48 | 112 | print_reverse_position += rand_lengths_[i]; |
| evanso | 9:1ddb8dc93e48 | 113 | position_x_map_ = reset_position_x_map_to_origonal_ - 2*rand_heights_[i] - print_reverse_position; |
| evanso | 9:1ddb8dc93e48 | 114 | draw_triangle(lcd,rand_heights_[i]); |
| evanso | 9:1ddb8dc93e48 | 115 | print_reverse_position += 2*rand_heights_[i]; |
| evanso | 9:1ddb8dc93e48 | 116 | } |
| evanso | 9:1ddb8dc93e48 | 117 | } |
| evanso | 7:0af4ced868f5 | 118 | } |
| evanso | 7:0af4ced868f5 | 119 | |
| evanso | 7:0af4ced868f5 | 120 | int Map::get_position_x_map(){ |
| evanso | 7:0af4ced868f5 | 121 | return position_x_map_; |
| evanso | 7:0af4ced868f5 | 122 | } |
| evanso | 7:0af4ced868f5 | 123 | |
| evanso | 9:1ddb8dc93e48 | 124 | |
| evanso | 9:1ddb8dc93e48 | 125 | |
| evanso | 9:1ddb8dc93e48 | 126 | |
| evanso | 9:1ddb8dc93e48 | 127 |