Missile Control Game with uLCD
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed wave_player
Fork of missile_command by
missile/missile.cpp@2:eba4ed0263a4, 2015-10-20 (annotated)
- Committer:
- ajindia6
- Date:
- Tue Oct 20 19:13:03 2015 +0000
- Revision:
- 2:eba4ed0263a4
- Parent:
- 0:532cb55d6136
Mbed Mission Control Game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
arvahsu | 0:532cb55d6136 | 1 | #include "missile_private.h" |
arvahsu | 0:532cb55d6136 | 2 | |
arvahsu | 0:532cb55d6136 | 3 | MISSILE missile_record[MAX_NUM_MISSILE]; |
arvahsu | 0:532cb55d6136 | 4 | int missile_tick=0; |
arvahsu | 0:532cb55d6136 | 5 | int missile_interval = 10; |
arvahsu | 0:532cb55d6136 | 6 | int missile_speed = 4; |
arvahsu | 0:532cb55d6136 | 7 | |
arvahsu | 0:532cb55d6136 | 8 | // See the comments in missile_public.h |
arvahsu | 0:532cb55d6136 | 9 | void missile_generator(void){ |
arvahsu | 0:532cb55d6136 | 10 | missile_tick++; |
arvahsu | 0:532cb55d6136 | 11 | // only fire the missile at certain ticks |
arvahsu | 0:532cb55d6136 | 12 | if((missile_tick % missile_interval)==0 || missile_tick==0){ |
arvahsu | 0:532cb55d6136 | 13 | missile_create(); |
arvahsu | 0:532cb55d6136 | 14 | } |
arvahsu | 0:532cb55d6136 | 15 | |
arvahsu | 0:532cb55d6136 | 16 | // update the missiles and draw them |
arvahsu | 0:532cb55d6136 | 17 | missile_update_position(); |
arvahsu | 0:532cb55d6136 | 18 | } |
arvahsu | 0:532cb55d6136 | 19 | |
arvahsu | 0:532cb55d6136 | 20 | // set missile speed (default speed is 4) |
arvahsu | 0:532cb55d6136 | 21 | void set_missile_speed(int speed){ |
arvahsu | 0:532cb55d6136 | 22 | ASSERT_P(speed>=1 && speed<=8,ERROR_MISSILE_SPEED); |
arvahsu | 0:532cb55d6136 | 23 | if(speed>=1 && speed<=8){ |
arvahsu | 0:532cb55d6136 | 24 | missile_speed = speed; |
arvahsu | 0:532cb55d6136 | 25 | } |
arvahsu | 0:532cb55d6136 | 26 | } |
arvahsu | 0:532cb55d6136 | 27 | |
arvahsu | 0:532cb55d6136 | 28 | // set missile interval (default interval is 10) |
arvahsu | 0:532cb55d6136 | 29 | void set_missile_interval(int interval){ |
arvahsu | 0:532cb55d6136 | 30 | ASSERT_P(interval>=1 && interval<=100,ERROR_MISSILE_INTERVAL); |
arvahsu | 0:532cb55d6136 | 31 | if(interval>=1 && interval<=100){ |
arvahsu | 0:532cb55d6136 | 32 | missile_interval = interval; |
arvahsu | 0:532cb55d6136 | 33 | } |
arvahsu | 0:532cb55d6136 | 34 | } |
arvahsu | 0:532cb55d6136 | 35 | |
arvahsu | 0:532cb55d6136 | 36 | // See the comments in missile_public.h |
arvahsu | 0:532cb55d6136 | 37 | MISSILE missile_get_info(int index){ |
arvahsu | 0:532cb55d6136 | 38 | // All interface for user should have error checking |
arvahsu | 0:532cb55d6136 | 39 | ASSERT_P(index<MAX_NUM_MISSILE,ERROR_MISSILE_INDEX_GET_INFO); |
arvahsu | 0:532cb55d6136 | 40 | |
arvahsu | 0:532cb55d6136 | 41 | return missile_record[index]; |
arvahsu | 0:532cb55d6136 | 42 | } |
arvahsu | 0:532cb55d6136 | 43 | |
arvahsu | 0:532cb55d6136 | 44 | // See the comments in missile_public.h |
arvahsu | 0:532cb55d6136 | 45 | void missile_set_exploded(int index){ |
arvahsu | 0:532cb55d6136 | 46 | // All interface for user should have error checking |
arvahsu | 0:532cb55d6136 | 47 | ASSERT_P(index<MAX_NUM_MISSILE,ERROR_MISSILE_INDEX_UPDATE_STATUS); |
arvahsu | 0:532cb55d6136 | 48 | |
arvahsu | 0:532cb55d6136 | 49 | missile_record[index].status = MISSILE_EXPLODED; |
arvahsu | 0:532cb55d6136 | 50 | } |
arvahsu | 0:532cb55d6136 | 51 | |
arvahsu | 0:532cb55d6136 | 52 | /** This function finds an empty slot of missile record, and active it. |
arvahsu | 0:532cb55d6136 | 53 | */ |
arvahsu | 0:532cb55d6136 | 54 | void missile_create(void){ |
arvahsu | 0:532cb55d6136 | 55 | int i; |
arvahsu | 0:532cb55d6136 | 56 | for(i=0;i<MAX_NUM_MISSILE;i++){ |
arvahsu | 0:532cb55d6136 | 57 | if(missile_record[i].status == MISSILE_DEACTIVE){ |
arvahsu | 0:532cb55d6136 | 58 | missile_record[i].y = 0; |
arvahsu | 0:532cb55d6136 | 59 | //each missile has its own tick |
arvahsu | 0:532cb55d6136 | 60 | missile_record[i].tick = 0; |
arvahsu | 0:532cb55d6136 | 61 | //set a random source for the missile |
arvahsu | 0:532cb55d6136 | 62 | missile_record[i].source_x = rand() % SIZE_X; |
arvahsu | 0:532cb55d6136 | 63 | //set a random target for the missile |
arvahsu | 0:532cb55d6136 | 64 | missile_record[i].target_x = rand() % SIZE_X; |
arvahsu | 0:532cb55d6136 | 65 | //the missile starts at its source |
arvahsu | 0:532cb55d6136 | 66 | missile_record[i].x = missile_record[i].source_x; |
arvahsu | 0:532cb55d6136 | 67 | missile_record[i].status = MISSILE_ACTIVE; |
arvahsu | 0:532cb55d6136 | 68 | break; |
arvahsu | 0:532cb55d6136 | 69 | } |
arvahsu | 0:532cb55d6136 | 70 | } |
arvahsu | 0:532cb55d6136 | 71 | } |
arvahsu | 0:532cb55d6136 | 72 | |
arvahsu | 0:532cb55d6136 | 73 | /** This function update the position of all missiles and draw them |
arvahsu | 0:532cb55d6136 | 74 | */ |
arvahsu | 0:532cb55d6136 | 75 | void missile_update_position(void){ |
arvahsu | 0:532cb55d6136 | 76 | int i; |
arvahsu | 0:532cb55d6136 | 77 | //controls how fast the missile will move |
arvahsu | 0:532cb55d6136 | 78 | int rate = missile_speed * 25; |
arvahsu | 0:532cb55d6136 | 79 | //delta_x and delta_y account for the slope of the missile |
arvahsu | 0:532cb55d6136 | 80 | double delta_x,delta_y; |
arvahsu | 0:532cb55d6136 | 81 | for(i=0;i<MAX_NUM_MISSILE;i++){ |
arvahsu | 0:532cb55d6136 | 82 | if(missile_record[i].status == MISSILE_ACTIVE){ |
arvahsu | 0:532cb55d6136 | 83 | // update missile position |
arvahsu | 0:532cb55d6136 | 84 | delta_y = 200/rate; |
arvahsu | 0:532cb55d6136 | 85 | delta_x = (missile_record[i].target_x - missile_record[i].source_x)/rate; |
arvahsu | 0:532cb55d6136 | 86 | missile_draw(missile_record[i], BACKGROUND_COLOR); |
arvahsu | 0:532cb55d6136 | 87 | missile_record[i].y = (int)(delta_y*(missile_record[i].tick%rate)); |
arvahsu | 0:532cb55d6136 | 88 | missile_record[i].x = (int)(missile_record[i].source_x + delta_x*(missile_record[i].tick%rate)); |
arvahsu | 0:532cb55d6136 | 89 | // draw missile |
arvahsu | 0:532cb55d6136 | 90 | missile_draw(missile_record[i], MISSILE_COLOR); |
arvahsu | 0:532cb55d6136 | 91 | //update missile's internal tick |
arvahsu | 0:532cb55d6136 | 92 | missile_record[i].tick++; |
arvahsu | 0:532cb55d6136 | 93 | } |
arvahsu | 0:532cb55d6136 | 94 | else if(missile_record[i].status == MISSILE_EXPLODED){ |
arvahsu | 0:532cb55d6136 | 95 | // clear the missile on the screen |
arvahsu | 0:532cb55d6136 | 96 | missile_draw(missile_record[i], BACKGROUND_COLOR); |
arvahsu | 0:532cb55d6136 | 97 | |
arvahsu | 0:532cb55d6136 | 98 | // we have done with this missile, remove it from record |
arvahsu | 0:532cb55d6136 | 99 | missile_record[i].status = MISSILE_DEACTIVE; |
arvahsu | 0:532cb55d6136 | 100 | //resets the missile's internal tick |
arvahsu | 0:532cb55d6136 | 101 | missile_record[i].tick = 0; |
arvahsu | 0:532cb55d6136 | 102 | } |
arvahsu | 0:532cb55d6136 | 103 | |
arvahsu | 0:532cb55d6136 | 104 | } |
arvahsu | 0:532cb55d6136 | 105 | } |
arvahsu | 0:532cb55d6136 | 106 | |
arvahsu | 0:532cb55d6136 | 107 | /** This function draw a missile. |
arvahsu | 0:532cb55d6136 | 108 | @param missile The missile to be drawn |
arvahsu | 0:532cb55d6136 | 109 | @param color The color of the missile |
arvahsu | 0:532cb55d6136 | 110 | */ |
arvahsu | 0:532cb55d6136 | 111 | void missile_draw(MISSILE missile, int color){ |
arvahsu | 0:532cb55d6136 | 112 | int init_x,init_y,current_x,current_y; |
arvahsu | 0:532cb55d6136 | 113 | |
arvahsu | 0:532cb55d6136 | 114 | init_x = missile.source_x; |
arvahsu | 0:532cb55d6136 | 115 | init_y = 0; |
arvahsu | 0:532cb55d6136 | 116 | current_x = missile.x; |
arvahsu | 0:532cb55d6136 | 117 | current_y = missile.y; |
arvahsu | 0:532cb55d6136 | 118 | uLCD.line(init_x, init_y, current_x, current_y, color); |
arvahsu | 0:532cb55d6136 | 119 | } |
arvahsu | 0:532cb55d6136 | 120 | |
arvahsu | 0:532cb55d6136 | 121 |