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.cpp
00001 #include "Map.h" 00002 00003 // Constant Definitions 00004 #define MAP_TERRAIN_Y_POSITION 41 00005 #define MAP_TERRAIN_X_POSITION -84 00006 #define MAP_TERRAIN_X_LENGTH 0 00007 00008 Map::Map() { 00009 00010 } 00011 00012 Map::~Map() { 00013 00014 } 00015 00016 void Map::init(Gamepad &pad) { 00017 // Assign values to variables 00018 map_length_ = MAP_TERRAIN_X_LENGTH; 00019 position_x_ = MAP_TERRAIN_X_POSITION; 00020 position_y_ = MAP_TERRAIN_Y_POSITION; 00021 00022 // Initialises random arrays to make random map 00023 fill_random_arrays(pad); 00024 00025 } 00026 00027 void Map::fill_random_arrays(Gamepad &pad) { 00028 srand(pad.read_adc()*64000); 00029 for (int i = 0; i < 11; i++) { 00030 rand_heights_[i]= rand() % 8 + 5; 00031 rand_lengths_[i]= rand() % 16 + 15; 00032 } 00033 00034 // To check random numbers generated are actually random 00035 #ifdef RANDOM_NUMBER_DEBUG 00036 for (int i = 0; i < 11; i++) { 00037 printf("map height random array = %d\n", rand_heights_[i]); 00038 } 00039 #endif 00040 } 00041 00042 void Map::draw_triangle(N5110 &lcd,int triangle_height) { 00043 // Draws triangle by drawing two lines with one line having negative gradient 00044 lcd.drawLine(position_x_, position_y_, position_x_ + triangle_height, 00045 position_y_ - triangle_height,1); 00046 lcd.drawLine(position_x_ + triangle_height, position_y_ - triangle_height, 00047 position_x_ + 2*triangle_height,position_y_,1); 00048 00049 // Changes the position of the map next draw line starts at the end of the 00050 // drawn triangle 00051 position_x_ = position_x_ + 2*triangle_height,position_y_; 00052 } 00053 00054 void Map::draw_line(N5110 &lcd,int line_length) { 00055 lcd.drawLine(position_x_, position_y_, position_x_ + line_length, 00056 position_y_,1); 00057 00058 // Changes the position of the map next draw triangle starts at the end of 00059 // the drawn line 00060 position_x_ += line_length; 00061 } 00062 00063 void Map::draw_map(N5110 &lcd, Direction d_) { 00064 // printf("position_x_ map = %d\n", position_x_); 00065 // printf("move map = %d\n", move_map); 00066 00067 reset_position_x_map_to_origonal_ = position_x_; 00068 map_length_ = 0; 00069 00070 // Prints main part of map 00071 for (int i = 0; i < 11; i++) { 00072 draw_triangle(lcd,rand_heights_[i]); 00073 draw_line(lcd,rand_lengths_[i]); 00074 final_random_element_used_ = i; 00075 00076 // Calculates the length of the random map produced 00077 map_length_ += rand_lengths_[i] + 2*rand_heights_[i]; 00078 00079 // Stops random maps lengths being to large only want it about 3 screen 00080 // widths 00081 if (map_length_ >252) { 00082 break; 00083 } 00084 } 00085 00086 // Checks if the map need duplicating in forward and backwards loop and 00087 // fills gap 00088 check_duplicates_map_forward(lcd); 00089 check_duplicates_map_backwards(lcd); 00090 00091 // Resets position of map and moves it 00092 position_x_ = reset_position_x_map_to_origonal_ + calc_map_movement(d_); 00093 00094 // Moves map to different position so make it look like its looping 00095 if (position_x_+ map_length_ < 0) { 00096 position_x_ = 0; 00097 } else if (position_x_ > 84) { 00098 position_x_ = 84 - map_length_; 00099 } 00100 } 00101 00102 void Map::check_duplicates_map_forward(N5110 &lcd) { 00103 // Prints 1st part of map to fill gap wear map isn't present just before its 00104 // about to loop round it's self 00105 if (reset_position_x_map_to_origonal_ + map_length_ <84 ) { 00106 for (int i = 0; i < 4; i++) { 00107 draw_triangle(lcd,rand_heights_[i]); 00108 draw_line(lcd,rand_lengths_[i]); 00109 } 00110 } 00111 00112 } 00113 00114 void Map::check_duplicates_map_backwards(N5110 &lcd) { 00115 // Prints last part of map to fill gap wear map isn't present just before its 00116 // about to loop round it's self 00117 if (reset_position_x_map_to_origonal_ > 0 ) { 00118 int print_reverse_position = 0; 00119 00120 // prints the last 4 parts of the map to fill gap 00121 for (int i = final_random_element_used_ ; 00122 i > final_random_element_used_ - 4; i--) { 00123 position_x_ = reset_position_x_map_to_origonal_ - rand_lengths_[i] - 00124 print_reverse_position; 00125 draw_line(lcd,rand_lengths_[i]); 00126 print_reverse_position += rand_lengths_[i]; 00127 position_x_ = reset_position_x_map_to_origonal_ - 2*rand_heights_[i] 00128 - print_reverse_position; 00129 draw_triangle(lcd,rand_heights_[i]); 00130 print_reverse_position += 2*rand_heights_[i]; 00131 } 00132 } 00133 } 00134 00135 int Map::calc_map_movement(Direction d_) { 00136 // Moves the map in opposite direction to spaceship when it's position is at 00137 // min and max x positions and joystick has direction 00138 if (d_ == W || d_ == NW || d_ == SW) { 00139 return 2; 00140 }else if (d_ == E || d_ == NE || d_ == SE) { 00141 return -2; 00142 }else { 00143 return 0; 00144 } 00145 00146 // Debug and check variables when defined 00147 #ifdef CALCULATE_MAP_MOVEMENT_DEBUG 00148 printf("move map = %d\n", move_map_); 00149 printf("direction = %d\n", d_); 00150 printf("x = %d\n", position_x_spaceship_); 00151 #endif 00152 } 00153 00154 int Map::get_position_x_map() { 00155 return position_x_; 00156 } 00157 00158 int Map::get_length_map() { 00159 return map_length_; 00160 }
Generated on Fri Aug 5 2022 06:55:07 by
1.7.2