Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Revision:
11:ab578a151f67
Parent:
9:1ddb8dc93e48
Child:
13:12276eed13ac
diff -r aca793aa7abc -r ab578a151f67 Map/Map.cpp
--- a/Map/Map.cpp	Wed Apr 22 20:02:22 2020 +0000
+++ b/Map/Map.cpp	Thu Apr 23 18:17:28 2020 +0000
@@ -1,6 +1,9 @@
 #include "Map.h"
+
 #define MAP_TERRAIN_Y_POSITION 42
-Serial usb(USBTX, USBRX); 
+#define MAP_TERRAIN_X_POSITION -84
+#define MAP_TERRAIN_X_LENGTH 0
+
 Map::Map() {
     
 }
@@ -10,41 +13,39 @@
 }
 
 void Map::init(AnalogIn &adc) {
-    position_x_map_ = -84;
+    map_length_ = MAP_TERRAIN_X_LENGTH;
+    position_x_map_ = MAP_TERRAIN_X_POSITION;
     position_y_map_ = MAP_TERRAIN_Y_POSITION;
-    get_random_height_array(adc);
-    get_random_length_array(adc);
-    //usb.printf("randome seed = %f\n", rand_seed_);
-    map_length_ = 0;
+    
+    // Initialises random arrays to make random map
+    get_random_arrays(adc);
 }
 
-void Map::get_random_height_array(AnalogIn &adc){
+void Map::get_random_arrays(AnalogIn &adc){
     srand(adc.read()*64000);
     for(int i = 0; i < 11; i++){
         rand_heights_[i]= rand() % 8 + 5;
+        rand_lengths_[i]= rand() % 16 + 15;
     }   
     //printf loop to check correct random numbers are generated
-    //for (int i = 0; i < 12; i++){ 
+    //for (int i = 0; i < 11; i++){ 
        //usb.printf("map height random array = %d\n", rand_heights_[i]);
     //}
 }
 
-void Map::get_random_length_array(AnalogIn &adc){
-    srand(adc.read()*64000);
-    for(int i = 0; i < 11; i++){
-        rand_lengths_[i]= rand() % 16+ 15;
-    }   
-}
-
 void Map::draw_triangle(N5110 &lcd,int triangle_height){ 
     // draws triangle by drawing two lines with one line having negative gadient 
     lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + triangle_height, position_y_map_ - triangle_height,1); 
     lcd.drawLine(position_x_map_ + triangle_height, position_y_map_ - triangle_height,position_x_map_ + 2*triangle_height,position_y_map_,1);
+    
+    // changes the position of the map next draw line starts at the end of the drawn triangle
     position_x_map_ = position_x_map_ + 2*triangle_height,position_y_map_;
 }
 
 void Map::draw_line(N5110 &lcd,int line_length){ 
     lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + line_length, position_y_map_,1);
+    
+    // changes the position of the map next draw triangle starts at the end of the drawn line
     position_x_map_ += line_length;
 }
 
@@ -60,28 +61,32 @@
         draw_triangle(lcd,rand_heights_[i]);
         draw_line(lcd,rand_lengths_[i]);
         final_random_element_used_ = i;
+        
+        // calculates the length of the random map produced
         map_length_ += rand_lengths_[i] + 2*rand_heights_[i];
-        if (map_length_ >252){ // stops random maps lengths being to large only want it about 3 screen widths
+        
+        // stops random maps lengths being to large only want it about 3 screen widths
+        if (map_length_ >252){ 
             break;
         }
     }
     
     //checks is map need duplicating on forward and backwards loop and fills gap 
-    check_duplicates_map_forward(lcd, move_map);
-    check_duplicates_map_backwards(lcd, move_map);
+    check_duplicates_map_forward(lcd);
+    check_duplicates_map_backwards(lcd);
     
+    // Resets postion of map and moves it 
     position_x_map_ = reset_position_x_map_to_origonal_ + move_map;
     
-    // Moves map to different persiton so make it look like its looping 
+    // Moves map to different persition so make it look like its looping 
     if(position_x_map_+ map_length_ < 0){
        position_x_map_ = 0;
-    } 
-     if(position_x_map_ > 84){
+    } else if(position_x_map_ > 84){
         position_x_map_ = 84 - map_length_;
     }
 }
 
-void Map::check_duplicates_map_forward(N5110 &lcd, int move_map){
+void Map::check_duplicates_map_forward(N5110 &lcd){
     // Prints 1st part of map to fill gap wear map isn't present just befor its about to loop round it's self
     if(reset_position_x_map_to_origonal_ + map_length_ <84 ){
         for(int i = 0; i < 4; i++){
@@ -92,11 +97,12 @@
     
 }
 
-void Map::check_duplicates_map_backwards(N5110 &lcd, int move_map){
+void Map::check_duplicates_map_backwards(N5110 &lcd){
     // Prints last part of map to fill gap wear map isn't present just befor its about to loop round it's self
     if(reset_position_x_map_to_origonal_ > 0 ){
         int print_reverse_position = 0;
-        // prints last 4 parts of map to fill gap
+        
+        // prints the last 4 parts of map to fill gap
         for(int i = final_random_element_used_ ; i > final_random_element_used_ - 4; i--){
             position_x_map_ = reset_position_x_map_to_origonal_ - rand_lengths_[i] - print_reverse_position;
             draw_line(lcd,rand_lengths_[i]);