Skip to content

Commit de48c1a

Browse files
committed
#67 Uhr hinzugefuegt
Uebers Skript manipulierbar
1 parent 4373d7c commit de48c1a

20 files changed

+339
-9
lines changed

Koramu/BaseCommand.h

+3
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ class BaseCommand
4242
// getter-Funktionen
4343
COMMAND_TYPE getType() const { return m_type; }
4444
bool isDone() const { return m_done; }
45+
46+
// setter-Funktionen
47+
void setDone() { m_done = true; }
4548
};

Koramu/Clock.cpp

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

Koramu/Clock.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#pragma once
2+
#include "ObjectRectangle.h"
3+
4+
5+
/* Die Uhr wird in der oberen linken Ecke des Bildschirmes angezeigt.
6+
* Sie wird in die Story eingebaut. Sie bleibt jedoch nicht permanent sichtbar,
7+
* sondern wird von einem Skript bei Bedarf sichtbar und wieder unsichtbar gemacht.
8+
*
9+
*/
10+
class Clock
11+
{
12+
private:
13+
ObjectRectangle m_rect; // In dieses Rechteck wird die Uhr gerendert
14+
int m_timeInSeconds; // Die aktuelle Zeitangabe, die die Uhr macht
15+
const char* m_textureId; // Die textureId des Ziffernblattes
16+
bool m_isVisible; // Bestimmt, ob die Uhr gerendert werden soll
17+
18+
int m_desiredTimeInSeconds; // Sobald dieser Wert von 'm_timeInSeconds' abweicht, bewegen sich die Zeiger
19+
int m_handVelocity; // Anzahl der Sekunden, die per Frame hinzugefügt werden
20+
21+
/* Diese Speicherstruktur enthält alle Informationen zu einem Uhrzeiger.
22+
* Z.B. die Ausrichtung, die bei jedem Aufruf von update() der 'Clock' anhand
23+
* von 'm_timeInSeconds' neu berechnet wird.
24+
*
25+
* Hiervon braucht die Uhr drei Instanzen (Sekunden, Minuten, Stunden)
26+
*/
27+
struct ClockHand
28+
{
29+
int width;
30+
int height;
31+
int rotationAngle;
32+
const char* textureId;
33+
34+
ClockHand()
35+
: width(0), height(0), rotationAngle(0),
36+
textureId("") {}
37+
38+
}m_secondsHand, m_minutesHand, m_hoursHand;
39+
40+
public:
41+
Clock() // Konstruktor
42+
: m_timeInSeconds(0.0), m_textureId(nullptr),
43+
m_isVisible(false)
44+
{}
45+
46+
void init();
47+
void update();
48+
void draw();
49+
50+
void addTime(int seconds, int velocity); // Lässt die Zeiger der Uhr um eine bestimmte Anzahl an Sekunden wandern
51+
52+
53+
// getter-Funktionen
54+
int getTimeInSeconds() const { return m_timeInSeconds; }
55+
56+
// setter-Funktionen
57+
void setTime(int timeInSeconds) { m_timeInSeconds = timeInSeconds; }
58+
void setVisible(bool v) { m_isVisible = v; }
59+
60+
};
61+

Koramu/Game.cpp

+18-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "Story.h"
1313
#include "StoryParser.h"
1414
#include "Notebook.h"
15+
#include "Clock.h"
1516

1617
#include "GameObjectFactory.h"
1718
#include "Player.h"
@@ -41,11 +42,11 @@ Game* Game::s_pInstance = nullptr;
4142

4243
Game::Game() // Konstruktor
4344
: m_running(false),
44-
m_gameWidth(0), m_gameHeight(0),
45-
m_gameXPos(0), m_gameYPos(0),
46-
m_pStateMachine(nullptr), m_pCurrentState(nullptr),
47-
m_pWindow(nullptr), m_pRenderer(nullptr),
48-
m_pCamera(nullptr)
45+
m_gameWidth(0), m_gameHeight(0),
46+
m_gameXPos(0), m_gameYPos(0),
47+
m_pStateMachine(nullptr), m_pCurrentState(nullptr),
48+
m_pWindow(nullptr), m_pRenderer(nullptr),
49+
m_pCamera(nullptr)
4950
{
5051
// Die Logger initialisieren
5152
m_pStandardLog = new Logger();
@@ -59,6 +60,9 @@ Game::Game() // Konstruktor
5960

6061
// Notizbuch erstellen
6162
m_pNotebook = new Notebook(10);
63+
64+
// Uhr erstellen
65+
m_pClock = new Clock();
6266
}
6367

