void DBManager::createBuildingListInfo() { ValueVector data; loadCsvData(CSV_BUILDINGLISTINFO, data); string timeStamp = GM()->getIntToStr(GM()->getTimeStamp()); string sql = "create table BuildingListInfo(ID integer primary key autoincrement, BuildingID, PositionX, PositionY, BuildState, LastBuildTime, LastGoldHarvest, LastWoodHarvest)"; executeUpdate(sql); for (int i = 1; i < data.size(); i++) { ValueMap& map = data.at(i).asValueMap(); sql = "insert into BuildingListInfo values(" + map["ID"].asString() + ", '" + map["BuildingID"].asString() + "', '" + map["PositionX"].asString() + "', '" + map["PositionY"].asString() + "', '" + map["BuildState"].asString() + "', '" + timeStamp + "', '" + timeStamp + "', '" + timeStamp + "')"; // CCLOG("sql: %s", sql.c_str()); executeUpdate(sql); } }
void DBManager::createPlayerInfo() { ValueVector data; loadCsvData(CSV_PLAYERINFO, data); string sql = "create table PlayerInfo(ID integer primary key autoincrement, Name, Level, Exp, RingCount, GoldCount, WoodCount, GoldCapacity, WoodCapacity)"; executeUpdate(sql); for (int i = 1; i < data.size(); i++) { ValueMap& map = data.at(i).asValueMap(); sql = "insert into PlayerInfo values(" + map["ID"].asString() + ", '" + map["Name"].asString() + "', '" + map["Level"].asString() + "', '" + map["Exp"].asString() + "', '" + map["RingCount"].asString() + "', '" + map["GoldCount"].asString() + "', '" + map["WoodCount"].asString() + "', '" + map["GoldCapacity"].asString() + "', '" + map["WoodCapacity"].asString() + "')"; // CCLOG("sql: %s", sql.c_str()); executeUpdate(sql); } }
//加载声音 void PreLoad::loadEffect(ValueVector effectFiles){ for (unsigned int i = 0; i != effectFiles.size(); ++i){ Value v = effectFiles.at(i); SimpleAudioEngine::getInstance()->preloadEffect(v.asString().c_str()); progressUpdate(); } }
//加载背景音乐 void PreLoad::loadMusic(ValueVector musicFiles){ for (unsigned int i = 0; i != musicFiles.size(); ++i){ Value v = musicFiles.at(i); SimpleAudioEngine::getInstance()->preloadBackgroundMusic(v.asString().c_str()); progressUpdate(); } }
Value CsvUtil::getValue(int typeName, int property, const char *csvPath) { auto csvData = _dataMap.at(csvPath); if (csvData == nullptr) { this->loadFile(csvPath); csvData = _dataMap.at(csvPath); } ValueVector line = csvData->getLineData(typeName); return Value(line.at(property)); }
//加载精灵表单 void PreLoad::loadSpriteSheets(ValueVector spriteSheets){ //该函数会加载与plist文件名称相同但后缀名为png的纹理图片 //把该plist的所有spriteFrame信息读取出来,在之后的代码中可以通过 //Sprite::createWithSpriteFrameName(const std::string& spriteFrameName)函数 //获取对应的精灵帧 for (unsigned int i = 0; i != spriteSheets.size(); ++i){ Value v = spriteSheets.at(i); SpriteFrameCache::getInstance()->addSpriteFramesWithFile(v.asString().c_str()); progressUpdate(); } }
void User::loadModel(ValueMap model) { _model = model; _achieves.clear(); ValueVector achieves = model["achieves"].asValueVector(); for(auto pair:achieves) { _achieves.push_back(pair.asValueMap()); } _buffs.clear(); ValueVector buffs = model["buffs"].asValueVector(); for(auto pair:buffs) { auto buff = Buff::create(pair.asValueMap()); addBuff(buff); } _firstOpen = true; if(!_model["firstOpen"].isNull()) { _firstOpen = (bool)_model["firstOpen"].asInt(); } for(int i = 0; i < 3; i++) { _shareRewardsLock[i] = 0; } if(!_model["shareRewardsLock"].isNull()) { ValueVector shareRewardsLock = _model["shareRewardsLock"].asValueVector(); for(int i = 0; i < 3; i++) { _shareRewardsLock[i] = shareRewardsLock.at(i).asInt(); } } _shareRewardOpen = false; if(!_model["shareRewardOpen"].isNull()) { _shareRewardOpen = (bool)_model["shareRewardOpen"].asInt(); } _shareRewardOpenFirst = true; if(!_model["shareRewardOpenFirst"].isNull()) { _shareRewardOpenFirst = (bool)_model["shareRewardOpenFirst"].asInt(); } }
/* Your method must have these Node* and Value parameters */ void HelloWorld::gameTestMethod(Node *sender, Value data) { if (!data.isNull() && data.getType() == Value::Type::MAP) { ValueMap valueMap = data.asValueMap(); std::string response = valueMap["test_response"].asString(); CCLOG("Response sent from native: %s", response.c_str()); ValueVector testVector = valueMap["fibonacci"].asValueVector(); size_t vectorSize = testVector.size(); for (int i = 0; i < vectorSize; i++) { CCLOG("Vector value #%d: %d", i, testVector.at(i).asInt()); } } }
void DBManager::createMiwuStateInfo() { ValueVector data; loadCsvData(CSV_MIWUSTATEINFO, data); string timeStamp = GM()->getIntToStr(GM()->getTimeStamp()); string sql = "create table MiwuStateInfo(MiwuID integer primary key autoincrement, Type)"; executeUpdate(sql); for (int i = 1; i < data.size(); i++) { ValueMap& map = data.at(i).asValueMap(); sql = "insert into MiwuStateInfo values(" + map["MiwuID"].asString() + ", '" + map["Type"].asString() + "')"; // CCLOG("sql: %s", sql.c_str()); executeUpdate(sql); } }
void DBManager::createSoilderListInfo() { ValueVector data; loadCsvData(CSV_SOILDER_LISTINFO, data); string timeStamp = GM()->getIntToStr(GM()->getTimeStamp()); string sql = "create table SoilderListInfo(ID integer primary key autoincrement, SoilderID, Count)"; executeUpdate(sql); for (int i = 1; i < data.size(); i++) { ValueMap& map = data.at(i).asValueMap(); sql = "insert into SoilderListInfo values(" + map["ID"].asString() + ", '" + map["SoilderID"].asString() + "', '" + map["Count"].asString() + "')"; // CCLOG("sql: %s", sql.c_str()); executeUpdate(sql); } }
void GameScene::addHeart(TMXTiledMap *tmx){ ValueVector objects = map->getObjectGroup("heart")->getObjects(); for (int i = 0; i < objects.size(); i++) { ValueMap& dict = objects.at(i).asValueMap(); float x = dict["x"].asFloat(); float y = dict["y"].asFloat(); Sprite *heart = Sprite::create("heart.png"); auto physicsBody = PhysicsBody::createCircle(heart->getContentSize().width/2, PhysicsMaterial(0, 0, 1)); physicsBody->setGravityEnable(false); heart->setPhysicsBody(physicsBody); //加了物理属性后heart不跟随tmx一起移动 heart->setPosition(Point(x, y)); tmx->addChild(heart); } }
void DBManager::createHeroListInfo() { ValueVector data; loadCsvData(CSV_HERO_LISTINFO, data); string timeStamp = GM()->getIntToStr(GM()->getTimeStamp()); string sql = "create table HeroListInfo(ID integer primary key autoincrement, HeroID, Exp, State)"; executeUpdate(sql); for (int i = 1; i < data.size(); i++) { ValueMap& map = data.at(i).asValueMap(); sql = "insert into HeroListInfo values(" + map["ID"].asString() + ", '" + map["HeroID"].asString() + "', '" + map["Exp"].asString() + "', '" + map["State"].asString() + "')"; // CCLOG("sql: %s", sql.c_str()); executeUpdate(sql); } }
void User::getShareReward(int index) { if(getShareRewardsLock(index - 1) == 1) { PopPanel::getInstance()->note("shareReward","type:Text---text:"+ a2u("已经获得该奖励"),1.0f,false,true); return; } auto name = "shareReward" + cocos2d::Value(index).asString(); ValueVector rewards; ValueVector rewardVec; stringToValueVector(PlotScript::sharedHD()->getLuaVarString("script/Test.lua", name.c_str()),"###",rewardVec); for(auto pair:rewardVec) { ValueMap rewardMap; stringToValueMap(pair.asString(), rewardMap); rewards.push_back(cocos2d::Value(rewardMap)); } // 默认只有一个奖品 auto rewardMap = rewards.at(0).asValueMap(); int typeId = rewardMap["typeId"].asInt(); int num = rewardMap["num"].asInt(); for(int i = 0; i < num; i ++) { auto prop = Prop::create(typeId); // 获得奖励 auto player = Detect::shareDetect()->getPlayer(); player->addProp(prop); } auto prop = Prop::create(typeId); PopPanel::getInstance()->note("cantEquip","type:Text---text:"+ a2u("恭喜获得 ###") + "type:Text---text:"+ prop->getNickName() + "|color:{255,0,0}"); setShareRewardsLock(index - 1, 1); }
json_t *NDKHelper::getJsonFromValue(Value value) { if (value.getType() == Value::Type::MAP) { ValueMap valueMap = value.asValueMap(); json_t *jsonDict = json_object(); for (auto &element : valueMap) { json_object_set_new(jsonDict, element.first.c_str(), NDKHelper::getJsonFromValue(element.second)); } return jsonDict; } else if (value.getType() == Value::Type::VECTOR) { ValueVector valueVector = value.asValueVector(); json_t *jsonArray = json_array(); size_t sizeVector = valueVector.size(); for (unsigned int i = 0; i < sizeVector; i++) { json_array_append_new(jsonArray, NDKHelper::getJsonFromValue(valueVector.at(i))); } return jsonArray; } else if (value.getType() == Value::Type::BOOLEAN) { return json_boolean(value.asBool()); } else if (value.getType() == Value::Type::INTEGER) { return json_integer(value.asInt()); } else if (value.getType() == Value::Type::DOUBLE) { return json_real(value.asDouble()); } else if (value.getType() == Value::Type::STRING) { return json_string(value.asString().c_str()); } return NULL; }
void TestWriteValueVector::onEnter() { FileUtilsDemo::onEnter(); auto winSize = Director::getInstance()->getWinSize(); auto writeResult = Label::createWithTTF("show writeResult", "fonts/Thonburi.ttf", 18); this->addChild(writeResult); writeResult->setPosition(winSize.width / 2, winSize.height * 3 / 4); auto readResult = Label::createWithTTF("show readResult", "fonts/Thonburi.ttf", 18); this->addChild(readResult); readResult->setPosition(winSize.width / 2, winSize.height / 3); ValueVector array; ValueMap mapInArray; mapInArray["string1"] = "string in dictInArray key 0"; mapInArray["string2"] = "string in dictInArray key 1"; array.push_back(Value(mapInArray)); array.push_back(Value("string in array")); ValueVector arrayInArray; arrayInArray.push_back(Value("string 0 in arrayInArray")); arrayInArray.push_back(Value("string 1 in arrayInArray")); array.push_back(Value(arrayInArray)); //add boolean to the plist auto booleanObject = Value(true); array.push_back(booleanObject); //add interger to the plist auto intObject = Value(1024); array.push_back(intObject); //add float to the plist auto floatObject = Value(1024.1024f); array.push_back(floatObject); //add double to the plist auto doubleObject = Value(1024.123); array.push_back(doubleObject); // end with / std::string writablePath = FileUtils::getInstance()->getWritablePath(); std::string fullPath = writablePath + "testWriteValueVector.plist"; if (FileUtils::getInstance()->writeValueVectorToFile(array, fullPath.c_str())) { log("see the plist file at %s", fullPath.c_str()); writeResult->setString("write success"); } else { log("write plist file failed"); writeResult->setString("write failed"); } ValueVector readArray = FileUtils::getInstance()->getValueVectorFromFile(fullPath.c_str()); std::string readDataStr = "read data:\n"; // read value map data ValueMap readMapInArray = readArray.at(0).asValueMap(); readDataStr += " mapValue:[\"string1\"][" + readMapInArray["string1"].asString() + "]\n"; readDataStr += " mapValue:[\"string2\"][" + readMapInArray["string2"].asString() + "]\n"; // read string data readDataStr += " stringValue:" + readArray.at(1).asString() + "\n"; // read value vector data ValueVector readVectorInArray = readArray.at(2).asValueVector(); readDataStr += " vectorValue:[1]" + readVectorInArray.at(0).asString() + "\n"; readDataStr += " vectorValue:[2]" + readVectorInArray.at(1).asString() + "\n"; // read bool data readDataStr += " boolValue:" + StringUtils::format("%d", readArray.at(3).asBool()) + "\n"; // read int data readDataStr += " intValue:" + StringUtils::format("%d", readArray.at(4).asInt()) + "\n"; // read float data readDataStr += " floatValue:" + StringUtils::format("%f", readArray.at(5).asFloat()) + "\n"; // read double data readDataStr += " doubleValue:" + StringUtils::format("%f", readArray.at(6).asDouble()) + "\n"; readResult->setString(readDataStr); }
bool SparDiskPanel::init() { _sparDisk = Detect::shareDetect()->getPlayer()->getSparDisk(); _equipSparPan = static_cast<Layout*>(Helper::seekWidgetByName(_root, "Panel_equipSpar")); _bagSparPan = static_cast<Layout*>(Helper::seekWidgetByName(_root, "Panel_bagSpar")); _introPan = static_cast<Layout*>(Helper::seekWidgetByName(_root, "Panel_intro")); _nickNameLab = static_cast<Text*>(_introPan->getChildByName("Label_name")); _introLab = static_cast<Text*>(_introPan->getChildByName("Label_intro")); _refineBtn = static_cast<Button*>(_introPan->getChildByName("Button_refine")); _patchNumLab = static_cast<Text*>(_introPan->getChildByName("Label_patchNum")); _completeLab = static_cast<Text*>(_introPan->getChildByName("Label_complete")); _refineBtn->addTouchEventListener(CC_CALLBACK_2(SparDiskPanel::onRefine, this)); for(auto node:_buttons->getChildren()) { auto button = static_cast<Button*>(node); button->addTouchEventListener(CC_CALLBACK_2(SparDiskPanel::onButtonClicked, this)); } int i = 0; // ³õʼ»¯ÒõÓ° // disks i = 0; for(auto sparPan:_equipSparPan->getChildren()) { auto image = static_cast<ImageView*>(sparPan->getChildByName("Image_item")); auto bg = static_cast<ImageView*>(sparPan->getChildByName("Image_bg_item")); auto button = static_cast<Button*>(sparPan->getChildByName("Button_item")); auto num = static_cast<TextAtlas*>(sparPan->getChildByName("AtlasLabel")); auto sparBox = SparBox::create(button,bg,image,num); _equipSparBoxs.pushBack(sparBox); button->addTouchEventListener(CC_CALLBACK_2(SparDiskPanel::onEquipSparClicked, this)); sparBox->setTag(i); i++; } // spar i = 0; for(auto sparPan:_bagSparPan->getChildren()) { auto image = static_cast<ImageView*>(sparPan->getChildByName("Image_item")); auto bg = static_cast<ImageView*>(sparPan->getChildByName("Image_bg_item")); auto button = static_cast<Button*>(sparPan->getChildByName("Button_item")); auto num = static_cast<TextAtlas*>(sparPan->getChildByName("AtlasLabel")); auto sparBox = SparBox::create(button,bg,image,num); _sparBoxs.pushBack(sparBox); button->addTouchEventListener(CC_CALLBACK_2(SparDiskPanel::onSparClicked, this)); sparBox->setTag(i); i++; auto name = sparPan->getName(); ValueVector valueName; stringToValueVector(name, "_", valueName); auto typeId = valueName.at(valueName.size() - 1).asInt(); sparBox->setFixSpar(typeId); } updateSpars(); setSelectSparBox(nullptr); _selectEquipSparBox = _equipSparBoxs.at(0); return true; }
void SpaceGameLayer::getObjects(float sf) { const string objectName = "objects"; auto objectGroup = level->getMap()->getObjectGroup(objectName); ValueVector obj = objectGroup->getObjects(); for (int i = 0;i < obj.size();i++) { ValueMap vm = obj.at(i).asValueMap(); int x = vm["x"].asInt(); int y = vm["y"].asInt(); float w = vm["width"].asInt(); float h = vm["height"].asInt(); x *= level->getScale(); y *= level->getScale();; w *= level->getScale();; h *= level->getScale();; string name = vm["name"].asString(); string type = vm["type"].asString(); if (name == "player_start") { hero = new Hero(scale); load_player_stats(); hero->setPosition(x, y); this->addChild(hero, 6); this->addChild(hero->player_sprite, 5); hero->addToGameObjects = &addToGameObjects; } if (name == "passage") { string sc = vm["to_scene"].asString(); string ps = vm["to_passage"].asString(); Passage *pass = new Passage(x, y, w, h, sc, ps); exits.pushBack(pass); pass->target = hero; this->addChild(pass, 6); this->setScale(sf); } if (name == "robot_soldier") { Robot_Soldier *fb = new Robot_Soldier(scale); fb->setPosition(x, y); gameObjects.pushBack(fb); fb->target = hero; this->addChild(fb, 6); } if (name == "laser_trap") { uint32_t direction = vm["direction"].asInt(); uint32_t range = vm["range"].asInt(); float delay = vm["delay"].asFloat(); float duration = vm["duration"].asFloat(); float off_time = vm["off_time"].asFloat(); Laser_Trap *fb = new Laser_Trap(scale,duration,off_time,direction, range, delay); fb->setPosition(x, y); gameObjects.pushBack(fb); fb->target = hero; this->addChild(fb, 6); } /*if(name == "premade_textbox"){ //PremadeTextBox *tb = new PremadeTextBox(x,y,w,h); tb->setPosition(x,y); gameObjects.pushBack(tb); this->addChild(tb,6); } if(name == "space_pig"){ SpacePig *sp = new SpacePig(); sp->setPosition(x,y); sp->setZone(w*(type=="zone"), h*(type=="zone")); gameObjects.pushBack(sp); this->addChild(sp,6); //sp->setScale(sf); } if(name == "flying_blocker"){ Flying_Blocker *fb = new Flying_Blocker(); fb->setPosition(x,y); gameObjects.pushBack(fb); fb->target = hero; this->addChild(fb,6); } if(name == "flying_blocker"){ Flying_Blocker *fb = new Flying_Blocker(); fb->setPosition(x,y); gameObjects.pushBack(fb); fb->target = hero; this->addChild(fb,6); } if(name == "patrol_ufo"){ Patrol_Ufo *fb = new Patrol_Ufo(); fb->setPosition(x,y); gameObjects.pushBack(fb); fb->target = hero; this->addChild(fb,6); } if(name == "spitting_projectiles"){ Spitting_Projectiles *sp = new Spitting_Projectiles; sp->setPosition(x+0.5*w,y+0.5*h); sp->setZone(w*(type=="zone"), h*(type=="zone")); gameObjects.pushBack(sp); this->addChild(sp,6); sp->setScale(sf); sp->target = hero; sp->addToGameObjects = &addToGameObjects; sp->currentGameLayer = this; } if(name == "damage"){ Damage_Zone *dmg = new Damage_Zone(x,y,w,h); dmg->setPosition(x,y); gameObjects.pushBack(dmg); this->addChild(dmg,6); this->setScale(sf); } if(name == "brain_sucker"){ BrainSucker *bs = new BrainSucker(); bs->setPosition(x,y); gameObjects.pushBack(bs); bs->target = hero; this->addChild(bs,6); this->setScale(sf); } if(name == "death_rain"){ DeathRain *dr = new DeathRain(w*(type=="zone"),h*(type=="zone")); dr->setPosition(x,y); dr->setZone(w*(type=="zone"), h*(type=="zone")); gameObjects.pushBack(dr); this->addChild(dr,6); dr->setScale(sf); dr->target = hero; dr->addToGameObjects = &addToGameObjects; dr->currentGameLayer = this; } if(name == "moving_plattform"){ MovingPlattform *mp = new MovingPlattform(x, y); this->addChild(mp,6); mp->setScale(sf); mp->target = hero; mp->addToGameObjects = &addToGameObjects; gameObjects.pushBack(mp); mp->currentGameLayer = this; } */ } if (hero != NULL) { if (hero->nextEntrance != "") { for (int i = 0;i < exits.size();i++) { if (hero->nextEntrance == exits.at(i)->idString) { hero->toucing_exit = true; hero->setPositionX(exits.at(i)->getPositionX()); hero->setPositionY(exits.at(i)->getPositionY()); } } } for (int i = 0;i < gameObjects.size();i++) { gameObjects.at(i)->target = hero; gameObjects.at(i)->player_screen = &screen; gameObjects.at(i)->map_data = this->level; gameObjects.at(i)->addToGameObjects = &addToGameObjects; } for (int i = 0;i < exits.size();i++) { exits.at(i)->target = hero; } } }
void Face::initFaceForKey(std::string key, __Array* faceStates) { // load file plist auto frameCache = SpriteFrameCache::getInstance(); char plistFile[200]; sprintf(plistFile, "%s.plist", key.data()); frameCache->addSpriteFramesWithFile(plistFile); // load animation auto animationCache = AnimationCache::getInstance(); char animationFile[200]; std::string firstSpriteFrameName = ""; sprintf(animationFile, "ani-%s.plist", key.data()); std::string path = FileUtils::getInstance()->fullPathForFilename(animationFile); ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(path); ValueVector animations = dict.at("Animations").asValueVector(); __Array* animationNames = __Array::create(); for (int i = 0; i < animations.size(); i++) { ValueMap animationDict = animations.at(i).asValueMap(); ValueVector frames = animationDict.at("Frames").asValueVector(); ValueMap properties = animationDict.at("Properties").asValueMap(); std::string name = properties.at("Animation").asString(); animationNames->addObject(__String::create(name.data())); float delayPerUnit = 1 / properties.at("FPS").asFloat(); Animation* animation = NULL; if (frames.empty()) { continue; } ssize_t frameSize = frames.size(); Vector<AnimationFrame*> animationFrames(frameSize); for (auto& frame : frames) { ValueMap frameValueMap = frame.asValueMap(); std::string frameString = frameValueMap.at("FrameName").asString(); if (firstSpriteFrameName == "") { firstSpriteFrameName = frameString; } float duration = frameValueMap.at("Duration").asFloat(); SpriteFrame* spriteFrame = frameCache->getSpriteFrameByName(frameString); if (!spriteFrame) { continue; } AnimationFrame* animFrame = AnimationFrame::create(spriteFrame, duration, ValueMap()); animationFrames.pushBack(animFrame); } if (frames.empty()) { assert(NULL); continue; } else if (frames.size() != frameSize) { assert(NULL); } animation = Animation::create(animationFrames, delayPerUnit); animationCache->addAnimation(animation, name); } this->initWithSpriteFrameName(firstSpriteFrameName); this->getTexture()->setAliasTexParameters(); for (int i = 0; i < animationNames->count(); i++) { __String* animationName = (__String*) animationNames->getObjectAtIndex(i); std::string animationNameStr = animationName->getCString(); for (int j = 0; j < faceStates->count(); j++) { FaceState* state = (FaceState*) faceStates->getObjectAtIndex(j); std::string stateString = state->getStateName(); if (animationNameStr.find(stateString) != std::string::npos) { auto animation = animationCache->getAnimation(animationNameStr); auto animate = Animate::create(animation); if (state->isLoop()) { Action* action = RepeatForever::create(animate); this->registerState(stateString, action); } else { Action* action = (Action*) animate; this->registerState(stateString, action); } break; } } } }
void MapAnalysis::initMap(char* levelName) { GameManager* gameManager = GameManager::getInstance(); //¶ÁÈ¡TiledµØͼ×ÊÔ´ TMXTiledMap* tiledMap = TMXTiledMap::create(levelName); gameManager->gameLayer->addChild(tiledMap, -1); //todo--ÊӲ¾°£¬´Ë´¦ÐÞ¸Ä!TODO!ÔÆ²Ê Sprite* could1 = Sprite::create("Items/cloud1.png"); could1->setPosition(500, 500); gameManager->bkLayer->addChild(could1); Sprite* could2 = Sprite::create("Items/cloud2.png"); could2->setPosition(900, 700); gameManager->bkLayer->addChild(could2); Sprite* could3 = Sprite::create("Items/cloud3.png"); could3->setPosition(1400, 450); gameManager->bkLayer->addChild(could3); Sprite* could4 = Sprite::create("Items/cloud1.png"); could4->setPosition(1400 + 500, 500); gameManager->bkLayer->addChild(could4); Sprite* could5 = Sprite::create("Items/cloud2.png"); could5->setPosition(1400 + 700, 700); gameManager->bkLayer->addChild(could5); Sprite* could6 = Sprite::create("Items/cloud3.png"); could6->setPosition(1400 + 1400, 450); gameManager->bkLayer->addChild(could6); //BrickLayer µØ°å Spikes ·æ´Ì TMXObjectGroup* brickLayer = tiledMap->getObjectGroup("BrickLayer"); ValueVector bricks = brickLayer->getObjects(); for (int i = 0; i < bricks.size(); i++) { ValueMap brick = bricks.at(i).asValueMap(); float w = brick.at("width").asFloat(); float h = brick.at("height").asFloat(); float x = brick.at("x").asFloat() + w/2.0f; float y = brick.at("y").asFloat() + h/2.0f; if (brick.at("name").asString() == "Brick")//̨½× { Brick* b = Brick::create(x, y, w, h); gameManager->gameLayer->addChild(b); } else if (brick.at("name").asString() == "Wall")//ǽ { Wall* wa = Wall::create(x, y, w, h); gameManager->gameLayer->addChild(wa); } else if (brick.at("name").asString() == "Spikes")//·æ´Ì { Spikes* sk = Spikes::create(x, y, w, h); gameManager->gameLayer->addChild(sk); } else if (brick.at("name").asString() == "DeadRoof")//ËÀÍǫ̈½× { DeadRoof* dr = DeadRoof::create(x, y, w, h); gameManager->gameLayer->addChild(dr); } } //CoinLayer ½ð±Ò TMXObjectGroup* coinLayer = tiledMap->getObjectGroup("CoinLayer"); ValueVector coins = coinLayer->getObjects(); for (int i = 0; i < coins.size(); i++) { ValueMap coin = coins.at(i).asValueMap(); float w = SD_FLOAT("coin_float_width"); float h = SD_FLOAT("coin_float_height"); float x = coin.at("x").asFloat() + w / 2.0f; float y = coin.at("y").asFloat() + h / 2.0f; Coin* c = Coin::create(x, y, w, h); gameManager->thingLayer->addChild(c); } //MonsterLayer ¹ÖÎï TMXObjectGroup* monsterLayer = tiledMap->getObjectGroup("MonsterLayer"); ValueVector monsters = monsterLayer->getObjects(); for (int i = 0; i < monsters.size(); i++) { ValueMap monster = monsters.at(i).asValueMap(); //MonsterEx ÈËÐ͹ÖÎï if (monster.at("name").asString() == "MonsterEx") { float w = SD_FLOAT("monster_float_width"); float h = SD_FLOAT("monster_float_height"); float x = monster.at("x").asFloat() + w / 2.0f; float y = monster.at("y").asFloat() + h / 2.0f; MonsterEx* m = MonsterEx::create(x, y, w, h); gameManager->monsterLayer->addChild(m); } //FlyingSlime ·ÉÐÐÊ·À³Ä· if (monster.at("name").asString() == "FlyingSlime") { float w = SD_FLOAT("flyingslime_float_width"); float h = SD_FLOAT("flyingslime_float_height"); float x = monster.at("x").asFloat() + w / 2.0f; float y = monster.at("y").asFloat() + h / 2.0f; FlyingSlime* f = FlyingSlime::create(x, y, w, h); gameManager->monsterLayer->addChild(f); } //Slime Ê·À³Ä· if (monster.at("name").asString() == "Slime") { float w = SD_FLOAT("slime_float_width"); float h = SD_FLOAT("slime_float_height"); float x = monster.at("x").asFloat() + w / 2.0f; float y = monster.at("y").asFloat() + h / 2.0f; Slime* s = Slime::create(x, y, w, h); gameManager->monsterLayer->addChild(s); } //£¡£¡£¡Bird Äñ ÖÐÁ¢µÄ--------£¡£¡£¡£¡£¡ if (monster.at("name").asString() == "Bird") { float w = SD_FLOAT("bird_float_width"); float h = SD_FLOAT("bird_float_height"); float x = monster.at("x").asFloat() + w / 2.0f; float y = monster.at("y").asFloat() + h / 2.0f; Bird* b = Bird::create(x, y, w, h); gameManager->monsterLayer->addChild(b); } } //ArticleLayer ½»»¥ÎïÌå TMXObjectGroup* articleLayer = tiledMap->getObjectGroup("ArticleLayer"); ValueVector articles = articleLayer->getObjects(); for (int i = 0; i < articles.size(); i++) { ValueMap article = articles.at(i).asValueMap(); //Article ½»»¥ÎïÌå-Ïä×Ó if (article.at("name").asString() == "Article") { float w = SD_FLOAT("article_float_width"); float h = SD_FLOAT("article_float_height"); float x = article.at("x").asFloat() + w / 2.0f; float y = article.at("y").asFloat() + h / 2.0f; Article* a = Article::create(x, y, w, h); gameManager->thingLayer->addChild(a); } //Door ÃÅ if (article.at("name").asString() == "Door") { float w = SD_FLOAT("door_float_width"); float h = SD_FLOAT("door_float_height"); float x = article.at("x").asFloat() + w / 2.0f; float y = article.at("y").asFloat() + h / 2.0f; Door* a = Door::create(x, y, w, h); gameManager->thingLayer->addChild(a); } //JumpBoard Ìø°å if (article.at("name").asString() == "JumpBoard") { float w = SD_FLOAT("jump_float_width"); float h = SD_FLOAT("jump_float_height"); float x = article.at("x").asFloat() + w / 2.0f; float y = article.at("y").asFloat() + h / 2.0f; JumpBoard* jb = JumpBoard::create(x, y, w, h); gameManager->gameLayer->addChild(jb); } } //HeroLayer Ó¢ÐÛ TMXObjectGroup* heroLayer = tiledMap->getObjectGroup("HeroLayer"); ValueVector heros = heroLayer->getObjects(); for (int i = 0; i < heros.size(); i++) { ValueMap hero = heros.at(i).asValueMap(); //Article ½»»¥ÎïÌå-Ïä×Ó if (hero.at("name").asString() == "Hero") { float w = SD_FLOAT("hero_float_width"); float h = SD_FLOAT("hero_float_height"); float x = hero.at("x").asFloat() + w / 2.0f; float y = hero.at("y").asFloat() + h / 2.0f; Hero* e = Hero::create(x, y, w, h); gameManager->heroLayer->addChild(e); //ÎÞµÐ1Ãë e->setUnbeatable(1); GameManager::getInstance()->hero = e; } } }
void Inventory::addItem(InventoryObject *draggedObject) { items.pushBack(draggedObject); listView->pushBackCustomItem(draggedObject->getPicture()); EventListenerTouchOneByOne *backgroundListener = EventListenerTouchOneByOne::create(); backgroundListener->setSwallowTouches(true); backgroundListener->onTouchBegan = [draggedObject, this](Touch *touch, Event *event) { auto target = static_cast<ImageView *>(event->getCurrentTarget()); Size s = target->getContentSize(); Point locationInNode = target->convertToNodeSpace(touch->getLocation()); locationInNode = Point(locationInNode.x + s.width * 0.5f, locationInNode.y + s.height * 0.5f); Rect rect = Rect(0, 0, s.width, s.height); if (rect.containsPoint(locationInNode)) { draggedObject->startDrag(); simpleLayout->addChild(draggedObject->getPictureDrag()); draggedObject->getPictureDrag()->setPosition(touch->getLocation()); return true; } return false; }; backgroundListener->onTouchMoved = [draggedObject,this](Touch *touch, Event *event) { if(draggedObject->getPictureDrag() != nullptr) draggedObject->getPictureDrag()->setPosition(touch->getLocation()); }; backgroundListener->onTouchEnded = [draggedObject,this](Touch *touch, Event *event) { log("onTouchEnded draggedObject=%d", draggedObject ? draggedObject->getId() : 0); if(draggedObject->getPictureDrag() != nullptr) draggedObject->getPictureDrag()->setVisible(false); Vector<CollectObject *> gameObjects = FirstScene::getInstance()->getGameObjectsLayer()->getGameObjects(); for(int i = 0; i < gameObjects.size(); ++i) { log("collect obj=%d",gameObjects.at(i)->getId()); Size size = gameObjects.at(i)->getSpriteMain()->getContentSize(); Rect rect = Rect(0, 0, size.width, size.height); Point locationInNode = gameObjects.at(i)->getSpriteMain()->convertToNodeSpace(touch->getLocation()); //locationInNode = Point(locationInNode.x , locationInNode.y - size.height * 0.5f); log("!!!locationInNode=%f %f",locationInNode.x, locationInNode.y); if (rect.containsPoint(locationInNode)) { log("AHTUNG"); } } for(int i = 0; i < items.size(); ++i) { InventoryObject *droppedObject = items.at(i); Size s = droppedObject->getPicture()->getContentSize(); Rect rect = Rect(0, 0, s.width, s.height); Point locationInNode = droppedObject->getPicture()->convertToNodeSpace(touch->getLocation()); locationInNode = Point(locationInNode.x + s.width * 0.5f, locationInNode.y + s.height * 0.5f); log("2onTouchEnded draggedObject=%d obj=%d locationInNode=%f %f", draggedObject ? draggedObject->getId() : 0, droppedObject ? droppedObject->getId() : 0, locationInNode.x,locationInNode.y); if (droppedObject != draggedObject && rect.containsPoint(locationInNode)) { ValueMap *draggedObjectData = DataManager::getInstance()->getItemByID(draggedObject->getId()); ValueMap *draggedObjectCraft = &draggedObjectData->at("craft").asValueMap(); ValueMap *droppedObjectData = DataManager::getInstance()->getItemByID(droppedObject->getId()); ValueMap *droppedObjectCraft = &droppedObjectData->at("craft").asValueMap(); ValueVector draggedObjectCraftItems = draggedObjectCraft->at("items").asValueVector(); bool shouldCraft = false; for(int i = 0; i < draggedObjectCraftItems.size(); ++i) { if(draggedObjectCraftItems.at(i).asInt() == droppedObject->getId()) { shouldCraft = true; break; } } if(shouldCraft) { int draggedCapacity = draggedObjectCraft->at("capacity").asInt(); --draggedCapacity; draggedObjectCraft->at("capacity") = draggedCapacity; int droppedCapacity = droppedObjectCraft->at("capacity").asInt(); --droppedCapacity; droppedObjectCraft->at("capacity") = droppedCapacity; log("carft %d", draggedObjectCraft->at("result").asInt()); if(draggedObjectCraft->at("result").asInt() != 0) { addItem(InventoryObject::create(draggedObjectCraft->at("result").asInt())); } if(!draggedCapacity) { draggedObject->getPicture()->removeFromParent(); } if(!droppedCapacity) { droppedObject->getPicture()->removeFromParent(); } log("GOT IT! draggedCapacity=%d droppedCapacity=%d",draggedCapacity,droppedCapacity); DataManager::getInstance()->save(); } break; } } }; _eventDispatcher->addEventListenerWithSceneGraphPriority(backgroundListener, draggedObject->getPicture()); }