This commit is contained in:
chengstory 2013-11-14 13:28:42 +08:00
parent f528126d5a
commit 14d51f9f35
9 changed files with 28 additions and 170 deletions

View File

@ -28,8 +28,7 @@ using namespace cocos2d;
namespace cocostudio { namespace cocostudio {
ComAttribute::ComAttribute(void) ComAttribute::ComAttribute(void)
: _attributes(NULL) : _jsonDict(NULL)
, _jsonDict(NULL)
{ {
_name = "ComAttribute"; _name = "ComAttribute";
} }
@ -37,14 +36,10 @@ ComAttribute::ComAttribute(void)
ComAttribute::~ComAttribute(void) ComAttribute::~ComAttribute(void)
{ {
CC_SAFE_DELETE(_jsonDict); CC_SAFE_DELETE(_jsonDict);
CC_SAFE_RELEASE(_attributes);
} }
bool ComAttribute::init() bool ComAttribute::init()
{ {
_attributes = Dictionary::create();
_attributes->retain();
_jsonDict = new JsonDictionary(); _jsonDict = new JsonDictionary();
return true; return true;
} }
@ -63,128 +58,6 @@ ComAttribute* ComAttribute::create(void)
return pRet; return pRet;
} }
void ComAttribute::setInt(const char *key, int value)
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(Integer::create(value), key);
}
void ComAttribute::setDouble(const char *key, double value)
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(Double::create(value), key);
}
void ComAttribute::setFloat(const char *key, float value)
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(Float::create(value), key);
}
void ComAttribute::setBool(const char *key, bool value)
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(Bool::create(value), key);
}
void ComAttribute::setCString(const char *key, const char *value)
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(String::create(value), key);
}
void ComAttribute::setObject(const char *key, Object *value)
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(value, key);
}
int ComAttribute::getInt(const char *key) const
{
Object *ret = _attributes->objectForKey(key);
if( ret )
{
if( Integer *obj=dynamic_cast<Integer*>(ret) )
return obj->getValue();
CCASSERT(false, "Key found, type is not integer");
}
// XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key );
return 0;
}
double ComAttribute::getDouble(const char *key) const
{
Object *ret = _attributes->objectForKey(key);
if( ret )
{
if( Double *obj=dynamic_cast<Double*>(ret) )
return obj->getValue();
CCASSERT(false, "Key found, type is not double");
}
// XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key );
return 0.0;
}
float ComAttribute::getFloat(const char *key) const
{
Object *ret = _attributes->objectForKey(key);
if( ret )
{
if( Float *obj=dynamic_cast<Float*>(ret) )
return obj->getValue();
CCASSERT(false, "Key found, type is not float");
}
// XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key );
return 0.0;
}
bool ComAttribute::getBool(const char *key) const
{
Object *ret = _attributes->objectForKey(key);
if( ret )
{
if( Bool *boolobj=dynamic_cast<Bool*>(ret) )
return boolobj->getValue();
if( String *strobj=dynamic_cast<String*>(ret) )
return strobj->boolValue();
CCASSERT(false, "Key found, type is not Bool");
}
// XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key );
return false;
}
const char* ComAttribute::getCString(const char *key) const
{
Object *ret = _attributes->objectForKey(key);
if( ret )
{
if( String *str=dynamic_cast<String*>(ret) )
return str->getCString();
CCASSERT(false, "Key found, type is not CString");
}
// XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key );
return NULL;
}
Object* ComAttribute::getObject(const char *key) const
{
return _attributes->objectForKey(key);
}
JsonDictionary* ComAttribute::getDict() const JsonDictionary* ComAttribute::getDict() const
{ {
return _jsonDict; return _jsonDict;

View File

@ -47,24 +47,9 @@ protected:
public: public:
virtual bool init(); virtual bool init();
static ComAttribute* create(void); static ComAttribute* create(void);
void setInt(const char *key, int value);
void setDouble(const char *key, double value);
void setFloat(const char *key, float value);
void setBool(const char *key, bool value);
void setCString(const char *key, const char *value);
void setObject(const char *key, Object *value);
int getInt(const char *key) const;
double getDouble(const char *key) const;
float getFloat(const char *key) const;
bool getBool(const char *key) const;
const char* getCString(const char *key) const;
cocos2d::Object* getObject(const char *key) const;
JsonDictionary* getDict() const; JsonDictionary* getDict() const;
private: private:
cocos2d::Dictionary *_attributes;
JsonDictionary *_jsonDict; JsonDictionary *_jsonDict;
}; };

