summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorTeruaki Kawashima <teru@rockbox.org>2009-12-10 12:53:47 +0000
committerTeruaki Kawashima <teru@rockbox.org>2009-12-10 12:53:47 +0000
commit6ac0fbb1cbf2fb99e3215572c838336a2d04bdc4 (patch)
tree1e7b90d97c69b1e704f9a3c25f9cd64baf07d7e0 /apps
parentb4a40eb6f3e1e296271fa147cfef110c89f56a45 (diff)
downloadrockbox-6ac0fbb1cbf2fb99e3215572c838336a2d04bdc4.tar.gz
rockbox-6ac0fbb1cbf2fb99e3215572c838336a2d04bdc4.zip
Rework spacerocks:
* Use consistent name for variables/field names. * Consistent use of field of structures. * Simplify draw_polygon(). * Add some helper functions. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@23916 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps')
-rw-r--r--apps/plugins/spacerocks.c360
1 files changed, 171 insertions, 189 deletions
diff --git a/apps/plugins/spacerocks.c b/apps/plugins/spacerocks.c
index 9a6f907d1e..bbba9bfd48 100644
--- a/apps/plugins/spacerocks.c
+++ b/apps/plugins/spacerocks.c
@@ -277,7 +277,7 @@ CONFIG_KEYPAD == MROBE500_PAD
277 277
278#define SHOW_COL 0 278#define SHOW_COL 0
279#define SCALE 5000 279#define SCALE 5000
280#define WRAP_GAP 12*SCALE 280#define WRAP_GAP (LARGE*SCALE*3)
281#define POINT_SIZE 2 281#define POINT_SIZE 2
282#define START_LEVEL 1 282#define START_LEVEL 1
283#define SHOW_LEVEL_TIME 50 283#define SHOW_LEVEL_TIME 50
@@ -293,22 +293,28 @@ CONFIG_KEYPAD == MROBE500_PAD
293#define NUM_SHIP_VERTICES 4 293#define NUM_SHIP_VERTICES 4
294#define NUM_ENEMY_VERTICES 8 294#define NUM_ENEMY_VERTICES 8
295 295
296#define SPAWN_TIME 30 296#define INVULNERABLE_TIME 30
297#define BLINK_TIME 10 297#define BLINK_TIME 10
298#define EXTRA_LIFE 250 298#define EXTRA_LIFE 250
299#define START_LIVES 3 299#define START_LIVES 3
300#define MISSILE_SURVIVAL_LENGTH 40 300#define MISSILE_LIFE_LENGTH 40
301 301
302#define ASTEROID_SPEED (RES/20) 302#define ASTEROID_SPEED (RES/20)
303#define SPACE_CHECK_SIZE 30*SCALE 303#define SPACE_CHECK_SIZE 30*SCALE
304 304
305#if (LARGE_LCD)
306#define SIZE_SHIP_COLLISION 8*SCALE
307#else
308#define SIZE_SHIP_COLLISION 6*SCALE
309#endif
310
305#define LITTLE_SHIP 1 311#define LITTLE_SHIP 1
306#define BIG_SHIP 2 312#define BIG_SHIP 2
307#define ENEMY_BIG_PROBABILITY_START 10 313#define ENEMY_BIG_PROBABILITY_START 10
308#define ENEMY_APPEAR_PROBABILITY_START 35 314#define ENEMY_APPEAR_PROBABILITY_START 35
309#define ENEMY_APPEAR_TIMING_START 600 315#define ENEMY_APPEAR_TIMING_START 600
310#define ENEMY_SPEED 4 316#define ENEMY_SPEED 4
311#define ENEMY_MISSILE_SURVIVAL_LENGTH (RES/2) 317#define ENEMY_MISSILE_LIFE_LENGTH (RES/2)
312#if (LARGE_LCD) 318#if (LARGE_LCD)
313#define SIZE_ENEMY_COLLISION 7*SCALE 319#define SIZE_ENEMY_COLLISION 7*SCALE
314#else 320#else
@@ -507,8 +513,8 @@ struct Point
507 513
508struct TrailPoint 514struct TrailPoint
509{ 515{
510 int alive;
511 struct Point position; 516 struct Point position;
517 int alive;
512#ifdef HAVE_LCD_COLOR 518#ifdef HAVE_LCD_COLOR
513 short r; 519 short r;
514 short g; 520 short g;
@@ -532,18 +538,17 @@ struct Asteroid
532 538
533struct Ship 539struct Ship
534{ 540{
535 struct Point vertices[NUM_SHIP_VERTICES];
536 struct Point position; 541 struct Point position;
537 bool waiting_for_space; 542 struct Point vertices[NUM_SHIP_VERTICES];
543 bool exists;
538 int explode_countdown; 544 int explode_countdown;
539 bool invulnerable; 545 int invulnerable_time;
540 int spawn_time;
541}; 546};
542 547
543struct Enemy 548struct Enemy
544{ 549{
545 struct Point vertices[NUM_ENEMY_VERTICES];
546 struct Point position; 550 struct Point position;
551 struct Point vertices[NUM_ENEMY_VERTICES];
547 bool exists; 552 bool exists;
548 int explode_countdown; 553 int explode_countdown;
549 int appear_countdown; 554 int appear_countdown;
@@ -556,7 +561,7 @@ struct Missile
556{ 561{
557 struct Point position; 562 struct Point position;
558 struct Point oldpoint; 563 struct Point oldpoint;
559 int survived; 564 int alive;
560}; 565};
561 566
562static enum game_state game_state; 567static enum game_state game_state;
@@ -621,7 +626,7 @@ static bool is_point_within_rectangle(struct Point* rect, struct Point* p,
621 int dy = p->y - rect->y; 626 int dy = p->y - rect->y;
622#if SHOW_COL 627#if SHOW_COL
623 rb->lcd_drawrect((rect->x - size)/SCALE, (rect->y - size)/SCALE, 628 rb->lcd_drawrect((rect->x - size)/SCALE, (rect->y - size)/SCALE,
624 size*2/SCALE, size*2/SCALE); 629 (size*2+1)/SCALE, (size*2+1)/SCALE);
625#endif 630#endif
626 if (dx < -SCALED_WIDTH/2) dx += SCALED_WIDTH; 631 if (dx < -SCALED_WIDTH/2) dx += SCALED_WIDTH;
627 else if (dx > SCALED_WIDTH/2) dx -= SCALED_WIDTH; 632 else if (dx > SCALED_WIDTH/2) dx -= SCALED_WIDTH;
@@ -656,36 +661,32 @@ static void draw_polygon(struct Point* vertices, int num_vertices,
656{ 661{
657 int n, new_x, new_y, old_x, old_y; 662 int n, new_x, new_y, old_x, old_y;
658 struct Point *p; 663 struct Point *p;
659 bool bDrawAll = (px < WRAP_GAP || SCALED_WIDTH - px < WRAP_GAP || 664 bool draw_wrap;
660 py < WRAP_GAP || SCALED_HEIGHT - py < WRAP_GAP); 665
666 if (px > SCALED_WIDTH - WRAP_GAP)
667 px -= SCALED_WIDTH;
668 if (py > SCALED_HEIGHT - WRAP_GAP)
669 py -= SCALED_HEIGHT;
670
671 draw_wrap = (px < WRAP_GAP || py < WRAP_GAP);
661 672
662 p = vertices + num_vertices - 1; 673 p = vertices + num_vertices - 1;
663 old_x = (p->x + px)/SCALE; 674 old_x = (p->x + px)/SCALE;
664 old_y = (p->y + py)/SCALE; 675 old_y = (p->y + py)/SCALE;
665 p = vertices; 676 p = vertices;
666 n = num_vertices; 677 n = num_vertices;
667 while(n--) 678 while (n--)
668 { 679 {
669 new_x = (p->x + px)/SCALE; 680 new_x = (p->x + px)/SCALE;
670 new_y = (p->y + py)/SCALE; 681 new_y = (p->y + py)/SCALE;
671 682
672 rb->lcd_drawline(old_x, old_y, new_x, new_y); 683 rb->lcd_drawline(old_x, old_y, new_x, new_y);
673 684 if (draw_wrap)
674 if(bDrawAll)
675 { 685 {
676 rb->lcd_drawline(old_x - LCD_WIDTH, old_y, new_x - LCD_WIDTH, new_y);
677 rb->lcd_drawline(old_x + LCD_WIDTH, old_y, new_x + LCD_WIDTH, new_y); 686 rb->lcd_drawline(old_x + LCD_WIDTH, old_y, new_x + LCD_WIDTH, new_y);
678 rb->lcd_drawline(old_x - LCD_WIDTH, old_y + LCD_HEIGHT, 687 rb->lcd_drawline(old_x, old_y + LCD_HEIGHT, new_x, new_y + LCD_HEIGHT);
679 new_x - LCD_WIDTH, new_y + LCD_HEIGHT);
680 rb->lcd_drawline(old_x + LCD_WIDTH, old_y + LCD_HEIGHT, 688 rb->lcd_drawline(old_x + LCD_WIDTH, old_y + LCD_HEIGHT,
681 new_x + LCD_WIDTH, new_y + LCD_HEIGHT); 689 new_x + LCD_WIDTH, new_y + LCD_HEIGHT);
682
683 rb->lcd_drawline(old_x, old_y - LCD_HEIGHT, new_x, new_y - LCD_HEIGHT);
684 rb->lcd_drawline(old_x, old_y + LCD_HEIGHT, new_x, new_y + LCD_HEIGHT);
685 rb->lcd_drawline(old_x - LCD_WIDTH, old_y - LCD_HEIGHT,
686 new_x - LCD_WIDTH, new_y - LCD_HEIGHT);
687 rb->lcd_drawline(old_x + LCD_WIDTH, old_y - LCD_HEIGHT,
688 new_x + LCD_WIDTH, new_y - LCD_HEIGHT);
689 } 690 }
690 old_x = new_x; 691 old_x = new_x;
691 old_y = new_y; 692 old_y = new_y;
@@ -748,7 +749,7 @@ static void create_trail_blaze(int colour, struct Point* position)
748 { 749 {
749 /* find a space in the array of trail_points that is NULL or DEAD or 750 /* find a space in the array of trail_points that is NULL or DEAD or
750 whatever and place this one here. */ 751 whatever and place this one here. */
751 if (!tpoint->alive) 752 if (tpoint->alive <= 0)
752 { 753 {
753 numtoadd--; 754 numtoadd--;
754 /* take a random point near the position. */ 755 /* take a random point near the position. */
@@ -821,7 +822,7 @@ static void draw_and_move_trail_blaze(void)
821 n = NUM_TRAIL_POINTS; 822 n = NUM_TRAIL_POINTS;
822 while (n--) 823 while (n--)
823 { 824 {
824 if (tpoint->alive) 825 if (tpoint->alive > 0)
825 { 826 {
826 if (game_state != PAUSE_MODE) 827 if (game_state != PAUSE_MODE)
827 { 828 {
@@ -863,8 +864,8 @@ static void initialise_asteroid(struct Asteroid* asteroid,
863 int n; 864 int n;
864 865
865 asteroid->exists = true; 866 asteroid->exists = true;
866 asteroid->type = type;
867 asteroid->explode_countdown = 0; 867 asteroid->explode_countdown = 0;
868 asteroid->type = type;
868 869
869 /* Set the radius of the asteroid: */ 870 /* Set the radius of the asteroid: */
870 asteroid->radius = (int)type*SCALE*3; 871 asteroid->radius = (int)type*SCALE*3;
@@ -956,7 +957,7 @@ static void create_asteroid(enum asteroid_type type, struct Point *position)
956 n = MAX_NUM_ASTEROIDS; 957 n = MAX_NUM_ASTEROIDS;
957 while (n--) 958 while (n--)
958 { 959 {
959 if (!asteroid->exists && !asteroid->explode_countdown) 960 if (!asteroid->exists && asteroid->explode_countdown <= 0)
960 { 961 {
961 initialise_asteroid(asteroid, type, position); 962 initialise_asteroid(asteroid, type, position);
962 break; 963 break;
@@ -977,6 +978,11 @@ static void draw_and_move_asteroids(void)
977 n = MAX_NUM_ASTEROIDS; 978 n = MAX_NUM_ASTEROIDS;
978 while (n--) 979 while (n--)
979 { 980 {
981 if (asteroid->exists)
982 {
983 draw_polygon(asteroid->vertices, NUM_ASTEROID_VERTICES,
984 asteroid->position.x, asteroid->position.y);
985 }
980 if (game_state != PAUSE_MODE) 986 if (game_state != PAUSE_MODE)
981 { 987 {
982 if (asteroid->exists) 988 if (asteroid->exists)
@@ -984,20 +990,45 @@ static void draw_and_move_asteroids(void)
984 move_point(&asteroid->position); 990 move_point(&asteroid->position);
985 rotate_asteroid(asteroid); 991 rotate_asteroid(asteroid);
986 } 992 }
987 else if (asteroid->explode_countdown) 993 else if (asteroid->explode_countdown > 0)
988 { 994 {
989 asteroid->explode_countdown--; 995 asteroid->explode_countdown--;
990 } 996 }
991 } 997 }
992 if (asteroid->exists)
993 {
994 draw_polygon(asteroid->vertices, NUM_ASTEROID_VERTICES,
995 asteroid->position.x, asteroid->position.y);
996 }
997 asteroid++; 998 asteroid++;
998 } 999 }
999} 1000}
1000 1001
1002static void explode_asteroid(struct Asteroid* asteroid)
1003{
1004 struct Point p;
1005 p.dx = asteroid->position.dx;
1006 p.dy = asteroid->position.dy;
1007 p.x = asteroid->position.x;
1008 p.y = asteroid->position.y;
1009
1010 asteroid_count--;
1011 asteroid->exists = false;
1012
1013 switch(asteroid->type)
1014 {
1015 case SMALL:
1016 asteroid->explode_countdown = EXPLOSION_LENGTH;
1017 create_trail_blaze(EXPLOSION_ASTEROID, &p);
1018 break;
1019
1020 case MEDIUM:
1021 create_asteroid(SMALL, &p);
1022 create_asteroid(SMALL, &p);
1023 break;
1024
1025 case LARGE:
1026 create_asteroid(MEDIUM, &p);
1027 create_asteroid(MEDIUM, &p);
1028 break;
1029 }
1030}
1031
1001/************************************************* 1032/*************************************************
1002** Handle ship. 1033** Handle ship.
1003*************************************************/ 1034*************************************************/
@@ -1013,9 +1044,9 @@ static void initialise_ship(void)
1013 ship.position.y = CENTER_LCD_Y * SCALE; 1044 ship.position.y = CENTER_LCD_Y * SCALE;
1014 ship.position.dx = 0; 1045 ship.position.dx = 0;
1015 ship.position.dy = 0; 1046 ship.position.dy = 0;
1016 ship.explode_countdown = 0; 1047 ship.exists = true;
1017 ship.spawn_time = SPAWN_TIME; 1048 ship.explode_countdown = 0;
1018 ship.invulnerable = 1; 1049 ship.invulnerable_time = INVULNERABLE_TIME;
1019 1050
1020 point = ship.vertices; 1051 point = ship.vertices;
1021 lives_point = lives_points; 1052 lives_point = lives_points;
@@ -1040,8 +1071,7 @@ static void initialise_ship(void)
1040 */ 1071 */
1041static void draw_and_move_ship(void) 1072static void draw_and_move_ship(void)
1042{ 1073{
1043 if (ship.invulnerable && 1074 if (ship.invulnerable_time > BLINK_TIME || ship.invulnerable_time % 2 != 0)
1044 (ship.spawn_time > BLINK_TIME || ship.spawn_time % 2 == 0))
1045 { 1075 {
1046 SET_FG(COL_INVULN); 1076 SET_FG(COL_INVULN);
1047 } 1077 }
@@ -1050,36 +1080,24 @@ static void draw_and_move_ship(void)
1050 SET_FG(COL_PLAYER); 1080 SET_FG(COL_PLAYER);
1051 } 1081 }
1052 1082
1053 if (!ship.explode_countdown) 1083 if (ship.exists)
1054 { 1084 {
1055 /* make sure ship is invulnerable until spawn time over */ 1085 draw_polygon(ship.vertices, NUM_SHIP_VERTICES,
1056 if (game_state != PAUSE_MODE) 1086 ship.position.x, ship.position.y);
1057 {
1058 if (ship.spawn_time)
1059 {
1060 ship.spawn_time--;
1061 if (ship.spawn_time <= 0)
1062 {
1063 ship.invulnerable = 0;
1064 }
1065 }
1066 }
1067 if (!ship.waiting_for_space)
1068 {
1069 draw_polygon(ship.vertices, NUM_SHIP_VERTICES,
1070 ship.position.x, ship.position.y);
1071 if (game_state != PAUSE_MODE && game_state != GAME_OVER)
1072 {
1073 move_point(&ship.position);
1074 }
1075 }
1076 } 1087 }
1077 else 1088
1089 if (game_state != PAUSE_MODE)
1078 { 1090 {
1079 if (game_state != PAUSE_MODE) 1091 if (ship.exists)
1092 {
1093 if (ship.invulnerable_time > 0)
1094 ship.invulnerable_time--;
1095 move_point(&ship.position);
1096 }
1097 else if (ship.explode_countdown > 0)
1080 { 1098 {
1081 ship.explode_countdown--; 1099 ship.explode_countdown--;
1082 if (!ship.explode_countdown) 1100 if (ship.explode_countdown <= 0)
1083 { 1101 {
1084 num_lives--; 1102 num_lives--;
1085 if (!num_lives) 1103 if (!num_lives)
@@ -1089,17 +1107,27 @@ static void draw_and_move_ship(void)
1089 else 1107 else
1090 { 1108 {
1091 initialise_ship(); 1109 initialise_ship();
1092 ship.waiting_for_space = true;
1093 } 1110 }
1094 } 1111 }
1095 } 1112 }
1096 } 1113 }
1097} 1114}
1098 1115
1116static void explode_ship(void)
1117{
1118 if (!ship.invulnerable_time)
1119 {
1120 /* if not invulnerable, blow up ship */
1121 ship.explode_countdown = EXPLOSION_LENGTH;
1122 ship.exists = false;
1123 create_trail_blaze(EXPLOSION_SHIP, &ship.position);
1124 }
1125}
1126
1099/* Rotate the ship using the passed sin & cos values */ 1127/* Rotate the ship using the passed sin & cos values */
1100static void rotate_ship(int cos, int sin) 1128static void rotate_ship(int cos, int sin)
1101{ 1129{
1102 if (!ship.waiting_for_space && !ship.explode_countdown) 1130 if (ship.exists)
1103 { 1131 {
1104 rotate_polygon(ship.vertices, NUM_SHIP_VERTICES, cos, sin); 1132 rotate_polygon(ship.vertices, NUM_SHIP_VERTICES, cos, sin);
1105 } 1133 }
@@ -1107,7 +1135,7 @@ static void rotate_ship(int cos, int sin)
1107 1135
1108static void thrust_ship(void) 1136static void thrust_ship(void)
1109{ 1137{
1110 if (!ship.waiting_for_space && !ship.explode_countdown) 1138 if (ship.exists)
1111 { 1139 {
1112 ship.position.dx += ( ship.vertices[0].x - ship.vertices[2].x )/20; 1140 ship.position.dx += ( ship.vertices[0].x - ship.vertices[2].x )/20;
1113 ship.position.dy += ( ship.vertices[0].y - ship.vertices[2].y )/20; 1141 ship.position.dy += ( ship.vertices[0].y - ship.vertices[2].y )/20;
@@ -1123,7 +1151,7 @@ static void thrust_ship(void)
1123/* stop movement of ship, 'cos that's what happens when you go into hyperspace. */ 1151/* stop movement of ship, 'cos that's what happens when you go into hyperspace. */
1124static void hyperspace(void) 1152static void hyperspace(void)
1125{ 1153{
1126 if (!ship.waiting_for_space && !ship.explode_countdown) 1154 if (ship.exists)
1127 { 1155 {
1128 ship.position.dx = ship.position.dy = 0; 1156 ship.position.dx = ship.position.dy = 0;
1129 ship.position.x = (rb->rand()%SCALED_WIDTH); 1157 ship.position.x = (rb->rand()%SCALED_WIDTH);
@@ -1167,7 +1195,7 @@ static void initialise_missile(struct Missile* missile)
1167 missile->position.y = ship.position.y + ship.vertices[0].y; 1195 missile->position.y = ship.position.y + ship.vertices[0].y;
1168 missile->position.dx = (ship.vertices[0].x - ship.vertices[2].x)/2; 1196 missile->position.dx = (ship.vertices[0].x - ship.vertices[2].x)/2;
1169 missile->position.dy = (ship.vertices[0].y - ship.vertices[2].y)/2; 1197 missile->position.dy = (ship.vertices[0].y - ship.vertices[2].y)/2;
1170 missile->survived = MISSILE_SURVIVAL_LENGTH; 1198 missile->alive = MISSILE_LIFE_LENGTH;
1171 missile->oldpoint.x = missile->position.x; 1199 missile->oldpoint.x = missile->position.x;
1172 missile->oldpoint.y = missile->position.y; 1200 missile->oldpoint.y = missile->position.y;
1173} 1201}
@@ -1178,13 +1206,13 @@ static void fire_missile(void)
1178 struct Missile* missile; 1206 struct Missile* missile;
1179 int n; 1207 int n;
1180 1208
1181 if (!ship.explode_countdown && !ship.waiting_for_space) 1209 if (ship.exists)
1182 { 1210 {
1183 missile = missiles_array; 1211 missile = missiles_array;
1184 n = MAX_NUM_MISSILES; 1212 n = MAX_NUM_MISSILES;
1185 while (n--) 1213 while (n--)
1186 { 1214 {
1187 if (!missile->survived) 1215 if (missile->alive <= 0)
1188 { 1216 {
1189 initialise_missile(missile); 1217 initialise_missile(missile);
1190 break; 1218 break;
@@ -1207,7 +1235,7 @@ static void draw_and_move_missiles(void)
1207 n = MAX_NUM_MISSILES; 1235 n = MAX_NUM_MISSILES;
1208 while (n--) 1236 while (n--)
1209 { 1237 {
1210 if (missile->survived) 1238 if (missile->alive > 0)
1211 { 1239 {
1212 vertices[0].x = 0; 1240 vertices[0].x = 0;
1213 vertices[0].y = 0; 1241 vertices[0].y = 0;
@@ -1220,7 +1248,7 @@ static void draw_and_move_missiles(void)
1220 missile->oldpoint.x = missile->position.x; 1248 missile->oldpoint.x = missile->position.x;
1221 missile->oldpoint.y = missile->position.y; 1249 missile->oldpoint.y = missile->position.y;
1222 move_point(&missile->position); 1250 move_point(&missile->position);
1223 missile->survived--; 1251 missile->alive--;
1224 } 1252 }
1225 } 1253 }
1226 missile++; 1254 missile++;
@@ -1298,57 +1326,46 @@ static void draw_and_move_enemy(void)
1298 1326
1299 if (enemy.exists) 1327 if (enemy.exists)
1300 { 1328 {
1301 if (!enemy.explode_countdown) 1329 draw_polygon(enemy.vertices, NUM_ENEMY_VERTICES,
1302 { 1330 enemy.position.x, enemy.position.y);
1303 draw_polygon(enemy.vertices, NUM_ENEMY_VERTICES, 1331 }
1304 enemy.position.x, enemy.position.y);
1305 1332
1306 if (game_state != PAUSE_MODE) 1333 if (game_state != PAUSE_MODE)
1307 { 1334 {
1308 enemy.position.x += enemy.position.dx; 1335 if (enemy.exists)
1309 enemy.position.y += enemy.position.dy; 1336 {
1337 enemy.position.x += enemy.position.dx;
1338 enemy.position.y += enemy.position.dy;
1310 1339
1311 if (enemy.position.x > SCALED_WIDTH || enemy.position.x < 0) 1340 if (enemy.position.x > SCALED_WIDTH || enemy.position.x < 0)
1312 enemy.exists = false; 1341 enemy.exists = false;
1313 1342
1314 enemy.position.y %= SCALED_HEIGHT; 1343 enemy.position.y %= SCALED_HEIGHT;
1315 if (enemy.position.y < 0) 1344 if (enemy.position.y < 0)
1316 enemy.position.y += SCALED_HEIGHT; 1345 enemy.position.y += SCALED_HEIGHT;
1317 1346
1318 if ((rb->rand()%1000) < 10) 1347 if ((rb->rand()%1000) < 10)
1319 enemy.position.dy = -enemy.position.dy; 1348 enemy.position.dy = -enemy.position.dy;
1320 }
1321 } 1349 }
1322 else 1350 else if (enemy.explode_countdown > 0)
1323 { 1351 {
1324 if (game_state != PAUSE_MODE) 1352 enemy.explode_countdown--;
1325 {
1326 enemy.explode_countdown--;
1327 if (!enemy.explode_countdown)
1328 {
1329 enemy.exists = false;
1330 }
1331 }
1332 } 1353 }
1333 } 1354 else
1334 else
1335 {
1336 if (game_state != PAUSE_MODE)
1337 { 1355 {
1338 if (enemy.appear_countdown) 1356 if (enemy.appear_countdown > 0)
1339 enemy.appear_countdown--; 1357 enemy.appear_countdown--;
1340 else if (rb->rand()%100 >= enemy.appear_probability) 1358 else if (rb->rand()%100 >= enemy.appear_probability)
1341 initialise_enemy(); 1359 initialise_enemy();
1342 } 1360 }
1343 } 1361 }
1344 1362
1345 if (!enemy_missile.survived) 1363 if (enemy_missile.alive <= 0)
1346 { 1364 {
1347 /* if no missile and the enemy is here and not exploding.. 1365 /* if no missile and the enemy is here and not exploding..
1348 then shoot baby! */ 1366 then shoot baby! */
1349 if (!enemy.explode_countdown && enemy.exists && 1367 if (enemy.exists && ship.exists &&
1350 !ship.waiting_for_space && game_state == PLAY_MODE && 1368 game_state == PLAY_MODE && (rb->rand()%10) >= 5 )
1351 (rb->rand()%10) >= 5 )
1352 { 1369 {
1353 int dx = ship.position.x - enemy.position.x; 1370 int dx = ship.position.x - enemy.position.x;
1354 int dy = ship.position.y - enemy.position.y; 1371 int dy = ship.position.y - enemy.position.y;
@@ -1385,7 +1402,7 @@ static void draw_and_move_enemy(void)
1385 1402
1386 enemy_missile.position.dx *= SCALE; 1403 enemy_missile.position.dx *= SCALE;
1387 enemy_missile.position.dy *= SCALE; 1404 enemy_missile.position.dy *= SCALE;
1388 enemy_missile.survived = ENEMY_MISSILE_SURVIVAL_LENGTH; 1405 enemy_missile.alive = ENEMY_MISSILE_LIFE_LENGTH;
1389 } 1406 }
1390 } 1407 }
1391 else 1408 else
@@ -1396,7 +1413,7 @@ static void draw_and_move_enemy(void)
1396 if (game_state != PAUSE_MODE) 1413 if (game_state != PAUSE_MODE)
1397 { 1414 {
1398 move_point(&enemy_missile.position); 1415 move_point(&enemy_missile.position);
1399 enemy_missile.survived--; 1416 enemy_missile.alive--;
1400 } 1417 }
1401 } 1418 }
1402} 1419}
@@ -1424,32 +1441,20 @@ static bool is_point_within_asteroid(struct Asteroid* asteroid,
1424 point->x - asteroid->position.x, 1441 point->x - asteroid->position.x,
1425 point->y - asteroid->position.y)) 1442 point->y - asteroid->position.y))
1426 { 1443 {
1427 struct Point p; 1444 explode_asteroid(asteroid);
1428 p.dx = asteroid->position.dx; 1445 return true;
1429 p.dy = asteroid->position.dy; 1446 }
1430 p.x = asteroid->position.x; 1447 else
1431 p.y = asteroid->position.y; 1448 return false;
1432 1449}
1433 asteroid_count--;
1434 asteroid->exists = false;
1435
1436 switch(asteroid->type)
1437 {
1438 case SMALL:
1439 asteroid->explode_countdown = EXPLOSION_LENGTH;
1440 create_trail_blaze(EXPLOSION_ASTEROID, &p);
1441 break;
1442
1443 case MEDIUM:
1444 create_asteroid(SMALL, &p);
1445 create_asteroid(SMALL, &p);
1446 break;
1447 1450
1448 case LARGE: 1451static bool is_point_within_ship(struct Point* point)
1449 create_asteroid(MEDIUM, &p); 1452{
1450 create_asteroid(MEDIUM, &p); 1453 if (is_point_within_rectangle(&ship.position, point, SIZE_SHIP_COLLISION)
1451 break; 1454 && is_point_in_polygon(ship.vertices, NUM_SHIP_VERTICES,
1452 } 1455 point->x - ship.position.x,
1456 point->y - ship.position.y))
1457 {
1453 return true; 1458 return true;
1454 } 1459 }
1455 else 1460 else
@@ -1462,6 +1467,7 @@ static bool is_point_within_enemy(struct Point* point)
1462 { 1467 {
1463 add_score(5); 1468 add_score(5);
1464 enemy.explode_countdown = EXPLOSION_LENGTH; 1469 enemy.explode_countdown = EXPLOSION_LENGTH;
1470 enemy.exists = false;
1465 create_trail_blaze(EXPLOSION_ENEMY, &enemy.position); 1471 create_trail_blaze(EXPLOSION_ENEMY, &enemy.position);
1466 return true; 1472 return true;
1467 } 1473 }
@@ -1473,6 +1479,10 @@ static bool is_ship_within_asteroid(struct Asteroid* asteroid)
1473{ 1479{
1474 struct Point p; 1480 struct Point p;
1475 1481
1482 if (!is_point_within_rectangle(&asteroid->position, &ship.position,
1483 asteroid->radius+SIZE_SHIP_COLLISION))
1484 return false;
1485
1476 p.x = ship.position.x + ship.vertices[0].x; 1486 p.x = ship.position.x + ship.vertices[0].x;
1477 p.y = ship.position.y + ship.vertices[0].y; 1487 p.y = ship.position.y + ship.vertices[0].y;
1478 if (is_point_within_asteroid(asteroid, &p)) 1488 if (is_point_within_asteroid(asteroid, &p))
@@ -1498,7 +1508,6 @@ static void check_collisions(void)
1498 struct Asteroid* asteroid; 1508 struct Asteroid* asteroid;
1499 int m, n; 1509 int m, n;
1500 bool asteroids_onscreen = false; 1510 bool asteroids_onscreen = false;
1501 bool ship_cant_be_placed = false;
1502 1511
1503 asteroid = asteroids_array; 1512 asteroid = asteroids_array;
1504 m = MAX_NUM_ASTEROIDS; 1513 m = MAX_NUM_ASTEROIDS;
@@ -1512,14 +1521,14 @@ static void check_collisions(void)
1512 while (n--) 1521 while (n--)
1513 { 1522 {
1514 /* if the missiles exists: */ 1523 /* if the missiles exists: */
1515 if (missile->survived > 0) 1524 if (missile->alive > 0)
1516 { 1525 {
1517 /* has the missile hit the asteroid? */ 1526 /* has the missile hit the asteroid? */
1518 if (is_point_within_asteroid(asteroid, &missile->position) || 1527 if (is_point_within_asteroid(asteroid, &missile->position) ||
1519 is_point_within_asteroid(asteroid, &missile->oldpoint)) 1528 is_point_within_asteroid(asteroid, &missile->oldpoint))
1520 { 1529 {
1521 add_score(1); 1530 add_score(1);
1522 missile->survived = 0; 1531 missile->alive = 0;
1523 break; 1532 break;
1524 } 1533 }
1525 } 1534 }
@@ -1527,72 +1536,53 @@ static void check_collisions(void)
1527 } 1536 }
1528 1537
1529 /* now check collision with ship: */ 1538 /* now check collision with ship: */
1530 if (asteroid->exists && !ship.waiting_for_space && !ship.explode_countdown) 1539 if (asteroid->exists && ship.exists)
1531 { 1540 {
1532 if (is_ship_within_asteroid(asteroid)) 1541 if (is_ship_within_asteroid(asteroid))
1533 { 1542 {
1534 add_score(1); 1543 add_score(1);
1535 if (!ship.invulnerable) 1544 explode_ship();
1536 {
1537 /* if not invulnerable, blow up ship */
1538 ship.explode_countdown = EXPLOSION_LENGTH;
1539 create_trail_blaze(EXPLOSION_SHIP, &ship.position);
1540 }
1541 } 1545 }
1546 }
1542 1547
1543 /* has the enemy missile blown something up? */ 1548 /* has the enemy missile blown something up? */
1544 if (asteroid->exists && enemy_missile.survived) 1549 if (asteroid->exists && enemy_missile.alive > 0)
1550 {
1551 if (is_point_within_asteroid(asteroid, &enemy_missile.position))
1545 { 1552 {
1546 if (is_point_within_asteroid(asteroid, &enemy_missile.position)) 1553 enemy_missile.alive = 0;
1547 {
1548 enemy_missile.survived = 0;
1549 }
1550
1551 /* if it still exists, check if ship is waiting for space: */
1552 if (asteroid->exists && ship.waiting_for_space)
1553 {
1554 ship_cant_be_placed |=
1555 is_point_within_rectangle(&ship.position,
1556 &asteroid->position,
1557 SPACE_CHECK_SIZE);
1558 }
1559 } 1554 }
1560 } 1555 }
1561 } 1556 }
1562 1557
1563 /* is an asteroid still exploding? */ 1558 /* is an asteroid still exploding? */
1564 if (asteroid->explode_countdown) 1559 if (asteroid->explode_countdown > 0)
1565 asteroids_onscreen = true; 1560 asteroids_onscreen = true;
1566 1561
1567 asteroid++; 1562 asteroid++;
1568 } 1563 }
1569 1564
1570 /* now check collision between ship and enemy */ 1565 /* now check collision between ship and enemy */
1571 if (enemy.exists && !enemy.explode_countdown && 1566 if (enemy.exists && ship.exists)
1572 !ship.waiting_for_space && !ship.explode_countdown)
1573 { 1567 {
1574 /* has the enemy collided with the ship? */ 1568 /* has the enemy collided with the ship? */
1575 if (is_point_within_enemy(&ship.position)) 1569 if (is_point_within_enemy(&ship.position))
1576 { 1570 {
1577 if (!ship.invulnerable) 1571 explode_ship();
1578 {
1579 ship.explode_countdown = EXPLOSION_LENGTH;
1580 create_trail_blaze(EXPLOSION_SHIP, &ship.position);
1581 }
1582 create_trail_blaze(EXPLOSION_ENEMY, &enemy.position); 1572 create_trail_blaze(EXPLOSION_ENEMY, &enemy.position);
1583 } 1573 }
1584 1574
1585 if (enemy.exists && !enemy.explode_countdown) 1575 if (enemy.exists)
1586 { 1576 {
1587 /* Now see if the enemy has been shot at by the ships missiles: */ 1577 /* Now see if the enemy has been shot at by the ships missiles: */
1588 missile = missiles_array; 1578 missile = missiles_array;
1589 n = MAX_NUM_MISSILES; 1579 n = MAX_NUM_MISSILES;
1590 while (n--) 1580 while (n--)
1591 { 1581 {
1592 if (missile->survived > 0 && 1582 if (missile->alive > 0 &&
1593 is_point_within_enemy(&missile->position)) 1583 is_point_within_enemy(&missile->position))
1594 { 1584 {
1595 missile->survived = 0; 1585 missile->alive = 0;
1596 break; 1586 break;
1597 } 1587 }
1598 missile++; 1588 missile++;
@@ -1601,25 +1591,16 @@ static void check_collisions(void)
1601 } 1591 }
1602 1592
1603 /* test collision with enemy missile and ship: */ 1593 /* test collision with enemy missile and ship: */
1604 if (!ship_cant_be_placed && enemy_missile.survived > 0 && 1594 if (enemy_missile.alive > 0 && is_point_within_ship(&enemy_missile.position))
1605 is_point_in_polygon(ship.vertices, NUM_SHIP_VERTICES,
1606 enemy_missile.position.x - ship.position.x,
1607 enemy_missile.position.y - ship.position.y))
1608 { 1595 {
1609 if (!ship.invulnerable) 1596 explode_ship();
1610 { 1597 enemy_missile.alive = 0;
1611 ship.explode_countdown = EXPLOSION_LENGTH;
1612 create_trail_blaze(EXPLOSION_SHIP, &ship.position);
1613 }
1614 enemy_missile.survived = 0;
1615 enemy_missile.position.x = enemy_missile.position.y = 0; 1598 enemy_missile.position.x = enemy_missile.position.y = 0;
1616 } 1599 }
1617 1600
1618 if (!ship_cant_be_placed)
1619 ship.waiting_for_space = false;
1620
1621 /* if all asteroids cleared then start again: */ 1601 /* if all asteroids cleared then start again: */
1622 if (asteroid_count == 0 && !enemy.exists && !asteroids_onscreen) 1602 if (asteroid_count == 0 && !asteroids_onscreen
1603 && !enemy.exists && enemy.explode_countdown <= 0)
1623 { 1604 {
1624 current_level++; 1605 current_level++;
1625 enemy.appear_probability += 5; 1606 enemy.appear_probability += 5;
@@ -1682,7 +1663,8 @@ static void initialise_level(int start_num)
1682 1663
1683 /* no enemy */ 1664 /* no enemy */
1684 enemy.exists = 0; 1665 enemy.exists = 0;
1685 enemy_missile.survived = 0; 1666 enemy.explode_countdown = 0;
1667 enemy_missile.alive = 0;
1686 1668
1687 /* clear asteroids */ 1669 /* clear asteroids */
1688 asteroid = asteroids_array; 1670 asteroid = asteroids_array;
@@ -1702,7 +1684,7 @@ static void initialise_level(int start_num)
1702 n = MAX_NUM_MISSILES; 1684 n = MAX_NUM_MISSILES;
1703 while (n--) 1685 while (n--)
1704 { 1686 {
1705 missile->survived = 0; 1687 missile->alive = 0;
1706 missile++; 1688 missile++;
1707 } 1689 }
1708 1690