|
| 1 | +#include "Clock.h" |
| 2 | +#include "TextureManager.h" |
| 3 | + |
| 4 | +void Clock::init() |
| 5 | +{ |
| 6 | + // Sämtliche Attribute werden leider hardgecodet |
| 7 | + m_secondsHand.textureId = "clockHandSeconds"; |
| 8 | + m_minutesHand.textureId = "clockHandMinutes"; |
| 9 | + m_hoursHand.textureId = "clockHandHours"; |
| 10 | + |
| 11 | + m_secondsHand.width = 85; |
| 12 | + m_secondsHand.height = 2; |
| 13 | + |
| 14 | + m_minutesHand.width = 80; |
| 15 | + m_minutesHand.height = 4; |
| 16 | + |
| 17 | + m_hoursHand.width = 60; |
| 18 | + m_hoursHand.height = 4; |
| 19 | + |
| 20 | + m_secondsHand.rotationAngle = m_minutesHand.rotationAngle = m_hoursHand.rotationAngle = 0; |
| 21 | + |
| 22 | + |
| 23 | + // Attribute des Ziffernblattes werden ebenfalls hardgecodet |
| 24 | + m_textureId = "clockFace"; |
| 25 | + m_rect.positionVector.setX(10.0f); |
| 26 | + m_rect.positionVector.setY(10.0f); |
| 27 | + m_rect.width = m_rect.height = 192; |
| 28 | + |
| 29 | + m_rect.setShowText(false); |
| 30 | + m_rect.setColor(255, 130, 28, 255); |
| 31 | + m_rect.setVisible(true); |
| 32 | +} |
| 33 | + |
| 34 | +void Clock::update() |
| 35 | +{ |
| 36 | + // Bei einer 'm_velocity' > 1 kann es sein, dass über den Wert hinausgeschossen wird. |
| 37 | + if (m_desiredTimeInSeconds < m_timeInSeconds) |
| 38 | + { |
| 39 | + m_desiredTimeInSeconds = m_desiredTimeInSeconds; |
| 40 | + } |
| 41 | + else if (m_desiredTimeInSeconds > m_timeInSeconds) |
| 42 | + { |
| 43 | + // Hier soll die Zählervariable so oft um 'm_handVelocity' inkrementiert werden, bis die Werte gleich sind |
| 44 | + m_timeInSeconds += m_handVelocity; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + /* Hier wird der Rotationswinkel der einzelnen Zeiger, basierend auf 'm_timeInSeconds' berechnet. |
| 49 | + * |
| 50 | + * Hierzu wird die Modulo Operation genutzt, um zu errechnen, um wie viele Einheiten der Zeiger |
| 51 | + * vom Nullpunkt (Winkel = 90° bzw. 270°) entfernt ist. |
| 52 | + */ |
| 53 | + |
| 54 | + |
| 55 | + double surplusSecs = static_cast<double>(m_timeInSeconds) / 60.0 ; |
| 56 | + double surplusMinutes = static_cast<double>(m_timeInSeconds) / 3600.0; |
| 57 | + double surplusHours = static_cast<double>(m_timeInSeconds) / (3600.0 * 12.0); |
| 58 | + |
| 59 | + m_secondsHand.rotationAngle = static_cast<int>(270 + surplusSecs * 360.0) % 360; |
| 60 | + m_minutesHand.rotationAngle = static_cast<int>(270 + surplusMinutes * 360.0) % 360; |
| 61 | + m_hoursHand.rotationAngle = static_cast<int>(270 + surplusHours * 360.0) % 360; |
| 62 | +} |
| 63 | + |
| 64 | +void Clock::draw() |
| 65 | +{ |
| 66 | + // Die Uhr soll nur gezeichnet werden, wenn die Flag auf true gesetzt wird |
| 67 | + if (!m_isVisible) |
| 68 | + { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // Zum zeichnen brauchen wir das Zentrum des Ziffernblattes, weil jeder Zeiger von da ausgeht |
| 73 | + int centerX = m_rect.getX() + m_rect.getWidth() / 2; |
| 74 | + int centerY = m_rect.getY() + m_rect.getHeight() / 2; |
| 75 | + |
| 76 | + // Der TextureManager braucht außerdem |
| 77 | + |
| 78 | + // Ziffernblatt zeichnen |
| 79 | + TheTextureManager::Instance()->draw(m_textureId, m_rect.getX(), m_rect.getY(), m_rect.getWidth(), m_rect.getHeight()); |
| 80 | + |
| 81 | + // Stundenzeiger zeichnen |
| 82 | + TheTextureManager::Instance()->draw( |
| 83 | + m_hoursHand.textureId, |
| 84 | + centerX, centerY - m_hoursHand.height / 2, |
| 85 | + m_hoursHand.width, m_hoursHand.height, |
| 86 | + 0, m_hoursHand.height / 2, |
| 87 | + m_hoursHand.rotationAngle); |
| 88 | + |
| 89 | + // Minutenzeiger zeichnen |
| 90 | + TheTextureManager::Instance()->draw( |
| 91 | + m_minutesHand.textureId, |
| 92 | + centerX, centerY - m_minutesHand.height / 2, |
| 93 | + m_minutesHand.width, m_minutesHand.height, |
| 94 | + 0, m_minutesHand.height / 2, |
| 95 | + m_minutesHand.rotationAngle); |
| 96 | + |
| 97 | + // Sekundenzeiger zeichnen |
| 98 | + TheTextureManager::Instance()->draw( |
| 99 | + m_secondsHand.textureId, |
| 100 | + centerX, centerY - m_secondsHand.height / 2, |
| 101 | + m_secondsHand.width, m_secondsHand.height, |
| 102 | + 0, m_secondsHand.height / 2, |
| 103 | + m_secondsHand.rotationAngle); |
| 104 | + |
| 105 | + // Das ObjectRectangle zeichnen |
| 106 | + m_rect.draw(Vector2D(0.0f, 0.0f)); |
| 107 | +} |
| 108 | + |
| 109 | +void Clock::addTime(int seconds, int velocity) |
| 110 | +{ |
| 111 | + m_desiredTimeInSeconds = m_timeInSeconds + seconds; |
| 112 | + m_handVelocity = velocity; |
| 113 | +} |
0 commit comments