Game::Game(Item* parent) : DisplayItemFrame(parent), m_viewWorld(this), m_lightSystem(this, view()->world()), m_particleSystem(m_lightSystem.lightTexture()->sourceItem()) { view()->initialize(); lightSystem()->initialize(); particleSystem()->setLightSystem(lightSystem()); load(":/json/map00.json"); }
void StaticLight::initialize(QWorld *w) { auto fixture = std::make_unique<Box2DBox>(); fixture->setPosition(QPointF(-radius(), -radius())); fixture->setSize(QSizeF(2 * radius(), 2 * radius())); fixture->setSensor(true); fixture->setVisible(false); fixture->setShadowCaster(false); addFixture(std::move(fixture)); assert(lightSystem()); lightSystem()->addLight(this); QBody::initialize(w); }
void Game::sizeChanged() { DisplayItemFrame::sizeChanged(); #ifdef Q_OS_ANDROID const QSize maxres(800, 600); #else const QSize maxres(1920, 1080); #endif QSize resolution(std::min(int(size().width()), maxres.width()), std::min(int(size().height()), maxres.height())); lightSystem()->setResolution(resolution); lightSystem()->setSize(size()); }
void World::step() { QWorld::step(); particleSystem()->step(); lightSystem()->step(); mainAction()->step(); emit m_worldObject.fpsChanged(); emit m_worldObject.playerScoreChanged(); }
bool Game::write(QJsonObject& obj) const { QJsonObject viewWorld; view()->write(viewWorld); obj["viewWorld"] = viewWorld; QJsonObject lights; lightSystem()->write(lights); obj["lightSystem"] = lights; return true; }
std::unique_ptr<SceneGraph::Node> ParticleSystem::synchronize( std::unique_ptr<SceneGraph::Node> root) { Node* node = static_cast<Node*>(root.get()); if (!root) { root = std::make_unique<Node>(); node = static_cast<Node*>(root.get()); QColor color; color.setRgbF(1.0, 0.5, 0.5, 1.0); node->material()->setColor(color); node->material()->setLightPosition(QVector3D(0.5, 0.5, 0.1)); } assert(lightSystem()); assert(lightSystem()->normalMap()); node->update(m_particle); node->material()->setTime(time()); node->material()->setNormalMap(lightSystem()->normalMap()->shaderNode()); return root; }
std::unique_ptr<SceneGraph::Node> EnlightedItems::synchronize( std::unique_ptr<SceneGraph::Node> root) { if (!root) root = std::make_unique<Node>(); Node* node = static_cast<Node*>(root.get()); while (node->firstChild()) node->removeChild(node->firstChild()); if (m_state & Reset) { m_state &= ~Reset; node->clear(); } while (!m_destroyedFixture.empty()) { node->destroyedFixture(m_destroyedFixture.back()); m_destroyedFixture.pop_back(); } uint it = 0; QRectF visibleArea = world()->visibleRect(); for (StaticLight* light : lightSystem()->visibleLights()) { if (!light->dynamicLight()) continue; QRectF lightRect = light->matrix().mapRect(light->boundingRect()); QRectF rect = visibleArea.intersected(lightRect); for (QFixture* f : world()->fixtures(rect)) { if (f->shadowCaster()) { EnlightedNode* enlightedNode = node->getNode(f, light, it++); node->appendChild(enlightedNode); } } } update(); return root; }
void World::onFixtureDestroyed(QFixture *f) { lightSystem()->onFixtureDestroyed(f); }
void World::onBodyAdded(QBody *body) { QWorld::onBodyAdded(body); assert(lightSystem()); lightSystem()->addBody(body); }
QWorld* EnlightedItems::world() const { return lightSystem()->world(); }
void StaticLight::visibleChanged() { if (lightSystem()) { lightSystem()->lightVisibilityChanged(this); } }
void StaticLight::destroyBody() { if (lightSystem()) lightSystem()->removeLight(this); Light::destroyBody(); }
void Game::clear() { lightSystem()->clear(); view()->world()->clear(); }
bool Game::read(const QJsonObject& obj) { view()->read(obj["viewWorld"].toObject()); lightSystem()->read(obj["lightSystem"].toObject()); return true; }