Exemplo n.º 1
0
bool BattleMap::init()
{
	//////////////////////////////
	// 1. super init first
	if ( !CCLayer::init() )
	{
		return false;
	}

	m_notuseditems = new CCArray();

	m_touch = NULL; 
	m_caTarget = NULL;
	m_caTarCharas = NULL;

	cancontrol = false;
	vx= 0;vy = 0;
	m_touch = NULL;
	b_battle = -1;
	m_mi=-1;m_mj=-1;
	cs_y.clear();cs_r.clear();cs_b.clear();
	
	//2. TileMap初始化

	CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
	sheet = CCSpriteBatchNode::create("Images/test.png");
	cache->addSpriteFramesWithFile("Images/test.plist");

	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

	m_tilemap = CCTMXTiledMap::create(CCString::createWithFormat("map/%s.tmx",map_path.c_str())->getCString());
	addChild(m_tilemap, MapdDepth, kTagMap);
	CCSize s = m_tilemap->getContentSize();
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	m_tilemap->setAnchorPoint(ccp(0, 0));

	CCSize ms = m_tilemap->getMapSize();
	CCSize ts = m_tilemap->getTileSize();

	float r = (ms.width + ms.height) / 2;
	rw = r * ts.width;
	rh = r * ts.height;

	max_x = m_tilemap->getMapSize().width;
	max_y = m_tilemap->getMapSize().height;
	int mapWidth = m_tilemap->getMapSize().width * m_tilemap->getTileSize().width;
	int mapHeight = m_tilemap->getMapSize().height * m_tilemap->getTileSize().height;
	x0 = (ms.height - 1) * ts.width / 2;
	y0 = rh - ts.height;
	dtx = ts.width / 2;
	dty = ts.height / 2;

	dx = rw-mapWidth;
	dy = rh-mapHeight;

	m_tilemap->setPosition(ccp(0,0));
	x0 -= dx;
	y0 -= dy;

	//3. b2world初始化
	//////////////////////////////////////////////////////////////////////////

	b2Vec2 gravity = b2Vec2(0.0f, 0.0f);  

	_world = new b2World(gravity); 

	colse = new MyContactListener();
	_world->SetContactListener(colse);

	b2BodyDef groundBodyDef;  
	groundBodyDef.position.Set(0.0f, 0.0f);							// 设置位置   
	b2Body *groundBody = _world->CreateBody(&groundBodyDef);  

	b2ChainShape chain;												//使用chain将地图包围起来 

	b2FixtureDef sd;  
	sd.shape = &chain;  
	sd.density = 0.0f;  
	sd.restitution = 0.0f;  // Edge 的弹性如何~ 


	b2Vec2 vs[4];
	vs[0].Set((ms.height * ts.width/2 -dx)/ PTM_RATIO, (rh - dy) / PTM_RATIO);
	vs[1].Set((rw - dx) / PTM_RATIO, (ms.height * ts.height/2 - dy) / PTM_RATIO);
	vs[2].Set((ms.width * ts.width/2 - dx) / PTM_RATIO, -dy/PTM_RATIO);
	vs[3].Set(-dx/PTM_RATIO, (ms.width * ts.height/2 - dy )/ PTM_RATIO);


	chain.CreateLoop(vs, 4);

	Entiles* kted = new Entiles();
	kted->name = "edge";
	groundBody->SetUserData(kted);
	groundBody->CreateFixture(&sd);
	m_notuseditems->addObject(kted);
	//////////////////////////////////////////////////////////////////////////
#ifdef DEBUGDRAW

	m_debugDraw =  new GLESDebugDraw( PTM_RATIO );
	_world->SetDebugDraw(m_debugDraw);

	unsigned int flags = 0;
	flags += b2Draw::e_shapeBit;
	flags += b2Draw::e_jointBit;
	flags += b2Draw::e_aabbBit;
	flags += b2Draw::e_pairBit;
	flags += b2Draw::e_centerOfMassBit;
	m_debugDraw->SetFlags(flags);		

#endif	


	//////////////////////////////////////////////////////////////////////////
	if(!f_load_entile()) return false;

	if(m_itemlist->count() < 1) CC_SAFE_RELEASE_NULL(m_itemlist);
	m_controller = NULL;

	f_setcontroller(m_getEntile("chara_1"));	//ITEMMANAGER设置控制器和镜头
	f_setcamara(m_getEntile("chara_1"));

	//////////////////////////////////////////////////////////////////////////

	//setTouchEnabled(true);
	this->scheduleUpdate();

	//BattleMap的必要性初始化
	CCTMXTiledMap* m_tilemap = (CCTMXTiledMap*) getChildByTag(kTagMap);
	CCTMXLayer* layer = m_tilemap->layerNamed("Battle");
	CCDictionary* colordic;
	colordic = layer->getProperties();
	c_r = 0;c_b = 0; c_y =0;
	c_r = colordic->valueForKey("red")->intValue();
	c_b = colordic->valueForKey("blue")->intValue();
	c_y = colordic->valueForKey("yellow")->intValue();
	b_battle = 1;
	return true;
}