/** * 从数据包加载任务 */ bool MCTaskAccessor::loadTasks(const char *aFilePath) { bool result = false; do { JsonBox::Value in; JsonBox::Object o; JsonBox::Object tasksDict; JsonBox::Object::iterator oIter; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) CCString* pstrFileContent = CCString::createWithContentsOfFile(aFilePath); if (pstrFileContent) { in.loadFromString(pstrFileContent->getCString()); } #else in.loadFromFile(CCFileUtils::sharedFileUtils()->fullPathForFilename(aFilePath).c_str()); #endif CC_BREAK_IF(! in.isObject()); tasksDict = in.getObject(); for (oIter = tasksDict.begin(); oIter != tasksDict.end(); ++oIter) { std::string t_id = oIter->first; const char *s_t_id = t_id.c_str(); mc_object_id_t o_id = { s_t_id[0], s_t_id[1], s_t_id[2], s_t_id[3] }; MCTask *task = new MCTask; task->autorelease(); if (task) { task->setID(o_id); o = oIter->second.getObject(); task->loadTaskContent(o); /* 默认为未完成状态,然后会在加载存档数据的时候更新为正确状态 */ task->setTaskStatus(MCTaskUncompleted); task->proto_ = NULL; tasks_->setObject(task, MCObjectIdToDickKey(o_id)); } } result = true; } while (0); return result; }
void MCItemManager::loadEffectiveItems() { JsonBox::Value v; JsonBox::Object root; JsonBox::Object::iterator rootIterator; MCEffectiveItem *item; MCEffectManager *effectManager = MCEffectManager::sharedEffectManager(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) CCString* pstrFileContent = CCString::createWithContentsOfFile(kMCEffectiveItemFilepath); if (pstrFileContent) { v.loadFromString(pstrFileContent->getCString()); } #else v.loadFromFile(CCFileUtils::sharedFileUtils()->fullPathForFilename(kMCEffectiveItemFilepath).c_str()); #endif root = v.getObject(); for (rootIterator = root.begin(); rootIterator != root.end(); ++rootIterator) { const char *c_str_o_id = rootIterator->first.c_str(); JsonBox::Object object = rootIterator->second.getObject(); mc_object_id_t o_id = { c_str_o_id[0], c_str_o_id[1], c_str_o_id[2], c_str_o_id[3] }; if (o_id.class_ == 'P') { item = MCEffectiveItem::create(); } else { MCTrap *trap = new MCTrap; trap->init(o_id); trap->autorelease(); item = trap; } CCString *ccstring; item->setID(o_id); ccstring = CCString::create(object["name"].getString().c_str()); item->setName(ccstring); ccstring->retain(); ccstring = CCString::create(object["description"].getString().c_str()); item->setDescription(ccstring); ccstring->retain(); ccstring = CCString::create(object["icon"].getString().c_str()); item->setIcon(ccstring); ccstring->retain(); /* effect-id */ c_str_o_id = object["effect-id"].getString().c_str(); mc_object_id_t e_id = { c_str_o_id[0], c_str_o_id[1], c_str_o_id[2], c_str_o_id[3] }; MCEffect *effect = effectManager->effectForObjectId(e_id); item->effect = effect; effect->retain(); item->setPrice(object["price"].getInt()); item->radius = object["radius"].getInt() * 24 / CC_CONTENT_SCALE_FACTOR(); /* 24为一个单位 */ item->hp = object["hp"].getInt(); item->pp = object["pp"].getInt(); item->positive_state = object["positive-state"].getInt(); item->lasting_time = object["lasting-time"].isDouble() ? (float) object["lasting-time"].getDouble() : (float) object["lasting-time"].getInt(); effectiveItems_->setObject(item, MCObjectIdToDickKey(o_id)); } }
void MCItemManager::loadEquipmentItems() { JsonBox::Value weapon; JsonBox::Value armor; JsonBox::Object root; JsonBox::Object::iterator rootIterator; MCOreManager *oreManager = MCOreManager::sharedOreManager(); /* 读取武器 */ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) CCString* pstrFileContent = CCString::createWithContentsOfFile(kMCEquipmentItemWeaponFilepath); if (pstrFileContent) { weapon.loadFromString(pstrFileContent->getCString()); } #else weapon.loadFromFile(CCFileUtils::sharedFileUtils()->fullPathForFilename(kMCEquipmentItemWeaponFilepath).c_str()); #endif root = weapon.getObject(); for (rootIterator = root.begin(); rootIterator != root.end(); ++rootIterator) { const char *c_str_o_id = rootIterator->first.c_str(); JsonBox::Object object = rootIterator->second.getObject(); mc_object_id_t o_id = { c_str_o_id[0], c_str_o_id[1], c_str_o_id[2], c_str_o_id[3] }; MCEquipmentItem *item = MCEquipmentItem::create(MCEquipment::MCWeapon); CCString *ccstring; item->setID(o_id); ccstring = CCString::create(object["name"].getString().c_str()); item->setName(ccstring); ccstring->retain(); ccstring = CCString::create(object["icon"].getString().c_str()); item->setIcon(ccstring); ccstring->retain(); item->setPrice(object["price"].getInt()); JsonBox::Object damage = object["damage"].getObject(); MCWeapon *equipment = dynamic_cast<MCWeapon *>(item->equipment_); /* effect-id */ c_str_o_id = object["effect-id"].getString().c_str(); mc_object_id_t e_id = { c_str_o_id[0], c_str_o_id[1], c_str_o_id[2], c_str_o_id[3] }; MCEffectManager *effectManager = MCEffectManager::sharedEffectManager(); MCEffect *effect = effectManager->effectForObjectId(e_id); equipment->attackEffect = effect; effect->retain(); equipment->damage = MCMakeDiceType(damage["count"].getInt(), damage["size"].getInt()); equipment->criticalHit = object["critical-hit"].getInt(); JsonBox::Object diceRange = object["critical-hit-visible"].getObject(); JsonBox::Object diceRangeDice = diceRange["dice"].getObject(); equipment->criticalHitVisible.min = diceRange["min"].getInt(); equipment->criticalHitVisible.max = diceRange["max"].getInt(); equipment->criticalHitVisible.dice = MCMakeDiceType(diceRangeDice["count"].getInt(), diceRangeDice["size"].getInt()); diceRange = object["critical-hit-invisible"].getObject(); diceRangeDice = diceRange["dice"].getObject(); equipment->criticalHitInvisible.min = diceRange["min"].getInt(); equipment->criticalHitInvisible.max = diceRange["max"].getInt(); equipment->criticalHitInvisible.dice = MCMakeDiceType(diceRangeDice["count"].getInt(), diceRangeDice["size"].getInt()); equipment->distance = object["distance"].getInt(); if (object["effect"].isInteger()) { equipment->effect = object["effect"].getInt(); diceRange = object["effect-check"].getObject(); diceRangeDice = diceRange["dice"].getObject(); equipment->effectCheck.min = diceRange["min"].getInt(); equipment->effectCheck.max = diceRange["max"].getInt(); equipment->effectCheck.dice = MCMakeDiceType(diceRangeDice["count"].getInt(), diceRangeDice["size"].getInt()); } else { equipment->effect = MCNormalState; } /* consume Double */ equipment->consume = object["consume"].isDouble() ? (float) object["consume"].getDouble() : (float) object["consume"].getInt(); equipment->dexterity = object["dexterity"].getInt(); /* action-effect String */ equipment->actionEffect.assign(object["action-effect"].getString()); /* 读取默认矿石,加载背包的时候更新为正确矿石 */ item->ore_ = oreManager->defaultOre(); equipmentItems_->setObject(item, MCObjectIdToDickKey(o_id)); } /* 读取防具 */ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) pstrFileContent = CCString::createWithContentsOfFile(kMCEquipmentItemArmorFilepath); if (pstrFileContent) { armor.loadFromString(pstrFileContent->getCString()); } #else armor.loadFromFile(CCFileUtils::sharedFileUtils()->fullPathForFilename(kMCEquipmentItemArmorFilepath).c_str()); #endif root = armor.getObject(); for (rootIterator = root.begin(); rootIterator != root.end(); ++rootIterator) { const char *c_str_o_id = rootIterator->first.c_str(); JsonBox::Object object = rootIterator->second.getObject(); mc_object_id_t o_id = { c_str_o_id[0], c_str_o_id[1], c_str_o_id[2], c_str_o_id[3] }; MCEquipmentItem *item = MCEquipmentItem::create(MCEquipment::MCArmor); CCString *ccstring; item->setID(o_id); ccstring = CCString::create(object["name"].getString().c_str()); item->setName(ccstring); ccstring->retain(); ccstring = CCString::create(object["description"].getString().c_str()); item->setDescription(ccstring); ccstring->retain(); ccstring = CCString::create(object["icon"].getString().c_str()); item->setIcon(ccstring); ccstring->retain(); item->setPrice(object["price"].getInt()); MCArmor *equipment = dynamic_cast<MCArmor *>(item->equipment_); equipment->defense = object["defense"].getInt(); equipment->dexterity = object["dexterity"].getInt(); equipment->armorCheckPenalty = object["armor-check-penalty"].getInt(); /* 读取默认矿石,加载背包的时候更新为正确矿石 */ item->ore_ = oreManager->defaultOre(); equipmentItems_->setObject(item, MCObjectIdToDickKey(o_id)); } }
void GraphDog::receivedCommand(float dt) { //## for(std::map<int, CommandsType>::iterator commandQueueIter = commandQueue.begin(); commandQueueIter != commandQueue.end();) { CommandsType commands = commandQueueIter->second; try { if(commands.chunk.resultCode == CURLE_AGAIN) { throw commands.chunk.resultCode; } else if(commands.chunk.resultCode != CURLE_OK) { for(std::map<string, CommandType>::iterator commandTypeIter = commandQueueIter->second.commands.begin(); commandTypeIter != commandQueueIter->second.commands.end(); ++commandTypeIter) { JsonBox::Object resultobj; CommandType command = commandTypeIter->second; resultobj["state"] = JsonBox::Value("error"); resultobj["errorMsg"] = JsonBox::Value("check your network state"); resultobj["errorCode"] = JsonBox::Value(1002); CCLog("ssiba resultCode(report me!!!) : %d", commands.chunk.resultCode); //callbackparam if(command.paramStr!=""){ JsonBox::Object param = GraphDogLib::StringToJsonObject(command.paramStr); resultobj["param"]=JsonBox::Value(param); } if(command.target!=0 && command.selector!=0) ((command.target)->*(command.selector))(resultobj); } throw commands.chunk.resultCode; } JsonBox::Object resultobj = commands.result; //GraphDogLib::StringToJsonObject(resultStr);// result.getObject(); for(JsonBox::Object::iterator iter2 = resultobj.begin(); iter2 != resultobj.end(); ++iter2) { CommandType ct = commandQueueIter->second.commands[iter2->first]; if(ct.target != 0 && ct.selector != 0) { ((ct.target)->*(ct.selector))(iter2->second.getObject()); } } //메모리도 해제 if(commands.chunk.memory) free(commands.chunk.memory); //명령을 삭제한다. commandQueue.erase(commandQueueIter++); } catch (const CURLcode& resultCode) { if(resultCode == CURLE_AGAIN) { ++commandQueueIter; } else if(resultCode != CURLE_OK) { if(commands.chunk.memory) free(commands.chunk.memory); commandQueue.erase(commandQueueIter++); } } } }
/** * load all flags */ void MCFlagManager::loadAllFlags() { JsonBox::Value v; JsonBox::Object root; JsonBox::Object::iterator flagsIterator; JsonBox::Object flagValueRoot; const char *c_str_flag_id; mc_object_id_t flag_id; MCFlag *flag; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) CCString* pstrFileContent = CCString::createWithContentsOfFile(kMCFlagsFilepath); if (pstrFileContent) { v.loadFromString(pstrFileContent->getCString()); } #else v.loadFromFile(CCFileUtils::sharedFileUtils()->fullPathForFilename(kMCFlagsFilepath).c_str()); #endif root = v.getObject(); for (flagsIterator = root.begin(); flagsIterator != root.end(); ++flagsIterator) { c_str_flag_id = flagsIterator->first.c_str(); flagValueRoot = flagsIterator->second.getObject(); flag_id.class_ = c_str_flag_id[0]; flag_id.sub_class_ = c_str_flag_id[1]; flag_id.index_ = c_str_flag_id[2]; flag_id.sub_index_ = c_str_flag_id[3]; flag = MCFlag::create(flag_id, flagValueRoot); sourceFlags_->setObject(flag, MCObjectIdToDickKey(flag_id)); } /* 从存档读取数据 */ string data = CCUserDefault::sharedUserDefault()->getStringForKey(kMCFlagsKey, ""); if (MCGameState::sharedGameState()->isSaveFileExists() && data.size() > 0) { #if MC_DEBUG_SAVEDATA == 1 const char *output = data.c_str(); #else const char *input = data.c_str(); char *output; mc_size_t len = strlen(input); MCBase64Decode((mc_byte_t *) input, len, (mc_byte_t **) &output); #endif data.assign(output); vector<string> result = split(data, ","); for (vector<string>::iterator iterator = result.begin(); iterator != result.end(); ++iterator) { if (iterator->at(4) != '-') { /* 第4位为separator,若非则为不合法数据 */ continue; } mc_object_id_t o_id = { iterator->at(0), iterator->at(1), iterator->at(2), iterator->at(3) }; string count = iterator->substr(5); MCFlagState state = atoi(count.c_str()) - 1; /* MCInvalidState为-1。。。储存的时候多加了1。。。 */ MCFlag *flag = (MCFlag *) flags_->objectForKey(MCObjectIdToDickKey(o_id)); if (flag) { flag->setState(state); } } } /* 初始化特殊flag */ taskFlag_ = flagForObjectId(kMCTaskFlagId); areaBlockedFlag_ = flagForObjectId(kMCAreaBlockedFlagId); spawnFlag_ = flagForObjectId(kMCSpawnFlagId); }
void MCMercenaryManager::loadMercenaries() { JsonBox::Value v; JsonBox::Object mercenaries; JsonBox::Object::iterator mercenariesIterator; MCDiceMaker *diceMaker = MCDiceMaker::sharedDiceMaker(); CCString *ccstring; MCEffectManager *effectManager = MCEffectManager::sharedEffectManager(); MCSkillManager *skillManager = MCSkillManager::sharedSkillManager(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) CCString* pstrFileContent = CCString::createWithContentsOfFile(kMCMercenariesFilepath); if (pstrFileContent) { v.loadFromString(pstrFileContent->getCString()); } #else v.loadFromFile(CCFileUtils::sharedFileUtils()->fullPathForFilename(kMCMercenariesFilepath).c_str()); #endif mercenaries = v.getObject(); for (mercenariesIterator = mercenaries.begin(); mercenariesIterator != mercenaries.end(); ++mercenariesIterator) { const char *c_str_o_id = mercenariesIterator->first.c_str(); mc_object_id_t m_id = { c_str_o_id[0], c_str_o_id[1], c_str_o_id[2], c_str_o_id[3] }; JsonBox::Object mercenaryObject = mercenariesIterator->second.getObject(); MCMercenary *mercenary; if (m_id.sub_class_ == '9') { mercenary = new MCMercenary; } else if (m_id.sub_class_ == '8') { mercenary = new MCNervousMercenary; } else { CCAssert(false, "数据错误!"); continue; } mercenary->init(); mercenary->autorelease(); mercenary->setID(m_id); /* name String */ ccstring = CCString::create(mercenaryObject["name"].getString().c_str()); mercenary->setName(ccstring); ccstring->retain(); /* face String */ ccstring = CCString::create(mercenaryObject["face"].getString().c_str()); mercenary->setFace(ccstring); ccstring->retain(); /* build sprite sheet file path */ ccstring = CCString::createWithFormat("%s/%c-%s", kMCSpriteSheetBaseDirectory, c_str_o_id[0] | 32, c_str_o_id + 1); mercenary->setSpriteSheet(ccstring); ccstring->retain(); /* effect-id */ c_str_o_id = mercenaryObject["effect-id"].getString().c_str(); mc_object_id_t e_id = { c_str_o_id[0], c_str_o_id[1], c_str_o_id[2], c_str_o_id[3] }; MCEffect *effect = effectManager->effectForObjectId(e_id); mercenary->setAttackEffect(effect); effect->retain(); /* cost Integer */ mercenary->cost_ = mercenaryObject["cost"].getInt(); /* HP Integer */ mercenary->setHP(mercenaryObject["HP"].getInt()); mercenary->setMaxHP(mercenary->getHP()); /* dying Integer 胆怯佣兵独有 */ if (mercenary->mercenaryType_ == MCMercenary::MCNervousMercenary) { dynamic_cast<MCNervousMercenary *>(mercenary)->setDying(mercenaryObject["dying"].getInt()); } /* PP Integer */ mercenary->setPP(mercenaryObject["PP"].getInt()); mercenary->setMaxPP(mercenary->getPP()); /* consume Double */ mercenary->setConsume(mercenaryObject["consume"].isDouble() ? (float) mercenaryObject["consume"].getDouble() : (float) mercenaryObject["consume"].getInt()); /* exhaustion Integer */ mercenary->setExhaustion(mercenaryObject["exhaustion"].getInt()); /* tired Integer */ mercenary->setTired(mercenaryObject["tired"].getInt()); /* dexterity Integer */ mercenary->setDexterity(mercenaryObject["dexterity"].getInt()); /* AC Integer */ mercenary->setAC(mercenaryObject["AC"].getInt()); /* armor-check-penalty Integer */ mercenary->setArmorCheckPenalty(mercenaryObject["armor-check-penalty"].getInt()); /* damage Object */ /* damage.count Integer */ /* damage.size Integer */ JsonBox::Object damage = mercenaryObject["damage"].getObject(); mercenary->setDamage(diceMaker->diceWithType(MCMakeDiceType(damage["count"].getInt(), damage["size"].getInt()))); /* damage-bonus Integer */ mercenary->setDamageBonus(mercenaryObject["damage-bonus"].getInt()); /* critical-hit-visible Object */ /* critical-hit-visible.min Integer */ /* critical-hit-visible.max Integer */ /* critical-hit-visible.dice Object */ /* critical-hit-visible.dice.count Integer */ /* critical-hit-visible.dice.size Integer */ JsonBox::Object diceRange = mercenaryObject["critical-hit-visible"].getObject(); JsonBox::Object diceRangeDice = diceRange["dice"].getObject(); mercenary->criticalHitVisible_.min = diceRange["min"].getInt(); mercenary->criticalHitVisible_.max = diceRange["max"].getInt(); mercenary->criticalHitVisible_.dice = MCMakeDiceType(diceRangeDice["count"].getInt(), diceRangeDice["size"].getInt()); /* critical-hit-invisible Object */ /* critical-hit-invisible.min Integer */ /* critical-hit-invisible.max Integer */ /* critical-hit-invisible.dice Object */ /* critical-hit-invisible.dice.count Integer */ /* critical-hit-invisible.dice.size Integer */ diceRange = mercenaryObject["critical-hit-invisible"].getObject(); diceRangeDice = diceRange["dice"].getObject(); mercenary->criticalHitInvisible_.min = diceRange["min"].getInt(); mercenary->criticalHitInvisible_.max = diceRange["max"].getInt(); mercenary->criticalHitInvisible_.dice = MCMakeDiceType(diceRangeDice["count"].getInt(), diceRangeDice["size"].getInt()); /* critical-hit Double */ float floatValue = mercenaryObject["critical-hit"].isDouble() ? (float) mercenaryObject["critical-hit"].getDouble() : (float) mercenaryObject["critical-hit"].getInt(); mercenary->setCriticalHit(floatValue); /* distance Integer */ mercenary->setDistance(mercenaryObject["distance"].getInt()); /* skills Object */ JsonBox::Object skillsObject = mercenaryObject["skills"].getObject(); if (skillsObject["A"].isString()) { const char *c_str_s_id = skillsObject["A"].getString().c_str(); mc_object_id_t s_id = { c_str_s_id[0], c_str_s_id[1], c_str_s_id[2], c_str_s_id[3] }; mercenary->skills_->addObject(skillManager->skillForObjectId(s_id)); } if (skillsObject["B"].isString()) { const char *c_str_s_id = skillsObject["B"].getString().c_str(); mc_object_id_t s_id = { c_str_s_id[0], c_str_s_id[1], c_str_s_id[2], c_str_s_id[3] }; mercenary->skills_->addObject(skillManager->skillForObjectId(s_id)); }if (skillsObject["C"].isString()) { const char *c_str_s_id = skillsObject["C"].getString().c_str(); mc_object_id_t s_id = { c_str_s_id[0], c_str_s_id[1], c_str_s_id[2], c_str_s_id[3] }; mercenary->skills_->addObject(skillManager->skillForObjectId(s_id)); } if (skillsObject["D"].isString()) { const char *c_str_s_id = skillsObject["D"].getString().c_str(); mc_object_id_t s_id = { c_str_s_id[0], c_str_s_id[1], c_str_s_id[2], c_str_s_id[3] }; mercenary->skills_->addObject(skillManager->skillForObjectId(s_id)); } /* effect Integer */ if (mercenaryObject["effect"].isInteger()) { mercenary->setEffect(mercenaryObject["effect"].getInt()); /* effect-check Object */ /* effect-check.min Integer */ /* effect-check.max Integer */ /* effect-check.dice Object */ /* effect-check.dice.count Integer */ /* effect-check.dice.size Integer */ diceRange = mercenaryObject["effect-check"].getObject(); diceRangeDice = diceRange["dice"].getObject(); mercenary->effectCheck_.min = diceRange["min"].getInt(); mercenary->effectCheck_.max = diceRange["max"].getInt(); mercenary->effectCheck_.dice = MCMakeDiceType(diceRangeDice["count"].getInt(), diceRangeDice["size"].getInt()); } else { mercenary->setEffect(MCNormalState); } /* description String */ ccstring = CCString::create(mercenaryObject["description"].getString().c_str()); mercenary->setDescription(ccstring); ccstring->retain(); /* action-effect String */ ccstring = CCString::create(mercenaryObject["action-effect"].getString().c_str()); mercenary->setActionEffect(ccstring); ccstring->retain(); mercenaries_->setObject(mercenary, MCObjectIdToDickKey(m_id)); } }