GameObjectsView find_with_tag(const String &tag){ GameObjectsView result; std::copy_if(game_objects.begin(), game_objects.end(), std::back_inserter(result), [&](auto &item){ return item->tag == tag; }); return result; }
void EventManager::fire(Event* event) { GameObjects receivers = event->getReceivers(); GameObjects::iterator i = receivers.begin(); for(; i < receivers.end(); i++) { (*i)->receiveEvent(event); } delete event; }
void GSVe3ViewOtherPlayers::NextPlayer() { Player* localPlayer = GetLocalPlayer(); GameObjects* gos = TheObjectManager::Instance()->GetGameObjects(); int id = -1; if (m_player) { id = m_player->GetId(); } // Remember first player, go to if we reach the end Player* first = 0; bool found = false; for (auto it = gos->begin(); it != gos->end(); ++it) { GameObject* go = it->second; Player* p = dynamic_cast<Player*>(go); if (p == localPlayer) { continue; } if (p && !first) { first = p; } if (p && p->GetId() > id) { m_player = p; found = true; break; } } if (!found) { m_player = first; // wrap around if poss } if (m_player) { ShowPlayer(m_player, m_gui); } }
void DeleteDeadObjects() { static Game* game = TheGame::Instance(); GameObjects* objs = game->GetGameObjects(); // map for (auto it = objs->begin(); it != objs->end(); ) { PGameObject obj = it->second; ++it; WWGameObject* ww = dynamic_cast<WWGameObject*>(obj.GetPtr()); if (ww && ww->IsDead()) { #ifdef DEBUG_DELETE_DEAD std::cout << "Deleting object " << Describe(ww) << "\n"; #endif ww->RemoveFromGame(); } } }
int main (int argc, char * const argv[]) { GameObjectManager* objectManager = GameObjectManager::getInstance(); NodeManager *nodeManager = NodeManager::getInstance(); { Node *node; for (int y = 0; y <= 14; y++) { for (int x = 0; x <= 19; x++) { node = new Node(x * 32, y * 32); nodeManager->addNode(node); } } } Hill* hill1 = new Hill(StrainYellow, nodeManager->getEmptyRandomNode()); Hill* hill2 = new Hill(StrainRed, nodeManager->getEmptyRandomNode()); Hill* hill3 = new Hill(StrainBlue, nodeManager->getEmptyRandomNode()); objectManager->add(hill1); objectManager->add(hill2); objectManager->add(hill3); Ants ants; Strain antStrains[4] = { StrainYellow, StrainRed, StrainBlue, StrainRed }; for (int i = 0; i < 4; i++) { Ant* ant = new Ant(antStrains[i], nodeManager->getEmptyRandomNode()); objectManager->add(ant); ants.push_back(ant); } for (int i = 0; i < 10; i++) { Spice *spice = new Spice(nodeManager->getEmptyRandomNode()); objectManager->addSpice(spice); } sf::RenderWindow App(sf::VideoMode(640, 480), "Ants"); App.SetFramerateLimit(60); while (App.IsOpened()) { sf::Event Event; while (App.GetEvent(Event)) { if (Event.Type == sf::Event::Closed) App.Close(); if (Event.Type == sf::Event::MouseButtonPressed) { const sf::Input& Input = App.GetInput(); cout << "Mouse cursor position: " << Input.GetMouseX() << "/" << Input.GetMouseY() << endl; sf::Vector2f mouse(Input.GetMouseX(), Input.GetMouseY()); bool shiftDown = Input.IsKeyDown(sf::Key::LShift) || Input.IsKeyDown(sf::Key::RShift); // cycle all gameobjects // if position matches mouse input -> select // if nothing matches -> deselect all GameObjects allObjects = objectManager->getObjects(); GameObjects::iterator i = allObjects.begin(); for (; i < allObjects.end(); i++) { GameObject* object = *i; sf::Vector2f left_upper = object->getSprite()->GetPosition(); sf::Vector2f right_lower = left_upper; // TODO: clean up with new vector2f class left_upper.x -= 16; left_upper.y -= 16; right_lower.x += PIXELS_PER_NODE/2; right_lower.y += PIXELS_PER_NODE/2; if (left_upper.x <= mouse.x && right_lower.x >= mouse.x && left_upper.y <= mouse.y && right_lower.y >= mouse.y) { object->setSelectionStatus(true); } else if (!shiftDown) { object->setSelectionStatus(false); } } } } App.Clear(); // trigger updates objectManager->trigger(); //display nodes Nodes nodes = nodeManager->getNodes(); Nodes::iterator n = nodes.begin(); for (; n < nodes.end(); n++) { App.Draw(*(*n)->getSprite()); } // update ants Ants::iterator a = ants.begin(); for(; a < ants.end(); a++) { (*a)->handleCurrentAction(App.GetFrameTime()); } // display game objects GameObjects objects = objectManager->getObjects(); GameObjects::iterator i = objects.begin(); for (; i < objects.end(); i++) { GameObject* object = *i; if (object->hasSprite()) { App.Draw(*object->getSprite()); if (object->isSelected()) { // draw rect sf::Vector2f position = object->getSprite()->GetPosition(); sf::Vector2f new_position = position; // TODO: clean up with new vector2f class position.x -= 16; position.y -= 16; new_position.x += PIXELS_PER_NODE/2; new_position.y += PIXELS_PER_NODE/2; sf::Shape rect = sf::Shape::Rectangle(position, new_position, object->getSelectionColor(), true, object->getSelectionColor()); rect.EnableFill(false); App.Draw(rect); } } } App.Display(); } return EXIT_SUCCESS; }
GameObject* PickObject(const Vec2f& mouseScreen) { // Get object carried by player - don't pick Ve1Object* carried = 0; Player* player = GetLocalPlayer(); if (player) { carried = player->GetCarrying(); } Vec3f mouseWorldNear; Vec3f mouseWorldFar; Unproject(mouseScreen, 0, &mouseWorldNear); Unproject(mouseScreen, 1, &mouseWorldFar); LineSeg lineSeg(mouseWorldNear, mouseWorldFar); GameObject* selectedObj = 0; GameObjects* objs = TheGame::Instance()->GetGameObjects(); float bestDist = 9e20f; for (GameObjects::iterator it = objs->begin(); it != objs->end(); ++it) { GameObject* pgo = it->second; Assert(pgo); Ve1Object* v = dynamic_cast<Ve1Object*>(pgo); Assert(v); if (!v->IsPickable()) { #ifdef PICK_DEBUG std::cout << " Obj " << pgo->GetId() << " is not pickable.\n"; #endif continue; } if (v == carried) { // Can't select it then! std::cout << "Skipping carried object " << *v << "\n"; continue; } const AABB& aabb = pgo->GetAABB(); if (Clip(lineSeg, aabb, 0)) { #ifdef PICK_DEBUG std::cout << " Obj " << pgo->GetId() << " IS PICKED!\n"; #endif // Line seg intersects this box // Choose object whose centre (position) is closest to line seg..? // float dist = LineSeg(mouseWorldNear, mouseWorldFar).SqDist(pgo->GetPos()); float dist = (mouseWorldNear - pgo->GetPos()).SqLen(); // pick closest // Treat skybox as least attractive option, followed by terrain if (dynamic_cast<Skybox*>(v)) { #ifdef PICK_DEBUG std::cout << " Obj " << pgo->GetId() << " skybox so treated as far away\n"; #endif dist = 9e19f; } else if (dynamic_cast<Terrain*>(v)) { #ifdef PICK_DEBUG std::cout << " Obj " << pgo->GetId() << " terrain so treated as far away\n"; #endif dist = 9e18f; } #ifdef PICK_DEBUG std::cout << " Obj " << pgo->GetId() << " sqDist: " << dist << "\n"; #endif if (dist < bestDist) { #ifdef PICK_DEBUG std::cout << " Obj " << pgo->GetId() << " AND IS CLOSEST!\n"; #endif bestDist = dist; selectedObj = pgo; } } else { #ifdef PICK_DEBUG std::cout << " Obj " << pgo->GetId() << " is not picked.\n"; #endif } } return selectedObj; }
void GSMain::Update() { static Lurker* lurker = TheLurker::Instance(); static Game* game = TheGame::Instance(); SceneGraph* scenegraph = GetGameSceneGraph(); lurker->Update(); // Disable pause button if Lurk msg showing // Freeze if displaying tutorial text if (lurker->IsDisplayingMsg()) { m_pauseButton->SetVisible(false); scenegraph->Update(); // DO still update the scene graph? // TODO Also still update Bird? GameObjects* objs = game->GetGameObjects(); for (auto it = objs->begin(); it != objs->end(); ++it) { GameObject* obj = it->second; if (dynamic_cast<Bird*>(obj)) { obj->Update(); } } } else if (m_exitState == IS_EXITING) { m_pauseButton->SetVisible(false); scenegraph->Update(); // DO still update the scene graph? // TODO Also still update Bird? GameObjects* objs = game->GetGameObjects(); for (auto it = objs->begin(); it != objs->end(); ++it) { GameObject* obj = it->second; if (dynamic_cast<Player*>(obj)) { obj->Update(); } Pet* pet = dynamic_cast<Pet*>(obj); if (pet && pet->IsTeleporting()) { pet->Update(); } if (dynamic_cast<Exit*>(obj)) { obj->Update(); } } } else { if (m_exitState == FINISHED_EXITING) { m_exitTimer += TheTimer::Instance()->GetDt(); static const float EXIT_DELAY_2 = ROConfig()->GetFloat("exit-delay-2"); if (m_exitTimer > EXIT_DELAY_2) { game->SetCurrentState(TheGSLevelComplete::Instance()); } } else { m_pauseButton->SetVisible(true); Player* player = Player::GetPlayer(AMJU_P1); float y = -(player->GetPos().y); DepthUpdate(y); TheProcGen::Instance()->AddLayerWhenReady(player->GetPos().x); ThePowerUpManager::Instance()->Update(); game->UpdateGameObjects(); DeleteDeadObjects(); TheCollisionManager::Instance()->Update(); scenegraph->Update(); TheShadowManager::Instance()->Update(); } } }