ECE2035 class project

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed wave_player

Fork of missile_command by ECE 2035 TA

Committer:
slin77
Date:
Mon Nov 17 13:15:34 2014 +0000
Revision:
2:d39a6a36e0c0
Parent:
1:3da29f1d84b4
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
arvahsu 0:532cb55d6136 1 /* Gatech ECE2035 2014 FALL missile command
arvahsu 0:532cb55d6136 2 * Copyright (c) 2014 Gatech ECE2035
arvahsu 0:532cb55d6136 3 *
arvahsu 0:532cb55d6136 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
arvahsu 0:532cb55d6136 5 * of this software and associated documentation files (the "Software"), to deal
arvahsu 0:532cb55d6136 6 * in the Software without restriction, including without limitation the rights
arvahsu 0:532cb55d6136 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
arvahsu 0:532cb55d6136 8 * copies of the Software, and to permit persons to whom the Software is
arvahsu 0:532cb55d6136 9 * furnished to do so, subject to the following conditions:
arvahsu 0:532cb55d6136 10 *
arvahsu 0:532cb55d6136 11 * The above copyright notice and this permission notice shall be included in
arvahsu 0:532cb55d6136 12 * all copies or substantial portions of the Software.
arvahsu 0:532cb55d6136 13 *
arvahsu 0:532cb55d6136 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
arvahsu 0:532cb55d6136 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
arvahsu 0:532cb55d6136 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
arvahsu 0:532cb55d6136 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
arvahsu 0:532cb55d6136 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
arvahsu 0:532cb55d6136 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
arvahsu 0:532cb55d6136 20 * SOFTWARE.
arvahsu 0:532cb55d6136 21 */
arvahsu 0:532cb55d6136 22
arvahsu 0:532cb55d6136 23 #include "city_landscape_private.h"
slin77 2:d39a6a36e0c0 24 #include "globals.h"
slin77 2:d39a6a36e0c0 25 //
slin77 2:d39a6a36e0c0 26 //CITY city_record[];
slin77 2:d39a6a36e0c0 27 int building_height[NUM_BUILDING];
arvahsu 0:532cb55d6136 28
arvahsu 0:532cb55d6136 29
arvahsu 0:532cb55d6136 30 // See the comments in city_landscape_public.h
arvahsu 0:532cb55d6136 31 void city_landscape_init(int num_city) {
arvahsu 0:532cb55d6136 32 int i;
arvahsu 0:532cb55d6136 33 int city_distance = (SIZE_X-CITY_TO_SCREEN_MARGIN*2)/num_city;
arvahsu 0:532cb55d6136 34
arvahsu 0:532cb55d6136 35 // All interface for user should have error checking
arvahsu 0:532cb55d6136 36 ASSERT_P(num_city<=MAX_NUM_CITY,ERROR_CITY_NUMBER);
arvahsu 0:532cb55d6136 37
arvahsu 0:532cb55d6136 38 //initialize the record of cities
arvahsu 0:532cb55d6136 39 for(i=0;i<MAX_NUM_CITY;i++){
arvahsu 0:532cb55d6136 40 if(i<num_city){
arvahsu 0:532cb55d6136 41 // See the definition of CITY structure in city_landscape.h
arvahsu 0:532cb55d6136 42 city_record[i].y = REVERSE_Y(LANDSCAPE_HEIGHT)-1;
arvahsu 0:532cb55d6136 43 city_record[i].x = i*city_distance + CITY_TO_SCREEN_MARGIN;
arvahsu 0:532cb55d6136 44 city_record[i].width = CITY_WIDTH; // the width is fix number
arvahsu 0:532cb55d6136 45 city_record[i].height = MAX_BUILDING_HEIGHT; // the height is fix number
arvahsu 0:532cb55d6136 46 city_record[i].status = EXIST;
arvahsu 0:532cb55d6136 47 }
arvahsu 0:532cb55d6136 48 else{
arvahsu 1:3da29f1d84b4 49 city_record[i].status = DESTROYED;
arvahsu 0:532cb55d6136 50 }
arvahsu 0:532cb55d6136 51 }
arvahsu 0:532cb55d6136 52
arvahsu 0:532cb55d6136 53 //initialize the height of the buildings
arvahsu 0:532cb55d6136 54 srand(1);
arvahsu 0:532cb55d6136 55 for(i=0;i<NUM_BUILDING;i++){
arvahsu 0:532cb55d6136 56 building_height[i] = (rand() % MAX_BUILDING_HEIGHT*2/3)+MAX_BUILDING_HEIGHT/3;
arvahsu 0:532cb55d6136 57 }
arvahsu 0:532cb55d6136 58
arvahsu 0:532cb55d6136 59 //draw city landscape on the screen
arvahsu 0:532cb55d6136 60 draw_cities();
arvahsu 0:532cb55d6136 61 draw_landscape();
arvahsu 0:532cb55d6136 62
arvahsu 0:532cb55d6136 63 }
arvahsu 0:532cb55d6136 64
arvahsu 0:532cb55d6136 65 CITY city_get_info(int index){
arvahsu 0:532cb55d6136 66 // All interface for user should have error checking
arvahsu 0:532cb55d6136 67 ASSERT_P(index<MAX_NUM_CITY,ERROR_CITY_INDEX_GET_INFO);
arvahsu 0:532cb55d6136 68
arvahsu 0:532cb55d6136 69 return city_record[index];
arvahsu 0:532cb55d6136 70 }
arvahsu 0:532cb55d6136 71
arvahsu 1:3da29f1d84b4 72 void city_destroy(int index){
arvahsu 0:532cb55d6136 73 int j;
arvahsu 0:532cb55d6136 74 int city_x, city_y, building_x, building_y;
arvahsu 0:532cb55d6136 75 int height;
arvahsu 0:532cb55d6136 76
arvahsu 0:532cb55d6136 77 // error checking. the index must smaller than its max.
arvahsu 0:532cb55d6136 78 ASSERT_P(index<MAX_NUM_CITY,ERROR_CITY_INDEX_DESTROY);
arvahsu 0:532cb55d6136 79
arvahsu 0:532cb55d6136 80 // remove the record
arvahsu 1:3da29f1d84b4 81 city_record[index].status = DESTROYED;
arvahsu 0:532cb55d6136 82
arvahsu 0:532cb55d6136 83 // use the background color to cover the city
arvahsu 0:532cb55d6136 84 city_x = city_record[index].x;
arvahsu 0:532cb55d6136 85 city_y = city_record[index].y;
arvahsu 0:532cb55d6136 86 for(j=0;j<NUM_BUILDING;j++){
arvahsu 0:532cb55d6136 87 building_x = city_x+j*BUILDING_WIDTH;
arvahsu 0:532cb55d6136 88 building_y = city_y;
arvahsu 0:532cb55d6136 89 height = building_y-building_height[j]+1;
arvahsu 0:532cb55d6136 90 uLCD.filled_rectangle(building_x, building_y, building_x+BUILDING_WIDTH-1, height, BACKGROUND_COLOR);
arvahsu 0:532cb55d6136 91 }
arvahsu 0:532cb55d6136 92 }
arvahsu 0:532cb55d6136 93
arvahsu 0:532cb55d6136 94 void draw_cities(void){
arvahsu 0:532cb55d6136 95 int i,j;
arvahsu 0:532cb55d6136 96 int city_x, city_y, building_x, building_y;
arvahsu 0:532cb55d6136 97 int height;
arvahsu 0:532cb55d6136 98
arvahsu 0:532cb55d6136 99 for(i=0;i<MAX_NUM_CITY;i++){
arvahsu 0:532cb55d6136 100
arvahsu 0:532cb55d6136 101 // draw each city
arvahsu 0:532cb55d6136 102 if(city_record[i].status==EXIST){
arvahsu 0:532cb55d6136 103 city_x = city_record[i].x;
arvahsu 0:532cb55d6136 104 city_y = city_record[i].y;
arvahsu 0:532cb55d6136 105
arvahsu 0:532cb55d6136 106 // draw each building
arvahsu 0:532cb55d6136 107 for(j=0;j<NUM_BUILDING;j++){
arvahsu 0:532cb55d6136 108 building_x = city_x+j*BUILDING_WIDTH;
arvahsu 0:532cb55d6136 109 building_y = city_y;
arvahsu 0:532cb55d6136 110 height = building_y-building_height[j]+1;
arvahsu 0:532cb55d6136 111 uLCD.filled_rectangle(building_x, building_y, building_x+BUILDING_WIDTH-1, height, BUILDING_COLOR);
arvahsu 0:532cb55d6136 112 }
arvahsu 0:532cb55d6136 113 }
arvahsu 0:532cb55d6136 114 }
arvahsu 0:532cb55d6136 115 }
arvahsu 0:532cb55d6136 116
slin77 2:d39a6a36e0c0 117 int is_any_left() {
slin77 2:d39a6a36e0c0 118 int i, left = 0;
slin77 2:d39a6a36e0c0 119 for (i = 0; i < MAX_NUM_CITY; i++) {
slin77 2:d39a6a36e0c0 120 if (city_record[i].status == EXIST) {
slin77 2:d39a6a36e0c0 121 left = 1;
slin77 2:d39a6a36e0c0 122 break;
slin77 2:d39a6a36e0c0 123 }
slin77 2:d39a6a36e0c0 124 }
slin77 2:d39a6a36e0c0 125 return left;
slin77 2:d39a6a36e0c0 126 }
slin77 2:d39a6a36e0c0 127
arvahsu 0:532cb55d6136 128 void draw_landscape(void){
arvahsu 0:532cb55d6136 129 uLCD.filled_rectangle(0, SIZE_Y-1, SIZE_X-1, REVERSE_Y(LANDSCAPE_HEIGHT), LANDSCAPE_COLOR);
arvahsu 0:532cb55d6136 130 }