コード例 #1
0
ファイル: GameMap.cpp プロジェクト: insooneelife/MOBAserver
GameMap::GameMap(GameWorld* const game_world)
	:
	_game_world(game_world),
	_trigger_system(new TriggerSystem())
{

	_tile_map = cocos2d::TMXTiledMap::create("tilemap/100_160_2.tmx");
	//_tile_map = cocos2d::TMXTiledMap::create("tilemap/diablo2_outdoor_remove_walls.tmx");
	
	auto ms = _tile_map->getMapSize();
	auto ts = _tile_map->getTileSize();
	_sizeX = (ms.width + 0.5f) * ts.width;
	_sizeY = (ms.height + 1) * ts.height / 2;

	CCLOG("%f %f %f %f", _sizeX, _sizeY, ts.width, ts.height);

	_agent_cell_space.reset(
		new AgentCellSpace(
			_sizeX,
			_sizeY,
			Prm.NumCellsX,
			Prm.NumCellsY,
			Prm.NumMaxAgentCellSpace));

	createCollisionAreas();
	createStructures();
	
	/*_trigger_system->registerTrigger(
		new TriggerHealthGiver(
		_game_world,
		new geometry::Circle(Vec2(500, 500), 300),
		"health_trigger1",
		Vec2(500, 500), Vec2(1,1), 200, 0, 0, 1000));*/


	//first of all read and create the navgraph. This must be done before
	//the entities are read from the map file because many of the entities
	//will be linked to a graph node (the graph node will own a pointer
	//to an instance of the entity)
	_nav_graph.reset(new NavGraph(false));
	createGraph();

	//_nav_graph->render();
	CCLOG("Node : %d   Edge : %d", _nav_graph->numActiveNodes(), _nav_graph->numEdges());

	//determine the average distance between graph nodes so that we can
	//partition them efficiently
	_cellspace_neighbor_range = calculateAverageGraphEdgeLength(*_nav_graph) + 1;

	//partition the graph nodes
	partitionNavGraph();
	createTriggers();
	partitionCollisionArea();
}
コード例 #2
0
ファイル: tile_map.cpp プロジェクト: evinstk/Zelda
	TileMap::TileMap(Game& world, TextureManager& textureManager, const TMX& tmx)
		: BaseGameEntity(world, b2BodyDef())
		, mWorld(world)
		, mTextures()
		, mpCollider(nullptr)
		, mpNavGraph(nullptr)
		, mDrawFlags(0)
		, mCellSpaceNeighborhoodRange(1)
		, mpCellSpacePartition(nullptr)
	{
		setDrawOrder(std::numeric_limits<int>::max());

		std::vector<std::vector<sf::VertexArray>> layers;
		tmx.makeVertices(textureManager, mTextures, layers);
		for (auto it = layers.begin(); it != layers.end(); ++it)
		{
			int index = it - layers.begin();
			if (it->size() != mTextures.size()) {
				throw std::runtime_error("Texture and layer component counts are inconsistent.");
			}
			auto pLayer = std::make_unique<Layer>(mWorld, std::move(*it), mTextures);
			pLayer->setDrawOrder(index);
			attachNode(std::move(pLayer));
		}

		const sf::Transform& transform = getWorld().getPixelToWorldTransform();

		mpCollider = std::unique_ptr<CompositeCollider>(tmx.makeCollider(transform));

		mpNavGraph = std::unique_ptr<NavGraph>(tmx.makeNavGraph(transform));

		mCellSpaceNeighborhoodRange = calculateAverageGraphEdgeLength(*mpNavGraph) + 1;

		mpCellSpacePartition = std::make_unique<NavCellSpace>((float)tmx.getTileWidth() / tmx.getWidth(), (float)tmx.getTileHeight() * tmx.getHeight(), tmx.getWidth() / 4, tmx.getHeight() / 4, mpNavGraph->numNodes());

		TileMap::NavGraph::ConstNodeIterator nodeIter(*mpNavGraph);
		for (const TileMap::NavGraph::Node* pNode = nodeIter.begin(); !nodeIter.end(); pNode = nodeIter.next())
		{
			mpCellSpacePartition->addEntity(pNode);
		}

		std::vector<b2Fixture*> fixtures;
		mpCollider->createFixtures(getBody(), fixtures);
	}