Sizhe Lin / Mbed 2 deprecated missile_command_Sizhe_Lin

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed wave_player

Fork of missile_command by ECE 2035 TA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers missile.cpp Source File

missile.cpp

00001 /* Gatech ECE2035 2014 FALL missile command
00002  * Copyright (c) 2014 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 "missile_private.h"
00024 
00025 
00026 int missile_tick=0;
00027 int missile_interval = 10;
00028 int missile_speed = 4;
00029 
00030 // See the comments in missile_public.h
00031 void missile_generator(void){
00032     missile_tick++;
00033     // only fire the missile at certain ticks
00034     if((missile_tick % missile_interval)==0 || missile_tick==0){
00035         missile_create();
00036     }
00037     
00038     // update the missiles and draw them
00039     missile_update_position();
00040 }
00041 
00042 // set missile speed (default speed is 4)
00043 void set_missile_speed(int speed){
00044     ASSERT_P(speed>=1 && speed<=8,ERROR_MISSILE_SPEED);
00045     if(speed>=1 && speed<=8){  
00046         missile_speed = speed;
00047     }
00048 }
00049 
00050 // set missile interval (default interval is 10)
00051 void set_missile_interval(int interval){
00052     ASSERT_P(interval>=1 && interval<=100,ERROR_MISSILE_INTERVAL);
00053     if(interval>=1 && interval<=100){
00054         missile_interval = interval;
00055     }
00056 }
00057 
00058 // See the comments in missile_public.h
00059 MISSILE missile_get_info(int index){
00060     // All interface for user should have error checking
00061     ASSERT_P(index<MAX_NUM_MISSILE,ERROR_MISSILE_INDEX_GET_INFO);
00062     
00063     return missile_record[index];
00064 }
00065 
00066 // See the comments in missile_public.h
00067 void missile_set_exploded(int index){
00068     // All interface for user should have error checking
00069     ASSERT_P(index<MAX_NUM_MISSILE,ERROR_MISSILE_INDEX_UPDATE_STATUS);
00070     
00071     missile_record[index].status = MISSILE_EXPLODED;
00072 }
00073 
00074 /** This function finds an empty slot of missile record, and active it.
00075 */
00076 void missile_create(void){
00077     int i;
00078     for(i=0;i<MAX_NUM_MISSILE;i++){
00079         if(missile_record[i].status == MISSILE_DEACTIVE){
00080             missile_record[i].y = 0;
00081             //each missile has its own tick
00082             missile_record[i].tick = 0;
00083             //set a random source for the missile
00084             missile_record[i].source_x = rand() % SIZE_X;
00085             //set a random target for the missile
00086             missile_record[i].target_x = rand() % SIZE_X;
00087             //the missile starts at its source
00088             missile_record[i].x = missile_record[i].source_x;
00089             missile_record[i].status = MISSILE_ACTIVE;
00090             break;
00091         }
00092     }
00093 }
00094 
00095 /** This function update the position of all missiles and draw them
00096 */
00097 void missile_update_position(void){
00098     int i;
00099     //controls how fast the missile will move
00100     int rate = missile_speed * 25;
00101     //delta_x and delta_y account for the slope of the missile
00102     double delta_x,delta_y;
00103     for(i=0;i<MAX_NUM_MISSILE;i++){
00104         if(missile_record[i].status == MISSILE_ACTIVE){
00105             // update missile position
00106             delta_y = 200/rate;
00107             delta_x = (missile_record[i].target_x - missile_record[i].source_x)/rate;
00108             missile_draw(missile_record[i], BACKGROUND_COLOR);
00109             missile_record[i].y = (int)(delta_y*(missile_record[i].tick%rate));
00110             missile_record[i].x = (int)(missile_record[i].source_x + delta_x*(missile_record[i].tick%rate));
00111             // draw missile
00112             missile_draw(missile_record[i], MISSILE_COLOR);
00113             //update missile's internal tick
00114             missile_record[i].tick++;
00115         }       
00116         else if(missile_record[i].status == MISSILE_EXPLODED){
00117             // clear the missile on the screen
00118             missile_draw(missile_record[i], BACKGROUND_COLOR);
00119             
00120             // we have done with this missile, remove it from record
00121             missile_record[i].status = MISSILE_DEACTIVE;
00122             //resets the missile's internal tick
00123             missile_record[i].tick = 0;
00124         } 
00125         
00126         if (missile_record[i].y > 128) {
00127             //when the missile hit the ground
00128              missile_draw(missile_record[i], BACKGROUND_COLOR);
00129              missile_record[i].tick = 0;
00130              missile_record[i].status = MISSILE_DEACTIVE;
00131         }
00132         
00133     }
00134 }
00135 
00136 /** This function draw a missile.
00137     @param missile The missile to be drawn
00138     @param color The color of the missile
00139 */
00140 void missile_draw(MISSILE missile, int color){
00141     int init_x,init_y,current_x,current_y;
00142 
00143     init_x = missile.source_x;
00144     init_y = 0;
00145     current_x = missile.x;
00146     current_y = missile.y;
00147     uLCD.line(init_x, init_y, current_x, current_y, color);
00148 }
00149 
00150