kick
|
00001 #include "transform.h" 00002 00003 namespace kick { 00004 typedef std::vector<std::unique_ptr<GameObject>>::const_iterator GameObjectIter; 00005 00006 template <typename C, typename... T> 00007 inline std::shared_ptr<C> GameObject::addComponent(T... t){ 00008 auto res = std::make_shared<C>(this, t...); 00009 mComponents.push_back(res); 00010 res->activated(); 00011 componentEvent.notifyListeners({res, ComponentUpdateStatus::Created}); 00012 return res; 00013 } 00014 00015 template <typename C> 00016 inline std::shared_ptr<C> GameObject::component(){ 00017 for (auto c : mComponents){ 00018 auto comp = std::dynamic_pointer_cast<C>(c); 00019 if (comp){ 00020 return comp; 00021 } 00022 } 00023 return nullptr; 00024 } 00025 00026 template <typename C> 00027 inline std::vector<std::shared_ptr<C>> GameObject::components(){ 00028 std::vector<std::shared_ptr<C>> res; 00029 for (auto c : mComponents){ 00030 std::shared_ptr<C> comp = std::dynamic_pointer_cast<C>(c); 00031 if (comp){ 00032 res.push_back(comp); 00033 } 00034 } 00035 return res; 00036 } 00037 00038 template <typename C> 00039 inline std::shared_ptr<C> GameObject::componentInParent(){ 00040 auto p = mTransform->parent(); 00041 while (p){ 00042 std::shared_ptr<C> c = p->gameObject()->component<C>(); 00043 if (c){ 00044 return c; 00045 } 00046 p = p->parent(); 00047 } 00048 return nullptr; 00049 } 00050 00051 template <typename C> 00052 inline std::vector<std::shared_ptr<C>> GameObject::componentsInParent(){ 00053 std::vector<std::shared_ptr<C>> res; 00054 auto p = mTransform->parent(); 00055 while (p){ 00056 std::vector<std::shared_ptr<C>> c = p->gameObject()->components<std::shared_ptr<C>>(); 00057 if (c.size()){ 00058 res.insert(res.begin(), c.begin(), c.end()); 00059 } 00060 p = p->parent(); 00061 } 00062 return res; 00063 } 00064 00065 template <typename C> 00066 inline std::shared_ptr<C> GameObject::componentInChildren(){ 00067 auto t = mTransform; 00068 std::vector<std::shared_ptr<Transform>> q{t->begin(), t->end()}; 00069 int index = 0; 00070 while (index < q.size()){ 00071 t = q[index]; 00072 auto c = t->gameObject()->component<C>(); 00073 if (c){ 00074 return c; 00075 } 00076 q.insert(q.end(), t->begin(), t->end()); 00077 index++; 00078 } 00079 return nullptr; 00080 } 00081 00082 template <typename C> 00083 inline std::vector<std::shared_ptr<C>> GameObject::componentsInChildren(){ 00084 std::vector<std::shared_ptr<C>> res; 00085 auto t = mTransform; 00086 std::vector<std::shared_ptr<Transform>> q{t->begin(), t->end()}; 00087 int index = 0; 00088 while (index < q.size()){ 00089 t = q[index]; 00090 std::shared_ptr<C> c = t->gameObject()->component<C>(); 00091 if (c){ 00092 res.push_back(c); 00093 } 00094 q.insert(q.end(), t->begin(), t->end()); 00095 index++; 00096 } 00097 return res; 00098 } 00099 00100 }