6468
/* !! WICHTIG !!
@@ -183,6 +187,9 @@ bool Game::init(std::string title, int xPos, int yPos, int flags)
183187
// Story initialisieren
184188
m_pStory->init();
185189

190+
// Die Uhr initialisieren
191+
m_pClock->init();
192+
186193
// Informationen über das Fenster speichern
187194
m_gameWidth = width;
188195
m_gameHeight = height;
@@ -257,6 +264,9 @@ void Game::update()
257264

258265
// Die Story wird aktualisiert
259266
m_pStory->update();
267+
268+
// Die Uhr wird aktualisiert
269+
m_pClock->update();
260270
}
261271

262272
void Game::render()
@@ -278,6 +288,9 @@ void Game::render()
278288
TheTester::Instance()->testFunctions();
279289
#pragma endregion
280290

291+
// Die Uhr wird gerendert
292+
m_pClock->draw();
293+
281294
// Jetzt wird alles auf den Bildschirm geschmissen
282295
SDL_RenderPresent(m_pRenderer);
283296
}

Koramu/Game.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Camera;
1414
class Story;
1515
class ItemList;
1616
class Notebook;
17+
class Clock;
1718

1819
/* Konstruktor und Destruktor wurden aus folgendem Grund 'private' gesetzt:
1920
*
@@ -75,6 +76,9 @@ class Game
7576
// Notizbuch
7677
Notebook* m_pNotebook;
7778

79+
// Uhr
80+
Clock* m_pClock;
81+
7882
// Logger Variablen
7983
Logger* m_pStandardLog;
8084
Logger* m_pErrorLog;
@@ -121,7 +125,8 @@ class Game
121125
FiniteStateMachine::GameState* getCurrentState() const;
122126
Story* getStory() const { return m_pStory; }
123127
ItemList* getItemList() const { return m_pItemList; }
124-
Notebook* getNotebook() { return m_pNotebook; }
128+
Notebook* getNotebook() const { return m_pNotebook; }
129+
Clock* getClock() const { return m_pClock; }
125130

126131
/* Log Objekte werden zurückgegeben, damit man mit einem
127132
* Stream Operator auf sie zugreifen kann.

Koramu/GameLuaRegistration.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "NotebookState.h"
1010
#include "Notebook.h"
1111
#include "ItemList.h"
12+
#include "Clock.h"
1213

1314
using namespace LuaRegistrations;
1415

@@ -41,6 +42,11 @@ void GameLuaRegistration::registerToLua(lua_State* pLuaState)
4142
{ "addItem", l_addItem },
4243
{ "removeItem", l_removeItem },
4344
{ "getItemCount", l_getItemCount },
45+
{ "getClockTimeSeconds", l_getClockTimeSeconds },
46+
{ "setClockTimeSeconds", l_setClockTimeSeconds },
47+
{ "addClockTimeSeconds", l_addClockTimeSeconds },
48+
{ "showClock", l_showClock },
49+
{ "hideClock", l_hideClock },
4450
{nullptr, nullptr}
4551
};
4652

@@ -296,3 +302,57 @@ int LuaRegistrations::l_getItemCount(lua_State* pLuaState)
296302
// Die Anzahl wird zurück gegeben
297303
return 1;
298304
}
305+
306+
int LuaRegistrations::l_getClockTimeSeconds(lua_State* pLuaState)
307+
{
308+
// Die Variable auf den Stack pushen
309+
lua_pushinteger(pLuaState, TheGame::Instance()->getClock()->getTimeInSeconds());
310+
311+
// Die Zeit in Sekunden wird zurückgegeben
312+
return 1;
313+
}
314+
315+
int LuaRegistrations::l_setClockTimeSeconds(lua_State* pLuaState)
316+
{
317+
// Die Zahl vom Stack poppen
318+
int seconds = luaL_checkinteger(pLuaState, 2);
319+
320+
// Die Zeit setzen
321+
TheGame::Instance()->getClock()->setTime(seconds);
322+
323+
// Es gibt keinen Rückgabewert
324+
return 0;
325+
}
326+
327+
int LuaRegistrations::l_addClockTimeSeconds(lua_State* pLuaState)
328+
{
329+
// Die Zeit vom Stack poppen
330+
int seconds = luaL_checkinteger(pLuaState, 2);
331+
332+
// Die Geschwindigkeit vom Stack poppen
333+
int velocity = luaL_checkinteger(pLuaState, 3);
334+
335+
// Die Zeit addieren
336+
TheGame::Instance()->getClock()->addTime(seconds, velocity);
337+
338+
// Es gibt keinen Rückgabewert
339+
return 0;
340+
}
341+
342+
int LuaRegistrations::l_showClock(lua_State* pLuaState)
343+
{
344+
// Die flag setzen
345+
TheGame::Instance()->getClock()->setVisible(true);
346+
347+
// Es gibt keinen Rückgabewert
348+
return 0;
349+
}
350+
351+
int LuaRegistrations::l_hideClock(lua_State* pLuaState)
352+
{
353+
// Die flag setzen
354+
TheGame::Instance()->getClock()->setVisible(false);
355+
356+
// Es gibt keinen Rückgabewert
357+
return 0;
358+
}

Koramu/GameLuaRegistration.h

+5
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,10 @@ namespace LuaRegistrations
3939
int l_addItem(lua_State* pLuaState);
4040
int l_removeItem(lua_State* pLuaState);
4141
int l_getItemCount(lua_State* pLuaState);
42+
int l_getClockTimeSeconds(lua_State* pLuaState);
43+
int l_setClockTimeSeconds(lua_State* pLuaState);
44+
int l_addClockTimeSeconds(lua_State* pLuaState);
45+
int l_showClock(lua_State* pLuaState);
46+
int l_hideClock(lua_State* pLuaState);
4247
}
4348

Koramu/Koramu.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
<ClCompile Include="BaseLuaRegistration.cpp" />
147147
<ClCompile Include="Button.cpp" />
148148
<ClCompile Include="Camera.cpp" />
149+
<ClCompile Include="Clock.cpp" />
149150
<ClCompile Include="ClosureDefinitions.cpp" />
150151
<ClCompile Include="CollisionRectParser.cpp" />
151152
<ClCompile Include="CommandQueue.cpp" />
@@ -209,6 +210,7 @@
209210
<ClInclude Include="Button.h" />
210211
<ClInclude Include="ButtonLuaRegistration.h" />
211212
<ClInclude Include="Camera.h" />
213+
<ClInclude Include="Clock.h" />
212214
<ClInclude Include="Closure.h" />
213215
<ClInclude Include="ClosureDeclarations.h" />
214216
<ClInclude Include="CollisionRectParser.h" />

0 commit comments

Comments
 (0)