1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Mat Holton
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
27 /******************************* Globals ***********************************/
28 static struct plugin_api
* rb
; /* global api struct pointer */
29 /* variable button definitions */
30 #if CONFIG_KEYPAD == RECORDER_PAD
31 #define AST_PAUSE BUTTON_ON
32 #define AST_QUIT BUTTON_OFF
33 #define AST_THRUST_REP BUTTON_UP | BUTTON_REPEAT
34 #define AST_THRUST BUTTON_UP
35 #define AST_HYPERSPACE BUTTON_DOWN
36 #define AST_LEFT BUTTON_LEFT
37 #define AST_LEFT_REP BUTTON_LEFT | BUTTON_REPEAT
38 #define AST_RIGHT BUTTON_RIGHT
39 #define AST_RIGHT_REP (BUTTON_RIGHT | BUTTON_REPEAT)
40 #define AST_FIRE BUTTON_PLAY
41 #define AST_FIRE_REP BUTTON_PLAY | BUTTON_REPEAT
43 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
44 #define AST_PAUSE BUTTON_ON
45 #define AST_QUIT BUTTON_OFF
46 #define AST_THRUST_REP BUTTON_UP | BUTTON_REPEAT
47 #define AST_THRUST BUTTON_UP
48 #define AST_HYPERSPACE BUTTON_DOWN
49 #define AST_LEFT BUTTON_LEFT
50 #define AST_LEFT_REP BUTTON_LEFT | BUTTON_REPEAT
51 #define AST_RIGHT BUTTON_RIGHT
52 #define AST_RIGHT_REP (BUTTON_RIGHT | BUTTON_REPEAT)
53 #define AST_FIRE BUTTON_SELECT
54 #define AST_FIRE_REP BUTTON_SELECT | BUTTON_REPEAT
56 #elif CONFIG_KEYPAD == ONDIO_PAD
57 #define AST_PAUSE (BUTTON_MENU | BUTTON_OFF)
58 #define AST_QUIT BUTTON_OFF
59 #define AST_THRUST BUTTON_UP
60 #define AST_THRUST_REP BUTTON_UP | BUTTON_REPEAT
61 #define AST_HYPERSPACE BUTTON_DOWN
62 #define AST_LEFT BUTTON_LEFT
63 #define AST_LEFT_REP BUTTON_LEFT | BUTTON_REPEAT
64 #define AST_RIGHT BUTTON_RIGHT
65 #define AST_RIGHT_REP (BUTTON_RIGHT | BUTTON_REPEAT)
66 #define AST_FIRE BUTTON_MENU
67 #define AST_FIRE_REP BUTTON_MENU | BUTTON_REPEAT
69 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
70 (CONFIG_KEYPAD == IRIVER_H300_PAD)
71 #define AST_PAUSE BUTTON_REC
72 #define AST_QUIT BUTTON_OFF
73 #define AST_THRUST_REP BUTTON_UP | BUTTON_REPEAT
74 #define AST_THRUST BUTTON_UP
75 #define AST_HYPERSPACE BUTTON_DOWN
76 #define AST_LEFT BUTTON_LEFT
77 #define AST_LEFT_REP BUTTON_LEFT | BUTTON_REPEAT
78 #define AST_RIGHT BUTTON_RIGHT
79 #define AST_RIGHT_REP (BUTTON_RIGHT | BUTTON_REPEAT)
80 #define AST_FIRE BUTTON_SELECT
81 #define AST_FIRE_REP BUTTON_SELECT | BUTTON_REPEAT
83 #define AST_RC_QUIT BUTTON_RC_STOP
85 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
86 #define AST_PAUSE BUTTON_PLAY
87 #define AST_QUIT BUTTON_POWER
88 #define AST_THRUST_REP BUTTON_UP | BUTTON_REPEAT
89 #define AST_THRUST BUTTON_UP
90 #define AST_HYPERSPACE BUTTON_DOWN
91 #define AST_LEFT BUTTON_LEFT
92 #define AST_LEFT_REP BUTTON_LEFT | BUTTON_REPEAT
93 #define AST_RIGHT BUTTON_RIGHT
94 #define AST_RIGHT_REP (BUTTON_RIGHT | BUTTON_REPEAT)
95 #define AST_FIRE BUTTON_SELECT
96 #define AST_FIRE_REP BUTTON_SELECT | BUTTON_REPEAT
98 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
99 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
100 #define AST_PAUSE (BUTTON_SELECT | BUTTON_PLAY)
101 #define AST_QUIT (BUTTON_SELECT | BUTTON_MENU)
102 #define AST_THRUST BUTTON_MENU
103 #define AST_THRUST_REP (BUTTON_MENU | BUTTON_REPEAT)
104 #define AST_HYPERSPACE BUTTON_PLAY
105 #define AST_LEFT BUTTON_SCROLL_BACK
106 #define AST_LEFT_REP (BUTTON_SCROLL_BACK | BUTTON_REPEAT)
107 #define AST_RIGHT BUTTON_SCROLL_FWD
108 #define AST_RIGHT_REP (BUTTON_SCROLL_FWD | BUTTON_REPEAT)
109 #define AST_FIRE BUTTON_SELECT
110 #define AST_FIRE_REP (BUTTON_SELECT | BUTTON_REPEAT)
112 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
113 #define AST_PAUSE BUTTON_A
114 #define AST_QUIT BUTTON_POWER
115 #define AST_THRUST_REP BUTTON_UP | BUTTON_REPEAT
116 #define AST_THRUST BUTTON_UP
117 #define AST_HYPERSPACE BUTTON_DOWN
118 #define AST_LEFT BUTTON_LEFT
119 #define AST_LEFT_REP BUTTON_LEFT | BUTTON_REPEAT
120 #define AST_RIGHT BUTTON_RIGHT
121 #define AST_RIGHT_REP (BUTTON_RIGHT | BUTTON_REPEAT)
122 #define AST_FIRE BUTTON_SELECT
123 #define AST_FIRE_REP BUTTON_SELECT | BUTTON_REPEAT
125 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
126 #define AST_PAUSE BUTTON_REC
127 #define AST_QUIT BUTTON_POWER
128 #define AST_THRUST_REP (BUTTON_UP | BUTTON_REPEAT)
129 #define AST_THRUST BUTTON_UP
130 #define AST_HYPERSPACE BUTTON_DOWN
131 #define AST_LEFT BUTTON_SCROLL_UP
132 #define AST_LEFT_REP (BUTTON_SCROLL_UP | BUTTON_REPEAT)
133 #define AST_RIGHT BUTTON_SCROLL_DOWN
134 #define AST_RIGHT_REP (BUTTON_SCROLL_DOWN | BUTTON_REPEAT)
135 #define AST_FIRE BUTTON_SELECT
136 #define AST_FIRE_REP (BUTTON_SELECT | BUTTON_REPEAT)
138 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
139 #define AST_PAUSE BUTTON_REC
140 #define AST_QUIT BUTTON_POWER
141 #define AST_THRUST_REP (BUTTON_UP | BUTTON_REPEAT)
142 #define AST_THRUST BUTTON_UP
143 #define AST_HYPERSPACE BUTTON_DOWN
144 #define AST_LEFT BUTTON_LEFT
145 #define AST_LEFT_REP (BUTTON_LEFT | BUTTON_REPEAT)
146 #define AST_RIGHT BUTTON_RIGHT
147 #define AST_RIGHT_REP (BUTTON_RIGHT | BUTTON_REPEAT)
148 #define AST_FIRE BUTTON_SELECT
149 #define AST_FIRE_REP (BUTTON_SELECT | BUTTON_REPEAT)
151 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
152 #define AST_PAUSE BUTTON_PLAY
153 #define AST_QUIT BUTTON_POWER
154 #define AST_THRUST_REP BUTTON_SCROLL_UP | BUTTON_REPEAT
155 #define AST_THRUST BUTTON_SCROLL_UP
156 #define AST_HYPERSPACE BUTTON_SCROLL_DOWN
157 #define AST_LEFT BUTTON_LEFT
158 #define AST_LEFT_REP BUTTON_LEFT | BUTTON_REPEAT
159 #define AST_RIGHT BUTTON_RIGHT
160 #define AST_RIGHT_REP (BUTTON_RIGHT | BUTTON_REPEAT)
161 #define AST_FIRE BUTTON_REW
162 #define AST_FIRE_REP BUTTON_REW | BUTTON_REPEAT
166 #define ABS(x) ((x)>0?(x):-(x))
168 #define RES MAX(LCD_WIDTH, LCD_HEIGHT)
169 #define LARGE_LCD RES >= 200
170 #define ENEMY_MISSILE_SURVIVAL_LENGTH RES/2
171 #define ASTEROID_SPEED RES/20
172 #define MISSILE_SURVIVAL_LENGTH 40
174 #define EXTRA_LIFE 250
176 #define MISSILE_SCALE 5000
178 #define EXPLOSION_LENGTH 20
180 #define HISCORE_FILE PLUGIN_GAMES_DIR "/astrorocks.hs"
182 #define MAX_NUM_ASTEROIDS 25
183 #define MAX_NUM_MISSILES 6
184 #define ENEMY_BIG_PROBABILITY_START 10
185 #define ENEMY_APPEAR_PROBABILITY_START 35
186 #define ENEMY_APPEAR_TIMING_START 1800
187 #define LITTLE_SHIP 2
189 #define SHOW_GAME_OVER_TIME 100
190 #define SHOW_LEVEL_TIME 50
191 #define START_LIVES 3
192 #define START_LEVEL 1
193 #define NUM_ASTEROID_VERTICES 10
194 #define NUM_SHIP_VERTICES 4
195 #define NUM_ENEMY_VERTICES 6
196 #define MAX_LEVEL MAX_NUM_ASTEROIDS
197 #define ENEMY_SPEED 4
198 #define ENEMY_START_X 0
199 #define ENEMY_START_Y 0
200 #define SIZE_ENEMY_COLLISION 5*SCALE
201 #define ATTRACT_FLIP_TIME 100
203 #define NUM_TRAIL_POINTS 70
204 #define NUM_ROTATIONS 16
206 #define SIN_COS_SCALE 10000
208 #define FAST_ROT_CW_SIN 873
209 #define FAST_ROT_CW_COS 9963
210 #define FAST_ROT_ACW_SIN -873
211 #define FAST_ROT_ACW_COS 9963
213 #define MEDIUM_ROT_CW_SIN 350
214 #define MEDIUM_ROT_CW_COS 9994
215 #define MEDIUM_ROT_ACW_SIN -350
216 #define MEDIUM_ROT_ACW_COS 9994
218 #define SLOW_ROT_CW_SIN 350
219 #define SLOW_ROT_CW_COS 9994
220 #define SLOW_ROT_ACW_SIN - 350
221 #define SLOW_ROT_ACW_COS 9994
223 #ifdef HAVE_LCD_COLOR
224 #define SHIP_ROT_CW_SIN 2419
225 #define SHIP_ROT_CW_COS 9702
226 #define SHIP_ROT_ACW_SIN -2419
227 #define SHIP_ROT_ACW_COS 9702
229 #define SHIP_ROT_CW_SIN 3827
230 #define SHIP_ROT_CW_COS 9239
231 #define SHIP_ROT_ACW_SIN -3827
232 #define SHIP_ROT_ACW_COS 9239
236 #define SCALED_WIDTH (LCD_WIDTH*SCALE)
237 #define SCALED_HEIGHT (LCD_HEIGHT*SCALE)
238 #define CENTER_LCD_X (LCD_WIDTH/2)
239 #define CENTER_LCD_Y (LCD_HEIGHT/2)
241 #define SHIP_EXPLOSION_COLOUR 1
242 #define ASTEROID_EXPLOSION_COLOUR 2
243 #define ENEMY_EXPLOSION_COLOUR 3
244 #define THRUST_COLOUR 4
246 #define ASTEROID_R 230
247 #define ASTEROID_G 200
248 #define ASTEROID_B 100
259 #ifdef HAVE_LCD_COLOR
260 #define COL_MISSILE LCD_RGBPACK(200,0,0)
261 #define COL_PLAYER LCD_RGBPACK(200,200,200)
262 #define COL_STARS LCD_WHITE
263 #define COL_ASTEROID LCD_RGBPACK(ASTEROID_R,ASTEROID_G,ASTEROID_B)
264 #define COL_TEXT LCD_RGBPACK(200,200,255)
265 #define COL_ENEMY LCD_RGBPACK(ENEMY_R,ENEMY_G,ENEMY_B)
266 #define SET_FG rb->lcd_set_foreground
267 #define SET_BG rb->lcd_set_background
273 /* The array of points that make up an asteroid */
274 static const short asteroid_one
[NUM_ASTEROID_VERTICES
*2] =
288 /* The array of points that make up an asteroid */
289 static const short asteroid_two
[NUM_ASTEROID_VERTICES
*2] =
303 /* The array of points that make up an asteroid */
304 static const short asteroid_three
[NUM_ASTEROID_VERTICES
*2] =
318 /* The array od points the make up the ship */
319 static const short ship_vertices
[NUM_SHIP_VERTICES
*2] =
334 /* The array of points the make up the bad spaceship */
335 static const short enemy_vertices
[NUM_ENEMY_VERTICES
*2] =
389 struct Point position
;
396 /* Asteroid structure, contains an array of points */
399 enum asteroid_type type
;
401 struct Point position
;
402 struct Point vertices
[NUM_ASTEROID_VERTICES
];
406 int explode_countdown
;
411 struct Point vertices
[NUM_SHIP_VERTICES
];
412 struct Point position
;
413 bool waiting_for_space
;
414 int explode_countdown
;
419 struct Point vertices
[NUM_ENEMY_VERTICES
];
420 struct Point position
;
421 int explode_countdown
;
422 long last_time_appeared
;
423 short size_probability
;
424 short appear_probability
;
430 struct Point position
;
431 struct Point oldpoint
;
435 static enum game_state game_state
;
436 static int asteroid_count
;
437 static int next_missile_count
;
438 static int next_thrust_count
;
439 static int num_lives
;
440 static int extra_life
;
441 static int show_level_timeout
;
442 static int attract_flip_timeout
;
443 static int show_game_over
;
444 static int current_level
;
445 static int current_score
;
446 static int high_score
;
447 static int space_check_size
= 30*SCALE
;
449 static bool enemy_on_screen
;
450 static char phscore
[30];
451 static struct Ship ship
;
452 static struct Point stars
[NUM_STARS
];
453 static struct Asteroid asteroids_array
[MAX_NUM_ASTEROIDS
];
454 static struct Missile missiles_array
[MAX_NUM_MISSILES
];
455 static struct Missile enemy_missile
;
456 static struct Enemy enemy
;
457 static struct Point lives_points
[NUM_SHIP_VERTICES
];
458 static struct TrailPoint trailPoints
[NUM_TRAIL_POINTS
];
460 void draw_and_move_asteroids(void);
461 void initialise_game(int nStartNum
);
463 bool is_asteroid_near_ship(struct Asteroid
* asteroid
);
464 bool is_point_within_asteroid(struct Asteroid
* asteroid
, struct Point
* point
);
466 void initialise_asteroid(struct Asteroid
* asteroid
, enum asteroid_type eType
);
467 void draw_polygon(struct Point
* vertices
, int px
, int py
, int num_vertices
);
468 void rotate_asteroid(struct Asteroid
* asteroid
);
469 void create_asteroid(enum asteroid_type type
, int x
, int y
);
470 void create_stars(void);
472 void initialise_ship(void);
473 void draw_and_move_ship(void);
474 void rotate_ship(int s
, int c
);
475 void thrust_ship(void);
477 void initialise_missile(struct Missile
* missile
);
478 void draw_and_move_missiles(void);
479 void fire_missile(void);
481 void animate_and_draw_explosion(struct Point
* point
, int num_points
, int xoffset
, int yoffset
);
482 void initialise_explosion(struct Point
* point
, int num_points
);
484 void move_point(struct Point
* point
);
485 void hyperspace(void);
486 void check_collisions(void);
487 void initialise_enemy(void);
488 void draw_and_move_enemy(void);
489 void draw_lives(void);
490 void drawstars(void);
491 bool is_ship_within_asteroid(struct Asteroid
* asteroid
);
495 /*Hi-Score reading and writing to file - this needs moving to the hi-score plugin lib as
502 /* clear the buffer we're about to load the highscore data into */
503 rb
->memset(phscore
, 0, sizeof(phscore
));
505 fd
= rb
->open(HISCORE_FILE
,O_RDWR
| O_CREAT
);
508 rb
->splash(HZ
, "Highscore file read error");
512 /* highscore used to %d, is now %d\n
513 Deal with no file or bad file */
514 rb
->read(fd
,phscore
, sizeof(phscore
));
516 compare
= rb
->atoi(phscore
);
518 if(high_score
> compare
)
520 rb
->lseek(fd
,0,SEEK_SET
);
521 rb
->fdprintf(fd
, "%d\n", high_score
);
524 high_score
= compare
;
529 bool point_in_poly(struct Point
* _point
, int num_vertices
, int x
, int y
)
538 pj
+= num_vertices
-1;
543 if((((pi
->y
<= y
) && (y
< pj
->y
)) || ((pj
->y
<= y
) && (y
< pi
->y
))) &&
544 (x
< (pj
->x
- pi
->x
) * (y
- pi
->y
) / (pj
->y
- pi
->y
) + pi
->x
))
547 if(n
== num_vertices
- 1)
558 void move_point(struct Point
* point
)
560 point
->x
+= point
->dx
;
561 point
->y
+= point
->dy
;
563 /*check bounds on the x-axis:*/
564 if(point
->x
>= SCALED_WIDTH
)
566 else if(point
->x
<= 0)
567 point
->x
= SCALED_WIDTH
;
569 /*Check bounds on the y-axis:*/
570 if(point
->y
>= SCALED_HEIGHT
)
572 else if(point
->y
<= 0)
573 point
->y
= SCALED_HEIGHT
;
576 void create_trail(struct TrailPoint
* tpoint
)
578 tpoint
->position
.dx
= -( ship
.vertices
[0].x
- ship
.vertices
[2].x
)/10;
579 tpoint
->position
.dy
= -( ship
.vertices
[0].y
- ship
.vertices
[2].y
)/10;
582 void create_explosion_trail(struct TrailPoint
* tpoint
)
584 tpoint
->position
.dx
= (rb
->rand()%5050)-2500;
585 tpoint
->position
.dy
= (rb
->rand()%5050)-2500;
588 void create_trail_blaze(int colour
, struct Point
* position
)
591 struct TrailPoint
* tpoint
;
594 if(colour
!= SHIP_EXPLOSION_COLOUR
)
596 numtoadd
= NUM_TRAIL_POINTS
/5;
602 numtoadd
= NUM_TRAIL_POINTS
/8;
603 xadd
= ship
.position
.x
;
604 yadd
= ship
.position
.y
;
607 /* give the point a random countdown timer, so they dissapears at different times */
608 tpoint
= trailPoints
;
609 n
= NUM_TRAIL_POINTS
;
612 if(tpoint
->alive
<= 0 && numtoadd
)
615 /* take a random x point anywhere between bottom two points of ship. */
616 /* ship.position.x; */
617 tpoint
->position
.x
= (ship
.vertices
[2].x
+ (rb
->rand()%18000)-9000) + position
->x
;
618 tpoint
->position
.y
= (ship
.vertices
[2].y
+ (rb
->rand()%18000)-9000) + position
->y
;
622 case SHIP_EXPLOSION_COLOUR
:
626 create_explosion_trail(tpoint
);
630 case ASTEROID_EXPLOSION_COLOUR
:
631 tpoint
->r
= ASTEROID_R
;
632 tpoint
->g
= ASTEROID_G
;
633 tpoint
->b
= ASTEROID_B
;
634 create_explosion_trail(tpoint
);
638 case ENEMY_EXPLOSION_COLOUR
:
642 create_explosion_trail(tpoint
);
647 tpoint
->r
= THRUST_R
;
648 tpoint
->g
= THRUST_G
;
649 tpoint
->b
= THRUST_B
;
650 create_trail(tpoint
);
655 /* add a proportional bit to the x and y based on dx and dy */
657 /* give the points a speed based on direction of travel - i.e. opposite */
658 tpoint
->position
.dx
+= position
->dx
;
659 tpoint
->position
.dy
+= position
->dy
;
665 /* find a space in the array of trail_points that is NULL or DEAD or whatever.
666 and place this one here. */
670 void draw_trail_blaze(void)
672 struct TrailPoint
* tpoint
;
673 /* loop through, if alive then move and draw.
674 when drawn, countdown it's timer.
676 tpoint
= trailPoints
;
677 int n
= NUM_TRAIL_POINTS
;
683 if(game_state
!= PAUSE_MODE
)
686 move_point(&(tpoint
->position
));
688 #ifdef HAVE_LCD_COLOR
689 /* intensity = tpoint->alive/2; */
690 if(tpoint
->r
>0)tpoint
->r
-=tpoint
->dec
;
691 if(tpoint
->g
>0)tpoint
->g
-=tpoint
->dec
;
692 if(tpoint
->b
>0)tpoint
->b
-=tpoint
->dec
;
693 SET_FG(LCD_RGBPACK(tpoint
->r
, tpoint
->g
, tpoint
->b
));
695 rb
->lcd_drawpixel(tpoint
->position
.x
/SCALE
, tpoint
->position
.y
/SCALE
);
701 /*Check if point is within a rectangle*/
702 bool is_point_within_rectangle(struct Point
* rect
, struct Point
* p
, int size
)
705 int aTLx
= rect
->x
- size
;
706 int aTLy
= rect
->y
- size
;
707 int aBRx
= rect
->x
+ size
;
708 int aBRy
= rect
->y
+ size
;
709 rb
->lcd_drawline( aTLx
/SCALE
, aTLy
/SCALE
, aBRx
/SCALE
, aTLy
/SCALE
);
710 rb
->lcd_drawline( aTLx
/SCALE
, aTLy
/SCALE
, aTLx
/SCALE
, aBRy
/SCALE
);
711 rb
->lcd_drawline( aTLx
/SCALE
, aBRy
/SCALE
, aBRx
/SCALE
, aBRy
/SCALE
);
712 rb
->lcd_drawline( aBRx
/SCALE
, aBRy
/SCALE
, aBRx
/SCALE
, aTLy
/SCALE
);
713 return (p
->x
> aTLx
&& p
->x
< aBRx
&& p
->y
> aTLy
&& p
->y
< aBRy
);
715 return (p
->x
> rect
->x
- size
&& p
->x
< rect
->x
+ size
&&
716 p
->y
> rect
->y
- size
&& p
->y
< rect
->y
+ size
);
721 void draw_polygon(struct Point
* vertices
, int px
, int py
, int num_vertices
)
723 int n
, t1
, t2
, oldX
, oldY
;
725 bool bDrawAll
= px
< WRAP_GAP
|| LCD_WIDTH
- px
< WRAP_GAP
||
726 py
< WRAP_GAP
|| LCD_HEIGHT
- py
< WRAP_GAP
;
730 oldX
= p
->x
/SCALE
+ px
;
731 oldY
= p
->y
/SCALE
+ py
;
733 for(n
= num_vertices
+1; --n
;)
735 t1
= p
->x
/SCALE
+ px
;
736 t2
= p
->y
/SCALE
+ py
;
738 rb
->lcd_drawline(oldX
, oldY
, t1
, t2
);
742 rb
->lcd_drawline(oldX
- LCD_WIDTH
, oldY
, t1
- LCD_WIDTH
, t2
);
743 rb
->lcd_drawline(oldX
+ LCD_WIDTH
, oldY
, t1
+ LCD_WIDTH
, t2
);
744 rb
->lcd_drawline(oldX
- LCD_WIDTH
, oldY
+ LCD_HEIGHT
,
745 t1
- LCD_WIDTH
, t2
+ LCD_HEIGHT
);
746 rb
->lcd_drawline(oldX
+ LCD_WIDTH
, oldY
+ LCD_HEIGHT
,
747 t1
+ LCD_WIDTH
, t2
+ LCD_HEIGHT
);
749 rb
->lcd_drawline(oldX
, oldY
- LCD_HEIGHT
, t1
, t2
- LCD_HEIGHT
);
750 rb
->lcd_drawline(oldX
, oldY
+ LCD_HEIGHT
, t1
, t2
+ LCD_HEIGHT
);
751 rb
->lcd_drawline(oldX
- LCD_WIDTH
, oldY
- LCD_HEIGHT
,
752 t1
- LCD_WIDTH
, t2
- LCD_HEIGHT
);
753 rb
->lcd_drawline(oldX
+ LCD_WIDTH
, oldY
- LCD_HEIGHT
,
754 t1
+ LCD_WIDTH
, t2
- LCD_HEIGHT
);
762 void animate_and_draw_explosion(struct Point
* point
, int num_points
,
763 int xoffset
, int yoffset
)
766 for(n
= num_points
; --n
;)
768 if(game_state
!= PAUSE_MODE
)
770 point
->x
+= point
->dx
;
771 point
->y
+= point
->dy
;
773 rb
->lcd_fillrect( point
->x
/SCALE
+ xoffset
, point
->y
/SCALE
+ yoffset
,
774 POINT_SIZE
, POINT_SIZE
);
779 /*stop movement of ship, 'cos that's what happens when you go into hyperspace.*/
780 void hyperspace(void)
782 ship
.position
.dx
= ship
.position
.dy
= 0;
783 ship
.position
.x
= (rb
->rand()%SCALED_WIDTH
);
784 ship
.position
.y
= (rb
->rand()%SCALED_HEIGHT
);
787 void initialise_enemy(void)
793 if(rb
->rand()%100 > enemy
.size_probability
)
796 enemy
.size_probability
++;
797 if(enemy
.size_probability
< 90)
799 enemy
.size_probability
= ENEMY_BIG_PROBABILITY_START
;
807 enemy_missile
.survived
= 0;
808 enemy_on_screen
= true;
809 enemy
.explode_countdown
= 0;
810 enemy
.last_time_appeared
= *rb
->current_tick
;
811 point
= enemy
.vertices
;
812 for(n
= 0; n
< NUM_ENEMY_VERTICES
+NUM_ENEMY_VERTICES
; n
+=2)
814 point
->x
= enemy_vertices
[n
];
815 point
->y
= enemy_vertices
[n
+1];
816 point
->x
*= SCALE
/size
;
817 point
->y
*= SCALE
/size
;
821 if(ship
.position
.x
>= SCALED_WIDTH
/2)
823 enemy
.position
.dx
= ENEMY_SPEED
;
824 enemy
.position
.x
= 0;
828 enemy
.position
.dx
= -ENEMY_SPEED
;
829 enemy
.position
.x
= SCALED_WIDTH
;
832 if(ship
.position
.y
>= SCALED_HEIGHT
/2)
834 enemy
.position
.dy
= ENEMY_SPEED
;
835 enemy
.position
.y
= 0;
839 enemy
.position
.dy
= -ENEMY_SPEED
;
840 enemy
.position
.y
= SCALED_HEIGHT
;
843 enemy
.position
.dx
*= SCALE
/10;
844 enemy
.position
.dy
*= SCALE
/10;
847 void draw_and_move_enemy(void)
849 int enemy_x
, enemy_y
;
856 enemy_x
= enemy
.position
.x
/SCALE
;
857 enemy_y
= enemy
.position
.y
/SCALE
;
858 if(!enemy
.explode_countdown
)
860 point
= enemy
.vertices
;
861 draw_polygon(enemy
.vertices
, enemy_x
, enemy_y
, NUM_ENEMY_VERTICES
);
862 rb
->lcd_drawline(enemy
.vertices
[0].x
/SCALE
+ enemy_x
,
863 enemy
.vertices
[0].y
/SCALE
+ enemy_y
,
864 enemy
.vertices
[3].x
/SCALE
+ enemy_x
,
865 enemy
.vertices
[3].y
/SCALE
+ enemy_y
);
867 if(game_state
!= PAUSE_MODE
)
869 enemy
.position
.x
+= enemy
.position
.dx
;
870 enemy
.position
.y
+= enemy
.position
.dy
;
873 if(enemy
.position
.x
> SCALED_WIDTH
|| enemy
.position
.x
< 0)
874 enemy_on_screen
= false;
876 if(enemy
.position
.y
> SCALED_HEIGHT
)
877 enemy
.position
.y
= 0;
878 else if(enemy
.position
.y
< 0)
879 enemy
.position
.y
= SCALED_HEIGHT
;
881 if( (rb
->rand()%1000) < 10)
882 enemy
.position
.dy
= -enemy
.position
.dy
;
887 /* animate_and_draw_explosion(enemy.vertices, NUM_ENEMY_VERTICES,
888 enemy_x, enemy.position.y/SCALE); */
889 if(game_state
!= PAUSE_MODE
)
891 enemy
.explode_countdown
--;
892 if(!enemy
.explode_countdown
)
893 enemy_on_screen
= false;
899 if( (*rb
->current_tick
- enemy
.last_time_appeared
) > enemy
.appear_timing
)
900 if(rb
->rand()%100 > enemy
.appear_probability
) initialise_enemy();
903 if(!enemy_missile
.survived
&& game_state
!= GAME_OVER
)
905 /*if no missile and the enemy is here and not exploding..then shoot baby!*/
906 if( !enemy
.explode_countdown
&& enemy_on_screen
&&
907 !ship
.waiting_for_space
&& (rb
->rand()%10) > 5 )
909 enemy_missile
.position
.x
= enemy
.position
.x
;
910 enemy_missile
.position
.y
= enemy
.position
.y
;
912 /*lame, needs to be sorted - it's trying to shoot at the ship*/
913 if(ABS(enemy
.position
.y
- ship
.position
.y
) <= 5*SCALE
)
915 enemy_missile
.position
.dy
= 0;
919 if( enemy
.position
.y
< ship
.position
.y
)
920 enemy_missile
.position
.dy
= 1;
922 enemy_missile
.position
.dy
= -1;
925 if(ABS(enemy
.position
.x
- ship
.position
.x
) <= 5*SCALE
)
926 enemy_missile
.position
.dx
= 0;
929 if( enemy
.position
.x
< ship
.position
.x
)
930 enemy_missile
.position
.dx
= 1;
932 enemy_missile
.position
.dx
= -1;
935 if(enemy_missile
.position
.dx
== 0 &&
936 enemy_missile
.position
.dy
== 0)
937 enemy_missile
.position
.dx
= enemy_missile
.position
.dy
= -1;
939 enemy_missile
.position
.dx
*= SCALE
;
940 enemy_missile
.position
.dy
*= SCALE
;
941 enemy_missile
.survived
= ENEMY_MISSILE_SURVIVAL_LENGTH
;
947 rb
->lcd_fillrect( enemy_missile
.position
.x
/SCALE
,
948 enemy_missile
.position
.y
/SCALE
,
949 POINT_SIZE
, POINT_SIZE
);
950 if(game_state
!= PAUSE_MODE
)
952 move_point(&enemy_missile
.position
);
953 enemy_missile
.survived
--;
959 * Lame method of collision
960 * detection. It's checking for collision
961 * between point and a big rectangle around the asteroid...
963 bool is_point_within_asteroid(struct Asteroid
* asteroid
, struct Point
* point
)
965 if( !is_point_within_rectangle(&asteroid
->position
, point
,
966 asteroid
->radius
+4*SCALE
) )
969 if(point_in_poly(asteroid
->vertices
, NUM_ASTEROID_VERTICES
,
970 point
->x
- asteroid
->position
.x
,
971 point
->y
- asteroid
->position
.y
))
973 switch(asteroid
->type
)
976 asteroid
->explode_countdown
= EXPLOSION_LENGTH
;
977 create_trail_blaze(ASTEROID_EXPLOSION_COLOUR
, &asteroid
->position
);
981 create_asteroid(MEDIUM
, asteroid
->position
.x
,
982 asteroid
->position
.y
);
983 create_asteroid(MEDIUM
, asteroid
->position
.x
,
984 asteroid
->position
.y
);
988 create_asteroid(SMALL
, asteroid
->position
.x
, asteroid
->position
.y
);
989 create_asteroid(SMALL
, asteroid
->position
.x
, asteroid
->position
.y
);
994 if(current_score
> extra_life
)
997 extra_life
= current_score
+EXTRA_LIFE
;
1000 asteroid
->exists
= false;
1007 bool is_point_within_enemy(struct Point
* point
)
1009 if( is_point_within_rectangle(&enemy
.position
, point
, 7*SCALE
) )
1012 /*enemy_missile.survived = 0;*/
1013 enemy
.explode_countdown
= EXPLOSION_LENGTH
;
1014 /* initialise_explosion(enemy.vertices, NUM_ENEMY_VERTICES); */
1015 create_trail_blaze(ENEMY_EXPLOSION_COLOUR
, &enemy
.position
);
1022 bool is_ship_within_asteroid(struct Asteroid
* asteroid
)
1027 p
.x
= ship
.position
.x
+ ship
.vertices
[0].x
;
1028 p
.y
= ship
.position
.y
+ ship
.vertices
[0].y
;
1029 hit
|= is_point_within_asteroid(asteroid
, &p
);
1033 p
.x
= ship
.position
.x
+ ship
.vertices
[1].x
;
1034 p
.y
= ship
.position
.y
+ ship
.vertices
[1].y
;
1035 hit
|= is_point_within_asteroid(asteroid
, &p
);
1038 p
.x
= ship
.position
.x
+ ship
.vertices
[3].x
;
1039 p
.y
= ship
.position
.y
+ ship
.vertices
[3].y
;
1040 hit
|= is_point_within_asteroid(asteroid
, &p
);
1047 void initialise_explosion(struct Point
* point
, int num_points
)
1051 point
->x
+= point
->dx
;
1052 point
->y
+= point
->dy
;
1053 for(n
= num_points
; --n
;)
1055 point
->dx
= point
->x
;
1056 point
->dy
= point
->y
;
1062 /* Check for collsions between the missiles and the asteroids and the ship */
1063 void check_collisions(void)
1066 bool asteroids_onscreen
= false;
1067 struct Missile
* missile
;
1068 struct Asteroid
* asteroid
;
1069 bool ship_cant_be_placed
= false;
1071 asteroid
= asteroids_array
;
1072 m
= MAX_NUM_ASTEROIDS
;
1075 /*if the asteroids exists then test missile collision:*/
1076 if(asteroid
->exists
)
1078 missile
= missiles_array
;
1079 n
= MAX_NUM_MISSILES
;
1082 /*if the missiles exists:*/
1083 if(missile
->survived
> 0)
1085 /*has the missile hit the asteroid?*/
1086 if(is_point_within_asteroid(asteroid
, &missile
->position
)
1087 || is_point_within_asteroid(asteroid
,
1088 &missile
->oldpoint
))
1090 missile
->survived
= 0;
1097 /*now check collision with ship:*/
1098 if(asteroid
->exists
&& !ship
.waiting_for_space
&& !ship
.explode_countdown
)
1100 if(is_ship_within_asteroid(asteroid
))
1103 ship
.explode_countdown
= EXPLOSION_LENGTH
;
1104 /* initialise_explosion(ship.vertices, NUM_SHIP_VERTICES); */
1105 create_trail_blaze(SHIP_EXPLOSION_COLOUR
, &ship
.position
);
1108 /*has the enemy missile blown something up?*/
1109 if(asteroid
->exists
&& enemy_missile
.survived
)
1111 if(is_point_within_asteroid(asteroid
, &enemy_missile
.position
))
1113 /*take that score back then:*/
1114 if(current_score
> 0) current_score
--;
1115 enemy_missile
.survived
= 0;
1118 /*if it still exists, check if ship is waiting for space:*/
1119 if(asteroid
->exists
&& ship
.waiting_for_space
)
1120 ship_cant_be_placed
|=
1121 is_point_within_rectangle(&ship
.position
,
1122 &asteroid
->position
,
1127 /*is an asteroid still exploding?*/
1128 if(asteroid
->explode_countdown
)
1129 asteroids_onscreen
= true;
1134 /*now check collision between ship and enemy*/
1135 if(enemy_on_screen
&& !ship
.waiting_for_space
&&
1136 !ship
.explode_countdown
&& !enemy
.explode_countdown
)
1138 /*has the enemy collided with the ship?*/
1139 if(is_point_within_enemy(&ship
.position
))
1141 ship
.explode_countdown
= EXPLOSION_LENGTH
;
1142 /* initialise_explosion(ship.vertices, NUM_SHIP_VERTICES); */
1143 create_trail_blaze(SHIP_EXPLOSION_COLOUR
, &ship
.position
);
1144 create_trail_blaze(ENEMY_EXPLOSION_COLOUR
, &enemy
.position
);
1147 /*Now see if the enemy has been shot at by the ships missiles:*/
1148 missile
= missiles_array
;
1149 n
= MAX_NUM_MISSILES
;
1152 if(missile
->survived
> 0 &&
1153 is_point_within_enemy(&missile
->position
))
1155 missile
->survived
= 0;
1162 /*test collision with enemy missile and ship:*/
1163 if(!ship_cant_be_placed
&& enemy_missile
.survived
> 0 &&
1164 point_in_poly(ship
.vertices
, NUM_SHIP_VERTICES
,
1165 enemy_missile
.position
.x
- ship
.position
.x
,
1166 enemy_missile
.position
.y
- ship
.position
.y
))
1168 ship
.explode_countdown
= EXPLOSION_LENGTH
;
1169 /* initialise_explosion(ship.vertices, NUM_SHIP_VERTICES); */
1170 create_trail_blaze(SHIP_EXPLOSION_COLOUR
, &ship
.position
);
1171 enemy_missile
.survived
= 0;
1172 enemy_missile
.position
.x
= enemy_missile
.position
.y
= 0;
1175 if(!ship_cant_be_placed
)
1176 ship
.waiting_for_space
= false;
1178 /*if all asteroids cleared then start again:*/
1179 if(asteroid_count
== 0 && !enemy_on_screen
&& !asteroids_onscreen
)
1182 game_state
= SHOW_LEVEL
;
1183 enemy
.appear_probability
+= 5;
1184 enemy
.appear_timing
-= 200;
1185 if( enemy
.appear_probability
> 100)
1186 enemy
.appear_probability
= ENEMY_APPEAR_PROBABILITY_START
;
1187 show_level_timeout
= SHOW_LEVEL_TIME
;
1191 /*************************************************
1192 ** Creates a new asteroid of the given 4type (size)
1193 ** and at the given location.
1194 *************************************************/
1195 void create_asteroid(enum asteroid_type type
, int x
, int y
)
1197 struct Asteroid
* asteroid
;
1200 asteroid
= asteroids_array
;
1201 n
= MAX_NUM_ASTEROIDS
;
1204 if(!asteroid
->exists
&& !asteroid
->explode_countdown
)
1206 initialise_asteroid(asteroid
, type
);
1207 asteroid
->position
.x
= x
;
1208 asteroid
->position
.y
= y
;
1215 /* Initialise a missile */
1216 void initialise_missile(struct Missile
* missile
)
1218 missile
->position
.x
= ship
.position
.x
+ ship
.vertices
[0].x
;
1219 missile
->position
.y
= ship
.position
.y
+ ship
.vertices
[0].y
;
1220 missile
->position
.dx
= (ship
.vertices
[0].x
- ship
.vertices
[2].x
)/2;
1221 missile
->position
.dy
= (ship
.vertices
[0].y
- ship
.vertices
[2].y
)/2;
1222 missile
->survived
= MISSILE_SURVIVAL_LENGTH
;
1223 missile
->oldpoint
.x
= missile
->position
.x
;
1224 missile
->oldpoint
.y
= missile
->position
.y
;
1227 /* Draw and Move all the missiles */
1228 void draw_and_move_missiles(void)
1234 struct Missile
* missile
;
1235 missile
= missiles_array
;
1237 SET_FG(COL_MISSILE
);
1239 n
= MAX_NUM_MISSILES
;
1242 if(missile
->survived
)
1244 if(missile
->position
.dx
> 0)
1246 if(missile
->position
.x
>= missile
->oldpoint
.x
)
1248 p1x
= missile
->oldpoint
.x
;
1249 p2x
= missile
->position
.x
;
1254 p2x
= missile
->position
.x
;
1259 if(missile
->oldpoint
.x
>= missile
->position
.x
)
1261 p1x
= missile
->oldpoint
.x
;
1262 p2x
= missile
->position
.x
;
1266 p1x
= missile
->oldpoint
.x
;
1271 if(missile
->position
.dy
> 0)
1273 if(missile
->position
.y
>= missile
->oldpoint
.y
)
1275 p1y
= missile
->oldpoint
.y
;
1276 p2y
= missile
->position
.y
;
1281 p2y
= missile
->position
.y
;
1286 if(missile
->oldpoint
.y
>= missile
->position
.y
)
1288 p1y
= missile
->oldpoint
.y
;
1289 p2y
= missile
->position
.y
;
1293 p1y
= missile
->oldpoint
.y
;
1298 rb
->lcd_drawline( p1x
/SCALE
, p1y
/SCALE
, p2x
/SCALE
, p2y
/SCALE
);
1300 if(game_state
!= PAUSE_MODE
)
1302 missile
->oldpoint
.x
= missile
->position
.x
;
1303 missile
->oldpoint
.y
= missile
->position
.y
;
1304 move_point(&missile
->position
);
1305 missile
->survived
--;
1312 void draw_lives(void)
1315 int px
= (LCD_WIDTH
- num_lives
*4 - 1);
1317 int py
= (LCD_HEIGHT
-6);
1319 int py
= (LCD_HEIGHT
-4);
1327 draw_polygon(lives_points
, px
, py
, NUM_SHIP_VERTICES
);
1336 /*Fire the next missile*/
1337 void fire_missile(void)
1340 struct Missile
* missile
;
1342 if(!ship
.explode_countdown
&& !ship
.waiting_for_space
)
1344 missile
= missiles_array
;
1345 n
= MAX_NUM_MISSILES
;
1348 if(!missile
->survived
)
1350 initialise_missile(missile
);
1358 /* Initialise the passed Asteroid */
1359 void initialise_asteroid(struct Asteroid
* asteroid
, enum asteroid_type type
)
1363 struct Point
* point
;
1364 asteroid
->exists
= true;
1365 asteroid
->type
= type
;
1366 asteroid
->explode_countdown
= 0;
1368 /*Set the radius of the asteroid:*/
1369 asteroid
->radius
= (int)type
*SCALE
;
1371 /*shall we move Clockwise and Fast*/
1372 if((rb
->rand()%100)>75)
1374 asteroid
->speed_cos
= FAST_ROT_CW_COS
;
1375 asteroid
->speed_sin
= FAST_ROT_CW_SIN
;
1377 else if((rb
->rand()%100)>75)
1379 asteroid
->speed_cos
= FAST_ROT_ACW_COS
;
1380 asteroid
->speed_sin
= FAST_ROT_ACW_SIN
;
1382 else if((rb
->rand()%100)>75)
1384 asteroid
->speed_cos
= SLOW_ROT_ACW_COS
;
1385 asteroid
->speed_sin
= SLOW_ROT_ACW_SIN
;
1389 asteroid
->speed_cos
= SLOW_ROT_CW_COS
;
1390 asteroid
->speed_sin
= SLOW_ROT_CW_SIN
;
1393 b
= (rb
->rand()%100)>66;
1394 b2
= (rb
->rand()%100)>66;
1395 point
= asteroid
->vertices
;
1396 for(n
= 0; n
< NUM_ASTEROID_VERTICES
*2; n
+=2)
1400 point
->x
= asteroid_one
[n
];
1401 point
->y
= asteroid_one
[n
+1];
1405 point
->x
= asteroid_two
[n
];
1406 point
->y
= asteroid_two
[n
+1];
1410 point
->x
= asteroid_three
[n
];
1411 point
->y
= asteroid_three
[n
+1];
1414 point
->x
*= asteroid
->radius
/6;
1415 point
->y
*= asteroid
->radius
/6;
1420 asteroid
->radius
+= 6*SCALE
;
1421 if(asteroid
->type
== SMALL
)
1422 asteroid
->radius
/= 3;/*2*/
1423 else if(asteroid
->type
== LARGE
)
1424 asteroid
->radius
+= 3*SCALE
;/*2*/
1428 /*Set the position randomly:*/
1429 asteroid
->position
.x
= (rb
->rand()%SCALED_WIDTH
);
1430 asteroid
->position
.y
= (rb
->rand()%SCALED_HEIGHT
);
1432 asteroid
->position
.dx
= 0;
1433 while(asteroid
->position
.dx
== 0)
1434 asteroid
->position
.dx
= (rb
->rand()%ASTEROID_SPEED
)-ASTEROID_SPEED
/2;
1436 asteroid
->position
.dy
= 0;
1437 while(asteroid
->position
.dy
== 0)
1438 asteroid
->position
.dy
= (rb
->rand()%ASTEROID_SPEED
)-ASTEROID_SPEED
/2;
1440 asteroid
->position
.dx
*= SCALE
/10;
1441 asteroid
->position
.dy
*= SCALE
/10;
1443 b
= is_point_within_rectangle(&ship
.position
, &asteroid
->position
,
1447 /*Now rotate the asteroid a bit, so they all look a bit different*/
1448 for(n
=(rb
->rand()%30) + 2;--n
;)
1449 rotate_asteroid(asteroid
);
1451 /*great, we've created an asteroid, don't forget to increment the total:*/
1455 /*Initialise the ship*/
1456 void initialise_ship(void)
1458 struct Point
* point
;
1459 struct Point
* lives_point
;
1462 ship
.position
.x
= CENTER_LCD_X
;
1463 ship
.position
.y
= CENTER_LCD_Y
;
1464 ship
.position
.x
*= SCALE
;
1465 ship
.position
.y
*= SCALE
;
1466 ship
.position
.dx
= ship
.position
.dy
= 0;
1468 point
= ship
.vertices
;
1469 lives_point
= lives_points
;
1470 for(n
= 0; n
< NUM_SHIP_VERTICES
*2; n
+=2)
1472 point
->x
= ship_vertices
[n
];
1473 point
->y
= ship_vertices
[n
+1];
1480 ship
.position
.dx
= 0;
1481 ship
.position
.dy
= 0;
1482 ship
.explode_countdown
= 0;
1484 /*grab a copy of the ships points for the lives display:*/
1485 point
= ship
.vertices
;
1486 lives_point
= lives_points
;
1487 for(n
= 0; n
< NUM_SHIP_VERTICES
*2; n
+=2)
1489 lives_point
->x
= point
->x
;
1490 lives_point
->y
= point
->y
;
1496 void rotate_asteroid(struct Asteroid
* asteroid
)
1498 struct Point
* point
;
1502 point
= asteroid
->vertices
;
1503 for(n
= NUM_ASTEROID_VERTICES
+1; --n
;)
1506 point
->x
= xtemp
*asteroid
->speed_cos
/SIN_COS_SCALE
-
1507 point
->y
*asteroid
->speed_sin
/SIN_COS_SCALE
;
1508 point
->y
= point
->y
*asteroid
->speed_cos
/SIN_COS_SCALE
+
1509 xtemp
*asteroid
->speed_sin
/SIN_COS_SCALE
;
1514 /*************************************************
1515 ** Draws the ship, moves the ship and creates a new
1516 ** one if it's finished exploding.
1517 **************************************************/
1518 void draw_and_move_ship(void)
1520 int nxoffset
= ship
.position
.x
/SCALE
;
1521 int nyoffset
= ship
.position
.y
/SCALE
;
1523 if(!ship
.explode_countdown
)
1525 if(!ship
.waiting_for_space
)
1527 draw_polygon(ship
.vertices
, nxoffset
, nyoffset
, NUM_SHIP_VERTICES
);
1528 if(game_state
!= PAUSE_MODE
&& game_state
!= GAME_OVER
)
1530 move_point(&ship
.position
);
1536 /* animate_and_draw_explosion(ship.vertices, NUM_SHIP_VERTICES,
1537 ship.position.x/SCALE,
1538 ship.position.y/SCALE); */
1539 if(game_state
!= PAUSE_MODE
)
1541 ship
.explode_countdown
--;
1542 if(!ship
.explode_countdown
)
1547 show_game_over
= SHOW_GAME_OVER_TIME
;
1548 game_state
= GAME_OVER
;
1553 ship
.waiting_for_space
= true;
1560 void thrust_ship(void)
1562 if(!ship
.waiting_for_space
)
1564 ship
.position
.dx
+= ( ship
.vertices
[0].x
- ship
.vertices
[2].x
)/20;
1565 ship
.position
.dy
+= ( ship
.vertices
[0].y
- ship
.vertices
[2].y
)/20;
1566 /*if dx and dy are below a certain threshold, then set 'em to 0
1567 but to do this we need to ascertain if the spacehip as moved on screen
1568 for more than a certain amount. */
1570 create_trail_blaze(THRUST_COLOUR
, &ship
.position
);
1574 /**************************************************
1575 ** Rotate the ship using the passed sin & cos values
1576 ***************************************************/
1577 void rotate_ship(int c
, int s
)
1579 struct Point
* point
;
1583 if(!ship
.waiting_for_space
&& !ship
.explode_countdown
)
1585 point
= ship
.vertices
;
1586 for(n
=NUM_SHIP_VERTICES
+1;--n
;)
1589 point
->x
= xtemp
*c
/SIN_COS_SCALE
- point
->y
*s
/SIN_COS_SCALE
;
1590 point
->y
= point
->y
*c
/SIN_COS_SCALE
+ xtemp
*s
/SIN_COS_SCALE
;
1606 rb
->lcd_drawpixel(p
->x
, p
->y
);
1611 /*************************************************
1612 ** Draw And Move all Asteroids
1613 *************************************************/
1614 void draw_and_move_asteroids(void)
1617 struct Asteroid
* asteroid
;
1619 asteroid
= asteroids_array
;
1620 SET_FG(COL_ASTEROID
);
1622 n
= MAX_NUM_ASTEROIDS
;
1625 if(game_state
!= PAUSE_MODE
)
1627 if(asteroid
->exists
)
1629 move_point(&asteroid
->position
);
1630 rotate_asteroid(asteroid
);
1631 draw_polygon(asteroid
->vertices
, asteroid
->position
.x
/SCALE
,
1632 asteroid
->position
.y
/SCALE
,
1633 NUM_ASTEROID_VERTICES
);
1635 else if(asteroid
->explode_countdown
)
1637 /* animate_and_draw_explosion(asteroid->vertices,
1638 NUM_ASTEROID_VERTICES,
1639 asteroid->position.x/SCALE,
1640 asteroid->position.y/SCALE); */
1641 asteroid
->explode_countdown
--;
1646 if(asteroid
->exists
)
1647 draw_polygon(asteroid
->vertices
,
1648 asteroid
->position
.x
/SCALE
,
1649 asteroid
->position
.y
/SCALE
,
1650 NUM_ASTEROID_VERTICES
);
1656 void create_stars(void)
1658 struct TrailPoint
* tpoint
;
1666 p
->x
= (rb
->rand()%LCD_WIDTH
);
1667 p
->y
= (rb
->rand()%LCD_HEIGHT
);
1672 /* give the point a random countdown timer, so they dissapears at different
1674 tpoint
= trailPoints
;
1675 n
= NUM_TRAIL_POINTS
;
1683 /*************************************************
1684 ** Creates start_num number of new asteroids of
1686 **************************************************/
1687 void initialise_game(int start_num
)
1690 asteroid_count
= next_missile_count
= next_thrust_count
= 0;
1691 struct Asteroid
* asteroid
;
1692 struct Missile
* missile
;
1693 extra_life
= EXTRA_LIFE
;
1696 enemy_on_screen
= 0;
1697 enemy_missile
.survived
= 0;
1700 asteroid
= asteroids_array
;
1701 n
= MAX_NUM_ASTEROIDS
;
1704 asteroid
->exists
= false;
1708 /*make some LARGE asteroids*/
1709 for(n
= 0; n
< start_num
; n
++)
1710 initialise_asteroid(&asteroids_array
[n
], LARGE
);
1712 /*ensure all missiles are out of action: */
1713 missile
= missiles_array
;
1714 n
= MAX_NUM_MISSILES
;
1717 missile
->survived
=0;
1722 void start_attract_mode(void)
1724 enemy
.appear_probability
= ENEMY_APPEAR_PROBABILITY_START
;
1725 enemy
.appear_timing
= ENEMY_APPEAR_TIMING_START
;
1727 num_lives
= START_LIVES
;
1729 attract_flip_timeout
= ATTRACT_FLIP_TIME
;
1730 game_state
= ATTRACT_MODE
;
1731 if(asteroid_count
< 3)
1732 initialise_game(current_level
);
1735 enum plugin_status
start_game(void)
1743 /*create stars once, and once only:*/
1750 /*game starts with at level 1
1752 start_attract_mode();
1757 end
= *rb
->current_tick
+ (CYCLETIME
* HZ
) / 1000;
1758 rb
->lcd_clear_display();
1763 if(attract_flip_timeout
< ATTRACT_FLIP_TIME
/2)
1765 rb
->lcd_putsxy(CENTER_LCD_X
- 39,
1766 CENTER_LCD_Y
+ CENTER_LCD_Y
/2 - 4,
1768 if(!attract_flip_timeout
)
1769 attract_flip_timeout
= ATTRACT_FLIP_TIME
;
1773 rb
->snprintf(s
, sizeof(s
), "Hi Score %d ", high_score
);
1774 rb
->lcd_putsxy(CENTER_LCD_X
- 30,
1775 CENTER_LCD_Y
+ CENTER_LCD_Y
/2 - 4, s
);
1777 attract_flip_timeout
--;
1781 rb
->lcd_putsxy(CENTER_LCD_X
- 25,
1782 CENTER_LCD_Y
+ CENTER_LCD_Y
/2 - 4, "Game Over");
1783 rb
->snprintf(s
, sizeof(s
), "score %d ", current_score
);
1784 rb
->lcd_putsxy(1,LCD_HEIGHT
-8, s
);
1787 start_attract_mode();
1791 rb
->snprintf(s
, sizeof(s
), "score %d ", current_score
);
1792 rb
->lcd_putsxy(1,LCD_HEIGHT
-8, s
);
1793 rb
->lcd_putsxy(CENTER_LCD_X
- 15,
1794 CENTER_LCD_Y
+ CENTER_LCD_Y
/2 - 4, "pause");
1795 draw_and_move_missiles();
1797 draw_and_move_ship();
1801 rb
->snprintf(s
, sizeof(s
), "score %d ", current_score
);
1802 rb
->lcd_putsxy(1,LCD_HEIGHT
-8, s
);
1803 draw_and_move_missiles();
1806 draw_and_move_ship();
1810 show_level_timeout
--;
1811 rb
->snprintf(s
, sizeof(s
), "score %d ", current_score
);
1812 rb
->lcd_putsxy(1,LCD_HEIGHT
-8, s
);
1813 rb
->snprintf(level
, sizeof(level
), "stage %d ", current_level
);
1814 rb
->lcd_putsxy(CENTER_LCD_X
- 20,
1815 CENTER_LCD_Y
+ CENTER_LCD_Y
/2 - 4, level
);
1816 draw_and_move_ship();
1818 if(!show_level_timeout
)
1820 initialise_game(current_level
);
1821 game_state
= PLAY_MODE
;
1828 draw_and_move_asteroids();
1829 draw_and_move_enemy();
1832 button
= rb
->button_get(false);
1834 #ifdef HAS_BUTTON_HOLD
1835 if (rb
->button_hold())
1836 game_state
= PAUSE_MODE
;
1842 if(game_state
== PLAY_MODE
)
1843 game_state
= PAUSE_MODE
;
1844 else if(game_state
== PAUSE_MODE
)
1845 game_state
= PLAY_MODE
;
1852 if(game_state
== ATTRACT_MODE
)
1854 else if(game_state
== GAME_OVER
)
1856 start_attract_mode();
1860 show_game_over
= SHOW_GAME_OVER_TIME
;
1861 game_state
= GAME_OVER
;
1865 case (AST_LEFT_REP
):
1867 if(game_state
== PLAY_MODE
|| game_state
== SHOW_LEVEL
)
1868 rotate_ship(SHIP_ROT_ACW_COS
, SHIP_ROT_ACW_SIN
);
1871 case (AST_RIGHT_REP
):
1873 if(game_state
== PLAY_MODE
|| game_state
== SHOW_LEVEL
)
1874 rotate_ship(SHIP_ROT_CW_COS
, SHIP_ROT_CW_SIN
);
1877 case (AST_THRUST_REP
):
1879 if((game_state
== PLAY_MODE
|| game_state
== SHOW_LEVEL
) && !next_thrust_count
)
1882 next_thrust_count
= 5;
1886 case (AST_HYPERSPACE
):
1887 if(game_state
== PLAY_MODE
)
1889 /*maybe shield if it gets too hard */
1892 case (AST_FIRE_REP
):
1894 if(game_state
== ATTRACT_MODE
)
1896 current_level
= START_LEVEL
;
1898 initialise_game(current_level
);
1899 show_level_timeout
= SHOW_LEVEL_TIME
;
1900 game_state
= PLAY_MODE
;
1902 else if(game_state
== PLAY_MODE
)
1904 if(!next_missile_count
)
1907 next_missile_count
= 10;
1910 else if(game_state
== PAUSE_MODE
)
1912 game_state
= PLAY_MODE
;
1917 if (rb
->default_event_handler(button
)==SYS_USB_CONNECTED
)
1918 return PLUGIN_USB_CONNECTED
;
1924 if(high_score
< current_score
)
1925 high_score
= current_score
;
1930 if(next_missile_count
)
1931 next_missile_count
--;
1933 if(next_thrust_count
)
1934 next_thrust_count
--;
1936 if (end
> *rb
->current_tick
)
1937 rb
->sleep(end
-*rb
->current_tick
);
1945 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
1947 enum plugin_status retval
;
1951 game_state
= ATTRACT_MODE
;
1954 rb
->lcd_set_backdrop(NULL
);
1956 /* universal font */
1957 rb
->lcd_setfont(FONT_SYSFIXED
);
1958 /* Turn off backlight timeout */
1959 backlight_force_on(rb
); /* backlight control in lib/helper.c */
1961 retval
= start_game();
1963 rb
->lcd_setfont(FONT_UI
);
1964 /* Turn on backlight timeout (revert to settings) */
1965 backlight_use_settings(rb
); /* backlight control in lib/helper.c */