Scene* Chapter7_1::createScene() { cocos2d::Rect visibleRect = Director::getInstance()->getOpenGLView()->getVisibleRect(); Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); // create a scene auto scene = Scene::create(); // add title auto label = LabelTTF::create("TileMap", "Marker Felt.ttf", 32); label->setPosition(Vec2(visibleRect.origin.x+visibleRect.size.width/2, visibleRect.origin.y+visibleRect.size.height/2).x, Vec2(visibleRect.origin.x+visibleRect.size.width/2, visibleRect.origin.y+visibleRect.size.height).y - 30); scene->addChild(label, -1); //add the menu item for back to main menu label = LabelTTF::create("MainMenu", "Marker Felt.ttf", 32); auto menuItem = MenuItemLabel::create(label); menuItem->setCallback([&](cocos2d::Ref *sender) { Director::getInstance()->replaceScene(Chapter7::createScene()); }); auto menu = Menu::create(menuItem, nullptr); menu->setPosition(Vec2::ZERO); menuItem->setPosition(Vec2(visibleRect.origin.x+visibleRect.size.width - 80, visibleRect.origin.y + 25)); scene->addChild(menu, 1); // TileMap auto map = TMXTiledMap::create("isometric_grass_and_water.tmx"); map->setPosition(Vec2(visibleSize.width/2 - map->getContentSize().width/2, 0)); auto listener1 = EventListenerTouchOneByOne::create(); listener1->onTouchBegan = [](Touch* touch, Event* event){ return true; // if you are consuming it }; listener1->onTouchEnded = [=](Touch* touch, Event* event){ auto layer = map->getLayer("layer0"); Size viewSize = Director::getInstance()->getWinSize(); Vec2 mapCordinate = map->convertToNodeSpace(Director::getInstance()->convertToGL(touch->getLocationInView())); int tileX = mapCordinate.x / map->getTileSize().width; int tileY = (viewSize.height - mapCordinate.y) / map->getTileSize().height; int id = layer->getTileGIDAt(Vec2(tileX, tileY)); Sprite* tileSprite = layer->getTileAt(Vec2(tileX, tileY)); std::cout << "Tile GID: " << id << std::endl; }; Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener1, map); scene->addChild(map, 0, 99); // with a tag of '99' return scene; }
TMXReadWriteTest::TMXReadWriteTest() { _gid = 0; auto map = TMXTiledMap::create("TileMaps/orthogonal-test2.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); CCLOG("ContentSize: %f, %f", s.width,s.height); auto layer = map->getLayer("Layer 0"); layer->getTexture()->setAntiAliasTexParameters(); map->setScale( 1 ); auto tile0 = layer->getTileAt(Vec2(1,63)); auto tile1 = layer->getTileAt(Vec2(2,63)); auto tile2 = layer->getTileAt(Vec2(3,62));//Vec2(1,62)); auto tile3 = layer->getTileAt(Vec2(2,62)); tile0->setAnchorPoint( Vec2(0.5f, 0.5f) ); tile1->setAnchorPoint( Vec2(0.5f, 0.5f) ); tile2->setAnchorPoint( Vec2(0.5f, 0.5f) ); tile3->setAnchorPoint( Vec2(0.5f, 0.5f) ); auto move = MoveBy::create(0.5f, Vec2(0,160)); auto rotate = RotateBy::create(2, 360); auto scale = ScaleBy::create(2, 5); auto opacity = FadeOut::create(2); auto fadein = FadeIn::create(2); auto scaleback = ScaleTo::create(1, 1); auto finish = CallFuncN::create(CC_CALLBACK_1(TMXReadWriteTest::removeSprite, this)); auto seq0 = Sequence::create(move, rotate, scale, opacity, fadein, scaleback, finish, NULL); auto seq1 = seq0->clone(); auto seq2 = seq0->clone(); auto seq3 = seq0->clone(); tile0->runAction(seq0); tile1->runAction(seq1); tile2->runAction(seq2); tile3->runAction(seq3); _gid = layer->getTileGIDAt(Vec2(0,63)); ////----CCLOG("Tile GID at:(0,63) is: %d", _gid); schedule(schedule_selector(TMXReadWriteTest::updateCol), 2.0f); schedule(schedule_selector(TMXReadWriteTest::repaintWithGID), 2.05f); schedule(schedule_selector(TMXReadWriteTest::removeTiles), 1.0f); ////----CCLOG("++++atlas quantity: %d", layer->textureAtlas()->getTotalQuads()); ////----CCLOG("++++children: %d", layer->getChildren()->count() ); _gid2 = 0; }
void HelloWorld::createMapAndGetTileProperity() { auto map = TMXTiledMap::create("ortho-objects.tmx"); addChild(map, 0, 1); auto layer = map->getLayer("logic"); Point playerindex = Point(0,0); int tilegid = layer->getTileGIDAt(playerindex); if(tilegid > 0){ log("Properties:%s",map->getPropertiesForGID(tilegid).asValueMap()["collion"].asString().c_str()); } }
void TMXLayer::setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags flags) { CCASSERT(tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position"); CCASSERT(_tiles, "TMXLayer: the tiles map has been released"); CCASSERT(gid == 0 || gid >= _tileSet->_firstGid, "TMXLayer: invalid gid" ); TMXTileFlags currentFlags; int currentGID = getTileGIDAt(tileCoordinate, ¤tFlags); if (currentGID == gid && currentFlags == flags) return; int gidAndFlags = gid | flags; // setting gid=0 is equal to remove the tile if (gid == 0) { removeTileAt(tileCoordinate); } // empty tile. create a new one else if (currentGID == 0) { int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; setFlaggedTileGIDByIndex(z, gidAndFlags); } // modifying an existing tile with a non-empty tile else { int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; auto it = _spriteContainer.find(z); if (it != _spriteContainer.end()) { Sprite *sprite = it->second.first; Rect rect = _tileSet->getRectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); sprite->setTextureRect(rect, false, rect.size); this->reorderChild(sprite, z); if (flags) { setupTileSprite(sprite, sprite->getPosition(), gidAndFlags); } it->second.second = gidAndFlags; } else { setFlaggedTileGIDByIndex(z, gidAndFlags); } } }
void TMXReadWriteTestNew::repaintWithGID(float dt) { // unschedule:_cmd); auto map = (cocos2d::experimental::TMXTiledMap*)getChildByTag(kTagTileMap); auto layer = (cocos2d::experimental::TMXLayer*)map->getChildByTag(0); auto s = layer->getLayerSize(); for( int x=0; x<s.width;x++) { int y = (int)s.height-1; unsigned int tmpgid = layer->getTileGIDAt( Vec2((float)x, (float)y) ); layer->setTileGID(tmpgid+1, Vec2((float)x, (float)y)); } }
void TMXOrthoFlipRunTimeTestNew::flipIt(float dt) { auto map = (cocos2d::experimental::TMXTiledMap*) getChildByTag(kTagTileMap); auto layer = map->getLayer("Layer 0"); //blue diamond auto tileCoord = Vec2(1,10); int flags; unsigned int GID = layer->getTileGIDAt(tileCoord, (TMXTileFlags*)&flags); // Vertical if( flags & kTMXTileVerticalFlag ) flags &= ~kTMXTileVerticalFlag; else flags |= kTMXTileVerticalFlag; layer->setTileGID(GID ,tileCoord, (TMXTileFlags)flags); tileCoord = Vec2(1,8); GID = layer->getTileGIDAt(tileCoord, (TMXTileFlags*)&flags); // Vertical if( flags & kTMXTileVerticalFlag ) flags &= ~kTMXTileVerticalFlag; else flags |= kTMXTileVerticalFlag; layer->setTileGID(GID ,tileCoord, (TMXTileFlags)flags); tileCoord = Vec2(2,8); GID = layer->getTileGIDAt(tileCoord, (TMXTileFlags*)&flags); // Horizontal if( flags & kTMXTileHorizontalFlag ) flags &= ~kTMXTileHorizontalFlag; else flags |= kTMXTileHorizontalFlag; layer->setTileGID(GID, tileCoord, (TMXTileFlags)flags); }
bool MapModel::canMoveTo(MapPoint checkMapLoc, int z){ if (!isCoordInsideLayer(checkMapLoc, z)) return false; if (!isCoordInsideLayer(checkMapLoc, z - 1)) return false; auto current = getTileGIDAt(checkMapLoc, z); auto below = getTileGIDAt(checkMapLoc, z - 1); if (current != EMPTY_TILE) return false; if (below == EMPTY_TILE) return false; Value props = _tileMap->getPropertiesForGID(below); if (!props.isNull()){ ValueMap map = props.asValueMap(); int type_int = 0; if (map.size() == 0) type_int = 0; else type_int = map.at("walkable").asInt(); if (1 == type_int) return true; } return false; }
void TMXLayer::setTileGID(int gid, const Point& pos, ccTMXTileFlags flags) { CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position"); CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released"); CCASSERT(gid == 0 || gid >= _tileSet->_firstGid, "TMXLayer: invalid gid" ); ccTMXTileFlags currentFlags; int currentGID = getTileGIDAt(pos, ¤tFlags); if (currentGID != gid || currentFlags != flags) { int gidAndFlags = gid | flags; // setting gid=0 is equal to remove the tile if (gid == 0) { removeTileAt(pos); } // empty tile. create a new one else if (currentGID == 0) { insertTileForGID(gidAndFlags, pos); } // modifying an existing tile with a non-empty tile else { int z = pos.x + pos.y * _layerSize.width; Sprite *sprite = static_cast<Sprite*>(getChildByTag(z)); if (sprite) { Rect rect = _tileSet->rectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); sprite->setTextureRect(rect, false, rect.size); if (flags) { setupTileSprite(sprite, sprite->getPosition(), gidAndFlags); } _tiles[z] = gidAndFlags; } else { updateTileForGID(gidAndFlags, pos); } } } }
bool SmartSnake::act() { auto begin_time = clock(); if (turn_1 >= 0) { return false; } bool safe = true; if (hunger > game_map->get_empty_n()) { safe = false; } if (target.first >= 0) { auto food = game_map->getLayer("food"); if (food->getTileGIDAt(Vec2(target.first, target.second)) == 0) { target = pii(-1, -1); } } if (target.first >= 0) { auto dir = game_map->get_target_shortest_path_dir(position, current_dir, target, this, safe); if (dir >= 0) { turn_1 = dir; //log("act 1, dir = %d, delay = %d", dir, clock() - begin_time); return true; } } else { auto targets = game_map->get_foods(this); for (auto t : targets) { auto dir = game_map->get_target_shortest_path_dir(position, current_dir, t, this, safe); if (dir >= 0) { target = t; turn_1 = dir; //log("act 2, dir = %d, delay = %d", dir, clock() - begin_time); return true; } } } int lenght_step_min = 0; auto dir = game_map->get_accessible_last_snake_node_dir(position, current_dir, this, lenght_step_min); if (dir >= 0) { //log("go to (%d, %d)", t.first, t.second); turn_1 = dir; //log("act 3, dir = %d, delay = %d", dir, clock() - begin_time); return true; } //log("!!!!!!!!!!!!!!!failed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); return false; }
bool TMXTiledMapTest::init() { BackLayer::init(); Size visibleSize = Director::getInstance()->getVisibleSize(); auto map = TMXTiledMap::create("tmx/orthogonal-test4.tmx"); addChild(map); auto layer = map->getLayer("Layer 0"); auto tile = layer->getTileAt(Vec2(0, 0)); tile->setScale(2); log("%d", layer->getTileGIDAt(Vec2(1, 0))); log("%d", layer->getTileGIDAt(Vec2(2, 0))); return true; }
TMXMapModel::TMXMapModel(std::string fname) { auto tmx = cocos2d::TMXTiledMap::create(fname); this->tmxView = tmx; auto ground = tmx->getLayer("Layer0"); auto dim = ground->getLayerSize(); this->init(dim.width, dim.height); for (int x = 0; x < dim.width; x++) { for (int y = 0; y < dim.height; y++) { if (ground->getTileGIDAt(cocos2d::Vec2(x, y)) != 11) { this->cells[y][x] = CellType::LAND; } } } }
void TMXLayer::removeTileAt(const Point& pos) { CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position"); CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released"); int gid = getTileGIDAt(pos); if (gid) { int z = pos.x + pos.y * _layerSize.width; ssize_t atlasIndex = atlasIndexForExistantZ(z); // remove tile from GID map _tiles[z] = 0; // remove tile from atlas position array ccCArrayRemoveValueAtIndex(_atlasIndexArray, atlasIndex); // remove it from sprites and/or texture atlas Sprite *sprite = (Sprite*)getChildByTag(z); if (sprite) { SpriteBatchNode::removeChild(sprite, true); } else { _textureAtlas->removeQuadAtIndex(atlasIndex); // update possible children for(const auto &obj : _children) { Sprite* child = static_cast<Sprite*>(obj); ssize_t ai = child->getAtlasIndex(); if ( ai >= atlasIndex ) { child->setAtlasIndex(ai-1); } } } } }
void TiledMapLoader::loadGidFactories(cocos2d::TMXTiledMap& map) { Value* info = nullptr; for (auto& child : map.getChildren()) { auto mapLayer = dynamic_cast<TMXLayer*>(child); if (!mapLayer) { continue; } for (int x = 0; x < map.getMapSize().width; ++x) { for (int y = 0; y < map.getMapSize().height; ++y) { auto currentGID = mapLayer->getTileGIDAt({static_cast<float>(x), static_cast<float>(y)}); if (!gidFactories.count(currentGID)) { continue; } map.getPropertiesForGID(currentGID, &info); ValueMap data; if (info) { data = info->asValueMap(); } if (!data.count("gid")) data["gid"] = Value(static_cast<int>(currentGID)); if (!data.count("x")) data["x"] = Value(static_cast<float>(x)); if (!data.count("y")) data["y"] = Value(static_cast<float>(y)); Configuration config{data, mapLayer->getLayerName(), map, box2dContainer}; for (auto& callback : gidFactories.at(currentGID)) { callback(config); } } } } }
bool TiledMapLayer::init() { if (Layer::init()) { auto map = TMXTiledMap::create("tiledmap/TestMap.tmx"); map->setAnchorPoint(Vec2(0.0f, 0.0f)); map->setPosition(0, 0); this->addChild(map); auto mapLayer = map->getLayer("layer1"); Size maxSize = mapLayer->getLayerSize(); CCLOG("maplayer maxwidth %f maxheight %f" , maxSize.width,maxSize.height); mapGraph = new GridMap((int)maxSize.width,(int)maxSize.height); for (int row = 0; row < maxSize.width; row++) { for (int col = 0; col < maxSize.height; col++) { int gid = mapLayer->getTileGIDAt(Vec2(row,col)); printf(" %d",gid); if (gid == 10) { mapGraph->setGridVertexType(row, col, vertex_vertex); } } printf("\n"); } Dijkstra dij; dij.Execute(*mapGraph, 10, 39 * ID_PARA + 13); Vertex * vertex = mapGraph->getVertex(39, 13); // AStar dij; // dij.Execute(*mapGraph, 10, 39 * ID_PARA + 13); // Vertex * vertex = mapGraph->getVertex(39, 13); printf("path tree length %lld \n", dij.pathTree.size()); // for (auto it = dij.pathTree.find(vertex) , end = dij.pathTree.end(); it->second != 0&& it != end ; it= dij.pathTree.find(it->second)) { // mapLayer->setTileGID(31, Vec2(vertex->getX(), vertex->getY())); // vertex = it->second; // } auto end = dij.pathTree.end(); while (vertex != nullptr) { mapLayer->setTileGID(31, Vec2(vertex->getX(), vertex->getY())); auto it = dij.pathTree.find(vertex); if (it != end && it->second != nullptr) { vertex = it->second; }else{ vertex = nullptr; } } auto dispatcher = Director::getInstance()->getEventDispatcher(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(TiledMapLayer::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(TiledMapLayer::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(TiledMapLayer::onTouchEnded, this); dispatcher->addEventListenerWithSceneGraphPriority(listener, this); return true; } return false; }