コード例 #1
0
ファイル: FloorMapLayer.cpp プロジェクト: anarki1234/mota
void FloorMapLayer::confirm_open(OKCancelDialog::RETURN_TYPE type, const npc_t& npc)
{
    if (type == OKCancelDialog::CANCEL)
    {
        stop_and_clear();
    }
    else
    {
        _warrior->stand_auto();
        auto npc_layer = _tiled_map->getLayer("npc");
        auto door = npc_layer->getTileAt(Vec2(npc.x, 11 - npc.y));
        npc_layer->setupTileSprite(door, Vec2(npc.x, 11 - npc.y), npc.gid);
        door->runAction(CCSequence::create(FadeOut::create(0.5f), RemoveSelf::create(),
            CCCallFunc::create(std::bind(&FloorMapLayer::confirm_open_impl, this, npc)), nullptr));

        // 如果是竖门需要将其上方的附加节点一并去除
        auto uplink = get_tile_prop(npc.gid, "uplink").asInt();
        if (1 == uplink)
        {
            auto npc_iter = find(_npcs.begin(), _npcs.end(), npc_t(npc.x, npc.y + 1, 0));
            if (npc_iter != _npcs.end())
            {
                const auto& npc_up = *npc_iter;
                auto door_up = npc_layer->getTileAt(Vec2(npc_up.x, 11 - npc_up.y));
                npc_layer->setupTileSprite(door_up, Vec2(npc_up.x, 11 - npc_up.y), npc_up.gid);
                door_up->runAction(CCSequence::create(FadeOut::create(0.5f), RemoveSelf::create(), nullptr));
            }
        }
    }
}
コード例 #2
0
ファイル: TileMapTest2.cpp プロジェクト: AnySDK/Sample_Lua
//------------------------------------------------------------------
//
// TMXOrthoTest4New
//
//------------------------------------------------------------------
TMXOrthoTest4New::TMXOrthoTest4New()
{
    auto map = cocos2d::experimental::TMXTiledMap::create("TileMaps/orthogonal-test4.tmx");
    addChild(map, 0, kTagTileMap);
    
    Size CC_UNUSED s1 = map->getContentSize();
    CCLOG("ContentSize: %f, %f", s1.width,s1.height);
    
    map->setAnchorPoint(Vec2(0, 0));

    auto layer = map->getLayer("Layer 0");
    auto s = layer->getLayerSize();
    
    Sprite* sprite;
    sprite = layer->getTileAt(Vec2(0,0));
    sprite->setScale(2);
    sprite = layer->getTileAt(Vec2(s.width-1,0));
    sprite->setScale(2);
    sprite = layer->getTileAt(Vec2(0,s.height-1));
    sprite->setScale(2);
    sprite = layer->getTileAt(Vec2(s.width-1,s.height-1));
    sprite->setScale(2);

    schedule( CC_SCHEDULE_SELECTOR(TMXOrthoTest4New::removeSprite), 2 );

}
コード例 #3
0
ファイル: wyHexagonAStarMap.cpp プロジェクト: Adoni/WiEngine
wyHexagonAStarMap::wyHexagonAStarMap(int width, int height) :
	wyAStarMap(), m_width(width), m_height(height) {

	m_tiles = wyArrayNew(m_width * m_height);
	for (int x = 0; x < m_width; x++) {
		for (int y = 0; y < m_height; y++) {
			wyArrayPush(m_tiles, WYNEW wyAStarTile(TILE_FREE, x, y));
		}
	}

	// auto clilds
	for (int i = 0; i < m_tiles->num; i++) {
		wyAStarTile* tile = (wyAStarTile*) wyArrayGet(m_tiles, i);
		int y_max = (tile->getX() % 2) ? 2 : 1;
		int y_min = (tile->getX() % 2) ? 0 : -1;
		int child_y = (tile->getX() % 2) ? -1 : 1;

		wyAStarTile* neighbor = getTileAt(tile->getX(), tile->getY() + child_y);
		if (neighbor != tile && neighbor != NULL) {
			tile->pushChild(neighbor);
		}

		for (int x = -1; x < 2; x++) {
			for (int y = y_min; y < y_max; y++) {
				neighbor = getTileAt(tile->getX() + x, tile->getY() + y);
				if (neighbor != tile && neighbor != NULL) {
					tile->pushChild(neighbor);
				}
			}
		}
	}
}
コード例 #4
0
ファイル: TileMapTest.cpp プロジェクト: 289997171/cocos2d-x
//------------------------------------------------------------------
//
// TMXOrthoTest4
//
//------------------------------------------------------------------
TMXOrthoTest4::TMXOrthoTest4()
{
    auto map = TMXTiledMap::create("TileMaps/orthogonal-test4.tmx");
    addChild(map, 0, kTagTileMap);
    
    Size CC_UNUSED s1 = map->getContentSize();
    CCLOG("ContentSize: %f, %f", s1.width,s1.height);

    SpriteBatchNode* child = nullptr;
    
    auto& children = map->getChildren();
    
    for(const auto &node : children) {
        child = static_cast<SpriteBatchNode*>(node);
        child->getTexture()->setAntiAliasTexParameters();
    }
    
    map->setAnchorPoint(Vec2(0, 0));

    auto layer = map->getLayer("Layer 0");
    auto s = layer->getLayerSize();
    
    Sprite* sprite;
    sprite = layer->getTileAt(Vec2(0,0));
    sprite->setScale(2);
    sprite = layer->getTileAt(Vec2(s.width-1,0));
    sprite->setScale(2);
    sprite = layer->getTileAt(Vec2(0,s.height-1));
    sprite->setScale(2);
    sprite = layer->getTileAt(Vec2(s.width-1,s.height-1));
    sprite->setScale(2);

    schedule( schedule_selector(TMXOrthoTest4::removeSprite), 2 );

}
コード例 #5
0
ファイル: HelloWorldScene.cpp プロジェクト: Ratel13/book-code
void HelloWorld::createMapAndGetTile()
{
    auto mapnotchange = TMXTiledMap::create("orthogonal-test4.tmx");
    addChild(mapnotchange, 0, 1);
    mapnotchange->setPosition(50,240);
    
    auto map = TMXTiledMap::create("orthogonal-test4.tmx");
    addChild(map, 0, 2);
    map->setPosition(570,240);
    
    SpriteBatchNode* child = nullptr;
    
    auto& children = map->getChildren();
    
    for(const auto &node : children) {
        child = static_cast<SpriteBatchNode*>(node);
        child->getTexture()->setAntiAliasTexParameters();
    }
    
    map->setAnchorPoint(Point(0, 0));
    
    auto layer = map->getLayer("Layer 0");
    auto s = layer->getLayerSize();
    
    Sprite* sprite;
    sprite = layer->getTileAt(Point(0,0));
    sprite->setScale(2);
    sprite = layer->getTileAt(Point(s.width-1,0));
    sprite->setScale(2);
    sprite = layer->getTileAt(Point(0,s.height-1));
    sprite->setScale(2);
    sprite = layer->getTileAt(Point(s.width-1,s.height-1));
    sprite->setScale(2);
}
コード例 #6
0
ファイル: FloorMapLayer.cpp プロジェクト: zxsean/mota
void FloorMapLayer::confirm_open(OKCancelDialog::RETURN_TYPE type, const Floor::npc_t& npc)
{
    if (type == OKCancelDialog::CANCEL)
    {
        stop_and_clear();
    }
    else
    {
        _warrior->stand_auto();
        _warrior->set_lock(true);   // 设置等待开门的过程中不可重新寻路,以免出现计算混乱
        auto npc_layer = _tiled_map->getLayer("npc");
        auto door = npc_layer->getTileAt(Vec2(npc.pos.x, 11 - npc.pos.y));
        npc_layer->setupTileSprite(door, Vec2(npc.pos.x, 11 - npc.pos.y), npc.gid);
        const float OPEN_DURATION = 0.5f;
        door->runAction(CCSequence::create(FadeOut::create(OPEN_DURATION), RemoveSelf::create(),
            CCCallFunc::create(std::bind(&FloorMapLayer::confirm_open_impl, this, npc)), nullptr));

        // 如果是竖门需要将其上方的附加节点一并去除
        auto uplink = get_tile_prop(npc.gid, "uplink").asInt();
        if (1 == uplink)
        {
            auto npc_up = Floor::GetInstance()->get_floor_info(_floor).find_npc_by_pos(Floor::position_t(npc.pos.x, npc.pos.y + 1));
            if (nullptr != npc_up)
            {
                auto door_up = npc_layer->getTileAt(Vec2(npc_up->pos.x, 11 - npc_up->pos.y));
                npc_layer->setupTileSprite(door_up, Vec2(npc_up->pos.x, 11 - npc_up->pos.y), npc_up->gid);
                door_up->runAction(CCSequence::create(FadeOut::create(OPEN_DURATION), RemoveSelf::create(), nullptr));
            }
        }
    }
}
コード例 #7
0
ファイル: GridWorld.cpp プロジェクト: UBC-Snowbots/IGVC2015
/*
BEFORE ATTEMPTING TO READ THIS FILE, PLEASE HAVE A BASIC UNDERSTANDING OF 
MT-D*LITE FROM READING ITS ORIGINAL RESEARCH PAPER'S PSEUDO-CODE.
*/
GridWorld::GridWorld(unsigned int length, unsigned int width, int radius,
	Coords startCoords, Coords goalCoords){
	this->length = length;
	this->width = width;
	this->radius = radius;

	for (unsigned int y = 0; y < length; y++){
		for (unsigned int x = 0; x < width; x++){
			world.push_back(new Tile(x, y, 10));
		}
	}
	
	//In version B, start and goal are switch
	goal = getTileAt(startCoords.first, startCoords.second);
	start = getTileAt(goalCoords.first, goalCoords.second);

	//Initializing the pathfinder's default values
	km = 0;
	previous = goal;
	start->rhs = 0;

	start->isOpen = true;
	start->h = calculateH(start);
	start->key = GridWorld::KeyPair(start->h, 0);
	open.push_back(start);
}
コード例 #8
0
ファイル: TileMapTest.cpp プロジェクト: 289997171/cocos2d-x
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;
}
コード例 #9
0
ファイル: Farm.cpp プロジェクト: Samuel-Lewis/Crypt
Farm::Farm(float density): Region(density, "Farm")
{
	// Set standard background
	for (int x = 0; x < REGIONSIZE; x++)
	{
		for (int y = 0; y < REGIONSIZE; y++)
		{
			if (lbRNG::decision(0.05))
			{
				// Random tree or two
				replace(x,y, new Tile("grass-light","tree-light"));
			} else {
				replace(x,y, new Tile("grass-light"));
				
				if (lbRNG::decision(0.01))
				{
					getTileAt(x, y)->setMob(new Sheep(getTileAt(x, y)));
					
				}
			}
			
		}
	}
	
	// Add Barn
	Feature barn(5,5);
	barn.addAll("floor-wood-light");
	barn.addBorder("wall-wood-light");
	barn.addDoor("door-wood-open","door-wood-closed", N);

	// Add fields
	Field field1(8,5);
	Field field2(4,9);
	
	field1.generate();
	field2.generate();
	
	field1.clearBorder();
	
	field1.addBorder("dirt-light");
	field1.addBorder("fence-wood");
	field1.addDoor("air","air",N);
	field1.addDoor("air","air",S);
	
	// Place down new tiles
	replace(3,3,barn.tiles);
	replace(2,9,field1.tiles);
	replace(10,5,field2.tiles);
	
	INFO("Generated Farm");
}
コード例 #10
0
ファイル: TileMapTest.cpp プロジェクト: 289997171/cocos2d-x
void TMXOrthoTest4::removeSprite(float dt)
{
    unschedule(schedule_selector(TMXOrthoTest4::removeSprite));

    auto map = static_cast<TMXTiledMap*>( getChildByTag(kTagTileMap) );
    auto layer = map->getLayer("Layer 0");
    auto s = layer->getLayerSize();

    auto sprite = layer->getTileAt( Vec2(s.width-1,0) );
    auto sprite2 = layer->getTileAt(Vec2(s.width-1, s.height-1));
    layer->removeChild(sprite, true);
    auto sprite3 = layer->getTileAt(Vec2(2, s.height-1));
    layer->removeChild(sprite3, true);
    layer->removeChild(sprite2, true);
}
コード例 #11
0
ファイル: TileMapTest2.cpp プロジェクト: AnySDK/Sample_Lua
void TMXOrthoTest4New::removeSprite(float dt)
{
    unschedule(CC_SCHEDULE_SELECTOR(TMXOrthoTest4New::removeSprite));

    auto map = static_cast<cocos2d::experimental::TMXTiledMap*>( getChildByTag(kTagTileMap) );
    auto layer = map->getLayer("Layer 0");
    auto s = layer->getLayerSize();

    auto sprite = layer->getTileAt( Vec2(s.width-1,0) );
    auto sprite2 = layer->getTileAt(Vec2(s.width-1, s.height-1));
    layer->removeChild(sprite, true);
    auto sprite3 = layer->getTileAt(Vec2(2, s.height-1));
    layer->removeChild(sprite3, true);
    layer->removeChild(sprite2, true);
}
コード例 #12
0
ファイル: TileMapTest2.cpp プロジェクト: AnySDK/Sample_Lua
void TileMapEditTestNew::updateMap(float dt)
{
    // IMPORTANT
    //   The only limitation is that you cannot change an empty, or assign an empty tile to a tile
    //   The value 0 not rendered so don't assign or change a tile with value 0

    auto tilemap = (TileMapAtlas*) getChildByTag(kTagTileMap);
    
    //
    // For example you can iterate over all the tiles
    // using this code, but try to avoid the iteration
    // over all your tiles in every frame. It's very expensive
    //    for(int x=0; x < tilemap.tgaInfo->width; x++) {
    //        for(int y=0; y < tilemap.tgaInfo->height; y++) {
    //            Color3B c =[tilemap tileAt:Size(x,y));
    //            if( c.r != 0 ) {
    //                ////----CCLOG("%d,%d = %d", x,y,c.r);
    //            }
    //        }
    //    }
    
    // NEW since v0.7
    Color3B c = tilemap->getTileAt(Vec2(13,21));        
    c.r++;
    c.r %= 50;
    if( c.r==0)
        c.r=1;
    
    // NEW since v0.7
    tilemap->setTile(c, Vec2(13,21) );             
}
コード例 #13
0
ファイル: Map.cpp プロジェクト: jotak/shahnarman
// -----------------------------------------------------------------
// Name : changeTerrainType
// -----------------------------------------------------------------
void Map::changeTerrainType(CoordsMap pos, u8 uType, Server * pServer)
{
    MapTile * pTile = getTileAt(pos);
    pTile->m_uTerrainType = uType;
    if (pServer != NULL)
    {
        NetworkData msg(NETWORKMSG_CHANGE_TERRAIN);
        msg.addLong(pos.x);
        msg.addLong(pos.y);
        msg.addLong(uType);
        pServer->sendMessageToAllClients(&msg);
    }
    else
    {
        pTile->resetTexture(pTile->getDisplay());
        for (int i = -1; i <= 1; i++)
        {
            for (int j = -1; j <= 1; j++)
            {
                int x = pos.x + i;
                int y = pos.y + j;
                if (x < 0 || y < 0 || x >= m_iWidth || y >= m_iHeight)
                    continue;
                setTileMask(x, y);
            }
        }
    }
}
コード例 #14
0
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;
}
コード例 #15
0
ファイル: ui.c プロジェクト: looselyrigorous/asciijong
tile_t *getTileFromUser(void) {
	double x,y;
	tile_t tile;

	printf("Enter two doubles separated by comma: ");
	scanf("%lf,%lf", &x, &y);

	tile = getTileAt(x,y);
	return tile;
}
コード例 #16
0
ファイル: GridWorld.cpp プロジェクト: UBC-Snowbots/IGVC2015
/*
This method handles the inflation of surrounding tiles. When an obsticle
is detected, the surrounding tile's cost can be inflated to make the robot
avoid getting anywhere near the obsticle. By the nature of pathfinding on
grid maps, the resultant path tends to "hug the wall", however this will make
the path have some distance between itself and any obsticles
*/
void GridWorld::inflate(unsigned int x, unsigned int y, double newCost){
	updateCost(x, y, newCost);
	for (int dy = -radius; dy <= radius; dy++){
		for (int dx = -radius; dx <= radius; dx++){
			if (abs(dy) + abs(dx) != 0){
				Tile* t = getTileAt(x + dx, y + dy);
				if (t != 0 && newCost > t->cost){
					updateCost(x + dx, y + dy, INFLATION);
				}

			}
		}
	}
}
コード例 #17
0
ファイル: GridWorld.cpp プロジェクト: UBC-Snowbots/IGVC2015
std::vector<GridWorld::Tile*> GridWorld::getNeighbours(Tile*& tile){
	std::vector<GridWorld::Tile*> neighbours;
	for (int dy = -1; dy <= 1; dy++){
		for (int dx = -1; dx <= 1; dx++){
			if (abs(dy) + abs(dx) != 0){
				Tile* neighbour = getTileAt(tile->x + dx, tile->y + dy);
				if (neighbour != 0){
					neighbours.push_back(neighbour);
				}
			}
		}
	}
	return neighbours;
}
コード例 #18
0
std::vector<sf::Vector2f> TileMap::getOverlappingTiles(const Aabb<float> & aabb)
{
	std::vector<sf::Vector2f> tiles;
	for (int x = aabb.left / m_tileSize; x <= (aabb.left + aabb.width) / m_tileSize; ++x)
	{
		for (int y = aabb.top / m_tileSize; y <= (aabb.top + aabb.height) / m_tileSize; ++y)
		{
			sf::Vector2f v = sf::Vector2f(m_tileSize * static_cast<float>(x), m_tileSize * static_cast<float>(y));
			if (getTileAt(v.x, v.y) > 0)
				tiles.push_back(v);
		}
	}
	return tiles;
}
コード例 #19
0
ファイル: city.cpp プロジェクト: Dirbaio/Durum-Lare
//Dice si una persona en FROM ve a una persona en TO
bool City::visible(sf::Vector2f from, sf::Vector2f to)
{
    bool fromGrass = getTileAt(from) == 11;
    bool toGrass = getTileAt(to) == 11;
    
    //Desde fuera no se puede ver dentro..
	if(!fromGrass && toGrass) return false;
	float d = Utils::distance(from, to);
	if(fromGrass && toGrass && d > 36) return false;
    if(d > 180) return false;

	
    for(int i = 0; i <= 20; i++)
    {
        sf::Vector2f pt = (from*float(i) + to*float(20-i))/20.0f;
        //Si los dos dentro, ha de ser todo grass.
        if(fromGrass && getTileAt(pt) != 11)
        	return false;
        if(occupedXY(pt.x, pt.y))
            return false;
    }

    return true;
}
コード例 #20
0
ファイル: GridWorld.cpp プロジェクト: UBC-Snowbots/IGVC2015
std::vector<GridWorld::Tile*> GridWorld::getNeighbours(Tile*& tile){
	//It should be impossible to return a null neighbour, therefore I didn't bother
	//with null pointer checks whenever I use this method
	std::vector<GridWorld::Tile*> neighbours;
	for (int dy = -1; dy <= 1; dy++){
		for (int dx = -1; dx <= 1; dx++){
			if (abs(dy) + abs(dx) != 0){
				Tile* neighbour = getTileAt(tile->x + dx, tile->y + dy);
				if (neighbour != 0){
					neighbours.push_back(neighbour);
				}
			}
		}
	}

	return neighbours;
}
コード例 #21
0
dtStatus dtTileCache::addTile(unsigned char* data, const int dataSize, unsigned char flags, dtCompressedTileRef* result)
{
	// Make sure the data is in right format.
	dtTileCacheLayerHeader* header = (dtTileCacheLayerHeader*)data;
	if (header->magic != DT_TILECACHE_MAGIC)
		return DT_FAILURE | DT_WRONG_MAGIC;
	if (header->version != DT_TILECACHE_VERSION)
		return DT_FAILURE | DT_WRONG_VERSION;
	
	// Make sure the location is free.
	if (getTileAt(header->tx, header->ty, header->tlayer))
		return DT_FAILURE;
	
	// Allocate a tile.
	dtCompressedTile* tile = 0;
	if (m_nextFreeTile)
	{
		tile = m_nextFreeTile;
		m_nextFreeTile = tile->next;
		tile->next = 0;
	}
	
	// Make sure we could allocate a tile.
	if (!tile)
		return DT_FAILURE | DT_OUT_OF_MEMORY;
	
	// Insert tile into the position lut.
	int h = TileCacheFunc::computeTileHash(header->tx, header->ty, m_tileLutMask);
	tile->next = m_posLookup[h];
	m_posLookup[h] = tile;
	
	// Init tile.
	const int headerSize = dtAlign4(sizeof(dtTileCacheLayerHeader));
	tile->header = (dtTileCacheLayerHeader*)data;
	tile->data = data;
	tile->dataSize = dataSize;
	tile->compressed = tile->data + headerSize;
	tile->compressedSize = tile->dataSize - headerSize;
	tile->flags = flags;
	
	if (result)
		*result = getTileRef(tile);
	
	return DT_SUCCESS;
}
コード例 #22
0
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;
}
コード例 #23
0
bool RogueScene::isTiledMapColisionLayer(MapIndex touchPointMapIndex)
{
    // 障害物判定
    auto pMapLayer = (TMXTiledMap*)getChildByTag(kTiledMapTag);
    
    auto pColisionLayer = pMapLayer->getLayer("colision");
    // TileMapは左上から0,0なので座標変換する
    auto touchPointTileIndex = mapIndexToTileIndex(touchPointMapIndex);
    auto pTileSprite = pColisionLayer->getTileAt(Point(touchPointTileIndex.x, touchPointTileIndex.y));
    if (pTileSprite)
    {
        // 障害物なので移動とかできない
        CCLOG("colision touchPointTileIndex x = %d y = %d", touchPointTileIndex.x, touchPointTileIndex.y);
        return true;
    }
    
    return false;
}
コード例 #24
0
ファイル: CB_Room.cpp プロジェクト: GaryCXJk/ClashBattle
bool CB_Room::isFree(uint32_t x, uint32_t y, uint32_t w, uint32_t h)
{
    if(x + w > width)
    {
        w = width - x;
    }
    if(y + h > height)
    {
        h = height - y;
    }

    for(uint32_t iY = 0; iY < h; iY++)
    {
        for(uint32_t iX = 0; iX < w; iX++)
        {
            if(getTileAt(x + iX, y + iY) != 0)
            {
                return false;
            }
        }
    }
    return true;
}
コード例 #25
0
ファイル: TileMapTest2.cpp プロジェクト: AnySDK/Sample_Lua
//------------------------------------------------------------------
//
// TMXIsoVertexZNew
//
//------------------------------------------------------------------
TMXIsoVertexZNew::TMXIsoVertexZNew()
{
    auto map = cocos2d::experimental::TMXTiledMap::create("TileMaps/iso-test-vertexz.tmx");
    addChild(map, 0, kTagTileMap);
    
    auto s = map->getContentSize();
    map->setPosition( Vec2(-s.width/2,0) );
    CCLOG("ContentSize: %f, %f", s.width,s.height);
    
    // because I'm lazy, I'm reusing a tile as an sprite, but since this method uses vertexZ, you
    // can use any Sprite and it will work OK.
    auto layer = map->getLayer("Trees");
    _tamara = layer->getTileAt( Vec2(29,29) );
    _tamara->retain();
    
    auto move = MoveBy::create(10, Vec2(300,250) * (1/CC_CONTENT_SCALE_FACTOR()));
    auto back = move->reverse();
    auto seq = Sequence::create(move, back,nullptr);
    _tamara->runAction( RepeatForever::create(seq) );
    
    schedule( CC_SCHEDULE_SELECTOR(TMXIsoVertexZNew::repositionSprite));
    
}
コード例 #26
0
ファイル: TileMapTest.cpp プロジェクト: 289997171/cocos2d-x
//------------------------------------------------------------------
//
// TMXOrthoVertexZ
//
//------------------------------------------------------------------
TMXOrthoVertexZ::TMXOrthoVertexZ()
{
    auto map = TMXTiledMap::create("TileMaps/orthogonal-test-vertexz.tmx");
    addChild(map, 0, kTagTileMap);
    
    Size CC_UNUSED s = map->getContentSize();
    CCLOG("ContentSize: %f, %f", s.width,s.height);
    
    // because I'm lazy, I'm reusing a tile as an sprite, but since this method uses vertexZ, you
    // can use any Sprite and it will work OK.
    auto layer = map->getLayer("trees");
    _tamara = layer->getTileAt(Vec2(0,11));
    CCLOG("%p vertexZ: %f", _tamara, _tamara->getPositionZ());
    _tamara->retain();

    auto move = MoveBy::create(10, Vec2(400,450) * (1/CC_CONTENT_SCALE_FACTOR()));
    auto back = move->reverse();
    auto seq = Sequence::create(move, back,NULL);
    _tamara->runAction( RepeatForever::create(seq));
    
    schedule(schedule_selector(TMXOrthoVertexZ::repositionSprite));
    
}
コード例 #27
0
ファイル: FloorMapLayer.cpp プロジェクト: anarki1234/mota
void FloorMapLayer::pick_up_item_impl(const npc_t& npc, const cocos2d::Vec2& target_pos, const std::function<void()>& callback)
{
    auto npc_layer = _tiled_map->getLayer("npc");
    auto item = npc_layer->getTileAt(Vec2(npc.x, 11 - npc.y));
    npc_layer->setupTileSprite(item, Vec2(npc.x, 11 - npc.y), npc.gid);
    auto start_pos = this->convertToNodeSpace(item->getParent()->convertToWorldSpace(item->getPosition()));
    item->retain();
    item->removeFromParentAndCleanup(false);
    item->setPosition(start_pos);
    this->addChild(item);
    item->release();

    // TODO.. cocos2d-x tiled bug. 如果一个Layer只剩下一个tile,getTileAt内部设置gid为0不起作用,目前找不到解决办法
    npc_layer->setTileGID(999, Vec2(npc.x, 11 - npc.y));

    auto duration = item->getPosition().distance(target_pos) / 1000.0f;
    item->runAction(Sequence::create(
        Spawn::createWithTwoActions(MoveTo::create(duration, target_pos), ScaleTo::create(duration, 0.6f)),
        CallFunc::create([item, callback]() {
        item->removeFromParentAndCleanup(true);
        callback();
    }), nullptr));
}
コード例 #28
0
ファイル: GridWorld.cpp プロジェクト: UBC-Snowbots/IGVC2015
void GridWorld::printWorld() const{
	std::cout << "H:" << std::endl;
	for (unsigned int y = 0; y < length; y++){
		for (unsigned int x = 0; x < width; x++){
			printf("%2.0lf ", getTileAt(x, y)->h);
		}
		std::cout << std::endl;
	}

	std::cout << "C:" << std::endl;
	for (unsigned int y = 0; y < length; y++){
		for (unsigned int x = 0; x < width; x++){
			double cost = getTileAt(x, y)->cost;
			if (cost == PF_INFINITY){
				printf("-1 ");
			}
			else if (cost == INFLATION){
				printf("^^ ");
			}
			else{
				printf("%2.0lf ", cost);
			}

		}
		std::cout << std::endl;
	}

	std::cout << "G:" << std::endl;
	for (unsigned int y = 0; y < length; y++){
		for (unsigned int x = 0; x < width; x++){
			printf("%2.0lf ", getTileAt(x, y)->g == PF_INFINITY ? -1 : getTileAt(x, y)->g);
		}
		std::cout << std::endl;
	}

	std::cout << "RHS:" << std::endl;
	for (unsigned int y = 0; y < length; y++){
		for (unsigned int x = 0; x < width; x++){
			printf("%2.0lf ", getTileAt(x, y)->rhs == PF_INFINITY ? -1 : getTileAt(x, y)->rhs);
		}
		std::cout << std::endl;
	}
	std::cout << std::endl;
}
コード例 #29
0
ファイル: DetourNavMesh.cpp プロジェクト: superwow/foton.core
/// @par
///
/// The add operation will fail if the data is in the wrong format, the allocated tile
/// space is full, or there is a tile already at the specified reference.
///
/// The lastRef parameter is used to restore a tile with the same tile
/// reference it had previously used.  In this case the #dtPolyRef's for the
/// tile will be restored to the same values they were before the tile was 
/// removed.
///
/// @see dtCreateNavMeshData, #removeTile
dtStatus dtNavMesh::addTile(unsigned char* data, int dataSize, int flags,
							dtTileRef lastRef, dtTileRef* result)
{
	// Make sure the data is in right format.
	dtMeshHeader* header = (dtMeshHeader*)data;
	if (header->magic != DT_NAVMESH_MAGIC)
		return DT_FAILURE | DT_WRONG_MAGIC;
	if (header->version != DT_NAVMESH_VERSION)
		return DT_FAILURE | DT_WRONG_VERSION;
		
	// Make sure the location is free.
	if (getTileAt(header->x, header->y, header->layer))
		return DT_FAILURE;
		
	// Allocate a tile.
	dtMeshTile* tile = 0;
	if (!lastRef)
	{
		if (m_nextFree)
		{
			tile = m_nextFree;
			m_nextFree = tile->next;
			tile->next = 0;
		}
	}
	else
	{
		// Try to relocate the tile to specific index with same salt.
		int tileIndex = (int)decodePolyIdTile((dtPolyRef)lastRef);
		if (tileIndex >= m_maxTiles)
			return DT_FAILURE | DT_OUT_OF_MEMORY;
		// Try to find the specific tile id from the free list.
		dtMeshTile* target = &m_tiles[tileIndex];
		dtMeshTile* prev = 0;
		tile = m_nextFree;
		while (tile && tile != target)
		{
			prev = tile;
			tile = tile->next;
		}
		// Could not find the correct location.
		if (tile != target)
			return DT_FAILURE | DT_OUT_OF_MEMORY;
		// Remove from freelist
		if (!prev)
			m_nextFree = tile->next;
		else
			prev->next = tile->next;

		// Restore salt.
		tile->salt = decodePolyIdSalt((dtPolyRef)lastRef);
	}

	// Make sure we could allocate a tile.
	if (!tile)
		return DT_FAILURE | DT_OUT_OF_MEMORY;
	
	// Insert tile into the position lut.
	int h = computeTileHash(header->x, header->y, m_tileLutMask);
	tile->next = m_posLookup[h];
	m_posLookup[h] = tile;
	
	// Patch header pointers.
	const int headerSize = dtAlign4(sizeof(dtMeshHeader));
	const int vertsSize = dtAlign4(sizeof(float)*3*header->vertCount);
	const int polysSize = dtAlign4(sizeof(dtPoly)*header->polyCount);
	const int linksSize = dtAlign4(sizeof(dtLink)*(header->maxLinkCount));
	const int detailMeshesSize = dtAlign4(sizeof(dtPolyDetail)*header->detailMeshCount);
	const int detailVertsSize = dtAlign4(sizeof(float)*3*header->detailVertCount);
	const int detailTrisSize = dtAlign4(sizeof(unsigned char)*4*header->detailTriCount);
	const int bvtreeSize = dtAlign4(sizeof(dtBVNode)*header->bvNodeCount);
	const int offMeshLinksSize = dtAlign4(sizeof(dtOffMeshConnection)*header->offMeshConCount);
	
	unsigned char* d = data + headerSize;
	tile->verts = (float*)d; d += vertsSize;
	tile->polys = (dtPoly*)d; d += polysSize;
	tile->links = (dtLink*)d; d += linksSize;
	tile->detailMeshes = (dtPolyDetail*)d; d += detailMeshesSize;
	tile->detailVerts = (float*)d; d += detailVertsSize;
	tile->detailTris = (unsigned char*)d; d += detailTrisSize;
	tile->bvTree = (dtBVNode*)d; d += bvtreeSize;
	tile->offMeshCons = (dtOffMeshConnection*)d; d += offMeshLinksSize;

	// If there are no items in the bvtree, reset the tree pointer.
	if (!bvtreeSize)
		tile->bvTree = 0;

	// Build links freelist
	tile->linksFreeList = 0;
	tile->links[header->maxLinkCount-1].next = DT_NULL_LINK;
	for (int i = 0; i < header->maxLinkCount-1; ++i)
		tile->links[i].next = i+1;

	// Init tile.
	tile->header = header;
	tile->data = data;
	tile->dataSize = dataSize;
	tile->flags = flags;

	connectIntLinks(tile);
	baseOffMeshLinks(tile);

	// Create connections with neighbour tiles.
	static const int MAX_NEIS = 32;
	dtMeshTile* neis[MAX_NEIS];
	int nneis;
	
	// Connect with layers in current tile.
	nneis = getTilesAt(header->x, header->y, neis, MAX_NEIS);
	for (int j = 0; j < nneis; ++j)
	{
		if (neis[j] != tile)
		{
			connectExtLinks(tile, neis[j], -1);
			connectExtLinks(neis[j], tile, -1);
		}
		connectExtOffMeshLinks(tile, neis[j], -1);
		connectExtOffMeshLinks(neis[j], tile, -1);
	}
	
	// Connect with neighbour tiles.
	for (int i = 0; i < 8; ++i)
	{
		nneis = getNeighbourTilesAt(header->x, header->y, i, neis, MAX_NEIS);
		for (int j = 0; j < nneis; ++j)
		{
			connectExtLinks(tile, neis[j], i);
			connectExtLinks(neis[j], tile, dtOppositeTile(i));
			connectExtOffMeshLinks(tile, neis[j], i);
			connectExtOffMeshLinks(neis[j], tile, dtOppositeTile(i));
		}
	}
	
	if (result)
		*result = getTileRef(tile);
	
	return DT_SUCCESS;
}
コード例 #30
0
ファイル: GridWorld.cpp プロジェクト: UBC-Snowbots/IGVC2015
/*
This method does the same thing as the pseudo-code's updateVertex(),
except for grids instead of graphs.

Pathfinding algorimths tend to be demonstraited with a graph rather than a grid,
in order to update the cost between two tiles we must update both the tile and its neighbour.
*/
void GridWorld::updateCost(unsigned int x, unsigned int y, double newCost){
	static int count = 1;
	count++;
	Tile* tile = getTileAt(x, y);
	
	printf("Updating <%d, %d> from %2.0lf to %2.0lf - Update: %d\n", x, y, tile->cost, newCost, count);
	km += calculateH(previous);
	previous = goal;

	//I am aware that the following code below could be refactored by 50%
	//since it's repeating itself with only a few changes

	double oldCost = tile->cost;
	double oldCostToTile, newCostToTile;

	//Update CURRENT by finding its new minimum RHS-value from NEIGHBOURS
	std::vector<Tile*> neighbours(getNeighbours(tile));
	for (int i = 0; i < neighbours.size(); i++){
		tile->cost = oldCost;
		oldCostToTile = calculateC(tile, neighbours[i]);

		tile->cost = newCost;
		newCostToTile = calculateC(tile, neighbours[i]);

		if (oldCostToTile > newCostToTile){
			if (tile != start && tile->rhs > neighbours[i]->g + newCostToTile){
				tile->successor = neighbours[i];
				tile->rhs = neighbours[i]->g + newCostToTile;
			}
		}
		else if (tile != start && tile->successor == neighbours[i]){
			TilePair minSucc(getMinSuccessor(tile));
			tile->rhs = minSucc.second;
			tile->successor = (tile->rhs == PF_INFINITY ? 0 : minSucc.first);
		}
	}

	updateVertex(tile);

	//Update all NEIGHBOURING cells by finding their new min RHS-values from CURRENT
	for (int i = 0; i < neighbours.size(); i++){
		tile->cost = oldCost;
		oldCostToTile = calculateC(tile, neighbours[i]);

		tile->cost = newCost;
		newCostToTile = calculateC(tile, neighbours[i]);

		if (oldCostToTile > newCostToTile){
			if (neighbours[i] != start && neighbours[i]->rhs > tile->g + newCostToTile){
				neighbours[i]->successor = tile;
				neighbours[i]->rhs = tile->g + newCostToTile;
				updateVertex(neighbours[i]);
			}

		}
		else if (neighbours[i] != start && neighbours[i]->successor == tile){
			TilePair minSucc(getMinSuccessor(neighbours[i]));
			neighbours[i]->rhs = minSucc.second;
			neighbours[i]->successor = (neighbours[i]->rhs == PF_INFINITY ? 0 : minSucc.first);

			updateVertex(neighbours[i]);
		}
	}

	computeShortestPath();
}