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 / SpriteHandlers.p < prev    next >
Encoding:
Text File  |  1995-04-04  |  3.3 KB  |  106 lines  |  [TEXT/MWPS]

  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.     begin
  35.     Case theSprite^.kind of    
  36.         playerSprite: begin
  37.             GetMouse(theSprite^.position);
  38.             gPlayerPosition := theSprite^.position;
  39.             end;
  40.         hunterSprite: begin
  41.             If  theSprite^.position.h < gPlayerPosition.h Then
  42.                 theSprite^.position.h := theSprite^.position.h +1
  43.             Else             If  theSprite^.position.h > gPlayerPosition.h Then
  44.                 theSprite^.position.h := theSprite^.position.h -1;
  45.             If  theSprite^.position.v < gPlayerPosition.v Then
  46.                 theSprite^.position.v := theSprite^.position.v +1
  47.             Else             If  theSprite^.position.v > gPlayerPosition.v Then
  48.                 theSprite^.position.v := theSprite^.position.v -1;
  49.             end;
  50.         evaderSprite: begin
  51.             If  theSprite^.position.h < gPlayerPosition.h Then
  52.                 theSprite^.position.h := theSprite^.position.h -1
  53.             Else             If  theSprite^.position.h > gPlayerPosition.h Then
  54.                 theSprite^.position.h := theSprite^.position.h +1;
  55.             If  theSprite^.position.v < gPlayerPosition.v Then
  56.                 theSprite^.position.v := theSprite^.position.v -1
  57.             Else             If  theSprite^.position.v > gPlayerPosition.v Then
  58.                 theSprite^.position.v := theSprite^.position.v +1;
  59.  
  60. (* Keep this sprite a bit inside the bounds so it doesn't just stand in a corner. *)
  61.             If  theSprite^.position.h < 50 Then
  62.                 theSprite^.position.h := 50;
  63.             If  theSprite^.position.h > gOffScreen^.portRect.right - theSprite^.face^.portRect.right - 50 Then
  64.                 theSprite^.position.h := gOffScreen^.portRect.right - theSprite^.face^.portRect.right - 50;
  65.             If  theSprite^.position.v < 50 Then
  66.                 theSprite^.position.v := 50;
  67.             If  theSprite^.position.v > gOffScreen^.portRect.bottom - theSprite^.face^.portRect.bottom - 50 Then
  68.                 theSprite^.position.v := gOffScreen^.portRect.bottom - theSprite^.face^.portRect.bottom - 50;
  69.             end;
  70.      End; {case}
  71.     
  72.     if KeepOnScreen(theSprite) then;
  73. end; (*MoveSprite*)
  74.  
  75.  
  76.     procedure HitSprite (theSprite: SpritePtr; anotherSprite: SpritePtr);
  77.     begin
  78.     end; (*HitSprite*)
  79.  
  80.     procedure InitSprites;
  81.         var
  82.             theSprite: SpritePtr;
  83.     begin
  84. (*Load all pictures*)
  85.         firstFace := LoadFaceFromCicn(128);            (*cicn resource #128.*)
  86.         secondFace := LoadFaceFromCicn(129);            (*cicn resource #129.*)
  87.         thirdFace := LoadFaceFromCicn(130);            (*cicn resource #130.*)
  88.  
  89. (*Create sprites*)
  90.     theSprite := NewSprite;
  91.     theSprite^.kind := hunterSprite;
  92.     theSprite^.face := firstFace;
  93.     SetPt(theSprite^.position, 100, 100);
  94.  
  95.     theSprite := NewSprite;
  96.     theSprite^.kind := playerSprite;
  97.     theSprite^.face := secondFace;
  98.     SetPt(theSprite^.position, 50, 50);
  99.  
  100.     theSprite := NewSprite;
  101.     theSprite^.kind := evaderSprite;
  102.     theSprite^.face := thirdFace;
  103.     SetPt(theSprite^.position, 150, 150);
  104.   end; (*InitSprites*)
  105.  
  106. end.