home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 09 Laramée / WorldGrid.h < prev   
Encoding:
C/C++ Source or Header  |  2001-08-22  |  1.9 KB  |  66 lines

  1. /***************************************************************
  2.  * Class WORLDGRID
  3.  * A representation of the world as a wrapping grid of squares
  4.  * that can be occupied by Entities.  Could easily be implemented
  5.  * as a Singleton (cf: Design Patterns by Gamma et al.)
  6.  **************************************************************/
  7.  
  8. #ifndef WORLDGRID_H
  9. #define WORLDGRID_H
  10.  
  11. #include "Globals.h"
  12. #include <stdlib.h>
  13.  
  14.  
  15. class WorldGrid
  16. {
  17.     // We store the troll's position 
  18.     // explicitly because many entities' behavior depends on it
  19.     int TrollX, TrollY;
  20.  
  21.     // A registry of world entities by location
  22.     struct WGRegistryEntry
  23.     {
  24.         int type;
  25.         int x;
  26.         int y;
  27.     };
  28.  
  29.     WGRegistryEntry Registry[ MAX_ENTITIES ];
  30.  
  31. public:
  32.     // Construction and access
  33.     WorldGrid();
  34.     WorldGrid( int x, int y );
  35.     void Copy( WorldGrid * src );
  36.     void Register( int id, int type, int x, int y );
  37.     void UnRegister( int id ) { Registry[ id ].type = ENTITY_NULL; }
  38.     int GetEntityX( int id ) { return Registry[ id ].x; }
  39.     int GetEntityY( int id ) { return Registry[ id ].y; }
  40.     int GetTrollX() { return TrollX; }
  41.     int GetTrollY() { return TrollY; }
  42.  
  43.     // Keep track of movement
  44.     void MoveTroll( int newx, int newy );
  45.     void MoveEntity( int id, int newx, int newy );
  46.  
  47.     // Check whether the troll is close enough to trigger some activity
  48.     // in the object associated with registry entry "id"
  49.     bool IsTrollWithinRange( int id );
  50.  
  51.     // Manhattan distance relationships between troll and entities
  52.     int DistanceFromTroll( int id ) 
  53.         { return( abs( Registry[ id ].x - TrollX ) + abs( Registry[ id ].y - TrollY ) ); }
  54.  
  55.     // Look for the object of class "type" closest to the troll
  56.     int FindClosestFromTroll( int type );
  57.  
  58.     // Look for objects of a certain class within a certain distance of the troll
  59.     int HowManyCloseToTroll( int type, int maxdist );
  60.  
  61.     // Debugging help
  62.     void Dump();
  63. };
  64.  
  65.  
  66. #endif