Guest User

Untitled

a guest
Oct 19th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.46 KB | None | 0 0
  1. /*
  2.  *
  3.  * Tehadon - Game Engine
  4.  *  Copyright (c) T-Team / Benedikt Eggers
  5.  *   Version 3.0 / August 2011
  6.  *   C++ / Try
  7.  *
  8. */
  9.  
  10. // ====================================================================
  11. // Includes
  12. #include "RegionComponent.h"
  13. // ====================================================================
  14.  
  15. namespace TGE
  16. {
  17.     namespace Gfx
  18.     {
  19.         // ====================================================================
  20.         // Constructor
  21.         RegionComponent::RegionComponent()
  22.         {
  23.             myBasicMapComponentList = new std::vector<BasicMapComponent*>();
  24.  
  25.             SetPosition(new Vector3());
  26.             SetMapComponentStatus(MapComponentStatus::COMPONENT_UNLOADED);
  27.         }
  28.         // ====================================================================
  29.  
  30.         // ====================================================================
  31.         /// Destructor
  32.         RegionComponent::~RegionComponent()
  33.         {
  34.             delete myBasicMapComponentList;
  35.         }
  36.         // ====================================================================
  37.  
  38.         // ====================================================================
  39.         // Load all properties
  40.         void RegionComponent::LoadProperties(TiXmlNode* Node, unsigned int Indent)
  41.         {
  42.             //Check if the Parent really exist
  43.             if(!Node)
  44.             {
  45.                 return;
  46.             }
  47.  
  48.             //
  49.             TiXmlNode* cChild;
  50.             TiXmlText* cText;
  51.  
  52.             int ElementType = Node->Type();
  53.            
  54.             std::string cName;
  55.             cName = "";
  56.  
  57.             //Switch throught all types
  58.             switch(ElementType)
  59.             {
  60.                 case TiXmlNode::NodeType::TINYXML_DOCUMENT:
  61.                     //printf( "Document" );
  62.                     break;
  63.  
  64.                 case TiXmlNode::NodeType::TINYXML_ELEMENT:
  65.                     cName = Node->Value();
  66.  
  67.                     LoadAttributes(Node->ToElement(), cName, Indent + 1);
  68.                     break;
  69.  
  70.                 case TiXmlNode::NodeType::TINYXML_COMMENT:
  71.                     //printf( "Unknown" );
  72.                     break;
  73.  
  74.                 case TiXmlNode::NodeType::TINYXML_UNKNOWN:
  75.                     //cText = Parent->ToText();
  76.                     //printf( "Text: [%s]", cText->Value() );
  77.                     break;
  78.  
  79.                 case TiXmlNode::NodeType::TINYXML_TEXT:
  80.                     break;
  81.  
  82.                 case TiXmlNode::NodeType::TINYXML_DECLARATION:
  83.                     //printf( "Declaration" );
  84.                     break;
  85.  
  86.                 default:
  87.                     break;
  88.             }
  89.  
  90.             // Call all childs
  91.             for ( cChild = Node->FirstChild(); cChild != 0; cChild = cChild->NextSibling())
  92.             {
  93.                 LoadProperties( cChild, Indent + 1 );
  94.             }
  95.         }
  96.  
  97.         // Load attribute
  98.         void RegionComponent::LoadAttributes(TiXmlElement* Element, std::string Name, unsigned int Indent)
  99.         {
  100.             // Check if there are any Attributes
  101.             if(!Element)
  102.             {
  103.                 return;
  104.             }
  105.  
  106.             TiXmlAttribute* cAttribute = Element->FirstAttribute();
  107.  
  108.             //Iterate thrwought all attributes
  109.             while(cAttribute)
  110.             {
  111.                 std::string cName = cAttribute->Name();
  112.                 std::string cValue = cAttribute->Value();
  113.  
  114.                 //Read the Settings
  115.                 if(Name == "Kind")
  116.                 {
  117.                     if(cName == "value")
  118.                     {
  119.                         myKind = cValue;
  120.                     }
  121.                 }
  122.                 else if(Name == "Name")
  123.                 {
  124.                     if(cName == "value")
  125.                     {
  126.                         SetName(cValue);
  127.                     }
  128.                 }
  129.                 else if(Name == "ID")
  130.                 {
  131.                     if(cName == "value")
  132.                     {
  133.                         SetID(atoi(cValue.c_str()));
  134.                     }
  135.                 }
  136.                 else if(Name == "Radius")
  137.                 {
  138.                     if(cName == "value")
  139.                     {
  140.                         myRadius = atof(cValue.c_str());
  141.                     }
  142.                 }
  143.                 else if(Name == "Position")
  144.                 {
  145.                     if(cName == "x")
  146.                     {
  147.                         Vector3* cVector = GetPosition();
  148.                         cVector->x = atof(cValue.c_str());
  149.                     }
  150.  
  151.                     if(cName == "y")
  152.                     {
  153.                         Vector3* cVector = GetPosition();
  154.                         cVector->y = atof(cValue.c_str());
  155.                     }
  156.  
  157.                     if(cName == "z")
  158.                     {
  159.                         Vector3* cVector = GetPosition();
  160.                         cVector->z = atof(cValue.c_str());
  161.                         SetPosition(cVector);
  162.                     }
  163.                 }
  164.                 //Get next attrubute
  165.                 cAttribute = cAttribute->Next();
  166.             }
  167.         }
  168.         // ====================================================================
  169.  
  170.         // ====================================================================
  171.         // Update-Queue-Commands
  172.         void RegionComponent::Update(Vector3* SourcePosition)
  173.         {
  174.             //If Regionkind = simpleradius
  175.             if(myKind == "simpleradius")
  176.             {
  177.                 //Calculate distance
  178.                 float cDistance = ((*SourcePosition) - (*GetPosition())).length();
  179.                 cDistance < 0 ? -cDistance : cDistance;
  180.  
  181.                 if(cDistance <= myRadius)
  182.                 {
  183.                     //If it isn't loaded, then load the Region
  184.                     if(GetMapComponentStatus() == MapComponentStatus::COMPONENT_UNLOADED)
  185.                     {
  186.                         //Load the Region
  187.                         SetMapComponentStatus(MapComponentStatus::COMPONENT_LOADING);
  188.  
  189.                         Load();
  190.                     }
  191.                 }
  192.                 else
  193.                 {
  194.                     //Load the Region
  195.                     SetMapComponentStatus(MapComponentStatus::COMPONENT_UNLOADING);
  196.  
  197.                     UnLoad();
  198.                 }
  199.             }
  200.         }
  201.  
  202.         void RegionComponent::Load()
  203.         {
  204.             std::vector<BasicMapComponent*>::iterator itr;
  205.             for ( itr = myBasicMapComponentList->begin(); itr < myBasicMapComponentList->end(); ++itr )
  206.             {
  207.                 //Load Sub-Component
  208.                 (*itr)->SetMapComponentStatus(MapComponentStatus::COMPONENT_LOADING);
  209.  
  210.                 (*itr)->Load();
  211.             }
  212.  
  213.             //Loading completet
  214.             SetMapComponentStatus(MapComponentStatus::COMPONENT_LOADED);
  215.         }
  216.  
  217.         void RegionComponent::UnLoad()
  218.         {
  219.             std::vector<BasicMapComponent*>::iterator itr;
  220.             for ( itr = myBasicMapComponentList->begin(); itr < myBasicMapComponentList->end(); ++itr )
  221.             {
  222.                 //Load Sub-Component
  223.                 (*itr)->SetMapComponentStatus(MapComponentStatus::COMPONENT_UNLOADING);
  224.  
  225.                 (*itr)->UnLoad();
  226.             }
  227.  
  228.             //UnLoading completet
  229.             SetMapComponentStatus(MapComponentStatus::COMPONENT_LOADED);
  230.         }
  231.         // ====================================================================
  232.     }
  233. }
Add Comment
Please, Sign In to add comment