www.pudn.com > RCApp-src.zip > PlayerManager.cpp


/* 
	RedEye Project (http://members.ozemail.com.au/~ndmcevoy/) 
	Copyright (C) 2003  Nick McEvoy 
 
	This library is free software; you can redistribute it and/or 
	modify it under the terms of the GNU Library General Public 
	License as published by the Free Software Foundation; either 
	version 2 of the License, or (at your option) any later version. 
 
	This library is distributed in the hope that it will be useful, 
	but WITHOUT ANY WARRANTY; without even the implied warranty of 
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
	Library General Public License for more details. 
 
	You should have received a copy of the GNU Library General Public 
	License along with this library; if not, write to the Free 
	Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 
	----------------------------------------------------------------- 
 
		Commented for use with Doxygen (http://www.doxygen.org) 
 
	----------------------------------------------------------------- 
*/ 
 
/*! \file PlayerManager.cpp 
 *	\brief Player manager. 
 * 
 *		This file contains the player manager. 
 */ 
 
// GLUT includes 
#include  
 
// RedEye includes 
#include "PlayerManager.h" 
#include "ModelManager.h" 
#include "Engine.h" 
#include "Camera.h" 
#include "Status.h" 
#include "GUI.h" 
#include "Collision.h" 
#include "RCDefs.h" 
 
//! The one and only instance 
rePlayerManager* rePlayerManager::mpInstance = NULL; 
 
 
 
 
rePlayerManager::rePlayerManager() 
{ 
	mpPlayer = NULL; 
} 
 
 
 
 
rePlayerManager::~rePlayerManager() 
{ 
	DestroyLevel(); 
	mpInstance = NULL; 
} 
 
 
 
 
rePlayerManager* 
rePlayerManager::GetInstance() 
{ 
	if (mpInstance == NULL) 
	{ 
		mpInstance = new rePlayerManager; 
	} 
	return mpInstance; 
} 
 
 
 
 
void 
rePlayerManager::CreateLevel() 
{ 
	printf("rePlayerManager::CreateLevel() - creating player...\n"); 
 
	sgCoord PlayerPos = {0}; 
	sgCoord ViewOffset = {0}; 
	sgCoord ExternalViewOffset = {0}; 
	sgCoord AttachedViewOffset = {0}; 
 
	// Get player data 
	TiXmlNode* pNode = reGetGameEngine()->GetXMLLevelNode(); 
	pNode = pNode->FirstChild("Player"); 
	reGetXMLChildAttribCoord(pNode, "StartPos", &PlayerPos); 
	reGetXMLChildAttribCoord(pNode, "ViewOffset", &ViewOffset); 
	reGetXMLChildAttribCoord(pNode, "ExternalViewOffset", &ExternalViewOffset); 
	reGetXMLChildAttribCoord(pNode, "AttachedViewOffset", &AttachedViewOffset); 
	TiXmlNode* pPlatform = pNode->FirstChild("Platform"); 
	const char* psFile = reGetXMLAttribute(pPlatform, "file", ""); 
 
	mpPlayer = reGameEntity::Create(psFile); 
 
	if (mpPlayer) 
	{ 
		// Set player altitude over terrain 
		PlayerPos.xyz[SG_Z] += reGetHeight(reGetWorld(), PlayerPos.xyz); 
		mpPlayer->SetPosition(&PlayerPos); 
 
		// Attach camera to player 
		reGameCamera* pCamera = reGetGameEngine()->GetCamera(); 
		pCamera->SetCameraMode(reGameCamera::eExternalAttachedView); 
		pCamera->SetViewOffset(ViewOffset); 
		pCamera->SetExternalViewOffset(ExternalViewOffset); 
		pCamera->SetAttachedViewOffset(AttachedViewOffset); 
		pCamera->SetEntityRef(mpPlayer); 
 
		// Add the player to the world 
		reGetWorld()->addKid(mpPlayer); 
	} 
	else // error 
	{ 
		ulSetError(UL_WARNING,  
		     "rePlayerManager::CreateLevel() Could not create player."); 
	} 
 
	printf("rePlayerManager::CreateLevel() - done\n"); 
} 
 
 
 
 
void 
rePlayerManager::DestroyLevel() 
{ 
	printf("rePlayerManager::DestroyLevel() - destroying player...\n"); 
 
	if (mpPlayer) 
	{ 
		// todo: game engine should have a remove method that 
		// checks if removed entity is a camera entity or target 
		reGetGameEngine()->GetCamera()->SetEntityRef(0); 
		reGetWorld()->removeKid(mpPlayer); 
		ssgDeRefDelete(mpPlayer); 
		mpPlayer = NULL; 
	} 
 
	printf("rePlayerManager::DestroyLevel() - done\n"); 
} 
 
 
 
 
void 
rePlayerManager::Update(float fDeltaSec) 
{ 
	if (mpPlayer) 
	{ 
		// Update player 
		mpPlayer->Update(fDeltaSec); 
 
		// Get player HOT (height of terrain) and altitude 
		sgCoord PlayerPos; 
		sgVec3 TerrainNormal; 
		mpPlayer->GetPosition(&PlayerPos); 
		ssgHit* pHit; 
		float fHOT = reGetHeightAndNormal(reGetWorld(), PlayerPos.xyz, TerrainNormal, &pHit); 
		float fAltitude = PlayerPos.xyz[SG_Z] - fHOT; 
 
		// Get player speed & power 
		sgVec3 Velocity; 
		mpPlayer->GetVelocity(Velocity); 
		float fSpeed = sgLengthVec3(Velocity)*RE_MPS_TO_KPH; 
		float fPower = ((rePlatformEntity*)mpPlayer)->GetPower()*100; 
 
		// Update player status 
		char sPlayerText[RE_GUI_STRING_MAX]; 
		sprintf(sPlayerText, 
			"power %.0f\n" 
			"speed %.0f kph\n" 
			"x:y:z %.0f:%.0f:%.0f\n" 
			"h:p:r %.0f:%.0f:%.0f\n" 
			"hot:alt %.0f:%.0f", 
				fPower, fSpeed, 
				PlayerPos.xyz[SG_X], PlayerPos.xyz[SG_Y], PlayerPos.xyz[SG_Z], 
				PlayerPos.hpr[SG_X], PlayerPos.hpr[SG_Y], PlayerPos.hpr[SG_Z], 
				fHOT, fAltitude); 
		reGetGameGUI()->SetText(reGameGUI::ePlayerText, sPlayerText, true); 
	} 
} 
 
 
 
 
void 
rePlayerManager::SpaceCollide() 
{ 
	if (mpPlayer) 
		mpPlayer->SpaceCollide(); 
} 
 
 
 
 
void 
rePlayerManager::Keyboard(unsigned char key, int x, int y, bool bKeyDown) 
{ 
	if (mpPlayer) 
		mpPlayer->Keyboard(key, x, y, bKeyDown); 
} 
 
 
 
 
void 
rePlayerManager::KeyboardSpecial(int key, int x, int y, bool bKeyDown) 
{ 
	if (mpPlayer) 
		mpPlayer->KeyboardSpecial(key, x, y, bKeyDown); 
}