home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Contributed / SpriteWorld / SpriteWorld Examples / Shark Attack / Sources & Headers / Level.c < prev    next >
Encoding:
Text File  |  2000-10-06  |  8.2 KB  |  334 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. // Level.c
  3. //
  4. // Created 10/7/96
  5. ///--------------------------------------------------------------------------------------
  6.  
  7. #include <Fonts.h>
  8. #include <TextUtils.h>
  9.  
  10. #include <SWIncludes.h>
  11. #include <SWGameUtils.h>
  12.  
  13. #include "Level.h"
  14. #include "SWSounds.h"
  15. #include "NewSprite.h"
  16. #include "Shark Attack.h"
  17. #include "SpriteMoveProcs.h"
  18. #include "Special Effects.h"
  19. #include "GlobalVariables.h"
  20.  
  21. #define kGetReadyTime            60        // How many frames to display "Get Ready" message
  22.  
  23. #define kMaxNumFishOnScreen        4        // Max number of fish and sharks that
  24. #define kMaxNumSharksOnScreen    1        // can be on the screen at once.
  25. #define kFishSpeed                3
  26. #define kSharkSpeed                4
  27.  
  28.  
  29.     // Stores whether the last fish was added on the left side of the screen or not.
  30. Boolean        gAddedOnLeftSide;
  31.  
  32.  
  33. ///--------------------------------------------------------------------------------------
  34. //  DoGetReadyMessage
  35. ///--------------------------------------------------------------------------------------
  36.  
  37. void    DoGetReadyMessage( void )
  38. {
  39.     unsigned long    junkTime;
  40.     
  41.     SWSetPortToWorkArea(gSpriteWorldP);
  42.     
  43.     ForeColor(blackColor);
  44.     PaintRect(&gSpriteWorldP->backRect);
  45.     
  46.     ForeColor(redColor);
  47.     DrawTextInWorkArea("\pGet Ready!", 48);
  48.     
  49.     WipeWindow(gSpriteWorldP);
  50.     Delay(90, &junkTime);
  51. }
  52.  
  53.  
  54. ///--------------------------------------------------------------------------------------
  55. //  DoNextLevelMessage
  56. ///--------------------------------------------------------------------------------------
  57.  
  58. void    DoNextLevelMessage( void )
  59. {
  60.     unsigned long    junkTime;
  61.     Str255            levelString;
  62.     
  63.     SWSetPortToWorkArea(gSpriteWorldP);
  64.     
  65.     ForeColor(blackColor);
  66.     PaintRect(&gSpriteWorldP->backRect);
  67.     
  68.     ForeColor(cyanColor);
  69.     DrawTextInWorkArea("\pLevel ", 48);
  70.     NumToString(gCurrentLevel, levelString);
  71.     ForeColor(cyanColor);
  72.     DrawString(levelString);
  73.     ForeColor(blackColor);
  74.     
  75.     WipeWindow(gSpriteWorldP);
  76.     Delay(120, &junkTime);
  77. }
  78.  
  79.  
  80. ///--------------------------------------------------------------------------------------
  81. //  DoGameOver
  82. ///--------------------------------------------------------------------------------------
  83.  
  84. void    DoGameOver( void )
  85. {
  86.     unsigned long    junkTime;
  87.     
  88.     SWSetPortToWorkArea(gSpriteWorldP);
  89.  
  90.     ForeColor(blackColor);
  91.     PaintRect(&gSpriteWorldP->backRect);
  92.     
  93.     ForeColor(blueColor);
  94.     DrawTextInWorkArea("\pGame Over", 64);
  95.     
  96.     WipeWindow(gSpriteWorldP);
  97.     PlaySound(kSharkDeadSnd, 2, kFindEmptyChannel);
  98.     Delay(120, &junkTime);
  99. }
  100.  
  101.  
  102. ///--------------------------------------------------------------------------------------
  103. //  DrawTextInWorkArea
  104. ///--------------------------------------------------------------------------------------
  105.  
  106. void    DrawTextInWorkArea(StringPtr textString, short fontSize)
  107. {
  108.     Rect    textRect;
  109.     
  110.     SWSetPortToWorkArea(gSpriteWorldP);
  111.     
  112.     TextFont(systemFont);
  113.     TextFace(bold);
  114.     TextSize(fontSize);
  115.     
  116.     textRect.top = 0;
  117.     textRect.left = 0;
  118.     textRect.bottom = 0;
  119.     textRect.right = StringWidth(textString);
  120.     CenterRect(&textRect, &gSpriteWorldP->backRect);
  121.     
  122.     MoveTo(textRect.left, textRect.top);
  123.     DrawString(textString);
  124.  
  125.     ForeColor(blackColor);
  126. }
  127.  
  128.  
  129. ///--------------------------------------------------------------------------------------
  130. //  SetUpLevel - prepare the animation
  131. ///--------------------------------------------------------------------------------------
  132.  
  133. void    SetUpLevel( void )
  134. {
  135.     gFishDelay = 0;
  136.     gSharkDelay = kMinNewSharkDelay;
  137.     
  138.     AddSubmarine();
  139. }
  140.  
  141.  
  142. ///--------------------------------------------------------------------------------------
  143. // RestartLevel - called when the submarine dies
  144. ///--------------------------------------------------------------------------------------
  145.  
  146. void    RestartLevel( void )
  147. {
  148.     RemoveAllSprites(gSpriteWorldP);
  149.     
  150.     DoGetReadyMessage();
  151.     
  152.     SetUpLevel();
  153.     SWUpdateSpriteWorld(gSpriteWorldP, false);
  154.     WipeWindow(gSpriteWorldP);
  155. }
  156.  
  157.  
  158. ///--------------------------------------------------------------------------------------
  159. // AdvanceLevel - called when it's time to go on to the next level
  160. ///--------------------------------------------------------------------------------------
  161.  
  162. void    AdvanceLevel( void )
  163. {
  164.     short    numFrames = 90;
  165.     
  166.         // Run animation for a little bit before stopping
  167.     while ( numFrames )
  168.     {
  169.         SWProcessSpriteWorld(gSpriteWorldP);
  170.         SWAnimateSpriteWorld(gSpriteWorldP);
  171.         UpdateKeys();
  172.         
  173.         if (gSpriteWorldP->frameHasOccurred)
  174.             numFrames--;
  175.     }
  176.  
  177.     RemoveAllSprites(gSpriteWorldP);
  178.     
  179.     gCurrentLevel++;
  180.     gNextLevelScore += kAdvanceLevelScore;    // Set the score for the next level
  181.     DoNextLevelMessage();
  182.     
  183.     SetUpLevel();
  184.     SWUpdateSpriteWorld(gSpriteWorldP, false);
  185.     WipeWindow(gSpriteWorldP);
  186. }
  187.             
  188.  
  189. ///--------------------------------------------------------------------------------------
  190. //  ProcessLevel - keep adding fish to the level
  191. ///--------------------------------------------------------------------------------------
  192.  
  193. void    ProcessLevel( void )
  194. {
  195.         // Don't add any fish if it's time to advance to the next level
  196.     if (gScore > gNextLevelScore)
  197.         return;
  198.     
  199.     if (gNumFishOnScreen < kMaxNumFishOnScreen)
  200.     {
  201.         if (gFishDelay > 0)
  202.         {
  203.             gFishDelay--;
  204.         }
  205.         else if (GetRandom(0, kFishRandomness) == 0)
  206.         {
  207.             AddFish();
  208.             gFishDelay = kMinNewFishDelay;
  209.         }
  210.     }
  211.     
  212.     
  213.     if (gNumSharksOnScreen < kMaxNumSharksOnScreen)
  214.     {
  215.         if (gSharkDelay > 0)
  216.         {
  217.             gSharkDelay--;
  218.         }
  219.         else if (GetRandom(0, kSharkRandomness) == 0)
  220.         {
  221.  
  222.             AddShark();
  223.             gSharkDelay = kMinNewSharkDelay;
  224.         }
  225.     }
  226. }
  227.  
  228.  
  229. ///--------------------------------------------------------------------------------------
  230. // AddSubmarine
  231. ///--------------------------------------------------------------------------------------
  232.  
  233. void    AddSubmarine( void )
  234. {
  235.     gSubCloneP = NewSubSprite();
  236.     
  237.     SWSetSpriteLocation(gSubCloneP, ((SubStructPtr)gSubCloneP)->horizPos, 
  238.             ((SubStructPtr)gSubCloneP)->vertPos);
  239. }
  240.  
  241.  
  242. ///--------------------------------------------------------------------------------------
  243. //  AddFish - add a fish to the animation
  244. ///--------------------------------------------------------------------------------------
  245.  
  246. void    AddFish( void )
  247. {
  248.     short        horizLoc, vertLoc;
  249.     short        addToLeftSide;
  250.     short        horizDelta;
  251.     SpritePtr    fishSpriteP;
  252.     
  253.     fishSpriteP = NewFishSprite();
  254.     
  255.         // Make the next fish come from the opposite side of the screen most of the time.
  256.     if (gAddedOnLeftSide)
  257.     {
  258.         if (GetRandom(0,30) == 0)
  259.             addToLeftSide = true;
  260.         else
  261.             addToLeftSide = false;
  262.     }
  263.     else
  264.     {
  265.         if (GetRandom(0,30) == 0)
  266.             addToLeftSide = false;
  267.         else
  268.             addToLeftSide = true;
  269.     }
  270.     
  271.     if (addToLeftSide)
  272.     {
  273.             // Add fish to left size
  274.         horizDelta = kFishSpeed;
  275.         horizLoc = -(fishSpriteP->destFrameRect.right - fishSpriteP->destFrameRect.left);
  276.         SWSetSpriteFrameRange(fishSpriteP, 0, 2);
  277.         SWSetCurrentFrameIndex(fishSpriteP, 0);
  278.         gAddedOnLeftSide = true;
  279.     }
  280.     else
  281.     {
  282.             // Add fish to right side
  283.         horizDelta = -kFishSpeed;
  284.         horizLoc = gSpriteWorldP->backRect.right;
  285.         SWSetSpriteFrameRange(fishSpriteP, 3, 5);
  286.         SWSetCurrentFrameIndex(fishSpriteP, 3);
  287.         gAddedOnLeftSide = false;
  288.     }
  289.     
  290.     vertLoc = GetRandom(0, gSpriteWorldP->backRect.bottom - 
  291.         (fishSpriteP->destFrameRect.bottom - fishSpriteP->destFrameRect.top));
  292.     
  293.     SWSetSpriteMoveDelta(fishSpriteP, horizDelta, 0);
  294.     SWSetSpriteLocation(fishSpriteP, horizLoc, vertLoc);
  295. }
  296.  
  297.  
  298. ///--------------------------------------------------------------------------------------
  299. //  AddShark - add a shark to the animation
  300. ///--------------------------------------------------------------------------------------
  301.  
  302. void    AddShark( void )
  303. {
  304.     short            horizPos, vertPos;
  305.     SpritePtr        sharkSpriteP;
  306.     SharkStructPtr    sharkStructP;
  307.     
  308.     sharkSpriteP = NewSharkSprite();
  309.     sharkStructP = (SharkStructPtr)sharkSpriteP;
  310.     
  311.         // Add shark on right or left side
  312.     if (GetRandom(0,1) == 0)
  313.     {
  314.         sharkStructP->horizDelta = -kSharkSpeed;
  315.         SWSetSpriteFrameRange(sharkSpriteP, 3, 5);
  316.         SWSetCurrentFrameIndex(sharkSpriteP, 3);
  317.         horizPos = gSpriteWorldP->backRect.right;
  318.     }
  319.     else
  320.     {
  321.         sharkStructP->horizDelta = kSharkSpeed;
  322.         SWSetSpriteFrameRange(sharkSpriteP, 0, 2);
  323.         SWSetCurrentFrameIndex(sharkSpriteP, 0);
  324.         horizPos = -(sharkSpriteP->destFrameRect.right - sharkSpriteP->destFrameRect.left);
  325.     }
  326.     
  327.         // Place shark at same vertical location as sub
  328.     vertPos = gSubCloneP->destFrameRect.top + kSubHeight/2 - kSharkHeight/2;
  329.     
  330.     sharkStructP->horizPos = horizPos;
  331.     sharkStructP->vertPos = vertPos;
  332.     SWSetSpriteLocation(sharkSpriteP, horizPos, vertPos);
  333. }
  334.