void Test::FireBallInit(unsigned count) { // Seed the RNG srand(static_cast<unsigned>(time(NULL))); // Set the Particle Demo space as the logical space for this gamestate. SetLogicalSpace("Particle Demo"); // Register the Particle Demo space as a space that should be updated each frame. RegisterSpace("Particle Demo"); // We also won't be using the "Game World" space, so we can deregister it. DeRegisterSpace("Game World"); // Create the space for the demo to run in. SpacePtr particleDemo = ENGINE->CreateSpace("Particle Demo"); // Clear out any existing objects in the space, except the camera. particleDemo->Clear(); // Specify which systems should be updated for this demo. particleDemo->AddSystem(GETSYS(CameraSystem)); particleDemo->AddSystem(GETSYS(GLGraphics)); }
/*! * @brief Updates every camera component * * @param dt time if needed */ void CameraSystem::Update(float dt) { for (auto &it : _entities) { //Getting the transform component on the object TransformPtr transform = it->GET_COMPONENT(Transform); //Getting the camera component on the object CameraPtr camera = it->GET_COMPONENT(Camera); WindowSDLPtr window = GETSYS(WindowSDL); std::pair<int, int> windowDimensions = window->GetWindowDimensions(); //Perspective of the camera, read more at //http://www.songho.ca/opengl/gl_projectionmatrix.html camera->_pespective = glm::perspectiveFov(camera->_fieldOfView, transform->scale.x, transform->scale.y, camera->_nearPlane, camera->_farPlane); //Orthographic projection for the camera, read more at //http://www.songho.ca/opengl/gl_projectionmatrix.html camera->_ortho = glm::ortho(transform->position.x - transform->scale.x * .5f, transform->position.x + transform->scale.x * .5f, transform->position.y - transform->scale.y * .5f, transform->position.y + transform->scale.y * .5f, camera->_nearPlane, camera->_farPlane); //View matrix for the camera, read more at //http://3dgep.com/understanding-the-view-matrix/ camera->_viewMatrix = glm::lookAt(glm::vec3(transform->position.x, transform->position.y, 5.0f), glm::vec3(transform->position.x, transform->position.y, 0.0f), camera->_upVec); } }
void Test::Update(float dt) { switch (DemoToUse) { case(Demo::Pretzel) : PretzelDemo(dt); break; case(Demo::FireBall) : FireBallDemo(dt); break; case Demo::Explosion: ExplosionDemo(200); break; default: break; } // This is wrapped in a try catch block because it's possible the entity acquired by name doesn't have the necessary components. // If that is the case, the try block will encounter and exception, that gets handled by just ignoring the operation and moving on. try { EntityPtr mouse = ENGINE->GetActiveSpace()->GetEntityByName("Mouse"); mouse->GET_COMPONENT(Transform)->position = GETSYS(WindowSDL)->GetMousePosition(); mouse->GET_COMPONENT(Transform)->scale = { 10.f, 10.f }; } catch (...) { } }
void Test::FireBallSpawn(unsigned count) { // Get a reference to the "Game World" space so that we can add an object to it. SpacePtr gameworld = ENGINE->GetSpace("Particle Demo"); glm::vec2 mousepos = GETSYS(WindowSDL)->GetMousePosition(); for (unsigned i = 0; i < count; ++i) { // get a degree anywhere in a circle (0 through 360) float angle = RandFloat() * 360.f; float scale = RandFloat() * 80.f; glm::vec2 startPos = { cosf(angle) * scale + mousepos.x, sinf(angle) * scale + mousepos.y }; glm::vec2 velocity = { 0.f, RandFloat() * 8.f - 16.f }; float lifetime = RandFloat() * 2.f; EntityPtr entity = ENGINE->Factory().create("Fire Particle"); entity->GET_COMPONENT(Transform)->scale = glm::vec2(3.5f, 3.5f); entity->GET_COMPONENT(Transform)->position = startPos; entity->GET_COMPONENT(Transform)->rotation = 0.f; entity->GET_COMPONENT(Particle)->velocity = velocity; entity->GET_COMPONENT(Particle)->lifetime = lifetime; // Create a string stream to generate a name for the entity. std::ostringstream name; name << "Fire Particle " << i / 2; // Set the generated name. entity->SetName(name.str()); // Add the object to the game world. gameworld->AddEntity(entity); } }