示例#1
0
void CFactory::CreatePlayerUnit(int nType)
{
	PROFILE("CFactory::CreatePlayerUnit(int)");
	//static float xPos = 100;
	//static float yPos = 50;
	CUnit* unit = new CUnit(nType);

	// Use default shallow copy since no dynamic info in creation
	CUnit temp = CGame::GetInstance()->GetUnitInfo(nType);
	unit->SetAttackPower(temp.GetAttackPower());
	unit->SetAttackSpeed(temp.GetAttackSpeed());
	unit->SetMaxHP(temp.GetMaxHP());
	unit->SetCurrentHP(temp.GetMaxHP());
	unit->SetRange(temp.GetRange());
	unit->SetSpeed(temp.GetSpeed());
		
	unit->SetState(IDLE);
	unit->SetDirection(SOUTH_WEST);
	unit->SetIsPlayerUnit(true);
	unit->SetAttackSoundID(CGame::GetInstance()->GetAttackSound(unit->GetType()));
	// Register Events
	unit->SetDeathSoundID(CGame::GetInstance()->GetDeathSound(unit->GetType()));


	// Add to manager
	ObjectManager::GetInstance()->AddObject(unit);

	// Let it know we aren't hanging on to it
	unit->Release();
	STOP("CFactory::CreatePlayerUnit(int)");

}
void CScriptManager::Execute( CAbility* pAbility, CTile* pTile, CUnit* pCaster, CTile* TileCharged )
{
		// Finds the facing for the specified unit
		int face = pCaster->GetFacing();
		std::vector< Vec2D > TilePos;
		
		std::vector< Vec2D > pat;
		if( pAbility->GetIfFacing() )
			pat = CAbilityManager::GetInstance()->GetProperFacing(pCaster->GetFacing(), pAbility, pTile);
		else
			pat = pAbility->GetPattern();
		
		for( unsigned int i = 0; i < pAbility->GetPattern().size(); i++ )
		{
			pat[i].nPosX += pTile->GetPosition().nPosX;
			pat[i].nPosY += pTile->GetPosition().nPosY;

			TilePos.push_back(pat[i]);
		}

		lua_getglobal(L, "OnUse");

		lua_newtable(L);
		CGameManager* pGM = CGameManager::GetInstance();
		vector< CUnit* > affected;
		int nCount = 0;
		int z = (int)TilePos.size()-1;
		for( int i = 0; i <= z; i++ )
		{
			CUnit* tmp = pGM->FindUnit(TilePos[i].nPosX, TilePos[i].nPosY);
		
			if( pAbility->GetType() == SP_CHARGE || pAbility->GetType() == SP_RUSH )
			{
				if( TilePos[i] == TileCharged->GetPosition() )
					break;
			}

			if( tmp == nullptr )
				continue;

			affected.push_back( tmp );

			lua_newtable(L);
			lua_pushstring(L, "posX");
			lua_pushnumber(L, tmp->GetPos().nPosX);
			lua_settable(L, -3);
			lua_pushstring(L, "posY");
			lua_pushnumber(L, tmp->GetPos().nPosY);
			lua_settable(L, -3);
			lua_pushstring(L, "health");
			lua_pushnumber(L, tmp->GetHP());
			lua_settable(L, -3);
			lua_pushstring(L, "speed");
			lua_pushnumber(L, tmp->GetSpeed());
			lua_settable(L, -3);
			lua_pushstring(L, "shielded");
			lua_pushnumber(L, tmp->GetShielded());
			lua_settable(L, -3);
			lua_pushstring(L, "uniqueID");
			lua_pushnumber(L, tmp->GetUniqueID());
			lua_settable(L, -3);
			lua_pushnumber(L, nCount+1);
			nCount++;
			lua_insert(L, -2);
			lua_settable(L, -3);
		}

		lua_setglobal(L, "tUnitData");

		std::string path = "Assets/Ability/" + pAbility->GetLua();
		luaL_dofile(L, path.c_str());
		lua_getglobal(L, "OnUse");
		
		lua_pcall(L, 0, 0, 0);

		lua_getglobal(L, "tUnitData");
		lua_pushnil(L);
	
		vector<std::pair<std::string, int>> tData;
	//	std::pair<std::string, int> tmp;

		tData.clear();

		while(lua_next(L, -2) != 0) 
		{
			if( lua_istable(L, -1) )
			{
				lua_pushnil(L);
				while( lua_next(L, -2) )
				{
					if(lua_isnumber(L, -1))
					{
						std::pair<std::string, int> tmp;
						tmp.first = lua_tostring(L, -2);
						tmp.second = (int)lua_tonumber(L, -1);
						tData.push_back(tmp);
					}
					lua_pop(L, 1);
				}
			}
			else
			{
				if(lua_isnumber(L, -1))
				{
					std::pair<std::string, int> tmp;
					tmp.first = lua_tostring(L, -2);
					tmp.second = (int)lua_tonumber(L, -1);
					tData.push_back(tmp);
				}	
			}
			lua_pop(L, 1);
		}

		/*try
		{
			lua_close(L);
		}
		catch (int e)
		{
			int x = 9;
		}*/

		//lua_close(L);

		int x = 0;
		int y = 0;

		//for( unsigned int i = 0; i < affected.size(); i++ )
		//{
		//	x = 0;
		//	y = 0;
		//	for( unsigned int l = 0 + i*5; l < 5 + 5*i; l++ )
		//	{
		//		if( tData[l].first == "health" )
		//			affected[i]->SetHP(tData[l].second);

		//		if( tData[l].first == "posX" )
		//			x = tData[l].second;

		//		if( tData[l].first == "posY" )
		//			y = tData[l].second;

		//		if( tData[l].first == "speed" )
		//			affected[i]->SetSpeed(tData[l].second);

		//		if( tData[l].first == "shielded" )
		//		{
		//			if( tData[l].second == 1 )
		//				affected[i]->SetShielded(tData[l].second);
		//		}
		//	}
		//	affected[i]->SetPos(x,y);
		//}

		CSoundManager* pSM = CSoundManager::GetInstance();
		pSM->Play(pAbility->GetSound(), false, false);
}
示例#3
0
void CFactory::CreateComputerUnit(int nType)
{
	PROFILE("CFactory::CreateComputerUnit(int)");
	//static float xPos = 100;
	//static float yPos = 50;
	CUnit* unit = new CUnit(nType);

	// Use default shallow copy since no dynamic info in creation
	CUnit temp = CGame::GetInstance()->GetCPUUnitInfo(nType);
	unit->SetAttackPower(temp.GetAttackPower());
	unit->SetAttackSpeed(temp.GetAttackSpeed());
	unit->SetMaxHP(temp.GetMaxHP());
	unit->SetCurrentHP(temp.GetMaxHP());
	unit->SetRange(temp.GetRange());
	unit->SetSpeed(temp.GetSpeed());

	unit->SetState(IDLE);
	unit->SetDirection(NORTH_WEST);
	unit->SetIsPlayerUnit(false);
	// Register Events
	unit->SetAttackSoundID(CGame::GetInstance()->GetAttackSound(unit->GetType()));
	unit->SetDeathSoundID(CGame::GetInstance()->GetDeathSound(unit->GetType()));

	switch(CGame::GetInstance()->GetSelectedCity()->GetID())
	{
		case KCITY1:

			break;
		case KCITY2:
			unit->SetAttackPower(unit->GetAttackPower()+2);
			break;
		case KCITY3:
			unit->SetAttackPower(unit->GetAttackPower()+2);
			unit->SetAttackSpeed(unit->GetAttackSpeed()-(unit->GetAttackSpeed()*.2f));
			unit->SetSpeed(unit->GetSpeed()-(unit->GetSpeed()*.5f));

			break;
		case XCITY1:

			break;
		case XCITY2:
			unit->SetAttackPower(unit->GetAttackPower()+2);

			break;
		case XCITY3:
			unit->SetAttackPower(unit->GetAttackPower()+2);
			unit->SetAttackSpeed(unit->GetAttackSpeed()-(unit->GetAttackSpeed()*.2f));
			unit->SetSpeed(unit->GetSpeed()-(unit->GetSpeed()*.5f));

			break;
		case JCITY1:

			break;
		case JCITY2:
			unit->SetAttackPower(unit->GetAttackPower()+2);

			break;
		case JCITY3:
			unit->SetAttackPower(unit->GetAttackPower()+2);
			unit->SetAttackSpeed(unit->GetAttackSpeed()-(unit->GetAttackSpeed()*.2f));
			unit->SetSpeed(unit->GetSpeed()-(unit->GetSpeed()*.5f));

			break;
	}



	// Add to manager
	ObjectManager::GetInstance()->AddObject(unit);

	// Let it know we aren't hanging on to it
	unit->Release();
	STOP("CFactory::CreateComputerUnit(int)");


}