James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Map.cpp Source File

Map.cpp

00001 #include "Map.h"
00002 
00003 Map::Map(){
00004 }
00005 
00006 Map::~Map(){
00007 }
00008 
00009 void Map::init(){
00010     _coord.x = 47;      //initialise the map region to the start area
00011     _coord.y = 25;
00012 }
00013 
00014 void Map::read_input(FXOS8700CQ &accelerometer, Ball &ball){
00015     Data values = accelerometer.get_values();
00016     int ball_speed = ball.get_ball_speed();     //get the sensitivity from ball object
00017     _map_change.x = -0.25*(1+0.5*ball_speed)*values.ay;      //axes of accelerometer different to gamepad so use -y for x and vice versa
00018     _map_change.y = -0.25*(1+0.5*ball_speed)*values.ax;
00019 }
00020 
00021 void Map::update(){
00022     _coord.x += _map_change.x;      //map change is the equivalent of velocity in ball.cpp
00023     _coord.y += _map_change.y;
00024     if(_coord.x < 0){ _coord.x = 0;}        //boundary conditions to stop the 
00025     if(_coord.y < 0){ _coord.y = 0;}        //the programme trying to display
00026     if(_coord.x > 506) {_coord.x = 506;}    //undefined region outside the
00027     if(_coord.y > 176) {_coord.y = 176;}    //gamemap array
00028 }
00029 
00030 void Map::draw(N5110 &lcd){
00031     bool pixelstate = false;
00032     for(int y = _coord.y; y < (48+_coord.y); y++){          //row by row
00033         for(int x = _coord.x; x < (84+_coord.x); x++){      //get each pixel from the
00034             if(gamemap[y][x] == 1){ pixelstate = true;}     //gamemap array
00035             else{ pixelstate = false;}                      //and set the pixel according to its value in gamemap array
00036             lcd.setPixel((x-_coord.x), (y-_coord.y), pixelstate);   //easier to use than draw sprite with only part of a very large sprite
00037         }
00038     }
00039 }
00040 
00041 Vector2D Map::get_map_display(){
00042     Vector2D top_left_coord = _coord;   //top left coord used to represent entire screen region
00043     return top_left_coord;
00044 }
00045 
00046 void Map::set_map_display(Vector2D coord){
00047     _coord = coord;     //change private member variable
00048 }
00049 
00050 bool Map::check_wall_collision(Gamepad &gamepad, Ball &ball){
00051     bool collision = false;
00052     Vector2D ball_screen_pos = ball.get_position();
00053     Vector2D c;
00054     c.x = ball_screen_pos.x + _coord.x;     //c represents the absolute position of the ball's centre
00055     c.y = ball_screen_pos.y + _coord.y;     //relative to the game map
00056     Vector2D ball_pixels[37] = {
00057         {c.x,c.y},{c.x+1,c.y},{c.x+2,c.y},{c.x+3,c.y},{c.x-1,c.y},{c.x-2,c.y},          //this is every pixel in ball
00058         {c.x-3,c.y},{c.x,c.y+1},{c.x+1,c.y+1},{c.x+2,c.y+1},{c.x+3,c.y+1},
00059         {c.x-1,c.y+1},{c.x-2,c.y+1},{c.x-3,c.y+1},{c.x,c.y-1},{c.x+1,c.y-1},            //could this be done with
00060         {c.x+2,c.y-1},{c.x+3,c.y-1},{c.x-1,c.y-1},{c.x-2,c.y-1},{c.x-3,c.y-1},          //same algorithm as for 
00061         {c.x,c.y+2},{c.x+1,c.y+2},{c.x+2,c.y+2},{c.x-1,c.y+2},{c.x-2,c.y+2},            //lcd.drawCircle?
00062         {c.x,c.y-2},{c.x+1,c.y-2},{c.x+2,c.y-2},{c.x-1,c.y-2},{c.x-2,c.y-2},
00063         {c.x,c.y+3},{c.x+1,c.y+3},{c.x-1,c.y+3},{c.x,c.y-3},{c.x+1,c.y-3},              //this is fixed for a radius of 3
00064         {c.x-1,c.y-3}   };
00065     for(int i = 0; i < 37; i++){
00066         int y = ball_pixels[i].y;
00067         int x = ball_pixels[i].x;
00068         if(gamemap[y][x] == 1){         //check each pixel in the ball to see if it is on a '1' in game map (representing wall)
00069             collision = true;
00070             break;          //break with true if wall collision
00071         } else { collision = false; }       //keep iterating if not
00072     }
00073     return collision;
00074 }
00075 
00076 int Map::get_coordinate(Vector2D coord){
00077     int position;
00078     int x = coord.x;
00079     int y = coord.y;
00080     if(gamemap[y][x] == 1){ position = 1; } //find the desired coord in the game map
00081     else{ position = 0; }      //return true if its a wall, false if it's pathway
00082     return position;
00083 }