00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00020
00021
00022
00024
00025
00026 #include "Player.hpp"
00027 #include <boost/format.hpp>
00028 #include <SFML/Window.hpp>
00029 #include <libintl.h>
00030 #define _(string) gettext(string)
00031
00032 #include "MathFuncs.hpp"
00033 #include <cmath>
00034 #include "Singletons.hpp"
00035
00036
00037 CPlayer::CPlayer()
00038 : m_Ship(NULL),
00039 m_Lives(PLAYER_INITIAL_LIVES),
00040 m_LiveString((boost::format(_("Lives: %1%")) % m_Lives).str()),
00041 m_Respawning(false),
00042 m_LastDeadTime(0)
00043 {}
00044
00045 CPlayer::~CPlayer()
00046 {
00047 delete m_Ship;
00048 }
00049
00050 void CPlayer::Init(const std::string& FileName)
00051 {
00052 if(m_Ship != NULL)
00053 {
00054 delete m_Ship;
00055 m_Ship = NULL;
00056 }
00057
00058 m_Ship = new CShip;
00059 m_Ship->Init(FileName);
00060 m_Ship->SetPosition((SCREEN_X_SIZE - HUD_SIZE_X)/2,SCREEN_Y_SIZE - 100);
00061 }
00062
00063 void CPlayer::Update()
00064 {
00065 static float last_frame(sgl::get_clock().GetElapsedTime());
00066
00067 float movement =
00068 (sgl::get_clock().GetElapsedTime() - static_cast<float>(last_frame))
00069 * 100;
00070
00071 last_frame = sgl::get_clock().GetElapsedTime();
00072
00073 if(m_Ship == NULL || IsDead())
00074 {
00075 return;
00076 }
00077
00078 if(m_Respawning == true)
00079 {
00080 m_Ship->SetAlpha(std::fabs(std::sin((sgl::get_clock().GetElapsedTime()*1000 - m_LastDeadTime) * 0.005f)));
00081
00082 if(sgl::get_clock().GetElapsedTime()*1000 - m_LastDeadTime > PLAYER_RESPAWN_TIME)
00083 {
00084 m_Respawning = false;
00085 m_Ship->SetAlpha(1.0f);
00086
00087 m_Ship->SetInvulnerable(false);
00088 }
00089 }
00090
00091
00092 if(sgl::get_window().GetInput().IsKeyDown(sf::Key::Left))
00093 {
00094 if(m_Ship->GetXPosition() > 0)
00095 {
00096 m_Ship->MoveX(-1 * movement);
00097 }
00098 }
00099 else if(sgl::get_window().GetInput().IsKeyDown(sf::Key::Right))
00100 {
00101 if(m_Ship->GetXPosition() < (SCREEN_X_SIZE - HUD_SIZE_X))
00102 {
00103 m_Ship->MoveX(1 * movement);
00104 }
00105 }
00106
00107 if(sgl::get_window().GetInput().IsKeyDown(sf::Key::Up))
00108 {
00109 if(m_Ship->GetYPosition() > 0)
00110 {
00111 m_Ship->MoveY(-1 * movement);
00112 }
00113 }
00114 else if(sgl::get_window().GetInput().IsKeyDown(sf::Key::Down))
00115 {
00116 if(m_Ship->GetYPosition() < SCREEN_Y_SIZE)
00117 {
00118 m_Ship->MoveY(1 * movement);
00119 }
00120 }
00121
00122 if(sgl::get_window().GetInput().IsKeyDown(sf::Key::Space))
00123 {
00124 m_Ship->Shoot();
00125 }
00126 }
00127
00128 bool CPlayer::CanBeRemoved()
00129 {
00130
00131
00132 if(m_Ship == NULL)
00133 {
00134 return false;
00135 }
00136
00137 if(m_Ship->CanBeRemoved() && m_Ship->IsDead())
00138 {
00139 if(m_Lives > 0)
00140 {
00141 --m_Lives;
00142
00143 m_LiveString = (boost::format(_("Lives: %1%")) % m_Lives).str();
00144
00145 m_Ship->SetPosition((SCREEN_X_SIZE - HUD_SIZE_X)/2,SCREEN_Y_SIZE - 100);
00146 m_Ship->SetCurArmor(m_Ship->GetMaxArmor());
00147 m_Respawning = true;
00148
00149 m_Ship->SetInvulnerable(true);
00150
00151 m_LastDeadTime = sgl::get_clock().GetElapsedTime()*1000;
00152
00153 return false;
00154 }
00155 else
00156 {
00157 return true;
00158 }
00159 }
00160 return false;
00161 }
00162