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 player.cpp Source File

player.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 // Template of player implementation
00024 #include "mbed.h"
00025 #include "uLCD_4DGL.h"
00026 #include "globals.h"
00027 #include "player.h"
00028 #include <math.h>
00029 
00030 // Example of drawing the player
00031 
00032 void player_init() {
00033     current_player.x = 60;
00034     current_player.y = 100;
00035     current_player.am_remain = 5;
00036     current_player.max_am = 5;
00037     current_player.status = PLAYER_ACTIVE;
00038     current_player.score = 0;
00039     current_player.current_level = 0;
00040     current_player.life = 1;//inital 1 hp  
00041     current_player.protector_num = 3; 
00042     current_player.is_diagnoal = 0;
00043     current_player.timer = 0; 
00044 }
00045 
00046 void update_protector() {
00047     if (current_player.protector.is_active) {
00048         current_player.protector.timer++;
00049         uLCD.line(0, 80, 128, 80, WHITE);
00050         int i,k;
00051         for (i = 0; i < MAX_NUM_MISSILE; i++) {
00052             if (missile_record[i].status == MISSILE_ACTIVE && missile_record[i].y < 80) {
00053                 for (k = 0; k < current_player.max_am; k++) {
00054                     if (ex[k].exploded == NO) {
00055                         //find a unused explosion activate it
00056                         ex[k].x = missile_record[i].x;
00057                         ex[k].y = missile_record[i].y;
00058                         ex[k].exploded = YES;
00059                         ex[k].color = WHITE;
00060                         break;   
00061                     }    
00062                 }
00063                 missile_record[i].status = MISSILE_EXPLODED;
00064             }
00065         }
00066         if (current_player.protector.timer == 10) {
00067             current_player.protector.is_active = 0;
00068             uLCD.line(0, 80, 128, 80, BACKGROUND_COLOR);
00069         }
00070     }    
00071 }
00072 
00073 void explosion_init() {
00074        int i;
00075         for (i = 0; i < current_player.max_am;i++) {
00076             ex[i].x = 0;
00077             ex[i].y  = 0;
00078             ex[i].tick = 0;
00079             ex[i].radius = 3;
00080             ex[i].exploded = NO;
00081             ex[i].color = PLAYER_COLOR;   
00082     }     
00083 }
00084 
00085 void update_explosion() {
00086     int i;
00087     for (i = 0;i < current_player.max_am;i++) {
00088         if (ex[i].exploded == YES) {
00089             ex[i].tick++;
00090             uLCD.circle(ex[i].x, ex[i].y, ex[i].radius - 2, BACKGROUND_COLOR);
00091             uLCD.circle(ex[i].x, ex[i].y, ex[i].radius, BACKGROUND_COLOR);
00092             draw_landscape();
00093             if (ex[i].tick <= 5) {
00094                 ex[i].radius = ex[i].radius + 2;
00095             } else {
00096                 ex[i].tick = 0;
00097                 ex[i].radius = 0;
00098                 ex[i].exploded = NO;
00099             }
00100         }
00101     }
00102 }
00103 
00104 void draw_explosion() {
00105     int i;
00106     for (i = 0;i < current_player.max_am;i++) {
00107         if (ex[i].exploded == YES) {
00108             uLCD.circle(ex[i].x, ex[i].y, ex[i].radius - 2, ex[i].color);
00109             uLCD.circle(ex[i].x, ex[i].y, ex[i].radius, ex[i].color);    
00110         }    
00111     } 
00112 }
00113 
00114 void antimissile_init() {
00115     int i;
00116     for (i = 0; i < current_player.max_am;i++) {
00117         am[i].x = current_player.x;
00118         am[i].y = current_player.y;
00119         am[i].speed = 5;
00120         am[i].tick = 0;
00121         am[i].status = DEACTIVE;
00122     }    
00123 }
00124 //(x,y) is the top left corner 
00125 void player_draw() {
00126    int x = current_player.x;
00127    int y = current_player.y;
00128    uLCD.filled_rectangle(x - 5, y, x- 5 + PLAYER_WIDTH, y+PLAYER_HEIGHT, PLAYER_COLOR);
00129    uLCD.line(x - 5, y - 3, x  - 5, y + PLAYER_HEIGHT + 3, PLAYER_COLOR);
00130    uLCD.line(x- 5 + PLAYER_WIDTH, y - 3, x- 5 + PLAYER_WIDTH, y + PLAYER_HEIGHT + 3, PLAYER_COLOR);
00131    uLCD.line(x, y - 3, x, y + PLAYER_HEIGHT + 3, PLAYER_COLOR); 
00132     //uLCD.filled_rectangle(x+PLAYER_DELTA, y-PLAYER_DELTA, x+PLAYER_WIDTH-PLAYER_DELTA, y+PLAYER_HEIGHT, PLAYER_COLOR);  
00133 }
00134 
00135 void player_move_left() {
00136      int x = current_player.x;
00137      int y = current_player.y;
00138     uLCD.filled_rectangle(x - 5, y, x- 5 + PLAYER_WIDTH, y+PLAYER_HEIGHT, BACKGROUND_COLOR);
00139     uLCD.line(x - 5, y - 3, x  - 5, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR);
00140     uLCD.line(x- 5 + PLAYER_WIDTH, y - 3, x- 5 + PLAYER_WIDTH, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR);
00141     uLCD.line(x, y - 3, x, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR); 
00142     if (current_player.x - PLAYER_DELTA > 0 && !current_player.is_diagnoal) {
00143         current_player.x = current_player.x - PLAYER_DELTA;    
00144     } 
00145 }
00146 
00147 void player_move_right() {
00148     int x = current_player.x;
00149     int y = current_player.y;
00150     uLCD.filled_rectangle(x - 5, y, x- 5 + PLAYER_WIDTH, y+PLAYER_HEIGHT, BACKGROUND_COLOR);
00151     uLCD.line(x - 5, y - 3, x  - 5, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR);
00152     uLCD.line(x- 5 + PLAYER_WIDTH, y - 3, x- 5 + PLAYER_WIDTH, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR);
00153     uLCD.line(x, y - 3, x, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR); 
00154     if(current_player.x + PLAYER_DELTA < 126) {
00155         current_player.x = current_player.x + PLAYER_DELTA;
00156     }
00157 }
00158 
00159 PLAYER get_player_info() {
00160     return current_player;
00161 }
00162 
00163 //find a available antimissile and set it active 
00164 void shoot() {
00165     int i;
00166     if (current_player.am_remain > 0) {
00167             for(i = 0; i < current_player.max_am; i++) {
00168                 if (am[i].status == DEACTIVE) {
00169                      am[i].status = ACTIVE;
00170                      am[i].x = current_player.x;
00171                      am[i].y = current_player.y;
00172                      am[i].speed = 5; // need to be improved
00173                      am[i].tick = 0;
00174                      am[i].is_diagnoal = current_player.is_diagnoal;
00175                      current_player.am_remain--;
00176                      break;
00177                  }
00178             }
00179     }    
00180 }
00181 
00182 void big_shoot() {
00183     int i, count = 0;
00184     if (current_player.am_remain > 0) {
00185         for(i = 0; i < current_player.max_am; i++) {
00186             if (am[i].status == DEACTIVE) {
00187              count++;
00188              am[i].status = ACTIVE;
00189              am[i].x = (current_player.x * 2 + count * 32) % 128 ;
00190              am[i].y = 100;
00191              am[i].speed = 5; 
00192              am[i].tick = 0;
00193              am[i].is_diagnoal = current_player.is_diagnoal;
00194              current_player.am_remain--;
00195             }
00196         }    
00197     }    
00198 }
00199 //update all active missiles position 
00200 void update_antimissile_positions() {
00201         int i, rate;
00202         for(i = 0; i < current_player.max_am;i++) {
00203             if(am[i].status == ACTIVE) {
00204                    am[i].tick++;
00205                    rate = 5 / am[i].speed;
00206                    if (am[i].y - 2 <= 0) {
00207                         //when the missile flies out of the screen
00208                         am[i].status = DEACTIVE;
00209                         uLCD.line(am[i].x, am[i].y, am[i].x, 100, BACKGROUND_COLOR);
00210                         am[i].y = current_player.y;
00211                         am[i].x = current_player.x;
00212                         current_player.am_remain++;
00213                            
00214                     } else if (am[i].tick % rate == 0) {
00215                         uLCD.line(am[i].x, am[i].y, am[i].x, 100, BACKGROUND_COLOR);
00216                         am[i].y = am[i].y - 2;
00217                     }
00218                     
00219             }
00220         }
00221 }
00222 //find all active antimissiles and draw their current position
00223 void draw_antimissiles() {
00224     int i;
00225     for(i = 0; i < current_player.max_am;i++) {
00226         if (am[i].status == ACTIVE ) {
00227             uLCD.line(am[i].x, am[i].y, am[i].x, 100, PLAYER_COLOR);
00228         }
00229     }
00230 }
00231 
00232 
00233 
00234 // ... You need to implement your own functions for player ...
00235 
00236