Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Map/Map.cpp

Committer:
evanso
Date:
2020-04-18
Revision:
7:0af4ced868f5
Parent:
6:12e8433382b3
Child:
8:dd1037c5435b

File content as of revision 7:0af4ced868f5:

#include "Map.h"
#define MAP_TERRIAN_Y_POSITION 42

AnalogIn adc(PTD5);
Serial usb(USBTX, USBRX);
Map::Map() {
    
}
 
Map::~Map() {
    
}

void Map::init() {
    position_x_map_ = -96;
    position_y_map_ = MAP_TERRIAN_Y_POSITION;
    get_random_hight_array();
    get_random_length_array();
    //usb.printf("randome seed = %f\n", rand_seed_);
}

void Map::get_random_hight_array(){
    srand(adc.read()*64000);
    for(int i = 0; i < 12; i++){
        rand_hights_[i]= rand() % 8 + 5;
    }   
    //printf loop to check correct random numbers are genreated
    //for (int i = 0; i < 12; i++){ 
       //usb.printf("map hight random array = %d\n", rand_hights_[i]);
    //}
}

void Map::get_random_length_array(){
    srand(adc.read()*64000);
    for(int i = 0; i < 12; i++){
        rand_lengths_[i]= rand() % 16+ 15;
    }   
}

void Map::draw_triangle(N5110 &lcd,int triangle_hight){ 
    // draws triangle by drawing two lines with one line having negative gadient 
    lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + triangle_hight, position_y_map_ - triangle_hight,1); 
    lcd.drawLine(position_x_map_ + triangle_hight, position_y_map_ - triangle_hight,position_x_map_ + 2*triangle_hight,position_y_map_,1);
    position_x_map_ = position_x_map_ + 2*triangle_hight,position_y_map_;
}

void Map::draw_line(N5110 &lcd,int line_length){ 
    // draws triangle by drawing two lines with one line having negative gadient 
    lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + line_length, position_y_map_,1);
    position_x_map_ = position_x_map_ + line_length;
}

void Map::draw_map(N5110 &lcd, int move_map){
    usb.printf("position_x_map_ = %d\n", position_x_map_);
    usb.printf("move map = %d\n", move_map);
    int reset_position_x_map_to_origonal = position_x_map_;
    for(int i = 0; i < 12; i++){
        draw_triangle(lcd,rand_hights_[i]);
        draw_line(lcd,rand_lengths_[i]);   
    } 
    position_x_map_ = reset_position_x_map_to_origonal + move_map;
}

int Map::get_position_x_map(){
    return position_x_map_;
}

void Map::clear_map(N5110 &lcd){
    // Clears spaceship from LCD by drawing a white sprite
    lcd.drawRect(0, 0, 84, 48 , FILL_WHITE);
}