Entity *XMLAreaLoader::LoadEntity(tinyxml2::XMLElement *element) { auto bus = std::make_shared<ComponentMsgBus>(); InputComponent *input = LoadInputComponent(element->FirstChildElement("InputComponent"), bus); MoveComponent *move = LoadMoveComponent(element->FirstChildElement("MoveComponent"), bus); RenderComponent *render = LoadRenderComponent(element->FirstChildElement("RenderComponent"), bus); Entity *result = new Entity(input, move, render); result->Dir = (DIR)atoi(element->FirstChildElement("Dir")->GetText()); result->Size = LoadVec2(element->FirstChildElement("Size")) * 64; Vec2 gridPos = LoadVec2(element->FirstChildElement("Pos")); result->SetGridXY((int)gridPos.x, (int)gridPos.y); result->SetMsgBus(bus.get()); return result; }
void StaticImage::Initialize(const StaticImageDef& image_def, std::vector<fplbase::Material*> materials, fplbase::Shader* shader, int cannonical_window_height) { image_def_ = &image_def; materials_ = materials; current_material_index_ = 0; shader_ = shader; scale_ = LoadVec2(image_def_->draw_scale()); texture_position_ = LoadVec2(image_def_->texture_position()); color_ = mathfu::kOnes4f; one_over_cannonical_window_height_ = 1.0f / static_cast<float>(cannonical_window_height); is_visible_ = image_def_->visible() != 0; assert(Valid()); }
WARPDETAILS_T XMLAreaLoader::LoadWarpDetails(tinyxml2::XMLElement *element) { WARPDETAILS_T result; result.area = element->FirstChildElement("Area")->GetText(); result.dir = (DIR)atoi(element->FirstChildElement("Dir")->GetText()); result.pos = LoadVec2(element->FirstChildElement("Pos")); return result; }
void TouchscreenButton::Render(fplbase::Renderer& renderer) { static const float kButtonZDepth = 0.0f; if (!is_visible_) { return; } renderer.set_color(vec4(color_)); auto mat = (button_.is_down() && down_material_ != nullptr) ? down_material_ : up_current_ < up_materials_.size() ? up_materials_[up_current_] : nullptr; if (!mat) return; // This is an invisible button. const vec2 window_size = vec2(renderer.window_size()); const float texture_scale = window_size.y() * one_over_cannonical_window_height_; vec2 base_size = LoadVec2( is_highlighted_ ? button_def_->draw_scale_highlighted() : (button_.is_down() ? button_def_->draw_scale_pressed() : button_def_->draw_scale_normal())); auto pulse = sinf(static_cast<float>(elapsed_time_) / 100.0f); if (is_highlighted_) { base_size += mathfu::kOnes2f * pulse * 0.05f; } vec3 texture_size = texture_scale * vec3(mat->textures()[0]->size().x() * base_size.x(), -mat->textures()[0]->size().y() * base_size.y(), 0); vec3 position = vec3(button_def()->texture_position()->x() * window_size.x(), button_def()->texture_position()->y() * window_size.y(), kButtonZDepth); renderer.set_color(mathfu::kOnes4f); if (is_active_ || inactive_shader_ == nullptr) { shader_->Set(renderer); } else { inactive_shader_->Set(renderer); } mat->Set(renderer); fplbase::Mesh::RenderAAQuadAlongX(position - (texture_size / 2.0f), position + (texture_size / 2.0f), vec2(0, 1), vec2(1, 0)); #if defined(DEBUG_RENDER_BOUNDS) DebugRender(position, texture_size, renderer); #endif // DEBUG_RENDER_BOUNDS }
Area *XMLAreaLoader::LoadArea(tinyxml2::XMLElement *element, WorldGameState *world) { int width = atoi(element->Attribute("width")); int height = atoi(element->Attribute("height")); Area *result = new Area(Vec2(width, height), world); // Get player start position/direction auto startPosElement = element->FirstChildElement("startPos"); auto startDirElement = element->FirstChildElement("startDir"); if (startPosElement && startDirElement) { Vec2 startPos = LoadVec2(startPosElement); DIR startDir = (DIR)atoi(startDirElement->GetText()); result->SetStartPosAndDir(startPos, startDir); } tinyxml2::XMLElement *blockElement = element->FirstChildElement("Blocks")->FirstChildElement("Block"); int iBlock = 0; while (blockElement != NULL) { if (iBlock >= width*height) throw std::exception(); LoadBlock(result->GetBlock(iBlock % width, iBlock / width), blockElement); blockElement = blockElement->NextSiblingElement("Block"); iBlock++; } tinyxml2::XMLElement *entityElement = element->FirstChildElement("Entities")->FirstChildElement("Entity"); while (entityElement) { result->AddEntity(LoadEntity(entityElement)); entityElement = entityElement->NextSiblingElement("Entity"); } return result; }
bool Treasure::Load(File* f) { static int id = TREASURE_START_ID; SetId(id++); Vec3f pos; if (!LoadVec3(f, &pos)) { f->ReportError("Expected position"); return false; } SetPos(pos); std::string tex; if (!f->GetDataLine(&tex)) { f->ReportError("Expected texture filename"); return false; } Vec2i cells; if (!LoadVec2(f, &cells)) { f->ReportError("Expected num cells Vec2"); return false; } if (!f->GetInteger(&m_points)) { f->ReportError("Expected num points"); return false; } // Only if player has not collected treasure in the current room // before! Player* p = GetLocalPlayer(); // Create scene node if no local player yet OR player exists and there is no treasure key for this room if (!p || !(p->Exists(MakeTreasureKey()))) { // Size depends on points value float size = XSIZE * (float)m_points / 500.0f; SpriteNode* sn = new SpriteNode(tex, cells.x, cells.y, size * 0.5f, size * 0.5f); SetSceneNode(sn); sn->GetSprite()->SetCellRange(0, cells.x * cells.y); sn->GetSprite()->SetCell(0); m_glow = new SpriteNode("flare.png", 1, 1, size, size); sn->AddChild(m_glow.GetPtr()); File f; if (!f.OpenRead("fighteffect.txt")) { f.ReportError("Failed to load effect"); Assert(0); } m_sparkle = new AttackEffect; if (!m_sparkle->Load(&f)) { f.ReportError("Failed to load effect"); Assert(0); } sn->AddChild(m_sparkle.GetPtr()); } return true; }
bool Baddie::Load(File* f) { static int id = BADDDIE_START_ID; SetId(id++); Vec3f pos; if (!LoadVec3(f, &pos)) { f->ReportError("Expected position"); return false; } SetPos(pos); std::string tex; if (!f->GetDataLine(&tex)) { f->ReportError("Expected texture filename"); return false; } Vec2i cells; if (!LoadVec2(f, &cells)) { f->ReportError("Expected num cells Vec2"); return false; } if (!LoadVec2(f, &m_size)) { f->ReportError("Expected size Vec2"); return false; } BaddieNode* bn = new BaddieNode(this, tex, cells.x, cells.y, m_size.x * 0.5f, m_size.y * 0.5f); // By default we just cycle through all cells bn->GetSprite()->SetCellRange(0, cells.x * cells.y); SetSceneNode(bn); if (!f->GetInteger(&m_damage)) { f->ReportError("Expected damage"); return false; } if (!f->GetInteger(&m_maxHealth)) { f->ReportError("Expected max health"); return false; } if (m_maxHealth == -1) { m_maxHealth = 1; m_isDestructible = false; } m_health = m_maxHealth; if (!f->GetInteger(&m_points)) { f->ReportError("Expected num points"); return false; } if (!f->GetDataLine(&m_attackString)) { f->ReportError("Expected baddie attack string"); return false; } std::string bb; if (!f->GetDataLine(&bb)) { f->ReportError("Expected baddie behaviour type"); return false; } m_bb = TheBBFactory::Instance()->Create(bb); if (m_bb) { m_bb->SetBaddie(this); if (!m_bb->Load(f)) { f->ReportError("Failed to load baddie behaviour"); return false; } } /* if (!m_shadow->Load(f)) { return false; } */ // Particle effect when attacked, etc. File fight; if (!fight.OpenRead("fighteffect.txt")) { fight.ReportError("Failed to load effect"); return false; } m_effect = new AttackEffect; if (!m_effect->Load(&fight)) { fight.ReportError("Failed to load effect"); return false; } m_effect->SetVisible(true); m_sceneNode->AddChild(m_effect.GetPtr()); return true; }
bool Room::Load(File* f) { SetId(THE_ROOM_ID); // only one room exists at a time, with this fixed ID s_room = this; int numTextures = 0; if (!f->GetInteger(&numTextures)) { f->ReportError("Expected num textures."); return false; } m_texNames.reserve(numTextures); std::string s; for (int i = 0; i < numTextures; i++) { if (!f->GetDataLine(&s)) { f->ReportError("Expected texture name"); return false; } if (!TheResourceManager::Instance()->GetRes(s)) { f->ReportError("Not a valid texture name: " + s); return false; } m_texNames.push_back(s); } if (!LoadVec2(f, &m_gridsize)) { f->ReportError("Expected grid size"); return false; } for (int i = 0; i < NUM_TILE_MAPS; i++) { if (!LoadGrid(i, f)) { return false; } } // Load destination room numbers to NSEW if (!f->GetDataLine(&s)) { f->ReportError("Expected destination rooms"); return false; } Strings dests = Split(s, ','); if (dests.size() != 4) { f->ReportError("Expected FOUR comma-sep destination rooms"); return false; } bool nonz = false; for (int i = 0; i < 4; i++) { m_dest[i] = ToInt(dests[i]); if (m_dest[i]) { nonz = true; } } if (!nonz) { f->ReportError("Room has no destinations from it"); return false; } if (!f->GetDataLine(&m_music)) { f->ReportError("Room has no music"); return false; } // TODO TEMP //m_music = "sound/rainforest-ambience1.ogg"; std::cout << "Room music: " << m_music << "\n"; return true; }