home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xinvaders / shot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  5.1 KB  |  251 lines

  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. /* shot.c - handle movement, etc. of the shots. */
  15.  
  16. #include "vaders.h"
  17.  
  18. extern int paused;
  19.  
  20. #define MAXSHOTS    1
  21. #define MAXVSHOTS    6
  22. #define SHOTSIZE    (8*scale)
  23.  
  24. typedef struct _ShotRec {
  25.     int x, y;        /* Location of this shot. */
  26. } ShotRec, *Shot;
  27.  
  28. ShotRec shots[MAXSHOTS], vshots[MAXVSHOTS];
  29.  
  30. XImage *vshot_image[2];
  31.  
  32. static int tick = 0;
  33.  
  34. int numshots;        /* Number of shots currently flying. */
  35. int numvshots;        /* Number of shots currently flying. */
  36.  
  37. static void PaintShot(shot, gc)
  38. Shot shot;
  39. GC gc;
  40. {
  41.     AddLine(shot->x, shot->y,
  42.         shot->x, shot->y + SHOTSIZE, gc);
  43. }
  44.  
  45. static void PaintVshot(vshot, gc)
  46. Shot vshot;
  47. GC gc;
  48. {
  49.   XPutImage(dpy, gamewindow, gc, vshot_image[tick],
  50.         0, 0, vshot->x, vshot->y, vshot_image[tick]->width, vshot_image[tick]->height);
  51. }
  52.  
  53.  
  54. void PaintAllShots()
  55. {
  56.     int i;
  57.     for (i=0 ; i<numshots ; i++)
  58.     PaintShot(shots + i, shotgc);
  59.     for (i=0 ; i<numvshots ; i++)
  60.     PaintVshot(vshots + i, vshotgc);
  61. }
  62.  
  63.  
  64. static void DestroyShot(i)
  65. int i;
  66. {
  67.     PaintShot(shots + i, backgc);
  68.     numshots--;
  69.     shots[i] = shots[numshots];
  70. }
  71.  
  72.  
  73. static void DestroyVshot(i)
  74. int i;
  75. {
  76.     PaintVshot(vshots + i, backgc);
  77.     numvshots--;
  78.     vshots[i] = vshots[numvshots];
  79. }
  80.  
  81. /*ARGSUSED*/
  82. void MoveShots(closure, id)
  83. Opaque closure;
  84. XtIntervalId id;
  85. {
  86.   int i, x, y, newy;
  87.   Shot shot;
  88.   if (closure != (Opaque) MoveShots) return;
  89.   if (!paused) {
  90.     if (numshots > 0)
  91.       shottimerid = XtAddTimeOut(shotwait, MoveShots, (Opaque) MoveShots);
  92.     else
  93.       shottimerid = NULL;
  94.     for (i=0 ; i<numshots ; i++) {
  95.       shot = shots + i;
  96.       newy = shot->y - SHOTSIZE/2;
  97.       x = shot->x;
  98.       y = shot->y;
  99.       if (ShotHitsVader(x, y)
  100.       || ShotHitsSpacer(x, y)
  101.       || ShotHitsBuilding(x, y)
  102.       || y < 0) {
  103.     DestroyShot(i);
  104.     i--;            /* Ensures we don't skip moving a shot. */
  105.       } else {
  106.     PaintShot(shot, backgc);
  107.     shot->y = newy;
  108.     PaintShot(shot, shotgc);
  109.       }
  110.     }
  111.   }
  112. }
  113.  
  114. Boolean VshotHitsShot(x, y)
  115. int x, y;
  116. {
  117.   int i, dx, dy;
  118.   Shot shot;
  119.  
  120.   for (i=0; i<numshots; i++) {
  121.     shot = shots + i;
  122.     dx = shot->x;
  123.     dy = shot->y;
  124.     if(dx >= x && dx < x+vshot_image[tick]->width
  125.        && dy >= y && dy < y+vshot_image[tick]->height) {
  126.       DestroyShot(i);
  127.       return TRUE;
  128.     }
  129.   }
  130.   return FALSE;
  131. }
  132.        
  133. /*ARGSUSED*/
  134. void MoveVshots(closure, id)
  135. Opaque closure;
  136. XtIntervalId id;
  137. {
  138.   int i, x, y, newy;
  139.   Shot vshot;
  140.  
  141.   if (closure != (Opaque) MoveVshots) return;
  142.   if (!paused) {
  143.     if (numvshots > 0)
  144.       vshottimerid = XtAddTimeOut(vshotwait, MoveVshots, (Opaque) MoveVshots);
  145.     else
  146.       vshottimerid = NULL;
  147.     for (i=0 ; i<numvshots ; i++) {
  148.       vshot = vshots + i;
  149.       newy = vshot->y + 2*scale;
  150.       x = vshot->x;
  151.       y = vshot->y;
  152.       if (y>gameheight ||
  153.       VshotHitsShot(x, y) ||
  154.       ShotHitsBase(x,y) ||
  155.       ShotHitsBuilding(x,y)) {
  156.     DestroyVshot(i);
  157.     i--;            /* Ensures we don't skip moving a shot. */
  158.       } else {
  159.     PaintVshot(vshot, backgc);
  160.     tick = tick ? 0 : 1;
  161.     vshot->y = newy;
  162.     PaintVshot(vshot, vshotgc);
  163.     tick = tick ? 0 : 1;
  164.       }
  165.     }
  166.     tick = tick ? 0 : 1;
  167.   }
  168. }
  169.  
  170.  
  171. void AddShot(x, y)
  172. int x, y;
  173. {
  174.     Shot shot;
  175.     if (numshots >= maxshots) return;
  176.     shot = shots + numshots;
  177.     numshots++;
  178.     shot->x = x;
  179.     shot->y = y-SHOTSIZE;
  180.     PaintShot(shot, shotgc);
  181.     if (shottimerid == NULL)
  182.         shottimerid = XtAddTimeOut(shotwait, MoveShots, (Opaque) MoveShots);
  183. }
  184.  
  185. void AddVshot(x, y)
  186. int x, y;
  187. {
  188.     Shot shot;
  189.     if (numvshots >= maxvshots) return;
  190.     shot = vshots + numvshots;
  191.     numvshots++;
  192.     shot->x = x;
  193.     shot->y = y;
  194.     PaintVshot(shot, vshotgc);
  195.     if (vshottimerid == NULL)
  196.       vshottimerid = XtAddTimeOut(shotwait, MoveVshots, (Opaque) MoveVshots);
  197. }
  198.  
  199. #include "sperma1.bit"
  200. #include "sperma2.bit"
  201. #include "spermb1.bit"
  202. #include "spermb2.bit"
  203.  
  204. int ReadVshotImages()
  205. {
  206.   vshot_image[0] = XCreateImage(dpy,
  207.                 DefaultVisual(dpy, DefaultScreen(dpy)),
  208.                 1,
  209.                 XYBitmap,
  210.                 0,
  211.                 (scale == 1) ? sperma1_bits : sperma2_bits,
  212.                 (scale == 1) ? sperma1_width : sperma2_width,
  213.                 (scale == 1) ? sperma1_height : sperma2_height,
  214.                 8, 0);
  215.   vshot_image[0]->bitmap_bit_order = LSBFirst;
  216.   vshot_image[0]->byte_order = LSBFirst;
  217.  
  218.   vshot_image[1] = XCreateImage(dpy,
  219.                 DefaultVisual(dpy, DefaultScreen(dpy)),
  220.                 1,
  221.                 XYBitmap,
  222.                 0,
  223.                 (scale == 1) ? spermb1_bits : spermb2_bits,
  224.                 (scale == 1) ? spermb1_width : spermb2_width,
  225.                 (scale == 1) ? spermb1_height : spermb2_height,
  226.                 8, 0);
  227.   vshot_image[1]->bitmap_bit_order = LSBFirst;
  228.   vshot_image[1]->byte_order = LSBFirst;
  229.  
  230.   return BitmapSuccess;
  231. }
  232.  
  233. InitShot()
  234. {
  235.     shottimerid = NULL;
  236.     numshots = 0;
  237.     vshottimerid = NULL;
  238.     numvshots = 0;
  239.     if( ReadVshotImages() != BitmapSuccess) {
  240.       fprintf(stderr, "Error reading vshot images.\n");
  241.       exit(20);
  242.     }
  243. }
  244.  
  245. void AddLine(fromx, fromy, tox, toy, gc)
  246. int fromx, fromy, tox, toy;
  247. GC gc;
  248. {
  249.     XDrawLine(dpy, gamewindow, gc, fromx, fromy, tox, toy);
  250. }
  251.