Game for Project 2

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers map.cpp Source File

map.cpp

00001 /* Gatech ECE2035 2015 SPRING PAC MAN
00002  * Copyright (c) 2015 Gatech ECE2035
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 
00023 #include "map_private.h"
00024 
00025 GRID map[NUM_GRID];
00026 int num_candy = 0;
00027 
00028 void map_init(){
00029     int i;
00030     int start_x = (SIZE_X-GRID_SIZE*NUM_GRID_X)/2;
00031     int start_y = (SIZE_Y-GRID_SIZE*NUM_GRID_Y);
00032     char temp_wall_type[] = DEFAULT_MAP;
00033 
00034     for(i=0;i<NUM_GRID;i++){
00035         map[i].x = start_x + IDX2X(i)*GRID_SIZE;
00036         map[i].y = start_y + IDX2Y(i)*GRID_SIZE;
00037         switch(temp_wall_type[i]){
00038             case MAP_ATTRIBUTE_SIDE_WALK:
00039                 map[i].status = GRID_SIDE_WALK;
00040                 break;
00041             case MAP_ATTRIBUTE_SIDE_WALK_2:
00042                 map[i].status = GRID_SIDE_WALK_2;
00043                 break;
00044             case MAP_ATTRIBUTE_CANDY:
00045                 map[i].status = GRID_CANDY;
00046                 num_candy++;
00047                 break;
00048             case MAP_ATTRIBUTE_BIG_CANDY:
00049                 map[i].status = GRID_BIG_CANDY;
00050                 num_candy++;
00051                 break;
00052             case MAP_ATTRIBUTE_V_LINE:
00053                 map[i].status = GRID_V_LINE;
00054                 break;
00055             case MAP_ATTRIBUTE_ROAD_L:
00056                 map[i].status = GRID_ROAD_L;
00057                 break;
00058             case MAP_ATTRIBUTE_ROAD_R:
00059                 map[i].status = GRID_ROAD_R;
00060                 break;
00061             default:
00062                 map[i].status = GRID_ROAD;
00063                 break;
00064         }
00065     }
00066     map_draw();
00067 }
00068 
00069 void map_draw(){
00070     int i;
00071     for(i=0;i<NUM_GRID;i++){
00072         map_draw_grid(IDX2X(i), IDX2Y(i));
00073     }
00074 }
00075 
00076 void map_draw_grid(unsigned grid_x, unsigned grid_y)
00077 {
00078     unsigned i=XY2IDX(grid_x,grid_y);
00079     if(map[i].status==GRID_SIDE_WALK) {
00080         uLCD.filled_rectangle(map[i].x, map[i].y, map[i].x+GRID_SIZE-1, map[i].y+GRID_SIZE-1, SIDE_WALK_COLOR);
00081     } else if(map[i].status==GRID_SIDE_WALK_2) {
00082         uLCD.filled_rectangle(map[i].x, map[i].y, map[i].x+GRID_SIZE-1, map[i].y+GRID_SIZE-1, SIDE_WALK_COLOR_2);
00083     } else if(map[i].status==GRID_CANDY) {
00084         uLCD.filled_circle(map[i].x+GRID_RADIUS, map[i].y+GRID_RADIUS, CANDY_RADIUS, CANDY_COLOR);
00085     } else if(map[i].status==GRID_BIG_CANDY) {
00086         uLCD.filled_circle(map[i].x+GRID_RADIUS, map[i].y+GRID_RADIUS, BIG_CANDY_RADIUS, CANDY_COLOR);
00087     } else if(map[i].status==GRID_ROAD_L) {
00088         if ((grid_x!=4)and ((grid_y%3)!=2)) {
00089             //draw line on the LEFT
00090             uLCD.filled_rectangle(map[i].x, map[i].y, map[i].x+1, map[i].y+GRID_SIZE-1, LINE_COLOR);
00091         }
00092     } else if(map[i].status==GRID_ROAD_R) {
00093         if ((grid_x!=12)and ((grid_y%3)!=2)) {
00094             //draw line on the RIGHT
00095             uLCD.filled_rectangle(map[i].x+GRID_SIZE-1, map[i].y, map[i].x+GRID_SIZE, map[i].y+GRID_SIZE-1, LINE_COLOR);
00096         }
00097     } else if(map[i].status==GRID_V_LINE) {
00098         if ((((grid_x%2)==1)and ((grid_y%2)==0)) or (((grid_x%2)==0)and ((grid_y%2)==1))) {
00099             uLCD.filled_rectangle(map[i].x, map[i].y, map[i].x+GRID_SIZE-1, map[i].y+GRID_SIZE-1, WHITE);
00100         } 
00101     }
00102 }
00103 
00104 GRID map_get_grid_status(int grid_x, int grid_y){
00105     return map[XY2IDX(grid_x,grid_y)];
00106 }
00107 
00108 bool map_eat_candy(int grid_x, int grid_y){
00109     int idx=XY2IDX(grid_x,grid_y);
00110     
00111     if(map[idx].status==GRID_CANDY || map[idx].status==GRID_BIG_CANDY){
00112         map[idx].status = GRID_ROAD;
00113         num_candy--;
00114         return 1;
00115     }
00116     return 0;
00117 }
00118 
00119 int map_remaining_candy(void){
00120     return num_candy;
00121 }
00122