void Test::SendMsg(EntityPtr e1, EntityPtr e2, Message::Message message) { EntityPtr camera = ENGINE->GetSpace("Particle Demo")->GetCamera(); switch (message) { case(Message::MV_Up) : camera->GET_COMPONENT(Transform)->position.y += 1000.f * ENGINE->Getdt(); break; case(Message::MV_Down) : camera->GET_COMPONENT(Transform)->position.y -= 1000.f * ENGINE->Getdt(); break; case(Message::MV_Left) : camera->GET_COMPONENT(Transform)->position.x -= 1000.f * ENGINE->Getdt(); break; case(Message::MV_Right) : camera->GET_COMPONENT(Transform)->position.x += 1000.f * ENGINE->Getdt(); break; case(Message::MV_BackButton) : ENGINE->PushGamestate(PauseMenuPtr(new PauseMenu())); break; case(Message::MV_PopGamestate) : ENGINE->PopGamestate(); break; default: break; } }
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 WeaponController::FireWeapon(EntityPtr entity) { WeaponPtr weapon = entity->GET_COMPONENT(Weapon); TransformPtr transform = entity->GET_COMPONENT(Transform); if (weapon->shotCooldownRemaining > 0.f) return; // Reset the shot cooldown because the weapon should only shoot at its rate of fire. weapon->shotCooldownRemaining = weapon->rateOfFire; weapon->shots += 1; EntityPtr bullet = ENGINE->Factory().create("Bullet"); bullet->GET_COMPONENT(Parent)->parent = entity; // Start the bullet at the shooter's location bullet->GET_COMPONENT(Transform)->position = transform->position; bullet->GET_COMPONENT(Transform)->rotation = transform->rotation; // Set up the physics info for the bullet float direction = transform->rotation * DEG2RAD; bullet->GET_COMPONENT(RigidBody)->direction = glm::normalize(glm::vec2(cos(direction), sin(direction))); bullet->GET_COMPONENT(RigidBody)->speed = weapon->projectileSpeed; // Set the duration of the bullet bullet->GET_COMPONENT(Lifetime)->lifetime = weapon->projectileLifetime; // Add the bullet to the world! ENGINE->GetActiveSpace()->AddEntity(bullet); }
void Test::ExplosionSpawn(unsigned count) { // Get a reference to the "Game World" space so that we can add an object to it. SpacePtr gameworld = ENGINE->GetSpace("Game World"); gameworld->GetCamera()->GET_COMPONENT(Transform)->scale = { 160.f, 90.f }; // Clear out any existing objects //gameworld.Clear(); for (unsigned i = 0; i < count; ++i) { EntityPtr entity; std::ostringstream name; // Create an entity from the "Box" archetype. entity = ENGINE->Factory().create("Box Particle"); // reduce the scale of the particle so it's a bit smaller entity->GET_COMPONENT(Transform)->scale = glm::vec2(0.5f, 0.5f); // Set the particle properties ParticlePtr particle = entity->GET_COMPONENT(Particle); float xvel = static_cast<float>((rand() % 10000) - 5000) / 10000.f; float yvel = static_cast<float>((rand() % 10000) - 5000) / 10000.f; particle->velocity = glm::normalize(glm::vec2(xvel, yvel)); float r = static_cast<float>(rand() % 1000) / 1000.f; float g = r - static_cast<float>(rand() % 1000) / 1000.f; entity->GET_COMPONENT(Sprite)->_color = glm::vec4(r, g, 0.f, 1.f); // Create a string stream to generate a name for the entity. name << "Fire Particle " << i / 2; // Set the generated name. entity->SetName(name.str()); // Add the object to the game world. gameworld->AddEntity(entity); } }
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); } }
/*! * @brief Draws everything that is in the entities list with a sprite component * @param Entity the entity that is going to be drawn * @param camera the camera used to draw the objects to */ void GLGraphics::DrawEntity(const EntityPtr &Entity, const CameraPtr &camera) { ShaderPtr shader = _shaders.find(Entity->GET_COMPONENT(Sprite)->_shaderName)->second; TransformPtr transform = Entity->GET_COMPONENT(Transform); if (shader.get() != nullptr) { //constructing the matrix of the transform glm::mat4x4 object2world; object2world = glm::translate(object2world, glm::vec3( Entity->GET_COMPONENT(Transform)->position.x, Entity->GET_COMPONENT(Transform)->position.y, 0.0f ) ); object2world = glm::rotate( object2world, transform->rotation * 3.141592f / 180.f, // degrees to radians conversion glm::vec3(0.f, 0.f, 1.0f) ); object2world = glm::scale(object2world, glm::vec3(transform->scale.x, transform->scale.y, 0.f) ); //Updating the uniforms in the shader, has to happen every frame shader->UpdateUniforms("model", object2world); shader->UpdateUniforms("view", camera->_viewMatrix); //Switching the camera's projection matrix based on the flag switch (camera->viewtype) { case Camera::CV_ORTHOGRAPHIC: shader->UpdateUniforms("proj", camera->_ortho); break; case Camera::CV_PERSPECTIVE: shader->UpdateUniforms("proj", camera->_pespective); break; } shader->UpdateUniforms("color", Entity->GET_COMPONENT(Sprite)->_color); shader->Use(); switch (Entity->GET_COMPONENT(Sprite)->mesh) { case Sprite::QUAD: glBindVertexArray(_quadInfo.vao); glDrawElements( GL_TRIANGLE_STRIP, //Draw Type 6, //Number of points GL_UNSIGNED_SHORT, //Data that it is when being read nullptr); break; case Sprite::CIRCLE: glBindVertexArray(_circleInfo.vao); glDrawElements(GL_TRIANGLE_FAN, 30000, GL_UNSIGNED_SHORT, nullptr); break; default: glBindVertexArray(_quadInfo.vao); glDrawElements(GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_SHORT, nullptr); break; } } }