//Handle end elements bool XMLParseHelperExpression::EndElementHandler(XmlParseMaster::SharedData* data, std::string name) { /** To end an action expression parse we must have a current action in the shared data that was created in the start action */ SharedDataWorld* reinterpereted = data->As<SharedDataWorld>(); Scope* curAction = reinterpereted->GetAction(); if (reinterpereted != nullptr && name == actionString && curAction != nullptr) { if (!curAction->Is("ActionExpression")) { return false; } reinterpereted->DecrementDepth(); Scope* curContainer = curAction->As<ActionExpression>()->GetContainer(); //If we're in an actionList, set the current action to that if (curContainer->Is("ActionList")) { reinterpereted->SetAction(curContainer->As<Action>()); return true; } //Otherwise (in the case that we're contained within an entity) just set it back to null reinterpereted->SetAction(nullptr); return true; } return false; }
//Create a new entity in our containing sector void ActionCreateEntity::Update(const WorldState& curState) { Scope* container = GetContainer(); while (!container->Is("Sector")) { container = container->GetParent(); } Sector* sector = container->As<Sector>(); if (sector != nullptr) { sector->CreateEntity(mClassName, mInstanceName); } }
//Handle start elements bool XMLParseHelperExpression::StartElementHandler(XmlParseMaster::SharedData* data, std::string name, Hashmap<std::string, std::string>& attributes) { /** Requirements for starting a new expression action: 1. Shared data must be SharedDataWorld 2. Name of element must be 'action' 3. Must contain a 'class' attribute 4. Must contain a 'target' attribute 5. Must contain a 'name' attribute 6. Class type must be 'ActionExpression' */ SharedDataWorld* reinterpereted = data->As<SharedDataWorld>(); if (reinterpereted != nullptr && name == actionString && attributes.ContainsKey(classString) && attributes.ContainsKey(nameString)) { if (attributes.Find(classString)->second == "ActionExpression") { //Ensure that we have either an actionList or an entity that is going to contain this new actionExpression Scope* actionList = reinterpereted->GetAction(); Scope* entity = reinterpereted->GetEntity(); if (actionList != nullptr && actionList->Is("ActionList")) { //Create a new actionExpression with the factory from the actionlist ActionExpression* newAction = actionList->As<ActionList>()->CreateAction("ActionExpression", attributes.Find(nameString)->second)->As<ActionExpression>(); reinterpereted->SetAction(newAction); } else if (entity != nullptr) { //Create a new actionExpression with the factory from the entity ActionExpression* newAction = entity->As<Entity>()->CreateAction("ActionExpression", attributes.Find(nameString)->second)->As<ActionExpression>(); reinterpereted->SetAction(newAction); } else { return false; } reinterpereted->IncrementDepth(); return true; } } return false; }