src/ResourceManager/ShipFactory.cpp

Go to the documentation of this file.
00001 /* Crown and Cutlass
00002  * TextureFactory Code
00003  */
00004 
00005 #include "../tinyxml.h"
00006 #include "../Log.h"
00007 #include "../GLee.h"
00008 #include "IResource.h"
00009 #include "IResourceFactory.h"
00010 #include "ShipResource.h"
00011 #include "ShipFactory.h"
00012 
00013 using namespace std;
00014 
00015 ShipFactory::~ShipFactory() {
00016   Log::s_log->Message("Ship factory deleted");
00017 }
00018 
00019 IResource* ShipFactory::NewResource(TiXmlElement *XmlElement) {
00020   TiXmlElement *generalElement;
00021   TiXmlElement *displayElement;
00022   TiXmlElement *physicsElement;
00023   string name, modelFileName, textureName;
00024   float scale;
00025   int damage, cargoSize;
00026   float mass, sailArea, drag, rudderArea, distToRudder;
00027 
00028   if (string(XmlElement->Attribute("type")) != ShipResource::s_type) {
00029     throw string("Invalid resource type");
00030   }
00031 
00032   name = string(XmlElement->Attribute("name"));
00033 
00034   generalElement = XmlElement->FirstChildElement("General");
00035   if (generalElement == NULL) throw string("Could not read General element");
00036   if (generalElement->QueryIntAttribute("damage", &damage) != TIXML_SUCCESS) {
00037     throw string("Could not read damage attribute");
00038   }
00039   if (generalElement->QueryIntAttribute("cargoSize", &cargoSize) != TIXML_SUCCESS) {
00040     throw string("Could not read cargoSize attribute");
00041   }
00042   if (cargoSize <= 0) {
00043     throw string("Invalid cargoSize attribute");
00044   }
00045 
00046   displayElement = XmlElement->FirstChildElement("Display");
00047   if (displayElement == NULL) throw string("Could not read Display element");
00048   modelFileName = string(displayElement->Attribute("modelFileName"));
00049   textureName = string(displayElement->Attribute("textureName"));
00050   if (displayElement->QueryDoubleAttribute("scale", &scale) != TIXML_SUCCESS) {
00051     throw string("Could not read scale attribute");
00052   }
00053 
00054   physicsElement = XmlElement->FirstChildElement("Physics");
00055   if (physicsElement == NULL) throw string("Could not read Physics element");
00056   if (physicsElement->QueryDoubleAttribute("mass", &mass) != TIXML_SUCCESS) {
00057     throw string("Could not read mass attribute");
00058   }
00059   if (physicsElement->QueryDoubleAttribute("sailArea", &sailArea) != TIXML_SUCCESS) {
00060     throw string("Could not read sailArea attribute");
00061   }
00062   if (physicsElement->QueryDoubleAttribute("drag", &drag) != TIXML_SUCCESS) {
00063     throw string("Could not read drag attribute");
00064   }
00065   if (physicsElement->QueryDoubleAttribute("rudderArea", &rudderArea) != TIXML_SUCCESS) {
00066     throw string("Could not read rudderArea attribute");
00067   }
00068   if (physicsElement->QueryDoubleAttribute("distToRudder", &distToRudder) != TIXML_SUCCESS) {
00069     throw string("Could not read distToRudder attribute");
00070   }
00071 
00072   return new ShipResource(name, modelFileName, textureName, scale, damage, (unsigned int) cargoSize, mass, sailArea, drag, rudderArea, distToRudder);
00073 }

Generated on Mon Jan 8 22:34:12 2007 for CrownandCutlass by  doxygen 1.4.7