1 #include "GameObject.h"
2 #include "GameObjectManager.h"
3 #include "Component.h"
4
5 #include "MemLeakDetect.h"
6
7
8 /**
9 * Base constructor
10 * @param id the GO id
11 */
12 GameObject::GameObject(const std::string& id) : mOID(id){}
13 //uncopyable
14 GameObject::GameObject(const GameObject& go){}
15
16 GameObject& GameObject::operator=(const GameObject& go)
17 {
18 return *this;
19 }
20
21 /**
22 * Base destructor
23 */
24 GameObject::~GameObject()
25 {
26 clearGOCs();
27 }
28 /**
29 * Get the string ID of this Game Object
30 * @return the std::string ID
31 */
32 const std::string& GameObject::getID()
33 {
34 return mOID;
35 }
36
37 /**
38 * set the string ID of this Game Object
39 * @param id std::string
40 * @throws IllegalParameterException if id is empty
41 */
42 void GameObject::setID(const std::string &id)
43 {
44 if(id == "")
45 {
46 throw new Ogre::InvalidParametersException((int)Ogre::Exception::ERR_ITEM_NOT_FOUND,"ID is empty","setID()","GameObject.cpp",41);
47 }
48 mOID = id;
49 }
50
51
52 /**
53 * Add a new component to this game object
54 * @param component the new component to be added
55 * @throws IllegalParameterException if component is NULL
56 */
57 void GameObject::addComponent(Component* component)
58 {
59 if(!component)
60 {
61 throw new Ogre::InvalidParametersException((int)Ogre::Exception::ERR_ITEM_NOT_FOUND,"component is NULL","addComponent()","GameObject.cpp",92);
62 }
63
64 mComponents[component->familyID()][component->getID()] = component;
65 }
66
67 /**
68 * Remove the component of the corresponding family
69 * @param componentFamily the family type
70 * @throws IllegalParameterException if componentFamily is empty
71 */
72 void GameObject::removeComponent(const std::string& componentFamily,const std::string& compID)
73 {
74 //removes the pointer to the component, not the actual component itself
75 //one has tocall delte explicitly to delete the actual data
76 Component* c = mComponents[componentFamily][compID];
77 if(!c)
78 {
79 throw new Ogre::InvalidParametersException((int)Ogre::Exception::ERR_ITEM_NOT_FOUND,"Component not found","removeComponent()","GameObject.cpp",110);
80 }
81 mComponents[componentFamily].erase(compID);
82 delete c;
83 }
84
85 /**
86 * clear and delete all Components
87 */
88 void GameObject::clearGOCs()
89 {
90 ComponentMap::iterator iter;
91 for(iter = mComponents.begin(); iter != mComponents.end(); iter++) {
92 std::map<const std::string, Component*>::iterator iter2 = (iter->second).begin();
93 for(;iter2 != (iter->second).end(); iter2++)
94 {
95 delete iter2->second;
96 }
97 (iter->second).clear();
98 }
99 mComponents.clear();
100 }
101