ECE2035 class project
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed wave_player
Fork of missile_command by
player.cpp@2:d39a6a36e0c0, 2014-11-17 (annotated)
- Committer:
- slin77
- Date:
- Mon Nov 17 13:15:34 2014 +0000
- Revision:
- 2:d39a6a36e0c0
- Parent:
- 0:532cb55d6136
- Child:
- 3:fd1f794b7f5d
first commit
Who changed what in which revision?
User | Revision | Line number | New 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 | // Template of player implementation |
arvahsu | 0:532cb55d6136 | 24 | #include "mbed.h" |
arvahsu | 0:532cb55d6136 | 25 | #include "uLCD_4DGL.h" |
arvahsu | 0:532cb55d6136 | 26 | #include "globals.h" |
arvahsu | 0:532cb55d6136 | 27 | #include "player.h" |
arvahsu | 0:532cb55d6136 | 28 | |
arvahsu | 0:532cb55d6136 | 29 | // Example of drawing the player |
slin77 | 2:d39a6a36e0c0 | 30 | |
slin77 | 2:d39a6a36e0c0 | 31 | void player_init() { |
slin77 | 2:d39a6a36e0c0 | 32 | current_player.x = 60; |
slin77 | 2:d39a6a36e0c0 | 33 | current_player.y = 100; |
slin77 | 2:d39a6a36e0c0 | 34 | current_player.am_remain = 5; |
slin77 | 2:d39a6a36e0c0 | 35 | current_player.max_am = 5; |
slin77 | 2:d39a6a36e0c0 | 36 | current_player.status = PLAYER_ACTIVE; |
slin77 | 2:d39a6a36e0c0 | 37 | current_player.score = 0; |
slin77 | 2:d39a6a36e0c0 | 38 | current_player.current_level = 0; |
slin77 | 2:d39a6a36e0c0 | 39 | current_player.life = 5;//inital 5 hp |
slin77 | 2:d39a6a36e0c0 | 40 | current_player.protector_num = 3; |
slin77 | 2:d39a6a36e0c0 | 41 | } |
slin77 | 2:d39a6a36e0c0 | 42 | |
slin77 | 2:d39a6a36e0c0 | 43 | void update_protector() { |
slin77 | 2:d39a6a36e0c0 | 44 | if (current_player.protector.is_active) { |
slin77 | 2:d39a6a36e0c0 | 45 | current_player.protector.timer++; |
slin77 | 2:d39a6a36e0c0 | 46 | uLCD.line(0, 80, 128, 80, WHITE); |
slin77 | 2:d39a6a36e0c0 | 47 | int i,k; |
slin77 | 2:d39a6a36e0c0 | 48 | for (i = 0; i < MAX_NUM_MISSILE; i++) { |
slin77 | 2:d39a6a36e0c0 | 49 | if (missile_record[i].status == MISSILE_ACTIVE && missile_record[i].y < 80) { |
slin77 | 2:d39a6a36e0c0 | 50 | for (k = 0; k < current_player.max_am; k++) { |
slin77 | 2:d39a6a36e0c0 | 51 | if (ex[k].exploded == NO) { |
slin77 | 2:d39a6a36e0c0 | 52 | //find a unused explosion activate it |
slin77 | 2:d39a6a36e0c0 | 53 | ex[k].x = missile_record[i].x; |
slin77 | 2:d39a6a36e0c0 | 54 | ex[k].y = missile_record[i].y; |
slin77 | 2:d39a6a36e0c0 | 55 | ex[k].exploded = YES; |
slin77 | 2:d39a6a36e0c0 | 56 | ex[k].color = WHITE; |
slin77 | 2:d39a6a36e0c0 | 57 | break; |
slin77 | 2:d39a6a36e0c0 | 58 | } |
slin77 | 2:d39a6a36e0c0 | 59 | } |
slin77 | 2:d39a6a36e0c0 | 60 | missile_record[i].status = MISSILE_EXPLODED; |
slin77 | 2:d39a6a36e0c0 | 61 | } |
slin77 | 2:d39a6a36e0c0 | 62 | } |
slin77 | 2:d39a6a36e0c0 | 63 | if (current_player.protector.timer == 10) { |
slin77 | 2:d39a6a36e0c0 | 64 | current_player.protector.is_active = 0; |
slin77 | 2:d39a6a36e0c0 | 65 | uLCD.line(0, 80, 128, 80, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 66 | } |
slin77 | 2:d39a6a36e0c0 | 67 | } |
slin77 | 2:d39a6a36e0c0 | 68 | } |
slin77 | 2:d39a6a36e0c0 | 69 | |
slin77 | 2:d39a6a36e0c0 | 70 | void explosion_init() { |
slin77 | 2:d39a6a36e0c0 | 71 | int i; |
slin77 | 2:d39a6a36e0c0 | 72 | for (i = 0; i < current_player.max_am;i++) { |
slin77 | 2:d39a6a36e0c0 | 73 | ex[i].x = 0; |
slin77 | 2:d39a6a36e0c0 | 74 | ex[i].y = 0; |
slin77 | 2:d39a6a36e0c0 | 75 | ex[i].tick = 0; |
slin77 | 2:d39a6a36e0c0 | 76 | ex[i].radius = 3; |
slin77 | 2:d39a6a36e0c0 | 77 | ex[i].exploded = NO; |
slin77 | 2:d39a6a36e0c0 | 78 | ex[i].color = PLAYER_COLOR; |
slin77 | 2:d39a6a36e0c0 | 79 | } |
slin77 | 2:d39a6a36e0c0 | 80 | } |
slin77 | 2:d39a6a36e0c0 | 81 | |
slin77 | 2:d39a6a36e0c0 | 82 | void update_explosion() { |
slin77 | 2:d39a6a36e0c0 | 83 | int i; |
slin77 | 2:d39a6a36e0c0 | 84 | for (i = 0;i < current_player.max_am;i++) { |
slin77 | 2:d39a6a36e0c0 | 85 | if (ex[i].exploded == YES) { |
slin77 | 2:d39a6a36e0c0 | 86 | ex[i].tick++; |
slin77 | 2:d39a6a36e0c0 | 87 | uLCD.circle(ex[i].x, ex[i].y, ex[i].radius - 2, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 88 | uLCD.circle(ex[i].x, ex[i].y, ex[i].radius, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 89 | draw_landscape(); |
slin77 | 2:d39a6a36e0c0 | 90 | if (ex[i].tick <= 5) { |
slin77 | 2:d39a6a36e0c0 | 91 | ex[i].radius = ex[i].radius + 2; |
slin77 | 2:d39a6a36e0c0 | 92 | } else { |
slin77 | 2:d39a6a36e0c0 | 93 | ex[i].tick = 0; |
slin77 | 2:d39a6a36e0c0 | 94 | ex[i].radius = 0; |
slin77 | 2:d39a6a36e0c0 | 95 | ex[i].exploded = NO; |
slin77 | 2:d39a6a36e0c0 | 96 | } |
slin77 | 2:d39a6a36e0c0 | 97 | } |
slin77 | 2:d39a6a36e0c0 | 98 | } |
slin77 | 2:d39a6a36e0c0 | 99 | } |
slin77 | 2:d39a6a36e0c0 | 100 | |
slin77 | 2:d39a6a36e0c0 | 101 | void draw_explosion() { |
slin77 | 2:d39a6a36e0c0 | 102 | int i; |
slin77 | 2:d39a6a36e0c0 | 103 | for (i = 0;i < current_player.max_am;i++) { |
slin77 | 2:d39a6a36e0c0 | 104 | if (ex[i].exploded == YES) { |
slin77 | 2:d39a6a36e0c0 | 105 | uLCD.circle(ex[i].x, ex[i].y, ex[i].radius - 2, ex[i].color); |
slin77 | 2:d39a6a36e0c0 | 106 | uLCD.circle(ex[i].x, ex[i].y, ex[i].radius, ex[i].color); |
slin77 | 2:d39a6a36e0c0 | 107 | } |
slin77 | 2:d39a6a36e0c0 | 108 | } |
slin77 | 2:d39a6a36e0c0 | 109 | } |
slin77 | 2:d39a6a36e0c0 | 110 | |
slin77 | 2:d39a6a36e0c0 | 111 | void antimissile_init() { |
slin77 | 2:d39a6a36e0c0 | 112 | int i; |
slin77 | 2:d39a6a36e0c0 | 113 | for (i = 0; i < current_player.max_am;i++) { |
slin77 | 2:d39a6a36e0c0 | 114 | am[i].x = current_player.x; |
slin77 | 2:d39a6a36e0c0 | 115 | am[i].y = current_player.y; |
slin77 | 2:d39a6a36e0c0 | 116 | am[i].speed = 5; |
slin77 | 2:d39a6a36e0c0 | 117 | am[i].tick = 0; |
slin77 | 2:d39a6a36e0c0 | 118 | am[i].status = DEACTIVE; |
slin77 | 2:d39a6a36e0c0 | 119 | } |
arvahsu | 0:532cb55d6136 | 120 | } |
slin77 | 2:d39a6a36e0c0 | 121 | //(x,y) is the top left corner |
slin77 | 2:d39a6a36e0c0 | 122 | void player_draw() { |
slin77 | 2:d39a6a36e0c0 | 123 | int x = current_player.x; |
slin77 | 2:d39a6a36e0c0 | 124 | int y = current_player.y; |
slin77 | 2:d39a6a36e0c0 | 125 | uLCD.filled_rectangle(x - 5, y, x- 5 + PLAYER_WIDTH, y+PLAYER_HEIGHT, PLAYER_COLOR); |
slin77 | 2:d39a6a36e0c0 | 126 | uLCD.line(x - 5, y - 3, x - 5, y + PLAYER_HEIGHT + 3, PLAYER_COLOR); |
slin77 | 2:d39a6a36e0c0 | 127 | uLCD.line(x- 5 + PLAYER_WIDTH, y - 3, x- 5 + PLAYER_WIDTH, y + PLAYER_HEIGHT + 3, PLAYER_COLOR); |
slin77 | 2:d39a6a36e0c0 | 128 | uLCD.line(x, y - 3, x, y + PLAYER_HEIGHT + 3, PLAYER_COLOR); |
slin77 | 2:d39a6a36e0c0 | 129 | //uLCD.filled_rectangle(x+PLAYER_DELTA, y-PLAYER_DELTA, x+PLAYER_WIDTH-PLAYER_DELTA, y+PLAYER_HEIGHT, PLAYER_COLOR); |
slin77 | 2:d39a6a36e0c0 | 130 | } |
slin77 | 2:d39a6a36e0c0 | 131 | |
slin77 | 2:d39a6a36e0c0 | 132 | void player_move_left() { |
slin77 | 2:d39a6a36e0c0 | 133 | int x = current_player.x; |
slin77 | 2:d39a6a36e0c0 | 134 | int y = current_player.y; |
slin77 | 2:d39a6a36e0c0 | 135 | uLCD.filled_rectangle(x - 5, y, x- 5 + PLAYER_WIDTH, y+PLAYER_HEIGHT, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 136 | uLCD.line(x - 5, y - 3, x - 5, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 137 | uLCD.line(x- 5 + PLAYER_WIDTH, y - 3, x- 5 + PLAYER_WIDTH, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 138 | uLCD.line(x, y - 3, x, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 139 | if (current_player.x - PLAYER_DELTA > 0) { |
slin77 | 2:d39a6a36e0c0 | 140 | current_player.x = current_player.x - PLAYER_DELTA; |
slin77 | 2:d39a6a36e0c0 | 141 | } |
slin77 | 2:d39a6a36e0c0 | 142 | } |
slin77 | 2:d39a6a36e0c0 | 143 | |
slin77 | 2:d39a6a36e0c0 | 144 | void player_move_right() { |
slin77 | 2:d39a6a36e0c0 | 145 | int x = current_player.x; |
slin77 | 2:d39a6a36e0c0 | 146 | int y = current_player.y; |
slin77 | 2:d39a6a36e0c0 | 147 | uLCD.filled_rectangle(x - 5, y, x- 5 + PLAYER_WIDTH, y+PLAYER_HEIGHT, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 148 | uLCD.line(x - 5, y - 3, x - 5, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 149 | uLCD.line(x- 5 + PLAYER_WIDTH, y - 3, x- 5 + PLAYER_WIDTH, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 150 | uLCD.line(x, y - 3, x, y + PLAYER_HEIGHT + 3, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 151 | if(current_player.x + PLAYER_DELTA < 126) { |
slin77 | 2:d39a6a36e0c0 | 152 | current_player.x = current_player.x + PLAYER_DELTA; |
slin77 | 2:d39a6a36e0c0 | 153 | } |
slin77 | 2:d39a6a36e0c0 | 154 | } |
slin77 | 2:d39a6a36e0c0 | 155 | |
slin77 | 2:d39a6a36e0c0 | 156 | PLAYER get_player_info() { |
slin77 | 2:d39a6a36e0c0 | 157 | return current_player; |
slin77 | 2:d39a6a36e0c0 | 158 | } |
slin77 | 2:d39a6a36e0c0 | 159 | |
slin77 | 2:d39a6a36e0c0 | 160 | //find a available antimissile and set it active |
slin77 | 2:d39a6a36e0c0 | 161 | void shoot() { |
slin77 | 2:d39a6a36e0c0 | 162 | int i; |
slin77 | 2:d39a6a36e0c0 | 163 | if (current_player.am_remain > 0) { |
slin77 | 2:d39a6a36e0c0 | 164 | for(i = 0; i < current_player.max_am; i++) { |
slin77 | 2:d39a6a36e0c0 | 165 | if (am[i].status == DEACTIVE) { |
slin77 | 2:d39a6a36e0c0 | 166 | am[i].status = ACTIVE; |
slin77 | 2:d39a6a36e0c0 | 167 | am[i].x = current_player.x; |
slin77 | 2:d39a6a36e0c0 | 168 | am[i].y = current_player.y; |
slin77 | 2:d39a6a36e0c0 | 169 | am[i].speed = 5; // need to be improved |
slin77 | 2:d39a6a36e0c0 | 170 | am[i].tick = 0; |
slin77 | 2:d39a6a36e0c0 | 171 | current_player.am_remain--; |
slin77 | 2:d39a6a36e0c0 | 172 | break; |
slin77 | 2:d39a6a36e0c0 | 173 | } |
slin77 | 2:d39a6a36e0c0 | 174 | } |
slin77 | 2:d39a6a36e0c0 | 175 | } |
slin77 | 2:d39a6a36e0c0 | 176 | } |
slin77 | 2:d39a6a36e0c0 | 177 | //update all active missiles position |
slin77 | 2:d39a6a36e0c0 | 178 | void update_antimissile_positions() { |
slin77 | 2:d39a6a36e0c0 | 179 | int i, rate; |
slin77 | 2:d39a6a36e0c0 | 180 | for(i = 0; i < current_player.max_am;i++) { |
slin77 | 2:d39a6a36e0c0 | 181 | if(am[i].status == ACTIVE) { |
slin77 | 2:d39a6a36e0c0 | 182 | am[i].tick++; |
slin77 | 2:d39a6a36e0c0 | 183 | rate = 5 / am[i].speed; |
slin77 | 2:d39a6a36e0c0 | 184 | if (am[i].y - 2 <= 0) { |
slin77 | 2:d39a6a36e0c0 | 185 | //when the missile flies out of the screen |
slin77 | 2:d39a6a36e0c0 | 186 | am[i].status = DEACTIVE; |
slin77 | 2:d39a6a36e0c0 | 187 | uLCD.line(am[i].x, am[i].y, am[i].x, am[i].y - 2, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 188 | am[i].y = current_player.y; |
slin77 | 2:d39a6a36e0c0 | 189 | am[i].x = current_player.x; |
slin77 | 2:d39a6a36e0c0 | 190 | current_player.am_remain++; |
slin77 | 2:d39a6a36e0c0 | 191 | |
slin77 | 2:d39a6a36e0c0 | 192 | } else if (am[i].tick % rate == 0) { |
slin77 | 2:d39a6a36e0c0 | 193 | uLCD.line(am[i].x, am[i].y, am[i].x, am[i].y - 2, BACKGROUND_COLOR); |
slin77 | 2:d39a6a36e0c0 | 194 | am[i].y = am[i].y - 2; |
slin77 | 2:d39a6a36e0c0 | 195 | } |
slin77 | 2:d39a6a36e0c0 | 196 | |
slin77 | 2:d39a6a36e0c0 | 197 | } |
slin77 | 2:d39a6a36e0c0 | 198 | } |
slin77 | 2:d39a6a36e0c0 | 199 | } |
slin77 | 2:d39a6a36e0c0 | 200 | //find all active antimissiles and draw their current position |
slin77 | 2:d39a6a36e0c0 | 201 | void draw_antimissiles() { |
slin77 | 2:d39a6a36e0c0 | 202 | int i; |
slin77 | 2:d39a6a36e0c0 | 203 | for(i = 0; i < current_player.max_am;i++) { |
slin77 | 2:d39a6a36e0c0 | 204 | if (am[i].status == ACTIVE ) { |
slin77 | 2:d39a6a36e0c0 | 205 | uLCD.line(am[i].x, am[i].y, am[i].x, am[i].y - 2, PLAYER_COLOR); |
slin77 | 2:d39a6a36e0c0 | 206 | } |
slin77 | 2:d39a6a36e0c0 | 207 | } |
slin77 | 2:d39a6a36e0c0 | 208 | } |
slin77 | 2:d39a6a36e0c0 | 209 | |
slin77 | 2:d39a6a36e0c0 | 210 | |
arvahsu | 0:532cb55d6136 | 211 | |
arvahsu | 0:532cb55d6136 | 212 | // ... You need to implement your own functions for player ... |
arvahsu | 0:532cb55d6136 | 213 | |
arvahsu | 0:532cb55d6136 | 214 |