home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / ReplicaNetFreewareV5_4.exe / data1.cab / Program_Executable_Files / Example4 / GameObject.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  2.4 KB  |  99 lines

  1. /* START_LICENSE_HEADER
  2.  
  3. Copyright (C) 2000 Martin Piper, original design and program code
  4. Copyright (C) 2001-2005 Replica Software
  5.  
  6. This program file is copyright (C) Replica Software and can only be used under license.
  7. For more information visit: http://www.replicanet.com/
  8. Or email: info@replicanet.com
  9.  
  10. END_LICENSE_HEADER */
  11. /*
  12. This file is the base class for any type of game object.
  13. When an object is allocated or deleted the GameDatabase::mGameObjects array tracks the list of allocated objects
  14. */
  15.  
  16. #include "Network.h"
  17. #include "GameObject.h"
  18. #include "GameDatabase.h"
  19. #include "RNReplicaNet/Inc/ReplicaObject.h"
  20.  
  21. using namespace RNReplicaNet;
  22.  
  23. GameObject::GameObject() : mDeleteMe(false) , mReplica(0)
  24. {
  25.     // Lock our own object list on delete
  26.     Network::mNetwork->LockObjects();
  27.  
  28.     GameDatabase::mGameObjects.push_back(this);
  29.  
  30.     // UnLock our own object list on delete
  31.     Network::mNetwork->UnLockObjects();
  32. };
  33.  
  34. GameObject::~GameObject()
  35. {
  36.     // Lock our own object list on delete
  37.     Network::mNetwork->LockObjects();
  38.  
  39.     std::vector<GameObject *>::iterator st,en;
  40.     st = GameDatabase::mGameObjects.begin();
  41.     en = GameDatabase::mGameObjects.end();
  42.     while (st != en)
  43.     {
  44.         GameObject *object = *st;
  45.         if (object == this)
  46.         {
  47.             GameDatabase::mGameObjects.erase(st);
  48.             break;
  49.         }
  50.         st++;
  51.     }
  52.  
  53.     // UnLock our own object list on delete
  54.     Network::mNetwork->UnLockObjects();
  55. };
  56.  
  57. void GameObject::PollIt(void)
  58. {
  59.     printf("Default PollIt()\n");
  60. };
  61.  
  62. void GameObject::Delete(void)
  63. {
  64.     mDeleteMe = true;
  65. }
  66.  
  67. /**
  68.  * This calculates a distance to another GameObject.
  69.  * If the distance cannot be calculated, since one or both objects do not know about position,
  70.  * then kReplicaObject_InfiniteDistance is returned.
  71.  * This works because every GameObject::GetPosition() is able to return the correct position (or false)
  72.  * from the allocated derived class.
  73.  * A good place to look is Camera.h to see how the classes are structured.
  74.  */
  75. float GameObject::CalculateDistanceToObject(GameObject *gameobject)
  76. {
  77.     if (gameobject)
  78.     {
  79.         D3DXVECTOR4 pos;
  80.         D3DXVECTOR4 otherpos;
  81.         bool ret = gameobject->GetPosition(otherpos);
  82.  
  83.         if (ret)
  84.         {
  85.             ret = GetPosition(pos);
  86.             if (ret)
  87.             {
  88.                 pos.x = otherpos.x - pos.x;
  89.                 pos.y = otherpos.y - pos.y;
  90.                 pos.z = otherpos.z - pos.z;
  91.                 return sqrtf( (pos.x*pos.x) + (pos.y*pos.y) + (pos.z*pos.z));
  92.             }
  93.         }
  94.     }
  95.  
  96.     return kReplicaObject_InfiniteDistance;
  97. }
  98.  
  99.