src/Player.cpp

Go to the documentation of this file.
00001 /* Crown and Cutlass
00002  * Player Data Object Code
00003  */
00004 /*
00005 #if defined (WIN32)
00006 #define WIN32_LEAN_AND_MEAN
00007 #include <windows.h>
00008 #endif
00009 */
00010 #include <cstdio>
00011 #include <vector>
00012 #include "GLee.h"
00013 #include "ISaveObject.h"
00014 #include "Player.h"
00015 #include "Log.h"
00016 #include "Config.h"
00017 #include "Ship.h"
00018 #include "tinyxml.h"
00019 
00020 using namespace std;
00021 
00022 Player* Player::player = NULL;
00023 
00024 Player::Player(const string playerNameIn, const string shipNameIn): ISaveObject("Player") {
00025   playerName = playerNameIn;
00026 
00027   gold = 100;
00028 
00029   ship = new Ship(Config::s_config->GetStartingShipType(), shipNameIn);
00030 }
00031 
00032 Player::~Player() {
00033   delete ship;
00034 }
00035 
00036 void Player::Load(TiXmlElement *parent) {
00037   TiXmlElement *selfNode;
00038 
00039   selfNode = parent->FirstChildElement(m_XMLName);
00040   if (selfNode->QueryIntAttribute("gold", &gold) != TIXML_SUCCESS) {
00041     // Should throw exception
00042     Log::s_log->Message("Warning: Gold attribute missing in player node");
00043     return;
00044   }
00045 
00046   // Load the player name
00047   playerName = string(selfNode->Attribute("playerName"));
00048 
00049   ship->Load(selfNode);
00050 }
00051 
00052 void Player::Save(TiXmlElement *parent) {
00053   TiXmlElement selfNode(m_XMLName);
00054 
00055   selfNode.SetAttribute("gold", gold);
00056   selfNode.SetAttribute("playerName", playerName);
00057 
00058   ship->Save(&selfNode);
00059   parent->InsertEndChild(selfNode);
00060 }
00061 
00062 void Player::Reset() {
00063   gold = 0;
00064   ship->Reset();
00065 }
00066 
00067 void Player::Dump() {
00068   Log::s_log->Message("Player: %s", playerName.c_str());
00069   ship->Dump();
00070 
00071   Log::s_log->Message("Gold:\t%i\n", gold);
00072 }
00073 
00074 shared_ptr<Cargo> Player::GetCargo() {
00075   return ship->GetCargo();
00076 }
00077 
00078 string Player::GetPlayerName() {
00079   return playerName;
00080 }
00081 
00082 void Player::CreateNewPlayer(const string playerName, const string shipName) {
00083   if (player != NULL) {
00084     DeletePlayer();
00085   }
00086   player = new Player(playerName, shipName);
00087 }
00088 
00089 void Player::DeletePlayer() {
00090   delete player;
00091   player = NULL;
00092 }

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