コード例 #1
0
ファイル: main.cpp プロジェクト: PetriTolonen/TekoalyKurssi
	// Send, when game object receives an event.
	// Event name tells the event in question, each event type may have eventObject data associated with the event.
	//
	// Currently supported eventNames and eventObject data desctiption:
	// - Collision: 
	//		Called when game object collides to another object. 
	//		eventObject: CollisionEvent* = Information about collision.
	// - TakingDamage:
	//		Called when game object is taking damage.
	//		eventObject: yam2d::GameObject* = object which give damage to the object. Typically this is bullet, missile, mine or grenade etc.
	// - ZeroHealth: 
	//		Called when game object is going to die, because of zero health.
	//		eventObject: 0 always.
	virtual void onGameObjectEvent(GameEnvironmentInfoProvider* environmentInfo, yam2d::GameObject* gameObject, const std::string& eventName, yam2d::Object* eventObject)
	{
		if (eventName == "Collision")
		{
			CollisionEvent* collisionEvent = dynamic_cast<CollisionEvent*>(eventObject);
			assert(collisionEvent != 0);
			if (!collisionEvent->isValid()) return;
			yam2d::GameObject* otherGo = collisionEvent->getOtherGameObject();
			std::string otherType = otherGo->getType();
			yam2d::vec2 localNormal = collisionEvent->getLocalNormal();
		}
		else if (eventName == "TakingDamage")
		{
			int teamIndex = gameObject->getProperties()["teamIndex"].get<int>();

			TakingDamageEvent* damageEvent = dynamic_cast<TakingDamageEvent*>(eventObject);
			yam2d::GameObject* damageFromObject = damageEvent->getFromObject();
			float newHealth = damageEvent->getNewHealth();
			yam2d::esLogMessage("%s(team=%d) %s: fromObject=%s. New health: %3.1f", gameObject->getType().c_str(), teamIndex, eventName.c_str(), damageFromObject->getType().c_str(), newHealth);
		}
		else if (eventName == "ZeroHealth")
		{
			int teamIndex = gameObject->getProperties()["teamIndex"].get<int>();
			yam2d::esLogMessage("%s(team=%d) %s.", gameObject->getType().c_str(), teamIndex, eventName.c_str());
		}
		else
		{
			assert(0); // Should be impossible, because unknown message id.
		}
	}
コード例 #2
0
		virtual void onMessage(const std::string& msgName, yam2d::Object* eventObject)
		{
			CharacterController::onMessage(msgName, eventObject);
			if (msgName == "Collision")
			{
				CollisionEvent* collisionEvent = dynamic_cast<CollisionEvent*>(eventObject);
				assert(collisionEvent != 0);
				assert(collisionEvent->getMyGameObject() == getGameObject());
				yam2d::GameObject* otherGo = collisionEvent->getOtherGameObject();
				std::string otherType = otherGo->getType();
				if (otherType == "HomeBase")
				{
					if (hasItem())
					{
						dropItem1();
					}
				}
			}
		}