1 /*
2 Copyright notice:
3
4 This is mine. I'm only letting you use it. Period. Feel free to rip off
5 any of the code you see fit, but have the courtesy to give me credit.
6 Otherwise great hairy beasties will rip your eyes out and eat your flesh
7 when you least expect it.
8
9 Jonny Goldman <jonathan@think.com>
10
11 Wed May 8 1991
12 */
13
14 /* spacers.c - handle movement, etc. of the little space ships. */
15
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include "vaders.h"
21
22 Boolean spacer_shown;
23
24 #define SPACERY 0
25 #define SPACERV 2*scale
26
27 typedef struct _SpacerRec {
28 int x; /* Location. */
29 int width, height; /* box of this rock. */
30 int score; /* value of this guy */
31 XImage *shape_image; /* an XImage for the spaceship */
32 } SpacerRec, *Spacer;
33
34 SpacerRec spacerrec;
35
36 Spacer spacer = &spacerrec;
37
38 #define SpacerNearPoint(spacer, x, y) \
39 ((spacer)->x <= (x) && (x) < (spacer)->x + (spacer)->width && \
40 y <= SPACERY + (spacer)->height && y > SPACERY)
41
42 int spacer_counter; /* number of steps to wait for new spacer */
43
44 int showing_sexplosion = FALSE;
45
46 /* now the code */
47
PaintSpacer(gc)48 void PaintSpacer(gc)
49 GC gc;
50 {
51 XPutImage(dpy, gamewindow, gc, spacer->shape_image,
52 0, 0, spacer->x, SPACERY, spacer->width, spacer->height);
53 }
54
55
ShowSexplosion(gc)56 void ShowSexplosion(gc)
57 GC gc;
58 {
59 char score[5];
60
61 sprintf(score,"%3d", spacer->score);
62 XDrawString(dpy, gamewindow, gc, spacer->x, SPACERY+spacer->height, score, 3);
63
64 }
65
66 /*
67 * Destroy the Spacer, much like the ship.
68 */
69
DestroySpacer()70 static void DestroySpacer()
71 {
72 score += spacer->score;
73 PaintScore();
74
75 if(!paused) {
76 PaintSpacer(backgc);
77 ShowSexplosion(spacergc);
78 if (spacertimerid)
79 XtRemoveTimeOut(spacertimerid);
80 XtAddTimeOut(1000, MoveSpacer, (Opaque) MoveSpacer);
81 showing_sexplosion = TRUE;
82 spacer_shown = FALSE;
83 }
84 }
85
ShotHitsSpacer(x,y)86 Boolean ShotHitsSpacer(x, y)
87 int x, y;
88 {
89 if(spacer_shown) {
90 if (SpacerNearPoint(spacer, x, y)) {
91 DestroySpacer();
92 return TRUE;
93 }
94 }
95 return FALSE;
96 }
97
98
99
MakeSpacer()100 void MakeSpacer()
101 {
102 spacer_shown = TRUE;
103
104 spacer->x=0;
105 spacer->score = 50*(random()%6+1);
106 PaintSpacer(spacergc);
107 }
108
109 /*ARGSUSED*/
MoveSpacer(closure,id)110 void MoveSpacer(closure, id)
111 Opaque closure;
112 XtIntervalId id;
113 {
114 if (closure != (Opaque) MoveSpacer) return;
115 spacertimerid = XtAddTimeOut(spacerwait, MoveSpacer, (Opaque) MoveSpacer);
116 if (!paused) {
117 if (showing_sexplosion) {
118 showing_sexplosion = FALSE;
119 ShowSexplosion(backgc);
120 spacer_shown = FALSE;
121 spacer_counter = 1000;
122 return;
123 }
124 if (spacer_shown) {
125 PaintSpacer(backgc);
126 spacer->x += SPACERV;
127 if (spacer->x < gamewidth-spacer->width) {
128 PaintSpacer(spacergc);
129 } else {
130 spacer_shown = FALSE;
131 spacer_counter = 1000;
132 }
133 } else
134 if (spacer_counter-- == 0) MakeSpacer();
135 }
136 }
137
138
139 #include "spacer1.bit"
140 #include "spacer2.bit"
141
ReadSpacerImages()142 int ReadSpacerImages()
143 {
144 spacer->width = (scale == 1) ? spacer1_width : spacer2_width;
145 spacer->height = (scale == 1) ? spacer1_height : spacer2_height;
146
147 spacer->shape_image = XCreateImage(dpy,
148 DefaultVisual(dpy, DefaultScreen(dpy)),
149 1,
150 XYBitmap,
151 0,
152 (scale == 1) ? spacer1_bits : spacer2_bits,
153 spacer->width, spacer->height,
154 8, 0);
155
156 spacer->shape_image->bitmap_bit_order = LSBFirst;
157 spacer->shape_image->byte_order = LSBFirst;
158
159 return BitmapSuccess;
160 }
161
InitSpacers()162 void InitSpacers()
163 {
164 if(ReadSpacerImages()!= BitmapSuccess) {
165 fprintf(stderr, _("Error reading Spacer image\n"));
166 exit(10);
167 }
168
169 spacertimerid = 0;
170 }
171