View File

@ -32,7 +32,7 @@ namespace cocostudio {
class ComController : public cocos2d::Component, public InputDelegate class ComController : public cocos2d::Component, public InputDelegate
{ {
protected: public:
ComController(void); ComController(void);
public: public:
@ -51,10 +51,10 @@ public:
* @js NA * @js NA
* @lua NA * @lua NA
*/ */
virtual void onExit(); virtual void onExit() override;
virtual void update(float delta); virtual void update(float delta) override;
virtual bool isEnabled() const; virtual bool isEnabled() const override;
virtual void setEnabled(bool b); virtual void setEnabled(bool b) override;
static ComController* create(void); static ComController* create(void);
}; };

View File

@ -14,7 +14,7 @@ public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, // Here's a difference. Method 'init' in cocos2d-x returns bool,
// instead of returning 'id' in cocos2d-iphone // instead of returning 'id' in cocos2d-iphone
virtual bool init(); virtual bool init() override;
// there's no 'id' in cpp, so we recommand to return the exactly class pointer // there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::Scene* scene(); static cocos2d::Scene* scene();

View File

@ -13,10 +13,10 @@ protected:
virtual ~EnemyController(void); virtual ~EnemyController(void);
public: public:
virtual bool init(); virtual bool init() override;
virtual void onEnter(); virtual void onEnter() override;
virtual void onExit(); virtual void onExit() override;
virtual void update(float delta); virtual void update(float delta) override;
static EnemyController* create(void); static EnemyController* create(void);
public: public:

View File

@ -13,13 +13,13 @@ protected:
virtual ~PlayerController(void); virtual ~PlayerController(void);
public: public:
void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event) override; virtual void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event) override;
public: public:
virtual bool init(); virtual bool init() override;
virtual void onEnter(); virtual void onEnter() override;
virtual void onExit(); virtual void onExit() override;
virtual void update(float delta); virtual void update(float delta) override;
static PlayerController* create(void); static PlayerController* create(void);
}; };

View File

@ -12,10 +12,10 @@ protected:
virtual ~ProjectileController(void); virtual ~ProjectileController(void);
public: public:
virtual bool init(); virtual bool init() override;
virtual void onEnter(); virtual void onEnter() override;
virtual void onExit(); virtual void onExit() override;
virtual void update(float delta); virtual void update(float delta) override;
static ProjectileController* create(void); static ProjectileController* create(void);
public: public:

View File

@ -39,7 +39,7 @@ void SceneController::onEnter()
_projectiles->retain(); _projectiles->retain();
((ComAudio*)(_owner->getComponent("Audio")))->playBackgroundMusic("background-music-aac.wav", true); ((ComAudio*)(_owner->getComponent("Audio")))->playBackgroundMusic("background-music-aac.wav", true);
((ComAttribute*)(_owner->getComponent("ComAttribute")))->setInt("KillCount", 0); ((ComAttribute*)(_owner->getComponent("ComAttribute")))->getDict()->insertItem("KillCount", 0);
} }
void SceneController::onExit() void SceneController::onExit()
@ -102,10 +102,10 @@ void SceneController::spriteMoveFinished(Node* sender)
void SceneController::increaseKillCount() void SceneController::increaseKillCount()
{ {
int nProjectilesDestroyed = ((ComAttribute*)(_owner->getComponent("ComAttribute")))->getInt("KillCount"); int nProjectilesDestroyed = ((ComAttribute*)(_owner->getComponent("ComAttribute")))->getDict()->getItemIntValue("KillCount", -1);
ComAttribute *p = (ComAttribute*)(_owner->getComponent("ComAttribute")); ComAttribute *p = (ComAttribute*)(_owner->getComponent("ComAttribute"));
p->setInt("KillCount", ++nProjectilesDestroyed); p->getDict()->insertItem("KillCount", ++nProjectilesDestroyed);
if (nProjectilesDestroyed >= 5) if (nProjectilesDestroyed >= 5)
{ {

View File

@ -12,10 +12,10 @@ protected:
virtual ~SceneController(void); virtual ~SceneController(void);
public: public:
virtual bool init(); virtual bool init() override;
virtual void onEnter(); virtual void onEnter() override;
virtual void onExit(); virtual void onExit() override;
virtual void update(float delta); virtual void update(float delta) override;
static SceneController* create(void); static SceneController* create(void);