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 / Enemy.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  4.0 KB  |  148 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 object have corresponding ROL files in the ROLFiles folder. The ROL files are compiled by the ROLCompiler.
  13. The CompiledGameObjects folder contains the compiled ROL definition file for this object. The member variables listed
  14. in the ROL file for this class are automatically updated when this object is allocated on another machine
  15. */
  16. #include "Enemy.h"
  17. #include "Projectile.h"
  18. #include "Camera.h"
  19. #include "RNReplicaNet/Inc/DataBlock.h"
  20. #include "Network.h"
  21. #include "GameDatabase.h"
  22.  
  23. using namespace RNReplicaNet;
  24.  
  25. void Enemy::SpawnEnemy(void)
  26. {
  27.     Enemy *enemy;
  28.     enemy = new Enemy();
  29.     enemy->SetPosition(D3DXVECTOR4(100.0f*float(rand())/float(RAND_MAX),0,100.0f*float(rand())/float(RAND_MAX),0));
  30.  
  31.     enemy->SetSpeed((10.0f*float(rand())/float(RAND_MAX))+0.2f);
  32.     enemy->SetRotation(D3DXVECTOR4(0,D3DXToRadian(360.0f*float(rand())/float(RAND_MAX)),0,0));
  33.  
  34.     enemy->Publish();
  35. }
  36.  
  37. Enemy::Enemy()
  38. {
  39.     SetReplicaObject(this);        // Setup our forward reference pointer
  40.     mPosition = D3DXVECTOR4(0,0,0,0);
  41.     mRotation = D3DXVECTOR4(0,0,0,0);
  42.  
  43.     mSpeed = 0.0f;
  44. }
  45.  
  46. Enemy::~Enemy()
  47. {
  48.     if (IsMaster())
  49.     {
  50.         SpawnEnemy();
  51.     }
  52. }
  53.  
  54. void Enemy::PollIt(void)
  55. {
  56.     // Only run the "physics" if we are a master object
  57.     if (!IsMaster())
  58.     {
  59.         return;
  60.     }
  61.  
  62.     int i;
  63.     for (i=0;i<(int)GameDatabase::mGameObjects.size();i++)
  64.     {
  65.         ReplicaObject *robject;
  66.         // Get the derived class object pointer that contains a base class type of ReplicaObject from the GameObject class
  67.         robject = GameDatabase::mGameObjects[i]->GetReplicaObject();
  68.         // If there is no object pointer then ignore this object
  69.         if (!robject)
  70.         {
  71.             continue;
  72.         }
  73.         // Compare the class id of the ReplicaObject with the class ID of the ReplicaObject Projectile object
  74.         // If they are the same then we can assume the type cast to a Plane class is valid
  75.         if (robject->GetClassID() == _MAKE_RO(Projectile)::StaticGetClassID())
  76.         {
  77.             D3DXVECTOR4 diff;
  78.             Projectile *proj = (Projectile *) robject;
  79.             diff = GetPosition() - proj->GetPosition();
  80.             float length = D3DXVec4Length(&diff);
  81.             if (length < 10.0f)
  82.             {
  83.                 Delete();
  84. //                proj->Delete();
  85.                 return;
  86.             }
  87.         }
  88.     }
  89.  
  90.     if (mSpeed > 0.1f)
  91.     {
  92.         D3DXVECTOR4 velocity = D3DXVECTOR4(0,0,-mSpeed,0);
  93.         mRotation.y += D3DXToRadian(1.0f) * GameDatabase::mGameFrameTick;
  94.  
  95.         // Calculate the velocity movement
  96.         D3DXMATRIX matrix;
  97.         D3DXMatrixRotationYawPitchRoll(&matrix,mRotation.y,mRotation.x,mRotation.z);
  98.         D3DXVECTOR4 trans;
  99.         D3DXVec4Transform(&trans,&velocity,&matrix);
  100.  
  101.         mPosition += trans * GameDatabase::mGameFrameTick;
  102.         GiveDeltaHint(mPosition.x,trans.x);
  103.         GiveDeltaHint(mPosition.y,trans.y);
  104.         GiveDeltaHint(mPosition.z,trans.z);
  105.  
  106. //        mRotation.y += 0.5f * GameDatabase::mGameFrameTick;
  107. //        GiveDeltaHint(mRotation.y,0.5f * GameDatabase::mGameFrameTick);
  108.     }
  109.  
  110. #define LIMIT (140.0f)
  111.  
  112.     if (mPosition.x < -LIMIT)
  113.     {
  114.         mPosition.x = 0;
  115.         ContinuityBreak(mPosition.x,DataBlock::ContinuityBreakTypes::kTeleport);
  116.         GiveDeltaHint(mPosition.x,0);
  117.     }
  118.     if (mPosition.z < -LIMIT)
  119.     {
  120.         mPosition.z = 0;
  121.         ContinuityBreak(mPosition.z,DataBlock::ContinuityBreakTypes::kTeleport);
  122.         GiveDeltaHint(mPosition.z,0);
  123.     }
  124.     if (mPosition.x > LIMIT)
  125.     {
  126.         mPosition.x = 0;
  127.         ContinuityBreak(mPosition.x,DataBlock::ContinuityBreakTypes::kTeleport);
  128.         GiveDeltaHint(mPosition.x,0);
  129.     }
  130.     if (mPosition.z > LIMIT)
  131.     {
  132.         mPosition.z = 0;
  133.         ContinuityBreak(mPosition.z,DataBlock::ContinuityBreakTypes::kTeleport);
  134.         GiveDeltaHint(mPosition.z,0);
  135.     }
  136.     if (mPosition.y < -6.0f)
  137.     {
  138.         mPosition.y = -6.0f;
  139.         GiveDeltaHint(mPosition.y,0);
  140.     }
  141.     if (mPosition.y > 500.0f)
  142.     {
  143.         mPosition.y = 500.0f;
  144.         GiveDeltaHint(mPosition.y,0);
  145.     }
  146. }
  147.  
  148.