home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / Book Demos in Pascal / SpriteEngine / SE Hunter & Evader 2 / SpriteHandlers.p < prev    next >
Encoding:
Text File  |  1995-04-04  |  4.0 KB  |  128 lines  |  [TEXT/PJMM]

  1. unit SpriteHandlers;
  2.  
  3. interface
  4.  
  5.     uses
  6. {$IFC UNDEFINED THINK_PASCAL}
  7.         Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
  8.         OSUtils, ToolUtils, OSEvents, 
  9. {$ENDC}
  10.         QDOffScreen, SpriteStructure, SpriteTools;
  11.  
  12.     procedure MoveSprite (theSprite: SpritePtr);
  13.     procedure HitSprite (theSprite: SpritePtr; anotherSprite: SpritePtr);
  14.     procedure InitSprites;
  15.  
  16. implementation
  17.  
  18.  
  19. (*** Custom handlers - application dependent ***)
  20.  
  21. {Edit this as necessary. It should always include the following three routines:}
  22.  
  23. {MoveSprite: move the sprite}
  24.  
  25. {HitSprite: handle collisions between two sprites}
  26.  
  27. {InitSprites: Load all faces and create initial sprites}
  28.  
  29.     var
  30.         firstFace, secondFace, thirdFace: GrafPtr;
  31.         gPlayerPosition: Point;
  32.  
  33.     procedure MoveSprite (theSprite: SpritePtr);
  34.         var
  35.             vector: Point;
  36.     begin
  37.         case theSprite^.kind of
  38.             playerSprite: 
  39.                 begin
  40.                     GetMouse(theSprite^.position);
  41.                     gPlayerPosition := theSprite^.position;
  42.                 end;
  43.             hunterSprite: 
  44.                 begin
  45.                     theSprite^.fixedPointPosition.h := theSprite^.fixedPointPosition.h + theSprite^.speed.h;
  46.                     theSprite^.fixedPointPosition.v := theSprite^.fixedPointPosition.v + theSprite^.speed.v;
  47.                     theSprite^.position.h := BSR(theSprite^.fixedPointPosition.h, 4);
  48.                     theSprite^.position.v := BSR(theSprite^.fixedPointPosition.v, 4);
  49.  
  50.                     vector.h := gPlayerPosition.h - theSprite^.position.h;
  51.                     vector.v := gPlayerPosition.v - theSprite^.position.v;
  52.  
  53.                     theSprite^.speed.h := vector.h div 8;
  54.                     theSprite^.speed.v := vector.v div 8;
  55.                 end;
  56.             evaderSprite: 
  57.                 begin
  58.                     theSprite^.fixedPointPosition.h := theSprite^.fixedPointPosition.h + theSprite^.speed.h;
  59.                     theSprite^.fixedPointPosition.v := theSprite^.fixedPointPosition.v + theSprite^.speed.v;
  60.                     theSprite^.position.h := BSR(theSprite^.fixedPointPosition.h, 4);
  61.                     theSprite^.position.v := BSR(theSprite^.fixedPointPosition.v, 4);
  62.  
  63.                     vector.h := gPlayerPosition.h - theSprite^.position.h;
  64.                     vector.v := gPlayerPosition.v - theSprite^.position.v;
  65.  
  66.                     theSprite^.speed.h := -vector.h div 8;
  67.                     theSprite^.speed.v := -vector.v div 8;
  68.  
  69. (* Keep this sprite a bit inside the bounds so it doesn't just stand in a corner. *)
  70.                     if theSprite^.position.h < 50 then
  71.                         begin
  72.                             theSprite^.position.h := 50;
  73.                             theSprite^.fixedPointPosition.h := BSL(theSprite^.position.h, 4);
  74.                         end;
  75.                     if theSprite^.position.h > gOffScreen^.portRect.right - theSprite^.face^.portRect.right - 50 then
  76.                         begin
  77.                             theSprite^.position.h := gOffScreen^.portRect.right - theSprite^.face^.portRect.right - 50;
  78.                             theSprite^.fixedPointPosition.h := BSL(theSprite^.position.h, 4);
  79.                         end;
  80.                     if theSprite^.position.v < 50 then
  81.                         begin
  82.                             theSprite^.position.v := 50;
  83.                             theSprite^.fixedPointPosition.v := BSL(theSprite^.position.v, 4);
  84.                         end;
  85.                     if theSprite^.position.v > gOffScreen^.portRect.bottom - theSprite^.face^.portRect.bottom - 50 then
  86.                         begin
  87.                             theSprite^.position.v := gOffScreen^.portRect.bottom - theSprite^.face^.portRect.bottom - 50;
  88.                             theSprite^.fixedPointPosition.v := BSL(theSprite^.position.v, 4);
  89.                         end;
  90.                 end; {case evaderSprite}
  91.         end; {case}
  92.  
  93.         if KeepOnScreen(theSprite) then
  94.             ;
  95.     end; (*MoveSprite*)
  96.  
  97.  
  98.     procedure HitSprite (theSprite: SpritePtr; anotherSprite: SpritePtr);
  99.     begin
  100.     end; (*HitSprite*)
  101.  
  102.     procedure InitSprites;
  103.         var
  104.             theSprite: SpritePtr;
  105.     begin
  106. (*Load all pictures*)
  107.         firstFace := LoadFaceFromCicn(128);            (*cicn resource #128.*)
  108.         secondFace := LoadFaceFromCicn(129);            (*cicn resource #129.*)
  109.         thirdFace := LoadFaceFromCicn(130);            (*cicn resource #130.*)
  110.  
  111. (*Create sprites*)
  112.         theSprite := NewSprite;
  113.         theSprite^.kind := hunterSprite;
  114.         theSprite^.face := firstFace;
  115.         SetPt(theSprite^.position, 100, 100);
  116.  
  117.         theSprite := NewSprite;
  118.         theSprite^.kind := playerSprite;
  119.         theSprite^.face := secondFace;
  120.         SetPt(theSprite^.position, 50, 50);
  121.  
  122.         theSprite := NewSprite;
  123.         theSprite^.kind := evaderSprite;
  124.         theSprite^.face := thirdFace;
  125.         SetPt(theSprite^.position, 150, 150);
  126.     end; (*InitSprites*)
  127.  
  128. end.