//--------------------------- // Includes //--------------------------- #include "Smiley.h" //--------------------------- // Defines //--------------------------- #define GAME_ENGINE (GameEngine::GetSingleton()) //--------------------------- // Constructor & Destructor //--------------------------- Smiley::Smiley(int posX, int posY,Bitmap * bmpNeutralPtr,Bitmap * bmpHappyPtr,Bitmap * bmpScaredPtr,Bitmap *bmpSleepingPtr) :m_Pos(posX,posY),m_Direction(randBetween(-1,1),randBetween(-1,1)),m_Speed(randBetween(0,399)),m_Diameter(bmpNeutralPtr->GetHeight()) ,m_IsHighest(false),m_IsSleeping(false),m_BmpNeutralPtr(bmpNeutralPtr),m_BmpHappyPtr(bmpHappyPtr) ,m_BmpScaredPtr(bmpScaredPtr),m_BmpSleepingPtr(bmpSleepingPtr),m_IsScared(0), m_Velocity(m_Speed * m_Direction.x, m_Speed * m_Direction.y) { } Smiley::~Smiley() { // nothing to destroy } //--------------------------- // Methods //--------------------------- void Smiley::Tick(double dTime) { // Only when the smiley is not sleeping // it moves vertical and horiontal with the given speed // If the smiley reaches a window border, the speed changes direction DOUBLE2 distance(m_Velocity.x * dTime,m_Velocity.y * dTime); if(!m_IsSleeping) { m_Pos.x += distance.x; m_Pos.y += distance.y; if (m_Pos.y <= 0 ) { m_Velocity.y *= -1; // turn } if (m_Pos.y + m_Diameter >= GAME_ENGINE->GetHeight() ) { m_Velocity.y *= -1; // turn } if (m_Pos.x <= 0 ) { m_Velocity.x *= -1; // turn } if (m_Pos.x +m_Diameter >= GAME_ENGINE->GetWidth() ) { m_Velocity.x *= -1; // turn } } } void Smiley::Paint() { // Draw the actual bitmap // If smiley is sleeping, draw sleeping bitmap // else if smiley is highest, draw happy bitmap // else if smiley is near to the window border (margin is 20), draw scared bitmap // else draw sad smiley if(m_IsSleeping) GAME_ENGINE->DrawBitmap(m_BmpSleepingPtr,m_Pos.x, m_Pos.y); else if(m_IsHighest) GAME_ENGINE->DrawBitmap(m_BmpHappyPtr,m_Pos.x, m_Pos.y); else if ( abs(m_Pos.x+m_Diameter-GAME_ENGINE->GetWidth())<20 || abs(m_Pos.y+m_Diameter-GAME_ENGINE->GetHeight())<20 || m_Pos.x<20 || m_Pos.y<20) GAME_ENGINE->DrawBitmap(m_BmpScaredPtr, m_Pos.x, m_Pos.y); else GAME_ENGINE->DrawBitmap(m_BmpNeutralPtr,m_Pos.x, m_Pos.y); DOUBLE2 centerPos(m_Pos.x + (m_Diameter / 2),m_Pos.y + (m_Diameter / 2)); GAME_ENGINE->DrawLine(centerPos, centerPos + m_Velocity, 2); } void Smiley::IncreaseSpeed() { // Increase the vertical and horizontal speed with 1 unit // Take care when de speed is negative you should subtract i.s.o. add if(m_Speed>0) m_Speed*=1.05; else m_Speed/=1.05; } void Smiley::DecreaseSpeed() { // Decrease the vertical and horizontal speed with 1 unit // Take care when de speed is negative you should add i.s.o. subtract if(m_Speed<0) m_Speed*=1.05; else m_Speed/=1.05; } void Smiley::SetHighest(bool isHighest) { m_IsHighest=isHighest; } DOUBLE2 Smiley::GetPosition() { return m_Pos; } bool Smiley::IsSleeping() { return m_IsSleeping; } void Smiley::HitTest(int x, int y) { // If x and y are within the surrounding box, // then the sleeping status of the smiley changes if(x > m_Pos.x && x < (m_Pos.x+m_Diameter) && y > m_Pos.y && y < (m_Pos.y+m_Diameter)) { m_IsSleeping = !m_IsSleeping; } } double randBetween(double from, double to) { double r=rand(); r=r/RAND_MAX; double d=to-from; r=r*d; r=r+from; return r; } DOUBLE2 polarVector(double angle, double lenght) { } DOUBLE2 randUnitVector() { double a = randBetween(0,2*M_PI); return polarVector(a,1); } DOUBLE2 Smiley::GetVelocity() { return m_Velocity; }