00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "Star.hpp"
00020 #include <cmath>
00021 #include "MathFuncs.hpp"
00022 #include "UpdateFrame.hpp"
00023 #include "Singletons.hpp"
00024
00025 CStar::CStar()
00026 : m_SpeedX(0.0f),
00027 m_SpeedY(0.0f),
00028 m_Layer(0),
00029 m_CanBeRemoved(false),
00030 m_LastAniUpdate(0),
00031 m_FieldWidth(SCREEN_X_SIZE - HUD_SIZE_X)
00032 {}
00033
00034 CStar::~CStar()
00035 {
00036 }
00037
00038 void CStar::Init(const std::string& FileName, int Layer, bool Planet, int FieldWidth)
00039 {
00040 m_Star.SetImage(sgl::get_imagemgr().get_img(FileName));
00041 framed = false;
00043
00044 float X = std::rand() % (FieldWidth);
00045 float Y = (std::rand() % (SCREEN_Y_SIZE + 100) - 100);
00046
00047 if(Planet == true)
00048 {
00049 Y = -(std::rand() % 100 + m_Star.GetSubRect().GetHeight());
00050 }
00051
00052 m_Star.SetPosition(X,Y);
00053
00054 if(Planet == false)
00055 {
00056 m_Star.SetColor(sf::Color(Layer+128,Layer+128,Layer+128,255));
00057 }
00058
00059 if(Planet == false)
00060 {
00061 m_Star.SetScale(0.6f,((float)Layer) / 128 + 0.5f);
00062 }
00063
00064 m_SpeedY = 3.0f*(((float)Layer) / 128 + 100.f);
00065 m_Layer = Layer;
00066 m_IsPlanet = Planet;
00067
00068 m_FieldWidth = FieldWidth;
00069 }
00070
00071 void CStar::InitAsteroid(AsteroidData* Data, int Layer, float XSpeed,
00072 float YSpeed, float X, float Y)
00073 {
00074 if(Data == NULL)
00075 {
00076 return;
00077 }
00078
00079 m_Star.SetImage(sgl::get_imagemgr().get_img(Data->FileName));
00080 m_Star.SetSubRect(sf::IntRect(0, 0, Data->FrameX, Data->FrameY));
00081 frame_count = Data->FrameCount;
00082 framed = true;
00083
00084 int SgnXSpeed = sgn(XSpeed);
00085 int SgnYSpeed = sgn(YSpeed);
00086
00087 float Radius = frnd(0.0f, STAR_ASTEROID_RADIUS);
00088 float Angle = frnd(0, M_PI * 2);
00089
00090 float XPos = std::cos(Angle) * Radius + X;
00091 float YPos = std::sin(Angle) * Radius + Y;
00092
00093 m_Star.SetPosition(XPos,YPos);
00094
00095 m_SpeedX = XSpeed;
00096 m_SpeedY = YSpeed;
00097
00098 m_Layer = Layer;
00099 m_IsPlanet = true;
00100 m_IsAsteroid = true;
00101 }
00102
00103 void CStar::Draw()
00104 {
00105 static float last_frame(sgl::get_clock().GetElapsedTime());
00106
00107 float movement =
00108 (sgl::get_clock().GetElapsedTime() - static_cast<float>(last_frame))
00109 * 100;
00110
00111 last_frame = sgl::get_clock().GetElapsedTime();
00112
00113 sgl::get_window().Draw(m_Star);
00114 m_Star.Move(m_SpeedX*movement,m_SpeedY*movement);
00115
00116 if(m_IsAsteroid == false)
00117 {
00118 if(m_Star.GetPosition().y >= SCREEN_Y_SIZE + 100)
00119 {
00120 if(m_IsPlanet == true)
00121 {
00122 m_CanBeRemoved = true;
00123 }
00124
00125 m_Star.SetPosition(std::rand() % (m_FieldWidth),-(std::rand() % 100 - 50 - m_Star.GetSubRect().GetHeight()));
00126 }
00127 }
00128 else
00129 {
00130 if(m_SpeedX < 0)
00131 {
00132 if(m_Star.GetPosition().x < -m_Star.GetSubRect().GetWidth())
00133 {
00134 m_CanBeRemoved = true;
00135 return;
00136 }
00137 }
00138 else
00139 {
00140 if(m_Star.GetPosition().x > SCREEN_X_SIZE + m_Star.GetSubRect().GetWidth())
00141 {
00142 m_CanBeRemoved = true;
00143 return;
00144 }
00145 }
00146
00147 if(m_SpeedY < 0)
00148 {
00149 if(m_Star.GetPosition().y < -m_Star.GetSubRect().GetHeight())
00150 {
00151 m_CanBeRemoved = true;
00152 return;
00153 }
00154 }
00155 else
00156 {
00157 if(m_Star.GetPosition().y > SCREEN_X_SIZE + m_Star.GetSubRect().GetHeight())
00158 {
00159 m_CanBeRemoved = true;
00160 return;
00161 }
00162 }
00163 }
00164
00165 if(sgl::get_clock().GetElapsedTime()*1000 - m_LastAniUpdate > STAR_ANI_UPDATE)
00166 {
00167 m_LastAniUpdate = sgl::get_clock().GetElapsedTime()*1000;
00168 if (framed) update_frame(m_Star, frame_count);
00169 }
00170 }