コード例 #1
0
ファイル: Player.cpp プロジェクト: TheZoq2/Silverheart
void Player::activation()
{
	float lowestDist = 90;
	bool partFound = false;
	Part* closestPart = NULL;
	int closestPartID;
	float partDist = lowestDist + 1;

	//Looping thru all of the parts
	for(int i = 0; i < world->getPartAmount(); i++)
	{
		if(world->getPartUsable(i) == 1) //Making sure we are only looking at parts that can actually be used
		{
			float partX = world->getPartX(i);
			float partY = world->getPartY(i);

			float distX = partX - x;
			float distY = partY - y;
			float dist = agk::Sqrt(distX * distX + distY * distY);

			int useRange = 90;
			if(dist < useRange) //Checking if the part is within reach
			{
				partFound = true;
				
				if(dist < lowestDist) //CHecking if this is the closest part found
				{
					lowestDist = dist; //Changing the lowest distance
					partDist = dist;

					closestPart = world->getPartFromID(i); //Changing the lowest part we have found
					closestPartID = i;
				}
			}
		}
	}

	bool npcFound = false;
	NPC* closestNPC = NULL;
	float npcDist = lowestDist + 1;
	
	//Looping thru all the NPCs (Using the)
	for(unsigned int i = 0; i < defaultNPCGroup->getNPCAmount(); i++)
	{
		NPC* npc = defaultNPCGroup->getNPC(i);

		if(npc != NULL)
		{
			float npcX = npc->getX();
			float npcY = npc->getY();

			float distX = npcX - x;
			float distY = npcY - y;
			float dist = agk::Sqrt(distX * distX + distY * distY);

			if(dist < lowestDist)
			{
				npcFound = true;

				lowestDist = dist;
				npcDist = dist;

				closestNPC = npc;
			}
		}
	}

	if((partFound == true && closestPart != NULL) || (npcFound == true && closestNPC != NULL))
	{
		if(partDist < npcDist)
		{
			//Positioning the activation text
			agk::SetTextVisible(activateText, 1);
			agk::SetTextPosition(activateText, agk::WorldToScreenX( closestPart->getX() ), agk::WorldToScreenY( closestPart->getY() ));

			//Changing the text to the use text of the part
			std::string fText;
			fText = i_activateName;
			fText.append(") ");
			fText.append(closestPart->getUseMsg());

			agk::SetTextString(activateText, fText.data());

			//Adding the background
			agk::SetSpritePosition(activateSprite, agk::WorldToScreenX(closestPart->getX()) -5.0f, agk::WorldToScreenY(closestPart->getY()) - 2.5f);
			agk::SetSpriteVisible(activateSprite, 1);
			agk::SetSpriteScale(activateSprite, agk::GetTextTotalWidth(activateText) + 10, agk::GetTextTotalHeight(activateText) + 5);

			//Checking if the part is activated
			if(Input::activate() == true)
			{
				//Getting the script of the item
				std::string actScript;
				actScript = ("scripts/");
				actScript.append(closestPart->getActScript());

				//Saving the part that was activated last for use with labels in lua
				world->setLastActive(closestPartID);

				//Checking if this is a lua script or an old script
				if(actScript.find(".lua") != -1)
				{
					LuaHandler::runScript(actScript);
				}
				else
				{
					//Starting the script
					Script::run(actScript.data(), closestPart, world, this);
				}
			}
		}
		else
		{
			//Positioning the activation text
			agk::SetTextVisible(activateText, 1);
			agk::SetTextPosition(activateText, agk::WorldToScreenX( closestNPC->getX() ), agk::WorldToScreenY( closestNPC->getY() ));

			//Changing the text to the use text of the part
			std::string fText;
			fText = i_activateName;
			fText.append(") ");
			fText.append("talk");

			agk::SetTextString(activateText, fText.data());

			//Adding the background
			agk::SetSpritePosition(activateSprite, agk::WorldToScreenX(closestNPC->getX()) -5.0f, agk::WorldToScreenY(closestNPC->getY()) - 2.5f);
			agk::SetSpriteVisible(activateSprite, 1);
			agk::SetSpriteScale(activateSprite, agk::GetTextTotalWidth(activateText) + 10, agk::GetTextTotalHeight(activateText) + 5);

			//Checking if the part is activated
			if(Input::activate() == true)
			{
				closestNPC->startConversation();
			}
		}
	}
	else //No parts where found, hide the text
	{
		agk::SetTextVisible(activateText, 0);
		agk::SetSpriteVisible(activateSprite, 0);
	}
}