Created
October 10, 2014 16:44
-
-
Save jeremyjh/ce3a516728590059f7af to your computer and use it in GitHub Desktop.
simple cocs2d-x Ref integration to entityx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: cocos_ref.h | |
* Author: jeremy | |
* | |
* Created on June 28, 2014, 8:38 PM | |
*/ | |
#ifndef COCOS_REF_H | |
#define COCOS_REF_H | |
#include "entityx/entityx.h" | |
#include "entityx/Entity.h" | |
#include "cocos2d.h" | |
namespace entityx { | |
namespace cocos { | |
template <typename T> | |
struct CocosRef : entityx::Component<CocosRef<T>> { | |
CocosRef(T* ref) : val(ref) { | |
assert(ref); | |
val->retain(); | |
} | |
~CocosRef() { | |
if(val) {val->release();} | |
} | |
T* val; | |
T* operator -> () {return this->val;} | |
}; | |
typedef CocosRef<cocos2d::Ref> Ref; | |
typedef CocosRef<cocos2d::Node> Node; | |
typedef CocosRef<cocos2d::Sprite> Sprite; | |
typedef CocosRef<cocos2d::Event> Event; | |
typedef CocosRef<cocos2d::Touch> Touch; | |
typedef CocosRef<cocos2d::PhysicsBody> PhysicsBody; | |
}} | |
//the purpose of this specialization is to override | |
//the get method and dereference operator -> so that it returns the | |
//contained Cocos object (member val) rather than the container | |
namespace entityx { | |
template <typename C> | |
class ComponentHandle<cocos::CocosRef<C>> { | |
public: | |
ComponentHandle() : manager_(nullptr) {} | |
bool valid() const; | |
operator bool() const; | |
C* operator -> (); | |
const C* operator -> () const; | |
C* get(); | |
const C* get() const; | |
/** | |
* Remove the component from its entity and destroy it. | |
*/ | |
void remove(); | |
bool operator == (const ComponentHandle<C> &other) const { | |
return manager_ == other.manager_ && id_ == other.id_; | |
} | |
bool operator != (const ComponentHandle<C> &other) const { | |
return !(*this == other); | |
} | |
private: | |
friend class EntityManager; | |
ComponentHandle(EntityManager *manager, Entity::Id id) : | |
manager_(manager), id_(id) {} | |
ComponentHandle(const EntityManager *manager, Entity::Id id) : | |
manager_(const_cast<EntityManager*>(manager)), id_(id) {} | |
EntityManager *manager_; | |
Entity::Id id_; | |
}; | |
template <typename C> | |
inline C *ComponentHandle<cocos::CocosRef<C>>::operator -> () { | |
assert(valid()); | |
return manager_->get_component_ptr<cocos::CocosRef<C>>(id_)->val; | |
} | |
template <typename C> | |
inline const C *ComponentHandle<cocos::CocosRef<C>>::operator -> () const { | |
assert(valid()); | |
return manager_->get_component_ptr<cocos::CocosRef<C>>(id_)->val; | |
} | |
template <typename C> | |
inline ComponentHandle<cocos::CocosRef<C>>::operator bool() const { | |
return valid(); | |
} | |
template <typename C> | |
inline bool ComponentHandle<cocos::CocosRef<C>>::valid() const { | |
return manager_ && manager_->valid(id_) && manager_->has_component<cocos::CocosRef<C>>(id_); | |
} | |
template <typename C> | |
inline C* ComponentHandle<cocos::CocosRef<C>>::get() { | |
assert(valid()); | |
return manager_->get_component_ptr<cocos::CocosRef<C>>(id_)->val; | |
} | |
template <typename C> | |
inline const C* ComponentHandle<cocos::CocosRef<C>>::get() const { | |
assert(valid()); | |
return manager_->get_component_ptr<cocos::CocosRef<C>>(id_)->val; | |
} | |
template <typename C> | |
inline void ComponentHandle<cocos::CocosRef<C>>::remove() { | |
assert(valid()); | |
manager_->remove<cocos::CocosRef<C>>(id_); | |
} | |
} | |
#endif /* COCOS_REF_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment