src/ResourceManager/ShipResource.cpp

Go to the documentation of this file.
00001 /* Crown and Cutlass
00002  * TextureResource Code
00003  */
00004 
00005 #include <string>
00006 #include "../ccmath.h"
00007 #include "../Log.h"
00008 #include "../GLee.h"
00009 #include "../Model.h"
00010 #include "../BoundBox.h"
00011 #include "../Point.h"
00012 #include "IResource.h"
00013 #include "ShipResource.h"
00014 
00015 using namespace std;
00016 
00017 const string ShipResource::s_type = string("Ship");
00018 
00019 ShipResource::ShipResource(string name, string modelFileName, string textureName, float scale, int damage, unsigned int cargoSize, float mass, float sailArea, float drag, float rudderArea, float distToRudder): IResource(name) {
00020   m_name = name;
00021   m_modelFileName = modelFileName;
00022   m_textureName = textureName;
00023   m_model = NULL;
00024   m_scale = scale;
00025   m_maxDamage = damage;
00026   m_cargoSize = cargoSize;
00027   m_mass = mass;
00028   m_sailArea = sailArea;
00029   m_drag = drag;
00030   m_rudderArea = rudderArea;
00031   m_distToRudder = distToRudder;
00032   m_massMomentInertia = -1.0;
00033 
00034   Log::s_log->Message("ShipResource %s created (Key: %s)", m_name.c_str(), GetKey().c_str());
00035 }
00036 
00037 ShipResource::~ShipResource() {
00038   Log::s_log->Message("ShipResource %s destroyed", m_name.c_str());
00039 }
00040 
00041 string ShipResource::GetType() {
00042   return s_type;
00043 }
00044 
00045 void ShipResource::Load() {
00046   if (m_model != NULL) {
00047     Log::s_log->Message("Warning: Loading already loaded ship resource");
00048   }
00049 
00050   m_model = new Model(m_textureName);
00051   m_model->loadObj(m_modelFileName.c_str(), m_scale);
00052 
00053   if (m_massMomentInertia < 0.0) {
00054     CalcMassMomentInertia();
00055   }
00056   Log::s_log->Message("ShipResource %s loaded", m_name.c_str());
00057 }
00058 
00059 void ShipResource::Unload() {
00060   if (m_model == NULL) {
00061     Log::s_log->Message("Warning: Unloading already unloaded ship resource");
00062     return;
00063   }
00064 
00065   delete m_model;
00066   m_model = NULL;
00067   Log::s_log->Message("ShipResource %s unloaded", m_name.c_str());
00068 }
00069 
00070 void ShipResource::Draw() {
00071   m_model->draw();
00072 }
00073 
00074 int ShipResource::GetMaxDamage() {
00075   return m_maxDamage;
00076 }
00077 
00078 unsigned int ShipResource::GetCargoSize() {
00079   return m_cargoSize;
00080 }
00081 
00082 float ShipResource::GetMass() {
00083   return m_mass;
00084 }
00085 
00086 float ShipResource::GetSailArea() {
00087   return m_sailArea;
00088 }
00089 
00090 float ShipResource::GetDrag() {
00091   return m_drag;
00092 }
00093 
00094 float ShipResource::GetRudderArea() {
00095   return m_rudderArea;
00096 }
00097 
00098 float ShipResource::GetDistToRudder() {
00099   return m_distToRudder;
00100 }
00101 
00102 float ShipResource::GetMassMomentInertia() {
00103   return m_massMomentInertia;
00104 }
00105 
00106 void ShipResource::CalcMassMomentInertia() {
00107   float length, width;
00108 
00109   if (m_model == NULL) {
00110     Log::s_log->Message("Call to calcMassMomentInertia before model loaded");
00111     return;
00112   }
00113 
00114   length = fabs(m_model->modelBox->m_b1->m_x - m_model->modelBox->m_b4->m_x);
00115   width = fabs(m_model->modelBox->m_b1->m_z - m_model->modelBox->m_b4->m_z);
00116 
00117   m_massMomentInertia = (m_mass * (length * length + width * width)) / 12;
00118 }

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