kick
/Users/morten/Programmering/cpp/kick/src/kick/scene/game_object.cpp
00001 //
00002 //  GameObject.cpp
00003 //  KickCPP
00004 //
00005 //  Created by Morten Nobel-Jørgensen on 5/21/13.
00006 //  Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved.
00007 //
00008 
00009 #include "kick/scene/game_object.h"
00010 #include "kick/scene/transform.h"
00011 #include "kick/scene/scene.h"
00012 
00013 using namespace std;
00014 
00015 namespace kick {
00016     
00017     GameObject::GameObject(const string &name, Scene* scene, int uniqueId)
00018     : mName(name), mScene{scene}, mUniqueId{uniqueId}, mComponents{}, componentListeners{}
00019     {
00020         mTransform = addComponent<Transform>();
00021     }
00022     
00023     GameObject::GameObject(const GameObject& other)
00024         : mName(other.mName), mComponents(other.mComponents), componentListeners(other.componentListeners){
00025     }
00026  
00027     GameObject::~GameObject(){
00028     }
00029     
00030     GameObject::GameObject(GameObject&& other)
00031     : mName(move(other.mName)), mComponents(move(other.mComponents)), componentListeners(move(other.componentListeners)){
00032         other.mDestroyed = true;
00033     }
00034     
00035     GameObject& GameObject::operator=(GameObject&& other) {
00036         if (this != &other)
00037         {
00038             mName = move(other.mName);
00039             mComponents = move(other.mComponents);
00040             componentListeners = move(other.componentListeners);
00041             other.mDestroyed = true;
00042         }
00043         return *this;
00044     }
00045     
00046     bool GameObject::destroyComponent(std::shared_ptr<Component> component){
00047         auto pos = find(mComponents.begin(), mComponents.end(), component);
00048         if (pos != mComponents.end()){
00049             component->deactivated();
00050             componentEvent.notifyListeners({component, ComponentUpdateStatus::Destroyed});
00051             mComponents.erase(pos);
00052             return true;
00053         }
00054         return false;
00055     }
00056     
00057     ComponentIter GameObject::begin() {
00058         return mComponents.begin();
00059     }
00060     
00061     ComponentIter GameObject::end() {
00062         return mComponents.end();
00063     }
00064     
00065     ConstComponentIter GameObject::begin() const{
00066         return mComponents.begin();
00067     }
00068     
00069     ConstComponentIter GameObject::end() const{
00070         return mComponents.end();
00071     }
00072     
00073     std::string GameObject::name() const {
00074         return mName;
00075     }
00076     
00077     void GameObject::setName(std::string str){
00078         mName = str;
00079     }
00080 
00081     int GameObject::layer() const {
00082         return mLayer;
00083     }
00084 
00085     void GameObject::setLayer(int layer) {
00086         if (GameObject::mLayer != layer){
00087             GameObject::mLayer = layer;
00088             for (auto c : mComponents){
00089                 componentEvent.notifyListeners({c, ComponentUpdateStatus::Updated});
00090             }
00091         }
00092     }
00093 
00094     int GameObject::uniqueId() {
00095         return mUniqueId;
00096     }
00097 };
 All Classes Functions Variables