virtual void onGameEvent(GameEnvironmentInfoProvider* environmentInfo, const std::string& eventName, yam2d::Object* eventObject) { if (eventName == "ItemPicked") { ItemEvent* itemEvent = dynamic_cast<ItemEvent*>(eventObject); assert(itemEvent != 0); yam2d::GameObject* item = itemEvent->getItemGameObject(); int teamIndex = itemEvent->getCharacterGameObject()->getProperties()["teamIndex"].get<int>(); if (teamIndex == getMyTeamIndex()) { yam2d::esLogMessage("My team picked item. Chase enemy HomeBase."); const yam2d::GameObject* homeBase = environmentInfo->getEnemyHomeBase(this); for (size_t i = 0; i < m_controllers.size(); ++i) { m_controllers[i]->resetTargetToShoot(); m_controllers[i]->setMoveTargetObject(homeBase, 1.0f); } } else { yam2d::esLogMessage("Other team picked item. Stop Moving and Shoot enemy."); // Stop and Shoot enemy for (size_t i = 0; i < m_controllers.size(); ++i) { m_controllers[i]->resetMoveTargetObject(); m_controllers[i]->setTargetToShoot(itemEvent->getCharacterController()->getGameObject(), 1.9f, 0.05f); } } } else if (eventName == "ItemDropped") { ItemEvent* itemEvent = dynamic_cast<ItemEvent*>(eventObject); assert(itemEvent != 0); yam2d::esLogMessage("Item dropped. Stop Shooting and Chase Dynamite."); for (size_t i = 0; i < m_controllers.size(); ++i) { m_controllers[i]->resetTargetToShoot(); } // Start going straight to dynamite const yam2d::GameObject* dynamite = environmentInfo->getDynamite(); for (size_t i = 0; i < m_controllers.size(); ++i) { m_controllers[i]->setMoveTargetObject(dynamite, 1.0f); } } else { // Ignore rest message ids. } }
// Called, when game event has ocurred. // 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: // - ObjectSpawned: // Called when new object is spawned to the level. // eventObject: yam2d::GameObject* = object which was spawn. // - ObjectDeleted: // Called when object is deleted from the level. // eventObject: yam2d::GameObject* = object which is going to be deleted. // - ItemPicked: // Called when item has beed picked by game object. // eventObject: ItemPickedEvent* = Item picked event // - ItemDropped: // Called when item has beed dropped by game object. // eventObject: yam2d::GameObject* = object which was dropped (Dynamite). virtual void onGameEvent(GameEnvironmentInfoProvider* environmentInfo, const std::string& eventName, yam2d::Object* eventObject) { if (eventName == "ObjectSpawned" || eventName == "ObjectDeleted") { yam2d::GameObject* gameObject = dynamic_cast<yam2d::GameObject*>(eventObject); assert(gameObject != 0); std::string type = gameObject->getType(); // Don't print spawned weapon projectiles or explosions. if (type != "Missile" && type != "Bullet" && type != "Grenade" && type != "Mine" && type != "SmallExplosion" && type != "MediumExplosion" && type != "BigExplosion" && type != "MineExplosion" && type != "GrenadeExplosion") { // Prints: Soldier, Robot, HomeBase, Flag if (gameObject->getProperties().hasProperty("team")) { std::string teamName = gameObject->getProperties()["team"].get<std::string>(); yam2d::esLogMessage("%s: gameObjectType=%s, teamName=%s", eventName.c_str(), gameObject->getType().c_str(), teamName.c_str()); } else { yam2d::esLogMessage("%s: gameObjectType=%s", eventName.c_str(), gameObject->getType().c_str()); } } } else if (eventName == "ItemPicked") { ItemEvent* itemEvent = dynamic_cast<ItemEvent*>(eventObject); assert(itemEvent != 0); yam2d::GameObject* item = itemEvent->getItemGameObject(); int teamIndex = itemEvent->getCharacterGameObject()->getProperties()["teamIndex"].get<int>(); yam2d::esLogMessage("%s: gameObjectType=%s, team=%d", eventName.c_str(), itemEvent->getItemGameObject()->getType().c_str(), teamIndex); for (size_t i = 0; i < m_autoAttackFlagCarryingBots.size(); ++i) { m_autoAttackFlagCarryingBots[i]->setTargetToShoot(itemEvent->getCharacterController()->getGameObject(), 1.9f, 0.05f); } if (teamIndex == getMyTeamIndex()) { // My team picked item. // Go to enemy home base. const yam2d::GameObject* homeBase = environmentInfo->getEnemyHomeBase(this); for (size_t i = 0; i < m_directMoverAIControllers.size(); ++i) { m_directMoverAIControllers[i]->setMoveTargetObject(homeBase, 1.0f); } for (size_t i = 0; i < m_MyAIControllers.size(); ++i) { m_MyAIControllers[i]->setMoveTargetObject(homeBase, 1.0f); m_MyAIControllers[i]->setNewPath(); } } else { // Other team picked the item. // Go to enemy's enemy == me home base. const yam2d::GameObject* homeBase = environmentInfo->getMyHomeBase(this); for (size_t i = 0; i < m_directMoverAIControllers.size(); ++i) { m_directMoverAIControllers[i]->setMoveTargetObject(homeBase, 1.0f); m_directMoverAIControllers[i]->stop(); } for (size_t i = 0; i < m_MyAIControllers.size(); ++i) { m_MyAIControllers[i]->setMoveTargetObject(homeBase, 1.0f); m_MyAIControllers[i]->setNewPath(); } for (size_t i = 0; i < m_MyAIControllers.size(); ++i) { m_MyAIControllers[i]->setTargetToShoot(itemEvent->getCharacterController()->getGameObject(), 1.1f, 0.05f); m_MyAIControllers[i]->setNewPath(); } } } else if (eventName == "ItemDropped") { ItemEvent* itemEvent = dynamic_cast<ItemEvent*>(eventObject); assert(itemEvent != 0); yam2d::esLogMessage("%s: gameObjectType=%s", eventName.c_str(), itemEvent->getItemGameObject()->getType().c_str()); for (size_t i = 0; i < m_autoAttackFlagCarryingBots.size(); ++i) { m_autoAttackFlagCarryingBots[i]->resetTargetToShoot(); } for (size_t i = 0; i < m_MyAIControllers.size(); ++i) { m_MyAIControllers[i]->resetTargetToShoot(); } // Item propped. // Start going straight to dynamite const yam2d::GameObject* dynamite = environmentInfo->getDynamite(); for (size_t i = 0; i < m_directMoverAIControllers.size(); ++i) { m_directMoverAIControllers[i]->setMoveTargetObject(dynamite, 1.0f); } for (size_t i = 0; i < m_MyAIControllers.size(); ++i) { m_MyAIControllers[i]->setMoveTargetObject(dynamite, 1.0f); m_MyAIControllers[i]->setNewPath(); } } else { assert(0); // Should be impossible, because unknown message id. } }