00001 00012 /*************************************************************************** 00013 * Copyright (C) 2007 by Jan Koci * 00014 * honza.koci@email.cz * 00015 * http://kengine.sourceforge.net/tutorial/ 00016 * * 00017 * This program is free software; you can redistribute it and/or modify * 00018 * it under the terms of the GNU General Public License as published by * 00019 * the Free Software Foundation; either version 2 of the License, or * 00020 * (at your option) any later version. * 00021 * * 00022 * This program is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00025 * GNU General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU General Public License * 00028 * along with this program; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00031 ***************************************************************************/ 00032 00033 #include "K3dRayBuild.h" 00034 00038 K3dRayBuild::K3dRayBuild(K3dGameData *_pGameData) 00039 { 00040 m_pGameData = _pGameData; 00041 // Set this pointer to the game data 00042 m_pGameData->SetRayBuild(this); 00043 m_pVector3Build = m_pGameData->GetVector3Build(); 00044 } 00045 00046 K3dRayBuild::~K3dRayBuild(void) 00047 { 00048 DeleteRays(); 00049 } 00050 00052 void K3dRayBuild::LoadRays() 00053 { 00054 cout << "void K3dRayBuild::LoadRays()" << endl; 00055 K3dString strFc = "void K3dRayBuild::LoadRays() -- "; 00056 K3dString strOut; 00057 // Get virtual machine from global data 00058 K3dLua *pLua = m_pGameData->GetLua(); 00059 // Get number of rays 00060 pLua->LuaGetGlobal(K_STR_NUM_RAYS); 00061 if (!pLua->LuaIsNumber(-1)) 00062 { 00063 //strOut = strFc + K_STR_NUM_RAYS; 00064 //strOut += " is not a valid number"; 00065 //pLua->LuaError(strOut.GetString().c_str()); 00066 cerr << "Warning :: void K3dRayBuild::LoadRays() -- Doesn`t load " << K_STR_NUM_RAYS << " from map file" << endl; 00067 } 00068 int iNumRays = (int) pLua->LuaToNumber(-1); 00069 for(int i=0; i<iNumRays; i++) 00070 { 00071 K3dRayObj *pRay = CreateNewRayObj(); 00072 K3dString kString(K_STR_RAY); 00073 // Add number behind string 00074 kString.AddNumber(i); 00075 pLua->LuaGetGlobal(kString.GetString().c_str()); 00076 if (!pLua->LuaIsTable(-1)) 00077 { 00078 strOut = strFc + kString; 00079 strOut += " is not a valid ray table"; 00080 pLua->LuaError(strOut.GetString().c_str()); 00081 } 00082 pRay->GetName() = pLua->LuaGetFieldString(K_STR_NAME); 00083 pRay->GetOrigin()->GetX() = (float)pLua->LuaGetFieldNumber(K_STR_ORIG_X); 00084 pRay->GetOrigin()->GetY() = (float)pLua->LuaGetFieldNumber(K_STR_ORIG_Y); 00085 pRay->GetOrigin()->GetZ() = (float)pLua->LuaGetFieldNumber(K_STR_ORIG_Z); 00086 pRay->GetDirection()->GetX() = (float)pLua->LuaGetFieldNumber(K_STR_DIRECT_X); 00087 pRay->GetDirection()->GetY() = (float)pLua->LuaGetFieldNumber(K_STR_DIRECT_Y); 00088 pRay->GetDirection()->GetZ() = (float)pLua->LuaGetFieldNumber(K_STR_DIRECT_Z); 00089 } 00090 } 00091 00092 void K3dRayBuild::DeleteRays() 00093 { 00094 std::cout << "void K3dBlockBuild::DeleteRays()" << endl; 00095 while(m_pGameData->GetRayObjSP().GetNum()) 00096 { 00097 K3dRayObj *p = m_pGameData->GetRayObjSP().Get(0); 00098 p = m_pGameData->GetRayObjSP().Delete(p); 00099 } 00100 } 00101 00104 K3dRayObj *K3dRayBuild::CreateNewRayObj() 00105 { 00106 // Create new ray 00107 K3dRayObj *pRay = m_pGameData->GetRayObjSP().New(); 00108 // Create new ray direction and origin vector 00109 K3dVector3Obj *pVector3 = m_pVector3Build->CreateNewVector3(); 00110 pRay->SetOrigin(pVector3); 00111 pVector3 = m_pVector3Build->CreateNewVector3(); 00112 pRay->SetDirection(pVector3); 00113 pRay->GetId() = m_pGameData->GetRayObjSP().GetNum(); 00114 return pRay; 00115 }